<script setup lang="ts" name="ChooseLab">
// 选择实验室
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import type { ILabList } from '@/views/business/manager/task/task-interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getDeptList } from '@/api/system/baseInfoDept'
import { getDictByCode } from '@/api/system/dict'

const emits = defineEmits(['confirmCheckout'])
// 是否多选
const multi = ref(false)
const dialogVisible = ref(false)
// 查询条件
const listQuery = ref({
  deptName: '', // 部门名称
  deptId: '', // 部门id
})

// 表头
const columns = ref<TableColumn[]>([
  { text: '实验室编号', value: 'deptId', align: 'center', width: '200' },
  { text: '实验室名称', value: 'fullName', width: '160', align: 'center' },
  { text: '负责人', value: 'directorName', align: 'center', width: '120' },
  { text: '计量专业', value: 'deptCategoryName', 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()

// -----------------------------------------字典--------------------------------------------------------------
const deptCategoryMap = ref({}) as any// 部门类别{无线电}

/**
 * 获取字典
 */
async function getDict() {
  // 获取部门类别
  const res = await getDictByCode('eqptDeviceType')
  // 部门类别{无线电}
  res.data.forEach((item: any) => {
    deptCategoryMap.value[`${item.value}`] = item.name
  })
}
// -----------------------------------------------------------------------------------------------
// 数据查询
function fetchData() {
  getDeptList(listQuery.value).then((response) => {
    // deptType === '3'是检定部门
    const tempArr = response.data.list.filter((item: { deptType: string }) => item.deptType == '3')
    list.value = tempArr.map((i: { tips: string }) => {
      return {
        ...i,
        deptCategoryName: deptCategoryMap.value[i.tips!],
      }
    })
    console.log(list.value)

    loadingTable.value = false
  })
  loadingTable.value = false
}
// 多选发生改变时
function handleSelectionChange(e: any) {
  checkoutList.value = e
}
// 点击搜索
const searchList = () => {
  fetchData()
}
// 点击重置
const clearList = () => {
  listQuery.value = {
    deptName: '', // 部门名称
    deptId: '', // 部门id
  }
}

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

// 初始化弹窗
const initDialog = (multiSelect: boolean) => {
  getDict().then(() => {
    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="65%" @close="cancle">
      <search-area
        :need-clear="true"
        @search="searchList" @clear="clearList"
      >
        <search-item>
          <el-input v-model.trim="listQuery.deptId" placeholder="实验室编号" class="short-input" clearable />
        </search-item>
        <search-item>
          <el-input v-model.trim="listQuery.deptName" placeholder="实验室名称" class="short-input" 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"
          :height="300"
          :pagination="false"
          @multi-select="handleSelectionChange"
        >
          <!-- :is-showmulti-select="multi" -->
          <!-- :is-multi="multi" -->
          <template #preColumns>
            <el-table-column label="#" width="55" align="center" fixed="left">
              <template #default="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column v-if="!multi" 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>
