<!-- 选择简历 -->
<template>
  <!-- 已上传的简历列表 -->
  <selectResumeDialog
    v-model="showResume"
    :list="resumeList"
    :selectLocalFile="true"
    @handleLocalFileClick="selectLocalFileClick"
    @submit="handleSubmit"
    @close="handleClose"
  ></selectResumeDialog>
  <!-- 选择本地简历 -->
  <CtDialog
    :visible="openUploadDialog"
    :widthType="2"
    titleClass="text-h6"
    submitText="立即投递"
    @close="openUploadDialog = false"
    title="选择简历"
    @submit="uploadFileSubmit"
  >
  <uploadForm ref="uploadFormRef"></uploadForm>
  <div class="color-666" style="font-size: 13px;">提示：立即投递会将已上传的简历进行投递</div>
  </CtDialog>
</template>

<script setup>
// 上传附件
import uploadForm from '../form/upload.vue'
import { savePersonResumeCv } from '@/api/recruit/personal/resume'
// 选择简历
import selectResumeDialog from '@/views/recruit/personal/position/components/jobDetails/selectResumeDialog'
import { hireJobCvDelivery } from '@/api/recruit/personal/shareJob'
import { jobCvRelSend } from '@/api/position'
import { getPersonResumeCv } from '@/api/recruit/personal/resume'
import { useI18n } from '@/hooks/web/useI18n'; const { t } = useI18n()
import Snackbar from '@/plugins/snackbar'
import { ref } from 'vue'
defineOptions({name: 'shareJob-sendResume-select'})
const emit = defineEmits(['refresh', 'close'])
const props = defineProps({
  jobId: {
    type: String,
    default: ''
  },
  hire: {
    type: Boolean,
    default: false
  },
  userId: {
    type: String,
    default: ''
  },
})

const resumeList = ref([])
const getResumeList = async () => {
  resumeList.value = []
  const data = await getPersonResumeCv()
  if (!data?.length) {
    // 没有上传过简历
    openUploadDialog.value = true
    return
  }
  // 可以选择之前上传的简历
  resumeList.value = data
  showResume.value = true
}
getResumeList()

const uploadFormRef = ref()
const openUploadDialog = ref(false)
// 上传附件-提交
const uploadFileSubmit = async () => {
  const obj = await uploadFormRef.value.getQuery()
  if (!obj?.url || !obj?.fileName) return Snackbar.warning(t('resume.selectResumeToSubmit'))
  const query = { title: obj.fileName, url: obj.url }
  await savePersonResumeCv(query)
  handleSubmit(query, '上传的文件提交_直接投递')
}

const selectLocalFileClick = () => {
  openUploadDialog.value = true
}

const showResume = ref(false)
const selectResume = ref()
const handleSubmit = async (val, type = '') =>{
  selectResume.value = val
  if (!selectResume.value) return Snackbar.warning(t('resume.selectResumeToSubmit'))
  const obj = type ? val : resumeList.value.find(e => e.id === selectResume.value)
  if (!obj && !type) 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, type: props.hire ? 1 : 0  })
  Snackbar.success(t('resume.deliverySuccess'))
  // setTimeout(() => {
  // }, 3000)
  emit('refresh')
  handleClose()
}

const handleClose = () => {
  showResume.value = false
  openUploadDialog.value = false
  selectResume.value = null
  emit('close')
}

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