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

<script setup>
defineOptions({name: 'shareJob-handleDeliveryCom'})
import { 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 } from '@/api/recruit/personal/shareJob'
import DeliveryForm from './deliveryForm.vue'
import { getPersonResumeCv } 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 baseInfoCompleteDialog = ref(false)
const resumeList = ref([])

//   if (baseInfoReady) {
//     // 必填人才信息已存在 -> 符合快速投递，进入查看是否存在简历
//   } else {
//     // 必填人才信息不完全 -> 不符合快速投递，进入完善人才信息流程
//   }
const inspectionProcess = async () => {
  try {
    // 已登录情况下: 1.是否投递过简历 2.是否有上传简历 3.已登录但未上传过简历->快速投递
    // const res = await jobCvRelCheckSend({ jobId: props.jobId }) //是否投递过简历
    // if (res) return Snackbar.warning(t('resume.alreadyResume'))
    //
    if (!props.baseInfo?.baseInfoReady) {
      baseInfoCompleteDialog.value = true
      return
    }
    const data = await getPersonResumeCv()// 简历列表
    resumeList.value = data
    //
    if (data?.length) showResume.value = true // 简历选择
    else showUploadFile.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>
