<template>
  <div style="padding: 20px 30px">
    <div class="resume-header mb-3">
      <div class="resume-title">学生信息认证</div>
    </div>
    <div class="d-flex flex-column align-center pt-5">
      <CtForm ref="CtFormRef" :items="items" style="width: 900px;"></CtForm>
      <v-btn class="buttons mt-5" color="primary" @click.stop="handleSubmit">{{ $t('common.save') }}</v-btn>
    </div>
  </div>

  <Loading :visible="overlay"></Loading>
</template>

<script setup>
defineOptions({name: 'personal-personCenter-studentInformation-index'})
import { ref } from 'vue'
import { saveStudentSimpleInfo } from '@/api/recruit/personal/shareJob'
import { schoolList, departmentList, getStudentInfo } from '@/api/recruit/personal/resume'
import { useI18n } from '@/hooks/web/useI18n'
import Snackbar from '@/plugins/snackbar'
import { isValidIdCard18 } from '@/utils/validate'

const { t } = useI18n()

const overlay = ref(false)

const CtFormRef = ref()
const items = ref({
  options: [
    {
      type: 'autocomplete',
      key: 'schoolId',
      value: null,
      default: null,
      label: '就读学校 *',
      outlined: true,
      itemText: 'schoolName',
      itemValue: 'schoolId',
      rules: [v => !!v || '请选择就读学校'],
      items: [],
      change: e => getDepartmentList(e),
    },
    {
      type: 'autocomplete',
      key: 'schoolDepartmentName',
      value: null,
      default: null,
      label: '所在院系 *',
      outlined: true,
      itemText: 'departmentTitle',
      itemValue: 'schoolDepartmentId',
      rules: [v => !!v || '请选择所在院系'],
      items: []
    },
    {
      type: 'text',
      key: 'majorName',
      value: '',
      default: null,
      label: '所学专业 *',
      outlined: true,
      rules: [v => !!v || '请输入所学专业']
    },
    {
      type: 'text',
      key: 'schoolClassName',
      value: '',
      default: null,
      label: '所在班级 *',
      outlined: true,
      rules: [v => !!v || '请填写所在班级']
    },
    {
      type: 'text',
      key: 'studentNo',
      value: '',
      default: null,
      label: '学号 *',
      outlined: true,
      rules: [v => !!v || '请填写学号']
    },
    {
      type: 'text',
      key: 'idCardNo',
      value: '',
      label: '身份证号码 *',
      rules: [
        value => {
          if (!value) {
            return '请输入您的身份证号码'
          }
          return true
        },
        value => {
          if (!isValidIdCard18(value)) {
            return '请输入正确的身份证号码'
          }
          return true
        }
      ]
    },
    {
      type: 'text',
      key: 'emergencyContactName',
      value: '',
      default: null,
      label: '紧急联系人姓名 *',
      outlined: true,
      rules: [v => !!v || '请填写紧急联系人姓名']
    },
    {
      type: 'phoneNumber',
      key: 'emergencyContactPhone',
      value: '',
      clearable: true,
      label: '紧急联系人手机号 *',
      rules: [v => !!v || '请填写紧急联系人手机号']
    },
  ]
})

// 左侧加mr
items.value.options.forEach((e, index) => {
  e.col = 6
  if ((index + 2) % 2 === 0) e.flexStyle = 'mr-3'
})

// // 学校下拉列表
const getSchoolListData = async () => {
  const item = items.value.options.find(e => e.key === 'schoolId')
  if (!item) return
  const { records } = await schoolList({current: 1,size: 9999})
  item.items = records || []
}
getSchoolListData()

const getDepartmentList = async (e) => {
  const item = items.value.options.find(e => e.key === 'schoolDepartmentName')
  if (!item) return
  const query = {
    page: { size: 9999,	current: 1	},
    entity: { schoolId: e }
  }
  const res = await departmentList(query)
  const list = res?.records?.length ? res.records : []
  item.items = list.map(e => e.entity)
}

// 获取学生基本信息
const getStudentInfoFun = async () => {
  const data = await getStudentInfo()
  if (data.schoolId) getDepartmentList(data.schoolId)
  // 回显
  items.value.options.forEach(e => {
    if (data[e.key]) e.value = data[e.key]
  })
}
getStudentInfoFun()


// 提交
const handleSubmit = async () => {
  const { valid } = await CtFormRef.value.formRef.validate()
  if (!valid) return
  overlay.value = true
  const params = {}
  items.value.options.forEach(item => {
    params[item.key] = item.value
  })
  await saveStudentSimpleInfo(params)
  // getStudentInfoFun()
  setTimeout(async () => {
    Snackbar.success(t('common.submittedSuccessfully'))
    overlay.value = false
  }, 1000)
}

</script>

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