import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    //用户登录token 存储
    token: "",
    userName: "",
    userid: "",
    channelid: "",
    termid: "",
    role: "",
  },

  getters: {
    channelid: (state) => state.channelid,
    token: (state) => state.token,
    userName: (state) => state.userName,
    role: (state) => state.role,
  },
  mutations: {
    //保存通道id
    SET_CHANNELID(state, channelid) {
      state.channelid = channelid;
      localStorage.setItem("channelid", channelid);
      console.log(state);
      console.log("store、localstorage保存channelid成功！");
    },
    //保存装置id  termid
    SET_TERMID(state, termid) {
      state.termid = termid;
      localStorage.setItem("termid", termid);
      console.log(state);
      console.log("store、localstorage保存termid成功！");
    },
    SET_TOKEN(state, token) {
      state.token = token;
      localStorage.setItem("token", state.token);
    },
    SET_USERINFO(state, val) {
      state.userName = val.userName;
      state.userid = val.id;
      state.role = val.role;
      localStorage.setItem("userName", state.userName);
      localStorage.setItem("userid", state.userid);
      localStorage.setItem("role", state.role);
    },
    //退出清除locastorge
    REMOVE_INFO(state) {
      localStorage.clear();
    },
  },
  actions: {},
  modules: {},
});
