<!-- 选择简历 -->
<template>
  <selectResumeDialog v-model="showResume" :list="resumeList" @submit="handleSubmit" @close="handleClose"></selectResumeDialog>
  <CtDialog
    :visible="showUploadFile"
    :widthType="2"
    titleClass="text-h6"
    @close="showUploadFile = false"
    title="简历上传"
    @submit="uploadFileSubmit"
  >
    <DeliveryForm ref="deliveryFormRef"></DeliveryForm>
  </CtDialog>
  <CtDialog
    :visible="baseInfoCompleteDialog"
    :widthType="2"
    titleClass="text-h6"
    @close="baseInfoCompleteDialog = false"
    title="快速填写"
    @submit="baseInfoSubmit"
  >
    <baseInfoCompleteForm ref="baseInfoCompleteFormRef" :baseInfo="baseInfo"></baseInfoCompleteForm>
  </CtDialog>
</template>

<script setup>
defineOptions({name: 'shareJob-handleDeliveryCom'})
import { nextTick, ref } from 'vue'
import selectResumeDialog from '@/views/recruit/personal/position/components/jobDetails/selectResumeDialog'
import Snackbar from '@/plugins/snackbar'
import { useI18n } from '@/hooks/web/useI18n'; const { t } = useI18n()
import { 
  // jobCvRelCheckSend, 
  jobCvRelSend 
} from '@/api/position'
import { hireJobCvDelivery, savePersonSimpleInfo } from '@/api/recruit/personal/shareJob'
import DeliveryForm from './deliveryForm.vue'
import baseInfoCompleteForm from './baseInfoCompleteForm.vue'
import { getPersonResumeCv, savePersonResumeCv } from '@/api/resume'

const emits = defineEmits(['refresh'])
const props = defineProps({
  jobId: {
    type: String,
    default: ''
  },
  hire: {
    type: Boolean,
    default: false
  },
  userId: {
    type: String,
    default: ''
  },
  baseInfo: {
    type: Object,
    default: () => {}
  }
})
const showResume = ref(false)
const showUploadFile = ref(false)
const resumeList = ref([])

// 填写必填信息
const baseInfoReady1 = ref(false)
const baseInfoCompleteDialog = ref(false)
const baseInfoCompleteFormRef = ref()
const baseInfoSubmit = async () => {
  const appPersonInfoSaveSimpleReqVO = await baseInfoCompleteFormRef.value.getQuery()
  await savePersonSimpleInfo(appPersonInfoSaveSimpleReqVO)
  localStorage.setItem('baseInfo', JSON.stringify(appPersonInfoSaveSimpleReqVO))
  baseInfoReady1.value = true
  baseInfoCompleteDialog.value = false
  inspectionProcess()
  // nextTick(() => { baseInfoCompleteDialog.value = false; inspectionProcess() })
}

const deliveryFormRef = ref()
const uploadFileSubmit = async () => {
  const obj = await deliveryFormRef.value.getQuery()
  await savePersonResumeCv({ title: obj.fileName, url: obj.url })
  showUploadFile.value = false
  inspectionProcess()
}

const inspectionProcess = async () => {
  try {
    // 已登录情况下流程：
    //
    // * 必填人才信息不完全 -> 不符合快速投递，进入完善人才信息流程
    const info = JSON.parse(localStorage.getItem('baseInfo'))
    const keyArr = ['name', 'phone', 'jobStatus', 'expType', 'eduType'] // 必填人才信息
    const baseInfoReady = keyArr.every(e => info[e] && info[e] !== 0) // 校验必填人才信息
    if (!baseInfoReady) {
      Snackbar.warning('请先完善个人基本信息')
      nextTick(() => { baseInfoCompleteDialog.value = true })
      return
    }
    // * 查看简历列表
    const data = await getPersonResumeCv()
    resumeList.value = data
    // * 上传简历 (已登录但未上传简历)
    if (!data?.length) {
      nextTick(() => { showUploadFile.value = true })
      return
    }
    // * 简历选择
    if (data?.length) nextTick(() => { showResume.value = true })
  } catch (error) {
    console.error('error', error)
  }
}
inspectionProcess()

// 简历投递
const selectResume = ref()
const handleClose = () => {
  showResume.value = false
  selectResume.value = null
}

const handleSubmit = async (val) =>{
  selectResume.value = val
  if (!selectResume.value) return Snackbar.warning(t('resume.selectResumeToSubmit'))
  const obj = resumeList.value.find(e => e.id === selectResume.value)
  if (!obj) return Snackbar.warning(t('resume.selectedResumeNotExist'))

  // 区分普通职位跟众聘职位投递
  if (props.hire) await hireJobCvDelivery({ jobId: props.jobId, recommendUserId: props.userId, url: obj.url })
  else await jobCvRelSend({ jobId: props.jobId, title: obj.title, url: obj.url })

  // setTimeout(() => {
    Snackbar.success(t('resume.deliverySuccess'))
  // }, 3000)
  emits('refresh')
  handleClose()
}

</script>
<style lang="scss" scoped>
</style>
