import { EnumDictName, EnumExamType } from "@/common/enum";
import { useDictStore } from "@/store/dictStore";
import { useAppStore } from "@/store/appStore";
import { DictItem, System } 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' | 'examMajor'>>>({})
  const provinceList = ref<System.ProvinceItem[]>([]);
  const examTypeList = ref<DictItem[]>([]);
  const examMajorList = ref<DictItem[]>([]);
  const endYearList = ref<DictItem[]>([]);
  const loadProvinceData = async () => {
    const { rows } = await getProvinces();
    provinceList.value = rows;
  }
  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 (location: string, examType: EnumExamType) => {
    if (location && examType) {
      const { data } = await getExamMajors(location, examType);
      examMajorList.value = data;
    } else {
      examMajorList.value = [];
    }
    return 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) => {
    if (examTypeList.value.length) {
      form.value.examType = undefined;
    }
    if (examMajorList.value.length) {
      form.value.examMajor = '';
    }
    if (endYearList.value.length) {
     form.value.endYear = undefined; 
    }
    if (val) {
      loadExamTypeData();
    }
    if (val && form.value.examType) {
      loadExamMajorData(val, form.value.examType);
    }
    loadGraduateYearData();
  });
  watch(() => form.value.examType, (val) => {
    if (val) {
      loadGraduateYearData();
    }
    if (val && form.value.location) {
      loadExamMajorData(form.value.location, val);
    }
  })
  loadProvinceData();

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