<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>供应商名称:</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" :selectable="selectable" />
      <el-table-column label="供应商名称" prop="name" align="center" />
      <el-table-column label="联系人" prop="linkMan" align="center" />
      <el-table-column label="手机号" prop="contractMobile" align="center">
        <template slot-scope="{ row }">
          {{ row.contractMobile ? row.contractMobile : '---' }}
        </template>
      </el-table-column>
      <el-table-column label="供应商状态" prop="status" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.status === 90" type="success" size="small">已上线</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[100, 200]"
      :page-size="100"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getShopList"
    />
    <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 { getMarketShopList } from '@/api/user/customer/customer'

export default {
  name: 'ShopDialog',
  filters: {
    NumFormat(value) {
      // 处理金额
      return Number(value).toFixed(2)
    }
  },
  data() {
    return {
      visible: true,
      listQuery: {
        pageNum: 1, // 页码
        pageSize: 100, // 页面数据数
        shopName: '' // 供应商公司名称
      },
      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.getShopList()
    },
    // 获取所有供应商列表
    async getShopList() {
      try {
        const res = await getMarketShopList(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>
