<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.name"
          placeholder="服务商名称"
          clearable
          style="width: 160px"
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>手机号:</span>
        <el-input
          v-model="listQuery.mobile"
          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="serviceProviderId" align="center" />
      <el-table-column label="服务商名称" prop="name" align="center" />
      <el-table-column label="手机号" prop="mobile" align="center" />
    </el-table>
    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[10, 20]"
      :page-size="10"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getServiceList"
    />
    <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 { getServiceList } from '@/api/serviceSettlement/group'

export default {
  name: 'GoodsDialog',
  data() {
    return {
      visible: true,
      listQuery: {
        name: '',
        mobile: '',
        pageNum: 1,
        pageSize: 100
      },
      list: [],
      total: 0,
      groupRadio: null,
      isLoading: true
    }
  },
  computed: {
    disabled() {
      return this.groupRadio === null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取所有供应商列表
    async getList() {
      this.list = []
      this.listQuery.pageNum = 1
      this.getServiceList()
    },
    // 获取所有服务商列表
    async getServiceList() {
      try {
        const res = await getServiceList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 选择服务商
    handleSelectionChange(row) {
      this.groupRadio = row
      console.log('row', row)
    },
    // 确认选择服务商
    handleConfirm() {
      this.$emit('confirm', this.groupRadio)
    },
    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>
