import { onMounted, ref, watch } from "vue";
import { cascaderAreaList } from "@/api/system/area";
import { listSchool } from "@/api/dz/school";
import { listClasses } from "@/api/dz/classes";
import { listCampus } from "@/api/dz/campus";
import { getExamTypes } from "@/api/dz/cards";

const defaultOptions = {
  autoLoad: true, // 是否自动加载省份
  loadCampus: false, // 切换省份时是否自动加载培训学校
  loadCampusClass: false, // 切换培训学校时是否自动加载培训班级
  loadClass: false, // 切换注册学校时是否自动加载注册班级
  loadExamType: false, // 切换省份时是否自动加载考生类型
};
const useSchool = (options = {}) => {
  const { autoLoad, loadCampus, loadCampusClass, loadClass, loadExamType } = Object.assign(
    defaultOptions,
    options
  );

  const area = ref({
    list: [],
    selected: [],
    foreignKeyValue: null,
  })

  // const areaList = ref([]);
  // const selectedArea = ref([]);

  const schoolList = ref([]);
  const selectedSchool = ref();
  const selectedAssignSchool = ref();

  const classList = ref([]);
  const selectedClass = ref();

  const campusList = ref([]);
  const selectedCampus = ref();

  const campusClassList = ref([]);
  const selectedCampusClass = ref();

  const examTypeList = ref([]);
  const selectedExamType = ref("");

  const getExamTypeList = async () => {
    examTypeList.value = [];
    const res = await getExamTypes((getProvinceName() || "").replace("省", ""));
    examTypeList.value = res.data;
    return res.data;
  };

  const getAreaList = async () => {
    const rows = await cascaderAreaList();
    // areaList.value = rows;
    return rows;
  };

  const getSchoolList = async () => {
    schoolList.value = [];
    const areaId = Array.isArray(selectedArea.value)
      ? selectedArea.value?.[selectedArea.value.length - 1]
      : selectedArea.value;
    const res = await listSchool({
      areaCode: areaId,
      pageNo: 1,
      pageSize: 100000,
    });
    schoolList.value = res.rows;
    return res.rows;
  };

  const getClassList = async () => {
    classList.value = [];
    const res = await listClasses({
      schoolId: selectedSchool.value,
      pageNo: 1,
      pageSize: 100000,
    });
    classList.value = res.rows;
    return res.rows;
  };

  const getCampusList = async () => {
    const areaId = Array.isArray(selectedArea.value)
      ? selectedArea.value?.[selectedArea.value.length - 1]
      : selectedArea.value;
    const res = await listCampus({
      areaCode: areaId,
    });
    campusList.value = res.rows;
    return res.rows;
  };

  const getCampusClassList = async () => {
    campusClassList.value = [];
    const res = await listClasses({
      campusId: selectedCampus.value,
    });
    campusClassList.value = res.rows;
    return res.rows;
  };

  watch(
    () => selectedArea.value,
    (newVal) => {
      if (schoolList.value.length > 0) {
        selectedSchool.value = null;
        selectedAssignSchool.value = null;
        schoolList.value = [];
      }
      if (campusList.value.length > 0) {
        selectedCampus.value = null;
        campusList.value = [];
      }
      if (examTypeList.value.length > 0) {
        selectedExamType.value = null;
        examTypeList.value = [];
      }
      if (isAreaValid(newVal)) {
        getSchoolList();
        if (loadCampus) {
          getCampusList();
        }
        if (loadExamType) {
          getExamTypeList();
        }
      }
    }
  );

  watch(
    () => selectedSchool.value,
    (newVal) => {
      if (classList.value.length > 0) {
        selectedClass.value = null;
        classList.value = [];
      }
      if (newVal !== null) {
        if (loadClass) {
          getClassList();
        }
      }
    }
  );

  watch(
    () => selectedCampus.value,
    (newVal) => {
      if (campusClassList.value.length > 0) {
        selectedCampusClass.value = null;
        campusClassList.value = [];
      }
      if (newVal !== null) {
        if (loadCampusClass) {
          getCampusClassList();
        }
      }
    }
  );

  const getProvinceName = () => {
    const areaId = Array.isArray(selectedArea.value)
      ? selectedArea.value?.[selectedArea.value.length - 1]
      : selectedArea.value;
    const area = areaList.value.find((item) => item.areaId === areaId);
    return area?.areaName;
  };

  const init = () => {
    getAreaList();
  };

  const reset = () => {
    selectedArea.value = Array.isArray(selectedArea.value) ? [] : null;
    selectedSchool.value = null;
    selectedAssignSchool.value = null;
    selectedClass.value = null;
    selectedCampus.value = null;
    selectedCampusClass.value = null;
    selectedExamType.value = "";
  };

  const isAreaValid = (value) => {
    return (
      (!Array.isArray(value) && value !== null) ||
      (Array.isArray(value) && value.length > 0)
    );
  };

  onMounted(() => {
    if (autoLoad) {
      getAreaList();
    }
  });

  return {
    init,
    reset,

    areaList,
    selectedArea,
    getAreaList,

    schoolList,
    selectedSchool,
    selectedAssignSchool,

    classList,
    selectedClass,
    getClassList,

    campusList,
    selectedCampus,
    getCampusList,

    campusClassList,
    selectedCampusClass,
    getCampusClassList,

    examTypeList,
    selectedExamType,
    getExamTypeList,
  };
};
export default useSchool;
