import { createStore, Store, useStore } from "vuex";
import { login } from "@/api/login/login";
import { initLedger, queryConfig } from "@/api/statistics/index";
import { set } from "xe-utils";
// const store = useStore();

// 定义状态接口
interface State {
  count: number;
  token: string;
  deptId: string;
  timeType: number;
  timeValue: string[];
  seriousNotice: number;
  normalNotic: number;
  voiceType: number;
  screenNotic: number;
  eventTypeList: string[];
  eventValue: number;
  eventDuration: number;
  realData: []; //实时数据
  iframeLoad: boolean;
  showMap: boolean;
}

// 初始状态
const state: State = {
  count: 0,
  token: "",
  deptId: "10001",
  timeType: 3, //类型(1年 2季度 3月份 4周 5日)")
  timeValue: [], //类型(1年 2季度 3月份 4周 5日)")
  seriousNotice: 0, //严重通知(0关闭 1开启)
  normalNotic: 1, //普通通知(0关闭 1开启)
  voiceType: 1, //语音类型(1人声音 2音频)
  screenNotic: 1, //屏幕通知(0关闭 1开启)
  eventTypeList: [], //触发类型(1告警 2事件)
  eventValue: 0.7,
  eventDuration: 5,
  realData: [], //实时数据
  iframeLoad: false,//无锡接线图是否加载完成
  showMap: true,//无锡地图显示
};

// 定义Mutation类型
enum Mutations {
  INCREMENT = "INCREMENT",
  SET_STATE= "SET_STATE",
  SET_TOKEN = "SET_TOKEN",
  SET_TIME = "SET-TIME",
  SET_CONFIG = "SET-CONFIG-TIME",
}

export default createStore({
  state,
  mutations: {
    [Mutations.INCREMENT](state: State) {
      state.count++;
    },
    [Mutations.SET_STATE](state: State, data: any) {
      console.log("🚀 ~ data:", data)
      state[data.key] = data.value;
    },
    [Mutations.SET_TOKEN](state: State, data: any) {
      window.sessionStorage.setItem("token", data.token);
      window.sessionStorage.setItem("deptId", data.deptId);
      state.token = data.token;
      state.deptId = data.deptId;
    },
    [Mutations.SET_TIME](state: State, data: any) {
      state.timeType = data.type;
      state.timeValue = data.value;
    },
    [Mutations.SET_CONFIG](state: State, data: any) {
      state.seriousNotice = data.seriousNotice;
      state.normalNotic = data.normalNotic;
      state.voiceType = data.voiceType;
      state.screenNotic = data.screenNotic;
      state.eventTypeList = data.eventTypeList;
      state.eventValue = data.eventValue;
      state.eventDuration = data.eventDuration;
    },
    SET_REAL_DATA(state, data) {
      state.realData = data;
    },
  },
  actions: {
    async loginAction({ commit }, data: any) {
      try {
        // const response = await login({
        //   username: "screen",
        //   password: "@#001njcnpqs",
        // });
        const response = await login(data);
        commit(Mutations.SET_TOKEN, response.data);

        // await initLedger({ deptId: response.data.deptId, type: 0 });
        return;

        return response.data;
      } catch (error) {
        console.error("登录失败:", error);
        throw error;
      }
    },
    increment({ commit }) {
      commit(Mutations.INCREMENT);
    },
    setStateKey({ commit }, data: any) {
      commit(Mutations.SET_STATE,data);
    },
    setTimeType({ commit }, data: any) {
      commit(Mutations.SET_TIME, data);
    },
    setConfig({ commit }, data: any) {
      queryConfig().then((res) => {
        commit(Mutations.SET_CONFIG, res.data);
      });
    },
    updateRealData({ commit }, data) {
      commit("SET_REAL_DATA", data);
    },
  },
  getters: {
    double(state: State) {
      return 2 * state.count;
    },
    isAuthenticated(state: State) {
      return !!state.token;
    },
  },
});
