<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="招聘需求ID" prop="recruitmentRequestId">
        <el-input
          v-model="queryParams.recruitmentRequestId"
          placeholder="请输入招聘需求ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select
          v-model="queryParams.gender"
          placeholder="请选择性别"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SYSTEM_USER_SEX)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="籍贯" prop="birthPlace">
        <el-input
          v-model="queryParams.birthPlace"
          placeholder="请输入籍贯"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入年龄"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入电子邮箱"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="学校" prop="school">
        <el-input
          v-model="queryParams.school"
          placeholder="请输入学校"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="专业" prop="major">
        <el-input
          v-model="queryParams.major"
          placeholder="请输入专业"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="出生日期" prop="birthday">
        <el-input
          v-model="queryParams.birthday"
          placeholder="请输入出生日期"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="岗位id" prop="postId">
        <el-input
          v-model="queryParams.postId"
          placeholder="请输入岗位id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="意向岗位名称" prop="desiredPosition">
        <el-input
          v-model="queryParams.desiredPosition"
          placeholder="请输入意向岗位名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="意向城市" prop="desiredCity">
        <el-input
          v-model="queryParams.desiredCity"
          placeholder="请输入意向城市"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="最高学历（大专、本科、硕士、博士）" prop="highestEducation">
        <el-input
          v-model="queryParams.highestEducation"
          placeholder="请输入最高学历（大专、本科、硕士、博士）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="个人照片" prop="personalPhoto">
        <el-input
          v-model="queryParams.personalPhoto"
          placeholder="请输入个人照片"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="工作案例附件" prop="workCaseAttachment">
        <el-input
          v-model="queryParams.workCaseAttachment"
          placeholder="请输入工作案例附件"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="简历附件" prop="resumeAttachment">
        <el-input
          v-model="queryParams.resumeAttachment"
          placeholder="请输入简历附件"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否内推（内部推荐、BOSS直聘、猎聘、智联招聘、脉脉、公众号）" prop="isInternalRef">
        <el-select
          v-model="queryParams.isInternalRef"
          placeholder="请选择是否内推（内部推荐、BOSS直聘、猎聘、智联招聘、脉脉、公众号）"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.HRM_INTERNAL_REF)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="婚育情况（未婚、已婚未育、已婚已育）" prop="maritalStatus">
        <el-select
          v-model="queryParams.maritalStatus"
          placeholder="请选择婚育情况（未婚、已婚未育、已婚已育）"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.HRM_MARITAL_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['hrm:talent-pool:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['hrm:talent-pool:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="招聘需求ID" align="center" prop="recruitmentRequestId" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gender">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_USER_SEX" :value="scope.row.gender" />
        </template>
      </el-table-column>
      <el-table-column label="籍贯" align="center" prop="birthPlace" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="电子邮箱" align="center" prop="email" />
      <el-table-column label="学校" align="center" prop="school" />
      <el-table-column label="专业" align="center" prop="major" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="出生日期" align="center" prop="birthday" />
      <el-table-column label="岗位id" align="center" prop="postId" />
      <el-table-column label="意向岗位名称" align="center" prop="desiredPosition" />
      <el-table-column label="意向城市" align="center" prop="desiredCity" />
      <el-table-column label="最高学历（大专、本科、硕士、博士）" align="center" prop="highestEducation" />
      <el-table-column label="个人照片" align="center" prop="personalPhoto" />
      <el-table-column label="工作案例附件" align="center" prop="workCaseAttachment" />
      <el-table-column label="简历附件" align="center" prop="resumeAttachment" />
      <el-table-column label="是否内推（内部推荐、BOSS直聘、猎聘、智联招聘、脉脉、公众号）" align="center" prop="isInternalRef">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HRM_INTERNAL_REF" :value="scope.row.isInternalRef" />
        </template>
      </el-table-column>
      <el-table-column label="婚育情况（未婚、已婚未育、已婚已育）" align="center" prop="maritalStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HRM_MARITAL_STATUS" :value="scope.row.maritalStatus" />
        </template>
      </el-table-column>
      <el-table-column label="部门ID" align="center" prop="deptId" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['hrm:talent-pool:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['hrm:talent-pool:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <TalentPoolForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { TalentPoolApi, TalentPoolVO } from '@/api/hrm/talentpool'
import TalentPoolForm from './TalentPoolForm.vue'

/** 人才库 列表 */
defineOptions({ name: 'TalentPool' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<TalentPoolVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  recruitmentRequestId: undefined,
  name: undefined,
  gender: undefined,
  birthPlace: undefined,
  age: undefined,
  email: undefined,
  school: undefined,
  major: undefined,
  phone: undefined,
  birthday: undefined,
  postId: undefined,
  desiredPosition: undefined,
  desiredCity: undefined,
  highestEducation: undefined,
  personalPhoto: undefined,
  workCaseAttachment: undefined,
  resumeAttachment: undefined,
  isInternalRef: undefined,
  maritalStatus: undefined,
  deptId: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TalentPoolApi.getTalentPoolPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await TalentPoolApi.deleteTalentPool(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await TalentPoolApi.exportTalentPool(queryParams)
    download.excel(data, '人才库.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>