<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>商品ID:</span>
        <el-input
          v-model="listQuery.productId"
          placeholder="商品ID"
          clearable
          @input="e => (listQuery.productId= checkedInput(e,1))"
        />
      </div>
      <div class="filter-control">
        <span>商品名称:</span>
        <el-input
          v-model="listQuery.name"
          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="productId" label="商品ID" align="center" width="80" />
      <el-table-column prop="coupon" label="商品图片" align="center" width="100">
        <template v-if="row.productId" 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="name" label="商品名称" align="center" />
      <el-table-column prop="shopName" label="供应商" align="center" width="250" />
      <el-table-column prop="price" label="机构价" align="center" width="160">
        <template slot-scope="{ row }">
          ￥{{ row.price ? row.price : '0.00' }}
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[20]"
      :page-size="20"
      :page.sync="listQuery.pageNum"
      :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 { fetchProductList } from '@/api/member/member'

export default {
  name: 'ProDialog',
  filters: {
    NumFormat(value) {
      // 处理金额
      return Number(value).toFixed(2)
    }
  },
  data() {
    return {
      visible: true,
      listQuery: {
        productId: '', // 商品id
        name: '', // 商品名称
        shopName: '', // 供应商名称
        pageNum: 1,
        pageSize: 20
      },
      list: [],
      total: 0,
      productRadio: null,
      isLoading: true
    }
  },
  computed: {
    disabled() {
      return this.productRadio === null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.isLoading = true
      const res = await fetchProductList(this.listQuery)
      this.list = res.data.results
      this.total = res.data.totalRecord
      this.isLoading = false
    },
    // 选择供应商
    handleSelect(selection, row) {
      this.$refs.table.clearSelection()
      this.$refs.table.toggleRowSelection(row)
      this.productRadio = row.productId
    },
    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>
