<!-- 完善个人信息 -->
<template>
  <v-app>
    <CtDialog
      :visible="dialog"
      :widthType="2"
      titleClass="text-h6"
      title="完善个人信息"
      :closeable="false"
      otherBtnText="退出登录"
      @other="handleLogout"
      @submit="simpleInfoSubmit"
    >
      <studentInfoForm v-if="isStudent" ref="formRef" :option="option"></studentInfoForm>
      <infoForm v-else ref="formRef" :option="option"></infoForm>
    </CtDialog>
    <Loading :visible="overlay"></Loading>
  </v-app>
</template>

<script setup>
defineOptions({name: 'dialogExtend-dialog'})
import infoForm from './infoForm.vue'
import studentInfoForm from './studentInfoForm.vue'
import { savePersonSimpleInfo, saveStudentSimpleInfo } from '@/api/recruit/personal/shareJob'
import CtDialog from '@/components/CtDialog'
import { useUserStore } from '@/store/user'; const userStore = useUserStore()
import { useRoute } from 'vue-router'; const route = useRoute()
import { useRouter } from 'vue-router'; const router = useRouter()
import Confirm from '@/plugins/confirm'
import { onMounted, ref } from 'vue'
import { getToken } from '@/utils/auth'
import Snackbar from '@/plugins/snackbar'
import { showImprovePersonaInfo } from '@/utils/whiteList'

const props = defineProps({
  // title: String,
  // text: String,
  // cancel: Function,
  sure: Function,
  other: Function,
  option: {
    type: Object,
    default: () => {}
  },
  setInfo: {
    type: Object,
    default: () => {}
  }
})

const dialog = ref(false)
const isStudent = ref(localStorage.getItem('chooseRole') === 'student')

// const isMobile = ref(false)
onMounted(() => {
  const parsedUrl = new URL(window.location.href)
  const pathName = parsedUrl.pathname
  // 在白名单内不弹窗
  if (showImprovePersonaInfo(pathName)) {
    props.sure() // 如果在白名单内->不打开弹窗且继续下一步调用
  } else {
    dialog.value = true // 打开弹窗
  }
  dialog.value = pathName ? Boolean(!showImprovePersonaInfo(pathName)) : true
})

const overlay = ref(false)
const formRef = ref()
const simpleInfoSubmit = async () => {
  if (!getToken()) {
    Confirm('系统提示', '登录失效，请重新登录', { hideCancelBtn: true }).then(() => {
      window.location.reload()
    })
    return
  }
  try {
    const obj = await formRef.value.getQuery()
    if (!obj) return
    overlay.value = true
    const apiFun = isStudent.value ? saveStudentSimpleInfo : savePersonSimpleInfo
    await apiFun(obj)
    Snackbar.success('保存成功')
    // const info = localStorage.getItem('baseInfo') ? JSON.parse(localStorage.getItem('baseInfo')) : {}
    // localStorage.setItem('baseInfo', JSON.stringify({ ...info, ...obj }))
    // localStorage.setItem('necessaryInfoReady', 'ready') //
    await useUserStore().getUserBaseInfos() // 更新用户信息
    if (isStudent.value) localStorage.removeItem('chooseRole')
    dialog.value = false
    overlay.value = false
    props.sure()
  } catch (error) {
    console.error('error', error)
  }
}

// 退出登录
const handleLogout = () => {
  Confirm('系统提示', '是否确定退出当前登录账号？').then(async () => {
    await userStore.userLogout(1)
    props.other()
    if (!route || route.path === '/recruitHome') location.reload()
    else router.push({ path: '/recruitHome' })
  })
}
</script>
<style lang="scss" scoped>
</style>
