<template>
  <el-dialog title="选择供应商" :visible.sync="visible" width="1100px">
    <div class="filter-container">
      <div class="filter-control">
        <span>供应商Id:</span>
        <el-input
          v-model="listQuery.shopId"
          placeholder="供应商Id"
          clearable
          @input="e => (listQuery.shopId= 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.sname"
          placeholder="供应商简称"
          clearable
        />
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList">搜索</el-button>
      </div>
    </div>
    <el-table ref="table" :data="list" height="350px" border @select="handleSelect">
      <el-table-column type="selection" width="55" />
      <el-table-column property="shopId" label="供应商ID" width="150" />
      <el-table-column property="name" label="供应商名称" width="150" />
      <el-table-column property="sname" label="供应商简称" width="200">
        <template slot-scope="{ row }">
          {{ row.sname ? row.sname : '---' }}
        </template>
      </el-table-column>
      <el-table-column property="linkMan" label="联系人" />
      <el-table-column property="contractMobile" label="手机号">
        <template slot-scope="{ row }">
          {{ row.contractMobile ? row.contractMobile : '---' }}
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer">
      <el-button @click="handleCanle">取 消</el-button>
      <el-button type="primary" :disabled="disabled" @click="handleAddProConfirm(shopRadio)">确 定</el-button>
    </div>
    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[20]"
      :page-size="20"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </el-dialog>
</template>

<script>
import { fetchFindShop } from '@/api/member/member'

export default {
  name: 'ShopDialog',
  data() {
    return {
      visible: true,
      listQuery: {
        shopId: '', // 供应商id
        name: '', // 供应商名称
        sname: '', // 供应商简称
        pageNum: 1,
        pageSize: 20
      },
      list: [],
      total: 0,
      shopRadio: null,
      isLoading: true
    }
  },
  computed: {
    disabled() {
      return this.shopRadio === null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取供应商列表
    async getList() {
      this.isLoading = true
      const res = await fetchFindShop(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.shopRadio = row
    },
    handleAddProConfirm() {
      // 确认选择商品
      this.$emit('confirm', this.shopRadio)
    },
    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>
