<!-- 计量人员列表弹窗 -->
<script lang="ts" setup name="selectPerson">
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'
import { getStaffDetail, getStaffList } from '@/api/measure/person'
import { getDeptTreeList } from '@/api/system/dept'
// import type { TreeStructure } from '@/views/system/user/user-interface'
import comTreeSelect from '@/views/system/user/selecTree.vue'
import { toTreeList } from '@/utils/structure'
import type { TableColumn } from '@/components/NormalTable/table_interface'
const emits = defineEmits(['add'])
const dialogFormVisible = ref(false)
// 查询条件
const listQuery = ref({
  staffNo: '', // 人员编号
  name: '', // 姓名
  deptId: '', // 所在部门
  limit: 5,
  offset: 1,
})
const loadingTable = ref<boolean>(false)
const list = ref([]) // 表格数据
const total = ref<number>(0)
const select = ref()
const columns = ref<TableColumn[]>([
  { text: '人员编号', value: 'staffNo', align: 'center', width: '160', fixed: true },
  { text: '姓名', value: 'name', align: 'center', fixed: true },
  { text: '学历', value: 'education', align: 'center' },
  { text: '性别', value: 'sex', align: 'center' },
  { text: '工作部门', value: 'deptName', align: 'center', width: '120' },
  { text: '技术职务', value: 'technologyJob', align: 'center', width: '120' },
  { text: '行政职务', value: 'administrationJob', align: 'center', width: '100' },
  { text: '计量专业', value: 'major', align: 'center', width: '100' },
  { text: '证书号', value: 'verifierCertificateNo', align: 'center', width: '120' },
  { text: '证书有效日期', value: 'certificateDate', align: 'center', width: '120' },
  { text: '技术考核', value: 'technologyExam', align: 'center', width: '100' },
])
// 获取部门
const deptProps = reactive({
  parent: 'pid', value: 'id', label: 'name', children: 'children',
})
const deptIdList = ref([])
const getDeptList = () => { // 获取部门
  getDeptTreeList().then((res) => {
    deptIdList.value = toTreeList(res.data, '0', true)
  })
}
// 获取数据列表
const fetchData = () => {
  select.value = -1
  const sexMap = {
    1: '男',
    2: '女',
  }
  const technologyExamMap = {
    0: '已考核',
    1: '未考核',
  }
  getStaffList(listQuery.value).then((res) => {
    if (res.code === 200) {
      list.value = res.data.records.map((item: { sex: string | number; certificateDate: string; technologyExam: string | number }) => {
        item.sex = sexMap[item.sex as keyof typeof sexMap]
        item.certificateDate = item.certificateDate ? dayjs(item.certificateDate).format('YYYY-MM-DD') : ''
        item.technologyExam = technologyExamMap[item.technologyExam as keyof typeof technologyExamMap]
        return item
      })
      total.value = parseInt(res.data.total)
    }
  })
}
// 搜索
const search = () => {
  fetchData()
}
const selectTreeRef = ref()
// 重置
const reset = () => {
  listQuery.value = {
    staffNo: '',
    name: '', // 姓名
    deptId: '', // 所在部门
    limit: 5,
    offset: 1,
  }
  selectTreeRef.value.clearSelected()
  search()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number;page?: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  search()
}
// 点击确定
const confirmSelect = () => {
  if (select.value >= 0) {
    const person = list.value.filter((item, index) => index == select.value)[0]
    getStaffDetail(person).then((res) => {
      if (res.code === 200) {
        emits('add', res.data)
        dialogFormVisible.value = false
        reset()
      }
    })
  }
  else {
    ElMessage.warning('请先选择人员')
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}
// 初始化
const initDialog = () => {
  dialogFormVisible.value = true
  getDeptList()
  fetchData()
}
defineExpose({ initDialog })
</script>

<template>
  <el-dialog v-if="dialogFormVisible" v-model="dialogFormVisible" title="选择人员" width="65%">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item class="short-input">
        <el-input v-model.trim="listQuery.staffNo" placeholder="人员编号" clearable />
      </search-item>
      <search-item class="short-input">
        <el-input v-model.trim="listQuery.name" placeholder="姓名" clearable />
      </search-item>
      <search-item class="tree-select">
        <!-- <el-select v-model="listQuery.deptId" placeholder="所在部门" clearable style="width: 150px;">
          <el-option v-for="item in deptIdList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select> -->
        <com-tree-select
          ref="selectTreeRef" v-model="listQuery.deptId" :options="deptIdList"
          placeholder="所在部门" :tree-props="deptProps"
        />
      </search-item>
    </search-area>
    <!-- 查询结果Table显示 -->
    <div style="padding: 12px;">
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :page-sizes="[5]"
        :list-loading="loadingTable" @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="#" width="55" align="center" fixed>
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="" width="55" align="center" fixed>
            <template #default="scope">
              <el-radio v-model="select" :label="scope.$index" class="radio" />
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="confirmSelect">确认</el-button>
        <el-button @click="resetForm">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

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

.tree-select {
  width: 180px;
}
</style>
