<!-- 校验是否完善人才必填信息 -->
<template>
  <scroll-view class="scrollBox" scroll-y="true">
    <view class="content">
      <view class="text-center ss-m-b-50 font-size-20 color-primary">学生信息认证</view>
      <uni-forms
        ref="baseInfoRef"
        v-model="formData"
        :rules="formRules"
        validateTrigger="bind"
        label-width="90px"
        labelAlign="center"
      >
        <uni-forms-item name="schoolId" label="就读学校" required>
          <uni-data-picker v-model="formData.schoolId" :localdata="schools" :clear-icon="false" popup-title="请选择就读学校" @change="getDepartmentList" :map="{ text: 'schoolName', value: 'schoolId' }"></uni-data-picker>
        </uni-forms-item>
        <uni-forms-item name="schoolDepartmentName" label="所在院系" required>
          <uni-data-picker v-model="formData.schoolDepartmentName" :localdata="schoolDepartmentList" :clear-icon="false" popup-title="请选择所在院系" :map="{ text: 'departmentTitle', value: 'schoolDepartmentId' }"></uni-data-picker>
        </uni-forms-item>
        <uni-forms-item name="majorName" label="所学专业" required>
          <uni-easyinput placeholder="请输入所学专业" v-model="formData.majorName" :inputBorder="false" type="text"></uni-easyinput>
        </uni-forms-item>
        <uni-forms-item name="schoolClassName" label="所在班级" required>
          <uni-easyinput placeholder="请填写所在班级" v-model="formData.schoolClassName" :inputBorder="false" type="text"></uni-easyinput>
        </uni-forms-item>
        <uni-forms-item name="studentNo" label="学号" required>
          <uni-easyinput placeholder="请填写学号" v-model="formData.studentNo" :inputBorder="false" type="text"></uni-easyinput>
        </uni-forms-item>
        <uni-forms-item name="idCardNo" label="身份证号码" required>
          <uni-easyinput placeholder="请输入身份证号码" v-model="formData.idCardNo" :inputBorder="false" type="text"></uni-easyinput>
        </uni-forms-item>
        <uni-forms-item name="emergencyContactName" label="紧急联系人姓名" required>
          <uni-easyinput placeholder="请填写紧急联系人姓名" v-model="formData.emergencyContactName" :inputBorder="false" type="text"></uni-easyinput>
        </uni-forms-item>
        <uni-forms-item name="emergencyContactPhone" label="紧急联系人手机号" required>
          <uni-easyinput placeholder="请填写紧急联系人手机号" v-model="formData.emergencyContactPhone" :inputBorder="false" type="number"></uni-easyinput>
        </uni-forms-item>
      </uni-forms>
      <view class="f-horizon-center">
        <button type="primary" size="default" class="send-button" @click="submit">保 存</button>
      </view>
    </view>
  </scroll-view>
</template>
<script setup>
import { ref, unref } from 'vue'
import { mobile, isValidIdCard18 } from '@/utils/validate'
import { saveStudentSimpleInfo, getStudentInfo } from '@/api/user'
// import { userStore } from '@/store/user'; const useUserStore = userStore()
import { schoolList, departmentList } from '@/api/pickerData'

const baseInfoRef = ref()
const formData = ref({ // 必填项目
  schoolId: null,
  schoolDepartmentName: null,
  majorName: null,
  schoolClassName: null,
  studentNo: null,
  idCardNo: null,
  emergencyContactName: null,
  emergencyContactPhone: null,
})

// 获取学生基本信息
const studentInfoFun = async () => {
  const { data } = await getStudentInfo()
  // 回显
  formData.value = { ...formData.value, ...data }
  getDepartmentList()
}
studentInfoFun()

const schools = ref([])
// // 学校下拉列表
const getSchoolListData = async () => {
  const res = await schoolList({current: 1, size: 9999})
  schools.value = res?.data?.records?.length ? res.data.records : []
}
getSchoolListData()

const schoolDepartmentList = ref([])
const getDepartmentList = async (e) => {
  if (!formData.value?.schoolId) return
  const query = {
    page: { size: 9999,	current: 1	},
    entity: { schoolId: formData.value.schoolId }
  }
  schoolDepartmentList.value = []
  if (e) formData.value.schoolDepartmentName = null // 切换学校的时清院系
  const res = await departmentList(query)
  const list = res?.data?.records?.length ? res.data.records : []
  schoolDepartmentList.value = list.map(e => e.entity)
}

const formRules = {
  idCardNo: isValidIdCard18,
  phone: mobile,
  schoolId:{
		rules: [{required: true, errorMessage: '请选择所在院系' }]
	},
  schoolDepartmentName:{
		rules: [{required: true, errorMessage: '请输入所学专业' }]
	},
  majorName: {
		rules: [{required: true, errorMessage: '请填写所在班级' }]
	},
  schoolClassName: {
		rules: [{required: true, errorMessage: '请填写学号' }]
	},
  studentNo: {
		rules: [{required: true, errorMessage: '请填写紧急联系人姓名' }]
	},
  emergencyContactName: {
		rules: [{required: true, errorMessage: '请填写紧急联系人手机号' }]
	},
  emergencyContactPhone: mobile,
}

const submit = async () => {
  const validate = await unref(baseInfoRef).validate()
  if (!validate) return uni.showToast({ title: '请将信息补充完整', icon: 'none' })
  try {
    await saveStudentSimpleInfo(formData.value)
    uni.showToast({
			icon: 'success',
			title: '保存成功'
		})
    // await useUserStore.getInfo()
		setTimeout(() => {
      uni.navigateBack({
        delta: 1
      })
    }, 1000)
  } catch (err) {
    uni.showToast({ title: err?.msg || '保存失败', icon: 'none' })
  }
}

</script>
<style lang="scss" scoped>
.scrollBox {
  width: 100vw;
  // height: 100vh;
  height: calc(100vh - 30rpx);
  margin-bottom: 30rpx;
}
.content {
  padding: 30rpx;
}

.changeRole {
  color: var(--color-666);
  font-size: 15px;
  line-height: 26px;
  margin-bottom: 40rpx;
}
.upload-img{
  position: relative;
  width: 200rpx;
  height: 200rpx;
  border: 1px solid #f1f1f1;
  margin: 10rpx;
}
.upload-file{
  width: 200rpx;
  height: 200rpx;
  border: 1px solid #f1f1f1;
  margin: 10rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 10rpx;
}
</style>
