<script setup lang="ts" name="ChooseLab">
// 选择实验室
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import type { ILabList, ILabSearchQuery } from '@/views/business/schedule/task/components/lab-interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'

const emits = defineEmits(['confirmCheckout'])
// 是否多选
const multi = ref(false)
const dialogVisible = ref(false)

// 查询条件
const listQuery = ref<ILabSearchQuery>({
  organizeNo: '', // 实验室编号
  organizeName: '', // 实验室名称
  meterMajor: '', // 专业
  organizeType: '3', // 组织类型
  director: '', // 负责人
  isLab: '1', // 是否是实验室
  measureItems: '',
})

// 表头
const columns = ref<TableColumn[]>([
  { text: '实验室名称', value: 'organizeName', width: '160', align: 'center' },
  { text: '所属部门', value: 'pDeptName', align: 'center', width: '150' },
  { text: '负责人', value: 'director', align: 'center', width: '120' },
  { text: '检测能力', value: 'measureItems', align: 'center' },
])
// 表格数据
const list = ref<ILabList[]>([])
// 总数
const total = ref(0)
// 表格加载状态
const loadingTable = ref(false)
// 多选的内容
const checkoutList = ref<ILabList[]>([])
// 单选
const singleChecked = ref()
// 数据查询
function fetchData(isNowPage = false) {
  // getCustomerList(listQuery.value).then((response) => {
  //   // 模拟数据
  //   response.data.rows = [{ bankAccount: '银行账户', bankAccountNumber: '银行账号', bankName: '工商银行', briefName: '京东', businessContent: '电商', businessScope: 'test1', grade: '1', gradeName: 'A', companySize: '1', companySizeName: '小微企业', businessSize: '1', businessSizeName: '0-10万', evaluation: '1', evaluationName: '优质', companyAddress: '五棵松', companyAreaName: '海淀区', companyCityName: '北京市', companyCountryName: '中国', companyProvinceName: '北京', companyArea: '110007', companyCity: '110000', companyCountry: 'CN', companyProvince: '100000', createTime: '2022-11-29 21:14:50', director: '刘强东', fax: 'test1', id: '1597579843411234817', invoiceAddress: '五棵松', invoiceAreaName: '海淀区', invoiceCityName: '北京市', invoiceCountryName: '中国', invoiceProvinceName: '北京', invoiceyArea: '110007', invoiceCity: '110000', invoiceCountry: 'CN', invoiceProvince: '100000', mailbox: 'test1', minioFileName: 'test', mobile: 'test', phone: '950618', postalCode: 'test', remark: 'test', customerName: '京东集团', customerNo: 'sygf202211290001', taxNumber: 'test', updateTime: '2023-01-10 09:56:57', website: 'test' }]
  //   list.value = response.data.rows.map((item: ILabList) => {
  //     if (item.companyProvinceName && item.companyCityName) {
  //       item.briefName = `${item.companyProvinceName}/${item.companyCityName}`
  //     }
  //     else {
  //       item.briefName = item.companyProvinceName || item.companyCityName
  //     }
  //     return item
  //   })
  //   total.value = parseInt(response.data.total)
  //   loadingTable.value = false
  // })

  // 模拟数据
  const response = [
    { id: '1', deptId: '1', director: '张三', phone: '1532421', meterMajor: '光学', organizeNo: '121', organizeName: '力学检测实验室', measureItems: '工频耐压、启动电压、净负荷拉力', pDeptId: '', pDeptName: '检测中心', organizeType: '3', remark: '' },
    { id: '2', deptId: '1', director: '张三', phone: '1532421', meterMajor: '光学', organizeNo: '121', organizeName: '特种检测实验室', measureItems: '工频耐压、启动电压、净负荷拉力', pDeptId: '', pDeptName: '校准中心', organizeType: '3', remark: '' },
    { id: '3', deptId: '1', director: '张三', phone: '1532421', meterMajor: '光学', organizeNo: '121', organizeName: '电磁兼容实验室', measureItems: '工频耐压、启动电压、净负荷拉力', pDeptId: '', pDeptName: '校准中心', organizeType: '3', remark: '' },
    { id: '4', deptId: '1', director: '张三', phone: '1532421', meterMajor: '光学', organizeNo: '121', organizeName: '环评实验室', measureItems: '工频耐压、启动电压、净负荷拉力', pDeptId: '', pDeptName: '校准中心', organizeType: '3', remark: '' },
    { id: '5', deptId: '1', director: '张三', phone: '1532421', meterMajor: '光学', organizeNo: '121', organizeName: '几何量实验室', measureItems: '工频耐压、启动电压、净负荷拉力', pDeptId: '', pDeptName: '检测中心', organizeType: '3', remark: '' },
    { id: '6', deptId: '1', director: '张三', phone: '1532421', meterMajor: '光学', organizeNo: '121', organizeName: '环境试验实验室', measureItems: '工频耐压、启动电压、净负荷拉力', pDeptId: '', pDeptName: '校准中心', organizeType: '3', remark: '' },
    { id: '7', deptId: '1', director: '张三', phone: '1532421', meterMajor: '光学', organizeNo: '121', organizeName: '环境试验实验室', measureItems: '工频耐压、启动电压、净负荷拉力', pDeptId: '', pDeptName: '校准中心', organizeType: '3', remark: '' },
    { id: '8', deptId: '1', director: '张三', phone: '1532421', meterMajor: '光学', organizeNo: '121', organizeName: '环境试验实验室', measureItems: '工频耐压、启动电压、净负荷拉力', pDeptId: '', pDeptName: '校准中心', organizeType: '3', remark: '' },
    { id: '9', deptId: '1', director: '张三', phone: '1532421', meterMajor: '光学', organizeNo: '121', organizeName: '环境试验实验室', measureItems: '工频耐压、启动电压、净负荷拉力', pDeptId: '', pDeptName: '校准中心', organizeType: '3', remark: '' },
    { id: '10', deptId: '1', director: '张三', phone: '1532421', meterMajor: '光学', organizeNo: '121', organizeName: '环境试验实验室', measureItems: '工频耐压、启动电压、净负荷拉力', pDeptId: '', pDeptName: '校准中心', organizeType: '3', remark: '' },
    { id: '11', deptId: '1', director: '张三', phone: '1532421', meterMajor: '光学', organizeNo: '121', organizeName: '环境试验实验室', measureItems: '工频耐压、启动电压、净负荷拉力', pDeptId: '', pDeptName: '校准中心', organizeType: '3', remark: '' },
    { id: '12', deptId: '1', director: '张三', phone: '1532421', meterMajor: '光学', organizeNo: '121', organizeName: '环境试验实验室', measureItems: '工频耐压、启动电压、净负荷拉力', pDeptId: '', pDeptName: '校准中心', organizeType: '3', remark: '' },
  ]
  list.value = response.map((item: ILabList) => {
    return item
  })
  total.value = 50
  loadingTable.value = false
}
// 多选发生改变时
function handleSelectionChange(e: any) {
  checkoutList.value = e
}
// 点击搜索
const searchList = () => {
  fetchData(true)
}
// 点击重置
const clearList = () => {
  listQuery.value = {
    organizeNo: '', // 实验室编号
    organizeName: '', // 实验室名称
    meterMajor: '', // 专业
    organizeType: '3', // 组织类型
    director: '', // 负责人
    isLab: '1', // 是否是实验室
    measureItems: '',
  }
}

