<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'
import { getOrganizeList } from '@/api/measure/measureDept'
import type { dictType } from '@/global'
import { getDictByCode } from '@/api/system/dict'
const emits = defineEmits(['confirmCheckout'])
// 是否多选
const multi = ref(false)
const dialogVisible = ref(false)

const measureMajorList = ref<dictType[]>([]) // 计量专业
// 获取字典值
function getDict() {
  // 获取计量专业
  getDictByCode('measureMajor').then((response) => {
    measureMajorList.value = response.data
  })
}
getDict()
// 查询条件
const listQuery = ref<ILabSearchQuery>({
  organizeNo: '', // 实验室编号
  organizeName: '', // 实验室名称
  meterMajor: '', // 专业
  organizeType: '3', // 组织类型
  director: '', // 负责人
  isLab: '1', // 是否是实验室
  createStartTime: '',
  createEndTime: '',
})

// 表头
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: 'meterMajorName', align: 'center' },
  { text: '实验室能力', value: 'labCapacityName', 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) {
  getOrganizeList(listQuery.value).then((response) => {
    list.value = response.data
    total.value = parseInt(response.data.length)
    loadingTable.value = false
  })
  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', // 是否是实验室
    createStartTime: '',
    createEndTime: '',
  }
}

// 点击取消
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-select
            v-model.trim="listQuery.meterMajor"
            clearable
            placeholder="计量专业"
          >
            <el-option
              v-for="item in measureMajorList"
              :key="item.id"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </search-item>
      </search-area>
      <div style="padding: 5px 10px;">
        <normal-table
          :data="list"
          :total="total"
          :columns="columns"
          :query="listQuery"
          :list-loading="loadingTable"
          :pagination="false"
          size="small"
          @multi-select="handleSelectionChange"
        >
          <!-- :is-showmulti-select="multi" -->
          <!-- :is-multi="multi" -->
          <template v-if="!multi" #preColumns>
            <el-table-column label="#" width="55" align="center" fixed="left">
              <template #default="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column label="" width="40" align="center" fixed>
              <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>
