<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="380px" border @select="handleSelect">
      <el-table-column type="selection" width="55" :selectable="selectable" />
      <el-table-column label="供应商名称" prop="shopName" align="center" />
      <el-table-column label="联系人" prop="linkMan" align="center" />
      <el-table-column label="手机号" prop="mobile" align="center">
        <template slot-scope="{ row }">
          {{ row.mobile ? row.mobile : '---' }}
        </template>
      </el-table-column>
      <el-table-column label="供应商状态" prop="shopStatus" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.shopStatus === 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="getCustomerShopList"
    />
    <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 { getCustomerShopList } from '@/api/user/customer/customer'

export default {
  name: 'ShopAdvertisDialog',
  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.getCustomerShopList()
    },
    // 获取所有供应商列表
    async getCustomerShopList() {
      try {
        const res = await getCustomerShopList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 选择供应商
    handleSelect(selection, row) {
      this.$refs.table.clearSelection()
      this.$refs.table.toggleRowSelection(row)
      this.shopsRadio = row
      console.log('row', row)
    },
    // 确认选择供应商
    handleConfirm() {
      this.$emit('confirm', this.shopsRadio)
    },
    handleCanle() {
      // 取消弹窗
      this.$emit('cancel')
    },
    // 已选择的禁用勾选框
    selectable(row) {
      if (row.status === 0) {
        return true
      } else {
        return false
      }
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep {
  thead .el-checkbox {
    display: none !important;
  }
}
</style>
