<template>
  <ie-page bg-color="#F6F8FA" :safeAreaInsetBottom="false">
    <ie-navbar title="完善信息" custom-back @left-click="handleBack" />
    <uv-form labelPosition="left" :model="examTypeForm" labelWidth="70px" ref="formRef">
      <content-card title="基本信息">
        <uv-form-item label="所在省份" prop="location" borderBottom required>
          <ie-picker ref="pickerRef" v-model="examTypeForm.location" :list="provinceList" placeholder="选择省份"
            :custom-style="customStyle" key-label="dictLabel" key-value="dictValue"></ie-picker>
        </uv-form-item>
        <uv-form-item label="考生类别" prop="examType" borderBottom required>
          <ie-picker ref="pickerRef" v-model="examTypeForm.examType" :list="examTypeList"
            :disabled="!examTypeForm.location" placeholder="选择考生类别" :custom-style="customStyle" key-label="dictLabel"
            key-value="dictValue"></ie-picker>
        </uv-form-item>
        <uv-form-item label="毕业年份" prop="year" required>
          <ie-picker ref="pickerRef" v-model="examTypeForm.endYear" :list="endYearList"
            :disabled="!examTypeForm.examType" placeholder="选择毕业年份" :custom-style="customStyle" key-label="dictLabel"
            key-value="dictValue"></ie-picker>
        </uv-form-item>
      </content-card>
    </uv-form>
    <ie-safe-toolbar :height="84" :shadow="false">
      <view class="px-30 py-16">
        <ie-button @click="handleSubmit">确认提交</ie-button>
      </view>
    </ie-safe-toolbar>
  </ie-page>
</template>

<script lang="ts" setup>
import ContentCard from '@/pagesSystem/components/content-card.vue';
import { useExamType } from '@/composables/useExamType';
import { updateUserInfo } from '@/api/modules/login';
import { useUserStore } from '@/store/userStore';
import { UserInfo } from '@/types/user';
import { useTransferPage } from '@/hooks/useTransferPage';
const { prevData, transferTo, transferBack } = useTransferPage();
const { form: examTypeForm, examTypeList, examMajorList, provinceList, endYearList } = useExamType();
const userStore = useUserStore();
const customStyle = {
  paddingLeft: '26px'
};
const handleBack = () => {
  uni.$ie.showToast('请先完善信息');
};
const handleSubmit = async () => {
  console.log('handleSubmit', examTypeForm.value)
  const { location, examType, endYear } = examTypeForm.value;
  if (!location) {
    uni.$ie.showToast('请先选择所在省份');
    return;
  }
  if (!examType) {
    uni.$ie.showToast('请先选择考试类别');
    return;
  }
  if (!endYear) {
    uni.$ie.showToast('请先选择毕业年份');
    return;
  }
  const params = {
    ...userStore.userInfo,
    location,
    examType,
    endYear,
  } as UserInfo;
  uni.$ie.showLoading();
  await updateUserInfo(params);
  await userStore.getUserInfo();
  uni.$ie.hideLoading();
  uni.$ie.showToast('保存成功');
  setTimeout(() => {
    transferTo('/pagesMain/pages/index/index', {
      type: 'reLaunch'
    });
  }, 800);
};
</script>

<style></style>
