<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter="handleQuery" class="!w-180px" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入联系电话" clearable @keyup.enter="handleQuery" class="!w-180px" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="queryParams.email" placeholder="请输入邮箱" clearable @keyup.enter="handleQuery" class="!w-180px" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" class="!w-180px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.MENDUNER_SEX)" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="学历" prop="eduType">
        <el-select v-model="queryParams.eduType" placeholder="请选择学历" class="!w-180px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.MENDUNER_EDUCATION_TYPE)" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工作经验" prop="expType">
        <el-select v-model="queryParams.expType" placeholder="请选择工作经验" class="!w-180px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.MENDUNER_EXP_TYPE)" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所在城市" prop="areaId">
        <el-cascader v-model="queryParams.areaId" :options="areaTreeData" :props="{ label: 'name', value: 'id', emitPath: false, checkStrictly: true }"  class="!w-180px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="primary" plain @click="handleAdd">
          <Icon icon="ep:plus" class="mr-5px" /> 新增人才
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true">
      <el-table-column label="头像" align="center" prop="avatar" width="70px" fixed="left">
        <template #default="scope">
          <el-image v-if="scope.row?.person.avatar" class="h-60px w-60px" :src="scope.row?.person.avatar" lazy preview-teleported :preview-src-list="[scope.row?.person.avatar]" fit="contain" />
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="person.name" fixed="left" />
      <el-table-column label="联系电话" align="center" prop="person.phone" />
      <el-table-column label="邮箱" align="center" prop="person.email" />
      <el-table-column label="性别" align="center" prop="person.sexStr" />
      <el-table-column label="出生日期" align="center" prop="person.birthday" :formatter="dateFormatter2" />
      <el-table-column label="学历" align="center" prop="person.eduTypeStr" />
      <el-table-column label="工作经验" align="center" prop="person.expTypeStr" />
      <el-table-column label="所在城市" align="center" prop="person.areaStr" />
      <el-table-column label="意向城市" align="center">
        <template #default="scope">
          {{ scope.row?.person?.interestedAreaStrList && scope.row?.person?.interestedAreaStrList.length ? scope.row?.person?.interestedAreaStrList.join('，') : '' }}
        </template>
      </el-table-column>
      <el-table-column label="求职意向" align="center">
        <template #default="scope">
          {{ scope.row?.person?.jobInterestedList && scope.row?.person?.jobInterestedList.length ? scope.row?.person?.jobInterestedList.join('，') : '' }}
        </template>
      </el-table-column>
      <el-table-column label="首次工作时间" align="center" prop="person.firstWorkTime" :formatter="dateFormatter2" />
      <el-table-column label="创建时间" align="center" prop="person.createTime" :formatter="dateFormatter" />
      <el-table-column label="操作" align="center" fixed="right" min-width="110">
        <template #default="scope">
          <el-button link type="primary" @click="openDetail(scope.row.person.id)">详情</el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.person.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup>
import { TalentMap } from '@/api/menduner/system/talentMap'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { getDict } from '@/hooks/web/useDictionaries'
import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'

/** 人才地图 列表 */
defineOptions({ name: 'TalentMap' })

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

const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  phone: undefined,
  sex: undefined,
  email: undefined,
  eduType: undefined,
  expType: undefined,
  areaId: undefined,
})
const queryFormRef = ref() // 搜索的表单

// 地区列表
const areaTreeData = ref([])
const getDictData = async () => {
  const { data } = await getDict('areaTreeData', {}, 'areaTreeData')
  const obj = data.find(e => e.name === '中国')
  const list = obj?.children ? obj.children.map(e =>{
    // 市辖区直接显示区
    const municipality = e.children && e.children.length && e.children[0].name === '市辖区'
    if (municipality && e.children[0].children?.length) e.children = e.children[0].children
    return e
  }) : []
  areaTreeData.value = list.length ? list : []
}
getDictData()

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TalentMap.getTalentMapInfoPage(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 { push } = useRouter()
const openDetail = (id) => {
  push({ name: 'TalentMapDetail', params: { id } })
}

/** 删除按钮操作 */
const handleDelete = async (id) => {
  if (!id) return message.warning('删除失败，请稍后再试')
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await TalentMap.deleteTalentMap(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const handleAdd = () => {
  push({ name: 'TalentMapDetail', params: { id: 'add' } })
}

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