<!-- 用户列表弹窗 -->
<script lang="ts" setup name="selectPerson">
import { ElMessage } from 'element-plus'
import { getUserList } from '@/api/system/user'
import { getDeptTreeList } from '@/api/system/dept'
import type { TreeStructure } from '@/views/system/user/user-interface'
const props = defineProps({
  list: {
    type: Array,
    default: () => [],
  },
})
const emits = defineEmits(['add'])
const dialogFormVisible = ref(false)

// 查询条件
const listQuery = ref({
  name: '', // 姓名
})
const loadingTable = ref<boolean>(false)
const total = ref<number>(0)
const select = ref<number>(-1)
const columns = ref([
  {
    text: '姓名',
    value: 'name',
  },
  {
    text: '单位名称',
    value: 'company',
  },
  {
    text: '技术职称',
    value: 'remark',
  },
  {
    text: '签到时间',
    value: 'signTime',
  },
  {
    text: '考核结果',
    value: 'examResult',
  },
])
const list = ref(props.list || [])
// 搜索
const search = () => {
  list.value = props.list.filter(item => item.name === listQuery.value.name)
}
const changePage = (e: any) => {
  console.log(e)
}
// 重置
const reset = () => {
  listQuery.value = {
    name: '', // 姓名
  }
  list.value = props.list
}
// 点击保存
const submitForm = () => {
  console.log(select.value)
  if (select.value >= 0) {
    const person = list.value.filter((item, index) => index == select.value)[0]
    emits('add', person)
    dialogFormVisible.value = false
  }
  else {
    ElMessage.warning('请先选择人员')
  }
}
// 监听数据变化
watch(
  [() => props.list], ([newNums], [oldNums]) => {
    list.value = newNums
  },
  {
    immediate: true,
    deep: true,
  },
)
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
}
// 初始化
const initDialog = () => {
  dialogFormVisible.value = true
}
defineExpose({ initDialog })
</script>

<template>
  <el-dialog v-if="dialogFormVisible" v-model="dialogFormVisible" title="选择人员">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.name" placeholder="真实姓名" clearable />
      </search-item>
    </search-area>
    <!-- 查询结果Table显示 -->
    <div style="padding: 12px;">
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery"
        :list-loading="loadingTable" :pagination="false"
      >
        <template #preColumns>
          <el-table-column label="选择" width="55" align="center">
            <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="submitForm">确认</el-button>
        <el-button @click="resetForm">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

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