<!-- 快速填写-简易人才信息 -->
<template>
  <CtDialog
    :visible="openDialog"
    :widthType="2"
    :closeText="closeText"
    titleClass="text-h6"
    title="补充基本信息"
    :closeable="props.closeable"
    @close="openDialog = props.closeable ? false : true"
    @submit="simpleInfoSubmit"
  >
    <simpleInfoForm ref="formRef"></simpleInfoForm>
  </CtDialog>
</template>

<script setup>
import { getToken } from '@/utils/auth'
import simpleInfoForm from '../form/simpleInfo.vue'
import { savePersonSimpleInfo } from '@/api/recruit/personal/shareJob'
import { useI18n } from '@/hooks/web/useI18n'; const { t } = useI18n()
import Snackbar from '@/plugins/snackbar'
import { ref } from 'vue'
defineOptions({name: 'shareJob-sendResume-simple'})
const emit = defineEmits(['simpleInfoReady'])
const props = defineProps({
  closeable: {
    type: Boolean,
    default: true
  },
  closeText: {
    type: String,
    default: '取消'
  }
})

const openDialog = ref(false) // 默认不打开弹窗，先检验simpleInfoReady
const info = ref(null)

// 查询用户基本信息
const timer = ref(null)
timer.value = setInterval(() => { getUserInfoVerify() }, 100)
// 十秒后停止获取清除timer
setTimeout(() => { if (!info.value) getUserInfoFail() }, 10000);

// 查询用户基本信息
const getUserInfoVerify = () => {
  if (!getToken()) {
    clearInterval(timer.value); timer.value = null
    return
  }
  if (info.value) {
    if (timer.value) clearInterval(timer.value); timer.value = null
    const keyArr = ['name', 'phone', 'jobStatus', 'expType', 'eduType'] // 必填人才信息
    const simpleInfoReady = Object.keys(info.value).length && keyArr.every(e => info.value[e] && info.value[e] !== 0) // 校验必填人才信息
    if (simpleInfoReady) {
      emit('simpleInfoReady') // 存在
    } else {
      openDialog.value = true // 不存在
      Snackbar.warning('请先完善个人基本信息')
      localStorage.setItem('simpleCompleteDialogHaveBeenShow', true)
    }
  }
  info.value = JSON.parse(localStorage.getItem('baseInfo'))
}

// 查询用户基本信息-失败 
const getUserInfoFail = () => {
  if (timer.value) clearInterval(timer.value); timer.value = null
  Snackbar.error(t('login.getUserInfoFailed')+'，'+t('login.loginAgain'))
}

const formRef = ref()
const simpleInfoSubmit = async () => {
  try {
    const obj = await formRef.value.getQuery()
    if (!obj) return
    await savePersonSimpleInfo(obj)
    localStorage.setItem('baseInfo', JSON.stringify(obj))
    openDialog.value = false
    emit('simpleInfoReady')
  } catch (error) {
    console.error('error', error)
  }
}
</script>
<style lang="scss" scoped>
</style>
