<template>
  <el-dialog
    center
    title="选择组合商品"
    :visible="_visible"
    :close-on-click-modal="false"
    :append-to-body="true"
    width="1024px"
    top="5vh"
    class="child-product"
    @close="handleClose"
  >
    <ex-table
      v-loading="loading"
      :table="table"
      :data="tableData"
      :columns="columns"
      :page="pageInfo"
      :size="size"
      @page-curr-change="handlePageChange"
      @page-size-change="handleSizeChange"
      @screen-reset="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
      @screen-submit="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
      @selection="selectionChange"
    >
      <template #table-header="{}">
        <div style="width: 100%">
          <el-row style="padding: 0 0 10px 0px">
            <el-col :span="6" style="width: 303px">
              <period-date-picker
                :type="1"
                :width="'135px'"
                :size="searchSize"
                :start="parmValue.start"
                :end="parmValue.end"
                @timeReturned="handleTime"
              />
            </el-col>
            <el-col :span="4" style="width: 135px">
              <search-brand
                :value="brandid"
                :disabled="false"
                :size="'mini'"
                :is-detail="true"
                :names="parmValue.supplierName"
                :placeholder="'商品品牌'"
                @searchChange="brandidsearchChange"
              />
            </el-col>
            <el-col :span="6" style="width: 310px; padding: 0 0 0 10px">
              <good-class
                :value="parmValue.cat_id"
                :disabled="false"
                :size="searchSize"
                :is-detail="false"
                :placeholder="'分类'"
                @handleChange="goods_class_change"
              />
            </el-col>

            <el-col :span="3" class="fr" style="width: 66px; padding: 0 0 0 10px">
              <el-button type="primary" :size="searchSize" @click="searchList">刷新</el-button>
            </el-col>

            <el-col :span="3" class="fr" style="width: 66px; padding: 0 0 0 10px">
              <el-button type="warning" :size="searchSize" @click="restSearch">重置</el-button>
            </el-col>
          </el-row>
          <el-row>
            <!-- <el-col :span="4" style="width: 226px">
              <el-select
                v-model="parmValue.status"
                filterable
                clearable
                :size="searchSize"
                placeholder="状态"
                style="width: 100%"
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              >
                <el-option
                  v-for="item in statusList"
                  :key="'status' + item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </el-col>-->
            <el-col :span="4" style="width: 145px; padding: 0 0 0 0px">
              <el-select
                v-model="parmValue.isonline"
                filterable
                clearable
                :size="searchSize"
                placeholder="是否上线"
                style="width: 100%"
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              >
                <el-option
                  v-for="item in isonlineoptions"
                  :key="'isonline' + item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-col>

            <!-- <el-col :span="4" style="width: 135px;margin-left:10px">
              <el-select
                v-model="parmValue.is_stock"
                size="mini"
                style="width: 100%"
                placeholder="商品类型"
                clearable
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              >
                <el-option
                  v-for="opt in isStockOptions"
                  :key="opt.value"
                  :value="opt.value"
                  :label="opt.label"
                />
              </el-select>
            </el-col>-->
            <el-col :span="4" style="margin: 0 10px">
              <search-supplier
                :size="'mini'"
                style="width: 165px"
                :value="supplierNo"
                :disabled="false"
                :placeholder="'供应商名称'"
                :is-detail="false"
                :no-disabled="true"
                :names="''"
                @searchChange="supplierChange"
              />
            </el-col>

            <el-col :span="4" style="width: 420px">
              <el-input
                v-model="sinput"
                :size="searchSize"
                :maxlength="40"
                placeholder="关键字"
                @blur="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              >
                <el-select
                  slot="prepend"
                  v-model="select"
                  style="width: 135px"
                  placeholder="关键字类型"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                >
                  <el-option label="商品编号" value="1" />
                  <el-option label="商品名称" value="2" />
                </el-select>
              </el-input>
            </el-col>
          </el-row>
        </div>
      </template>

      <template #spuCode="{ scope }">
        <div style="display:flex;align-items:center">
          <p>{{ scope.row.spuCode }}</p>
        </div>
      </template>

      <template #good_thumb_img="{ scope }">
        <div
          v-if="scope.row.good_thumb_img"
          v-viewer
          style="width: 20px; height: 20px"
          class="hover"
        >
          <img
            :src="scope.row.good_thumb_img"
            style="display: inline-block; width: 100%; height: 100%"
            alt
          >
        </div>
      </template>
      <template #status="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="
            (statusList.find((item) => item.code == scope.row.status) || {}).type || '--'
          "
          v-text="
            (statusList.find((item) => item.code == scope.row.status) || {}).name || '--'
          "
        />
      </template>
      <template #has_account="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="
            (
              has_account_list.find((item) => item.code == scope.row.has_account + '') ||
              {}
            ).type || '--'
          "
          v-text="
            (
              has_account_list.find((item) => item.code == scope.row.has_account + '') ||
              {}
            ).name || '--'
          "
        />
      </template>
      <template #isonline="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="scope.row.is_online == '0' ? 'warning' : ''"
          v-text="
            (isonlineoptions.find((item) => item.id == scope.row.is_online) || {}).name ||
              '--'
          "
        />
      </template>
      <template #is_stock="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="scope.row.is_stock == '0' ? 'warning' : ''"
          v-text="
            (options4.find((item) => item.id == scope.row.is_stock) || {}).name || '--'
          "
        />
      </template>
    </ex-table>

    <div style="display:flex;justify-content:flex-end">
      <el-button size="mini" type="primary" @click="onSave">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { has_account_list, isStockOptions } from '@/assets/js/statusList'
