<template>
  <el-table v-loading="loading" :data="tableData" :stripe="true" class="m-t-20px">
    <el-table-column label="职位名称" align="center" prop="job.name" />
    <el-table-column label="发布企业" align="center" prop="enterprise.anotherName">
      <template #default="scope">{{ scope.row.enterprise.anotherName || scope.row.enterprise.name}}</template>
    </el-table-column>
    <el-table-column label="地区" align="center" prop="job.areaName">
      <template #default="scope">{{ !scope.row.job.areaId ? '全国' : scope.row.job.areaName }}</template>
    </el-table-column>
    <el-table-column label="薪资" align="center" prop="job.payFrom">
      <template #default="scope">
        <span v-if="scope.row.job.payFrom && scope.row.job.payTo">
          {{ scope.row.job.payFrom }} - {{ scope.row.job.payTo }}/{{ payUnit.find(e => e.value === Number(scope.row.job.payUnit))?.label }}
        </span>
        <span v-else>面议</span>
      </template>
    </el-table-column>
    <el-table-column label="投递附件名称" align="center" prop="cvRel.title" />
    <el-table-column label="投递时间" align="center" prop="cvRel.createTime" :formatter="dateFormatter" width="180px" />
    <el-table-column label="投递状态" align="center">
      <template #default="scope">
        {{ scope.row.cvRel.status === '0' ? '未查看' : '已查看'}}
      </template>
    </el-table-column>
    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button link type="primary" @click="openDetail(scope.row)" class="m-r-10px">详情</el-button>
        <el-link type="primary" download :href="scope.row.cvRel.url" :underline="false" target="_blank">附件下载</el-link>
      </template>
    </el-table-column>
  </el-table>

  <Pagination
    :total="total"
    v-model:page="queryParams.pageNo"
    v-model:limit="queryParams.pageSize"
    @pagination="getList"
  />

  <Dialog title="职位详情" v-model="dialogVisible">
    <el-descriptions :column="1" border>
      <el-descriptions-item label="职位名称">{{ itemData.name }}</el-descriptions-item>
      <el-descriptions-item label="发布企业">{{ itemData.enterpriseName }}</el-descriptions-item>
      <el-descriptions-item label="职位类型">{{ itemData.positionName }}</el-descriptions-item>
      <el-descriptions-item label="工作城市">{{ !itemData.areaId ? '全国' : itemData.areaName }}</el-descriptions-item>
      <el-descriptions-item label="详细地址">{{ itemData.address }}</el-descriptions-item>
      <el-descriptions-item label="薪资">
        <span v-if="itemData.payFrom && itemData.payTo">
          {{ itemData.payFrom }} - {{ itemData.payTo }}/{{ payUnit.find(e => e.value === Number(itemData.payUnit))?.label }}
        </span>
        <span v-else>面议</span>
      </el-descriptions-item>
      <el-descriptions-item label="投递附件名称">
        <el-link type="primary" download :href="itemData.title" :underline="false" target="_blank">{{ itemData.title }}</el-link>
      </el-descriptions-item>
      <el-descriptions-item label="投递时间">
        {{ itemData.time ? timesTampChange(itemData.time, 'Y-M-D h:m:s') : '' }}
      </el-descriptions-item>
      <el-descriptions-item label="投递状态">
        {{ itemData.cvStatus === '0' ? '未查看' : '已查看' }}
      </el-descriptions-item>
      <el-descriptions-item label="学历要求">
        <dict-tag :type="DICT_TYPE.MENDUNER_EDUCATION_TYPE" :value="itemData.eduType" />
      </el-descriptions-item>
      <el-descriptions-item label="工作经验">
        <dict-tag :type="DICT_TYPE.MENDUNER_EXP_TYPE" :value="itemData.expType" />
      </el-descriptions-item>
      <el-descriptions-item label="刷新时间">{{ timesTampChange(itemData.updateTime, 'Y-M-D h:m') }}</el-descriptions-item>
      <el-descriptions-item label="到期时间">
        {{ itemData.expireTime ? timesTampChange(itemData.expireTime, 'Y-M-D') : '长期有效' }}
      </el-descriptions-item>
      <el-descriptions-item label="职位关键字">
        <el-tag type="primary" v-for="k in itemData.tagList" :key="k" class="m-r-5px m-b-5px">{{ k }}</el-tag>
      </el-descriptions-item>
    </el-descriptions>
    <template #footer>
      <el-button @click="dialogVisible = false; itemData = {}">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup>
defineOptions({ name: 'PersonEduList'})
import { PersonInfoApi } from '@/api/menduner/system/person'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { timesTampChange } from '@/utils/transform/date'
import { dealDictObjData } from '@/utils/transform/position'

const props = defineProps({
  userId: String
})

const loading = ref(false)
const tableData = ref([])
const total = ref(0)
const payUnit = getIntDictOptions(DICT_TYPE.MENDUNER_PAY_UNIT)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: props.userId
})

const getList = async () => {
  loading.value = true
  try {
    const data = await PersonInfoApi.getPersonJobDeliveryList(queryParams)
    tableData.value = data.list.map(e => {
      e.job = dealDictObjData({}, e.job)
      return e
    })
    total.value = data.total
  } finally {
    loading.value = false
  }
}
getList()

// 职位详情
const itemData = ref({})
const dialogVisible = ref(false)
const openDetail = (item) => {
  itemData.value = { 
    ...item.job, 
    enterpriseName: item.enterprise.anotherName || item.enterprise.name, 
    url: item.cvRel.url, 
    title: item.cvRel.title, 
    time: item.cvRel.createTime,
    cvStatus: item.cvRel.status
  }
  console.log(itemData.value, '===========')
  dialogVisible.value = true
}
</script>