// 点击取消
const cancle = () => {
  dialogVisible.value = false
}
// 点击确定
const clickConfirm = () => {
  // 如果多选
  if (multi.value) {
    if (!checkoutList.value.length) {
      ElMessage({
        message: '请选择至少一个实验室',
        type: 'warning',
      })
    }
    else {
      emits('confirmCheckout', checkoutList.value)
    }
  }
  else {
    if (!singleChecked.value) {
      ElMessage({
        message: '请选择检定实验室',
        type: 'warning',
      })
    }
    else {
      const checkValue = list.value.find(item => item.id == singleChecked.value)
      emits('confirmCheckout', checkValue)
    }
  }

  cancle()
}

// 初始化弹窗
const initDialog = (multiSelect: boolean) => {
  multi.value = multiSelect
  dialogVisible.value = true
  fetchData()
}
// ----------------------- 以下是暴露的方法内容 ----------------------------
defineExpose({ initDialog })
</script>

<template>
  <div class="choose-lab-dialog">
    <el-dialog v-model="dialogVisible" :title="`选择检定实验室${(checkoutList.length > 0 && multi) ? `(已选择${checkoutList.length}个实验室)` : ''}`" width="1000" @close="cancle">
      <search-area
        :need-clear="true"
        @search="searchList" @clear="clearList"
      >
        <search-item>
          <el-input
            v-model.trim="listQuery.organizeName"
            placeholder="实验室名称"
            clearable
          />
        </search-item>
        <search-item>
          <el-input
            v-model.trim="listQuery.measureItems"
            placeholder="检定项目"
            clearable
          />
        </search-item>
      </search-area>
      <div style="padding: 5px 10px;">
        <normal-table
          :data="list"
          :total="total"
          :columns="columns"
          :query="listQuery"
          :list-loading="loadingTable"
          :is-showmulti-select="multi"
          :is-multi="multi"
          :pagination="false"
          @multi-select="handleSelectionChange"
        >
          <template v-if="!multi" #preColumns>
            <el-table-column label="" width="40" align="center">
              <template #default="scope">
                <el-radio v-model="singleChecked" :label="scope.row.id" class="radio" />
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </div>

      <template #footer>
        <el-button type="primary" @click="clickConfirm">
          确定
        </el-button>
        <el-button @click="cancle">
          取消
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.choose-lab-dialog {
  :deep(.el-radio__label) {
    display: none;
  }
}
</style>
