import Vue from "vue";
import Vuex from "vuex";
import cacheModule from "./cache";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    //用户登录token 存储
    token: "",
    userName: "",
    uid: "",
    role: "",
    currentData: "",
    channelId: "",
    termId: "",
    protocol: "",
    cmdId: "",
    channelIdList: [],
    isRenderTab: true,
  },
  getters: {
    token: (state) => state.token,
    userName: (state) => state.userName,
    role: (state) => state.role,
    menuPermission: (state) => state.resources,
  },
  mutations: {
    //点击获取的左侧列表
    currentData(state, currentData) {
      state.currentData = currentData;
      console.log("保存起来currentData", state.currentData);
      localStorage.setItem("currentData", JSON.stringify(currentData));
    },

    channelId(state, channelId) {
      state.channelId = channelId;
      console.log("channelId", state.channelId);
      localStorage.setItem("channelId", JSON.stringify(channelId));
    },
    termId(state, termId) {
      state.termId = termId;
      console.log("termId", state.termId);
      localStorage.setItem("termId", JSON.stringify(termId));
    },

    protocol(state, protocol) {
      state.protocol = protocol;
      console.log("protocol", state.protocol);
      localStorage.setItem("protocol", JSON.stringify(protocol));
    },
    cmdId(state, cmdId) {
      state.cmdId = cmdId;
      console.log("protocol", state.cmdId);
      localStorage.setItem("protocol", JSON.stringify(cmdId));
    },
    channelIdList(state, channelIdList) {
      state.channelIdList = channelIdList;
      console.log("channelIdList", state.channelIdList);
      localStorage.setItem("channelIdList", JSON.stringify(channelIdList));
    },

    SET_TOKEN(state, token) {
      state.token = token;
      localStorage.setItem("token", state.token);
    },
    SET_USERINFO(state, val) {
      state.userName = val.userName;
      state.uid = val.uid;
      state.role = val.role;
      if (val.role !== 0) {
        state.resources = val.resources;
        const menuArr = val.resources
          .filter((item) => item.key.includes("/"))
          .sort((a, b) => a.id - b.id);
        const btnArr = val.resources
          .filter((item) => item.key.includes("Btn"))
          .sort((a, b) => a.id - b.id);
        localStorage.setItem("menuPermission", JSON.stringify(menuArr));
        localStorage.setItem("btnPermission", JSON.stringify(btnArr));
      }

      localStorage.setItem("userName", state.userName);
      localStorage.setItem("uid", state.uid);
      localStorage.setItem("role", state.role);
    },
    //退出清除locastorge
    REMOVE_INFO(state) {
      localStorage.clear();
    },
    setIsRenderTab(state, data) {
      state.isRenderTab = data;
    },
  },
  actions: {},
  modules: { cache: cacheModule },
});
