import { EnumDictName } from "@/common/enum";
import { useDictStore } from "@/store/dictStore";
import { useAppStore } from "@/store/appStore";
import { DictItem } from "@/types";
import { getExamMajors, getExamTypes, getGraduateYears, getProvinces } from "@/api/modules/system";
import { StudentExamInfo, UserInfo } from "@/types/user";
const dictStore = useDictStore();
const appStore = useAppStore();

export const useExamType = () => {
  const form = ref<Partial<Pick<StudentExamInfo, 'location' | 'examType' | 'endYear' | 'majorType'>>>({})
  const provinceList = ref<DictItem[]>([]);
  const examTypeList = ref<DictItem[]>([]);
  const examMajorList = ref<DictItem[]>([]);
  const endYearList = ref<DictItem[]>([]);
  const loadProvinceData = async () => {
    const { data } = await getProvinces();
    provinceList.value = data;
  }
  const loadExamTypeData = async () => {
    if (form.value.location) {
      uni.$ie.showLoading();
      const { data } = await getExamTypes(form.value.location);
      examTypeList.value = data;
      uni.$ie.hideLoading();
    }

  }
  const loadExamMajorData = async () => {
    if (form.value.location && form.value.examType) {
      const { data } = await getExamMajors(form.value.location, form.value.examType);
      examMajorList.value = data;
    } else {
      examMajorList.value = [];
    }
  }
  const loadGraduateYearData = async () => {
    if (form.value.location && form.value.examType) {
      const { data } = await getGraduateYears(form.value.location, form.value.examType);
      endYearList.value = data;
    } else {
      endYearList.value = [];
    }
  }
  watch(() => form.value.location, (val) => {
    form.value.examType = '';
    form.value.majorType = '';
    form.value.endYear = undefined;
    if (val) {
      loadExamTypeData();
    }
  });
  watch(() => form.value.examType, (val) => {
    if (val) {
      loadExamMajorData();
      loadGraduateYearData();
    }
  })
  loadProvinceData();

  return {
    form,
    provinceList,
    examTypeList,
    examMajorList,
    endYearList
  }
}