import asyncRequest from '@/apis/service/goodStore/goodsCost'
import mixinPage from '@/mixins/elPaginationHandle'
import resToken from '@/mixins/resToken'

export default {
  name: 'ProductListModal',
  mixins: [mixinPage, resToken],
  props: ['visible'],
  data() {
    return {
      options1: [
        { id: '0', name: '否' },
        { id: '1', name: '是' }
      ],
      options4: [
        { id: '0', name: '非库存品' },
        { id: '1', name: '库存品' }
      ],
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: []
      },
      loading: false,
      selected: [],
      size: 'mini',
      searchSize: 'mini',
      tablebtnSize: 'mini',
      sinput: '',
      select: '1',
      parmValue: {
        page: 1, // 页码
        size: 10, // 每页显示条数
        is_stock: '',
        start: '',
        end: '',
        status: '',
        good_name: '',
        spucode: '',
        cat_id: [],
        brandid: '',
        good_type: '',
        companyNo: '',
        supplierNo: '',
        isonline: '',
        company_name: '' // 创建人部门
      },
      has_account_list,
      isStockOptions,
      pageInfo: {
        size: 10,
        curr: 1,
        total: 0
      },
      isonlineoptions: [
        { id: '0', name: '未上线' },
        { id: '1', name: '已上线' }
      ],
      statusList: [
        {
          code: '0',
          name: '新建待审核',
          type: ''
        },
        {
          code: '1',
          name: '审核通过',
          type: 'success'
        },
        {
          code: '2',
          name: '基础修改待审核',
          type: ''
        },
        {
          code: '3',
          name: '成本修改待审核',
          type: ''
        },
        {
          code: '4',
          name: '基础修改驳回',
          type: 'danger'
        },
        {
          code: '5',
          name: '成本修改驳回',
          type: 'danger'
        },
        {
          code: '6',
          name: '新建审核驳回',
          type: 'danger'
        },
        {
          code: '7',
          name: '复制商品待编辑',
          type: 'info'
        },
        {
          code: '8',
          name: '竞价商品待编辑',
          type: 'info'
        }
      ],
      columns: [
        { type: 'selection', fixed: 'left', _noset_: true },
        {
          prop: 'spuCode',
          label: '编号',
          width: '180px',
          _slot_: 'spuCode'
        },
        {
          prop: 'good_thumb_img',
          label: '图片',
          _slot_: 'good_thumb_img',
          width: '45px'
        },
        {
          prop: 'good_name',
          label: '名称',
          'min-width': '160px'
        },
        {
          prop: 'cat_name',
          label: '分类'
        },
        {
          prop: 'brand_name',
          label: '品牌'
        },
        {
          prop: 'is_stock',
          label: '库存类型',
          _slot_: 'is_stock',
          width: '85px'
        },
        // {
        //   prop: "is_combind",
        //   label: "是否组合商品",
        //   width: "105px",
        //   _slot_: "is_combind"
        // },
        {
          prop: 'isonline',
          label: '是否上线',
          _slot_: 'isonline',
          width: '70px'
        },
        {
          prop: 'status',
          label: '状态',
          _slot_: 'status',
          width: '118px'
        },
        {
          prop: 'has_account',
          label: '供应商账号',
          _slot_: 'has_account',
          width: '85px'
        },
        {
          prop: 'supplierNo',
          label: '供应商编号',
          width: '110px'
        },
        {
          prop: 'supplierName',
          label: '供应商名称',
          width: '110px'
        },
        {
          prop: 'companyNo',
          label: '业务公司编号',
          width: '110px'
        },
        {
          prop: 'companyName',
          label: '业务公司名称',
          width: '110px'
        },
        {
          prop: 'charger_company_name',
          label: '负责人部门',
          minWidth: '150px'
        },

        {
          prop: 'charger',
          label: '负责人',
          width: '70px'
        },
        {
          prop: 'creater',
          label: '创建人',
          width: '70px'
        },

        {
          prop: 'addtime',
          label: '创建时间',
          width: '140px'
        }
      ],
      tableData: []
    }
  },
  computed: {
    _visible: {
      get() {
        return this.visible
      },
      set(nV) {
        this.$emit('update:visible', nV)
      }
    }
  },
  watch: {
    visible(v) {
      if (!v) return
      this.searchList()
    }
  },
  methods: {
    selectionChange(evt) {
      const { list } = evt
      this.selected = list
    },
    restSearch() {
      this.select = '2'
      this.sinput = ''
      this.supplierNo = []
      this.brandid = []
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0
      }
      this.parmValue = {
        page: 1, // 页码
        size: 15, // 每页显示条数
        start: '',
        end: '',
        status: '',
        good_name: '',
        company_name: '', // 创建人部门
        spucode: '',
        cat_id: [],
        brandid: '',
        good_type: '',
        isonline: '',
        companyNo: '',
        supplierNo: ''
      }
      this.searchList()
    },
    handleClose() {
      this.selected = []
      this._visible = false
    },
    proportionChange(proportion) {
      this.ruleForm.proportion = proportion
    },
    onSave() {
      if (this.selected.length === 0 || this.selected.length > 1) {
        const message =
          this.selected.length === 0 ? '请选择一条商品' : '只能选择一条商品'
        this.$message.warning(message)
        return
      }

      const { spuCode, id } = this.selected[0]
      this.$emit('selected', spuCode, id)
      this._visible = false
    },
    async searchList() {
      console.log(this.parmValue)
      if (
        (this.parmValue.start !== '' && this.parmValue.end === '') ||
        (this.parmValue.start === '' && this.parmValue.end !== '')
      ) {
        this.$message.warning('时间区间不完整！')
        return
      }
      this.loading = true
      const item = JSON.parse(JSON.stringify(this.parmValue))
      item.spucode = this.select === '1' ? this.sinput : ''
      item.good_name = this.select === '2' ? this.sinput : ''
      item.companyNo = this.select === '3' ? this.sinput : ''
      item.supplierNo = this.select === '4' ? this.sinput : ''
      item.company_name = this.select === '5' ? this.sinput : '' // 部门
      item.cat_id =
        item.cat_id.length > 0 ? item.cat_id[item.cat_id.length - 1] : ''
      const res = await asyncRequest.copy_list({
        ...item,
        supplierNo: Array.isArray(this.supplierNo) ? this.supplierNo[0] : '',
        needRela: true,
        is_stock: '1',
        status: '1',
        is_combind: '1'
      })

      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list
        this.tableData.forEach(a => {
          a.cat_name = ''
          const list = a.cat_info || []
          list.forEach((b, i) => {
            a.cat_name += i == 0 ? b.name : '/' + b.name
          })
        })
        this.pageInfo.total = Number(res.data.count)
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout()
      } else {
        this.tableData = []
        this.pageInfo.total = 0
      }
      // this.getresultlist();
      this.loading = false
    }
  }
}
</script>

<style lang="scss" scoped>
.child-product {
  /deep/ .el-pagination {
    float: left !important;
  }
}
</style>
