import { defineStore } from "pinia";
import { ref } from "vue";
// 分析相关 状态存储
export interface FilterObject {
  subjectCode: string;
  subjectName: string;
  subjectId: string;
  subjectGroupType: number;
  isTotal: boolean;
  subjectGroupCodes: string;
  schoolId: string;
  schoolLevel: string;
  schoolGroupId: string;
  schoolGroupName: string | null;
  schoolName: string | null;
  schoolGroupNames: string;
  registrationType: string;
  registrationName: string | null;
  registrationGroupId: string;
  statusGroupNames: string;
  scoreType: string;
  classType: string;
  classIdCode: string;
  classLevel: string | number;
  classGroupId: string;
  classGroupName: string;
  classGroupNames: string[];
}
//用户相关的状态管理
export const useAnalysisStore = defineStore("analysis", () => {
  const filterObject = ref<FilterObject>(
    JSON.parse(localStorage.getItem("filterObject") || "{}"),
  );
  const analysisExamInfo = ref<any>(
    JSON.parse(localStorage.getItem("analysisExamInfo") || "{}"),
  );
  const filtersData = ref<any>(
    JSON.parse(localStorage.getItem("filtersData") || "[]"),
  );
  // 设置筛选数据
  const setFiltersData = (info: any) => {
    filtersData.value = info;
    // 同步存储到 localStorage，确保持久化
    localStorage.setItem("filtersData", JSON.stringify(info));
  }
  // 设置公共参数
  const setFilterObject = (info: FilterObject) => {
    filterObject.value = info;
    // 同步存储到 localStorage，确保持久化
    localStorage.setItem("filterObject", JSON.stringify(info));
  }
  const setAnalysisExamInfo = (info: any) => {
    analysisExamInfo.value = info;
     // 同步存储到 localStorage，确保持久化
    localStorage.setItem("analysisExamInfo", JSON.stringify(info));
  }
  const childMenuPath = ref<string>(localStorage.getItem("childMenuPath") || '')//子级菜单路径
  // 设置子级菜单路径
  const setChildMenuPath = (info: string) => {
    childMenuPath.value = info;
    // 同步存储到 localStorage，确保持久化
    localStorage.setItem("childMenuPath", info);
  }
  // 是否显示分组分析菜单
  const isShowGroupAnalysis = ref<boolean>(localStorage.getItem("isShowGroupAnalysis") === "true");////是否显示分组分析菜单
  // 设置是否显示分组分析菜单
  const setIsShowGroupAnalysis = (info: boolean) => {
    isShowGroupAnalysis.value = info;
    // 同步存储到 localStorage，确保持久化
    localStorage.setItem("isShowGroupAnalysis", info.toString());
  }
  // 清除 localStorage 数据并重置 store 状态
  const removeLocalStorage = () => {
    // 1. 清除 localStorage 数据
    localStorage.removeItem("filterObject");
    localStorage.removeItem("analysisExamInfo");
    localStorage.removeItem("filtersData");
    localStorage.removeItem("childMenuPath");
    localStorage.removeItem("isShowGroupAnalysis");

    // 2. 重置 store 中的响应式状态
    filterObject.value = {} as FilterObject; // 重置为空对象（可根据业务需要设置默认值）
    analysisExamInfo.value = {};
    filtersData.value = [];
    childMenuPath.value = '';
    isShowGroupAnalysis.value = false;
  }
  return {
    filtersData,
    filterObject,
    analysisExamInfo,
    childMenuPath,
    isShowGroupAnalysis,
    setFiltersData,
    setFilterObject,
    setAnalysisExamInfo,
    setChildMenuPath,
    setIsShowGroupAnalysis,
    removeLocalStorage
  };
});
