<template>
  <el-dialog title="添加商品" :visible.sync="visible" width="1200px" :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
          style="width: 160px"
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>商品名称:</span>
        <el-input
          v-model="listQuery.name"
          placeholder="商品名称"
          clearable
          style="width: 160px"
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>供应商名称:</span>
        <el-input
          v-model="listQuery.shopName"
          placeholder="供应商名称"
          clearable
          style="width: 160px"
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </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
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="商品ID" prop="productId" align="center" width="100" />
      <el-table-column prop="mainImage" label="商品图片" align="center" width="120">
        <template slot-scope="{ row }">
          <img v-if="row.mainImage" :src="row.mainImage" alt="" style="width: 50px; height: 50px" />
          <span v-else>---</span>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" prop="productName" align="center" />
      <el-table-column label="供应商名称" prop="shopName" align="center" />
    </el-table>
    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[10, 20]"
      :page-size="10"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getTaskProducts"
    />
    <div slot="footer">
      <el-button @click="handleCanle"> 取 消 </el-button>
      <el-button type="primary" :disabled="disabled" @click="handleConfirm"> 确 定 </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getTaskProducts } from '@/api/serviceSettlement/task'

export default {
  name: 'GoodsDialog',
  data() {
    return {
      visible: true,
      listQuery: {
        name: '',
        shopName: '',
        productId: '',
        pageNum: 1, // 页码
        pageSize: 10 // 页面数据数
      },
      list: [],
      total: 0,
      shopsRadio: null,
      isLoading: true
    }
  },
  computed: {
    disabled() {
      return this.shopsRadio === null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取所有供应商列表
    async getList() {
      this.list = []
      this.listQuery.pageNum = 1
      this.getTaskProducts()
    },
    // 获取所有供应商列表
    async getTaskProducts() {
      try {
        const res = await getTaskProducts(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 选择供应商
    handleSelectionChange(row) {
      this.shopsRadio = row
      console.log('row', row)
    },
    // 确认选择供应商
    handleConfirm() {
      this.$emit('confirm', this.shopsRadio)
    },
    handleCanle() {
      // 取消弹窗
      this.$emit('cancel')
    },
    // 已选择的禁用勾选框
    selectable(row) {
      if (row.flag) {
        return true
      } else {
        return false
      }
    },
    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></style>
