<!--
  Description: 选择作业人员弹窗
  Author: 李亚光
  Date: 2023-04-28
 -->
<script lang="ts" setup name="selectPerson">
import { ElMessage } from 'element-plus'
import { getDeptTreeList } from '@/api/system/dept'
import { getPersonListPage } from '@/api/person'
import { toTreeList } from '@/utils/structure'
import type { TableColumn } from '@/components/NormalTable/table_interface'

const emits = defineEmits(['add'])
const dialogFormVisible = ref(false)
// 查询条件
const listQuery = ref({
  name: '',
  // startDate: '',
  status: '0',
  deptId: '',
  // endDate: '',
  // logType: '业务日志',
  offset: 1,
  limit: 5,
  workerType: '1',
})
const loadingTable = ref<boolean>(false)
const list = ref([]) // 表格数据
const total = ref(0)
const select = ref(-1)
// 表头
const columns = ref<TableColumn[]>([
  { text: '姓名', value: 'workerName' },
  {
    text: '性别',
    value: 'genderName',
  },
  { text: '所在部门', value: 'deptName' },
  { text: '联系方式', value: 'phoneNumber' },
])
// 获取人员数据列表
const fetchData = () => {
  select.value = -1
  getPersonListPage(listQuery.value).then((res) => {
    if (res.code === 200) {
      list.value = res.data.rows
      console.log()
      total.value = res.data.rows.length
    }
  })
}
fetchData()
// 搜索
const search = () => {
  fetchData()
}
// 重置
const reset = () => {
  listQuery.value = {
    name: '',
    // startDate: '',
    status: '0',
    deptId: '',
    // endDate: '',
    // logType: '业务日志',
    offset: 1,
    limit: 5,
  }
  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 submitForm = () => {
  if (select.value >= 0) {
    const person = list.value.filter((item, index) => index == select.value)[0]
    emits('add', person)
    dialogFormVisible.value = false
    reset()
  }
  else {
    ElMessage.warning('请先选择人员')
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}
// 初始化
const initDialog = () => {
  dialogFormVisible.value = true
}
defineExpose({ initDialog })
const deptTreeList = ref([])
// 加载组织机构树形下拉
const fetchDeptTree = () => {
  getDeptTreeList().then((res) => {
    if (res.data) { // 将列表转树结构
      deptTreeList.value = toTreeList(res.data, '0', true)
    }
  })
}
fetchDeptTree()
</script>

<template>
  <el-dialog v-model="dialogFormVisible" title="选择人员" width="50%">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.name" placeholder="姓名" clearable />
      </search-item>
      <search-item>
        <dept-select v-model="listQuery.deptId" :data="deptTreeList" placeholder="所属部门" style="width: 100%;" />
      </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">
            <template #default="scope">
              <el-radio v-model="select" :label="scope.$index" class="radio" />
            </template>
          </el-table-column>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </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>
