<!-- 用户列表弹窗 -->
<script lang="ts" setup name="selectPerson">
import { ElMessage } from 'element-plus'
import type { IOptions } from '../plan_interface'
import { SCHEDULE } from '@/utils/scheduleDict'
import { getDeptTree, getPlanList } from '@/api/system/plan'
import type { TableColumn } from '@/components/NormalTable/table_interface'
const props = defineProps({
  list: {
    type: Array,
    default: () => [],
  },
})
const emits = defineEmits(['add'])
const dialogFormVisible = ref(false)

// 查询条件
const listQuery = ref({
  createEndTime: '',
  createStartTime: '',
  deptId: '',
  director: '',
  effectiveCompany: '',
  ids: [],
  trainEndTime: '',
  formId: SCHEDULE.TRAIN_APPROVAL,
  trainStartTime: '',
  offset: 1,
  limit: 20,
})
// 主管部门下拉框
const options = ref<IOptions[]>([])
// 获取到组织信息
const getDept = () => {
  const params = {
    roleId: '',
    deptType: '',
    tips: '',
  }
  getDeptTree(params).then((response) => {
    options.value = response.data
  })
}
getDept()
const loadingTable = ref<boolean>(false)
const total = ref<number>(0)
const select = ref<number>(-1)
const columns = ref<TableColumn[]>([
  {
    text: '计划编号',
    value: 'planNo',
    align: 'center',
  },
  {
    text: '培训名称',
    value: 'planName',
    align: 'center',
  },
  {
    text: '主管部门',
    value: 'deptName',
    align: 'center',
  },
  {
    text: '实施单位',
    value: 'effectiveCompany',
    align: 'center',
  },
  {
    text: '培训内容',
    value: 'trainContent',
    align: 'center',
  },
])
const list = ref([])
const fetchData = (isNowPage: boolean) => {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  getPlanList(listQuery.value).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}
fetchData(true)
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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
  }
  fetchData(true)
}
// 搜索
const search = () => {
  fetchData(true)
}
// 重置
const reset = () => {
  listQuery.value = {
    createEndTime: '',
    createStartTime: '',
    deptId: '',
    director: '',
    effectiveCompany: '',
    ids: [],
    trainEndTime: '',
    formId: SCHEDULE.TRAIN_APPROVAL,
    trainStartTime: '',
    offset: 1,
    limit: 20,
  }
}
// 点击保存
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.director" placeholder="负责人" clearable />
      </search-item>
      <search-item>
        <el-select
          v-model="listQuery.deptId"
          clearable
          placeholder="主管部门"
          size="default"
        >
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </search-item>
    </search-area>
    <!-- 查询结果Table显示 -->
    <div style="padding: 12px;">
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery"
        :list-loading="loadingTable" :pagination="false" @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>
        </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>
