<template>
  <el-dialog
    title="选择商品"
    :visible.sync="visible"
    width="1100px"
    :close-on-click-modal="false"
    :show-close="false"
  >
    <div class="filter-container">
      <div class="filter-control">
        <span>商品名称:</span>
        <el-input
          v-model="listQuery.productName"
          placeholder="商品名称"
          clearable
          @input="e => (listQuery.name= checkedInput(e,2))"
        />
      </div>
      <div class="filter-control">
        <span>供应商名称:</span>
        <el-input
          v-model="listQuery.shopName"
          placeholder="供应商名称"
          clearable
          style="width:160px;"
          @input="e => (listQuery.shopName= checkedInput(e,2))"
        />
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList">查询</el-button>
      </div>
    </div>
    <el-table ref="table" v-loading="isLoading" :data="list" height="400px" border @select="handleSelect">
      <el-table-column type="selection" width="50" />
      <el-table-column prop="mainImage" label="商品图片" align="center" width="100">
        <template slot-scope="{ row }">
          <el-popover
            placement="top-start"
            title=""
            width="180"
            trigger="hover"
          >
            <img :src="row.mainImage" alt="" style="width:100px;height:100px;">
            <img slot="reference" :src="row.mainImage" alt="" style="width:50px;height:50px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="productName" label="商品名称" align="center" />
      <el-table-column prop="shopName" label="供应商" align="center" width="250" />
    </el-table>
    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[20]"
      :page-size="20"
      :page.sync="listQuery.index"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
    <div slot="footer">
      <el-button @click="handleCanle">取 消</el-button>
      <el-button type="primary" :disabled="disabled" @click="handleAddProConfirm(productRadio)">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getList } from '@/api/goods'

export default {
  name: 'ProDialog',
  filters: {
    NumFormat(value) {
      // 处理金额
      return Number(value).toFixed(2)
    }
  },
  data() {
    return {
      visible: true,
      listQuery: {
        productName: '', // 商品名称
        shopName: '', // 供应商名称
        validFlag: 1,
        index: 1,
        organizeId: this.$store.getters.organizeId,
        pageSize: 20

      },
      list: [],
      total: 0,
      productRadio: null,
      isLoading: true
    }
  },
  computed: {
    disabled() {
      return this.productRadio === null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      // 获取商品列表
      try {
        this.isLoading = true
        const res = await getList(this.listQuery)
        console.log('res', res)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log('error', error)
      }
    },
    // 选择商品
    handleSelect(selection, row) {
      this.$refs.table.clearSelection()
      this.$refs.table.toggleRowSelection(row)
      this.productRadio = row
    },
    handleAddProConfirm() {
      // 确认选择商品
      this.$emit('confirm', this.productRadio)
    },
    handleCanle() {
      // 取消弹窗
      this.$emit('cancel')
    },
    checkedInput(event, type) {
      let pattern = ''
      switch (type) {
        case 1:
          pattern = /[^\d]/g
          break
        case 2:
          pattern = /[^u4E00-u9FA5|d|a-zA-Z|rns,.?!，。？！…—&$=()-+/*{}[]]|s/g
          break
      }
      return event.replace(pattern, '')
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep{
  thead .el-checkbox{display: none !important;}
}
</style>
