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

export default new Vuex.Store({
  state: {
    userInfo: {},
    // 用户权限
    userPower: {
      // 1平台管理员 2区域管理员 3学校管理员
      atype: null,
      // 进入的区域层级列表
      regionids: [],
      // 进入的学校
      schoolid: null,
      // 所属位置(所属区域、所属学校)
      belong: []
    }
  },
  mutations: {
    // 登录后设置用户信息
    loginSetUserInfo(state, info) {
      state.userInfo = info;
      state.userPower.atype = info.atype;
      if (state.userPower.atype === 1) {
        state.userPower.regionids = [];
        state.userPower.schoolid = null;
        state.userPower.belong = [];
      } else if (state.userPower.atype === 2) {
        state.userPower.regionids = [info.regionid];
        state.userPower.schoolid = null;
        state.userPower.belong = [info.regionName];
      } else if (state.userPower.atype === 3) {
        state.userPower.regionids = [info.regionid];
        state.userPower.schoolid = info.schoolid;
        state.userPower.belong = [info.regionName, info.schoolName];
      }
      localStorage.setItem(
        "xh_control_userInfo",
        JSON.stringify({
          content: info,
          datetime: new Date().getTime()
        })
      );
      // 登录后设置用户的初始权限
      localStorage.setItem(
        "xh_control_userPower",
        JSON.stringify(state.userPower)
      );
    },
    // 刷新后设置权限
    setPowerAfterRefresh(state) {
      let _userInfo = localStorage.getItem("xh_control_userInfo");
      let _userPower = localStorage.getItem("xh_control_userPower");
      if (_userInfo && _userPower) {
        _userPower = JSON.parse(_userPower);
        state.userPower.atype = _userPower.atype;
        state.userPower.regionids = _userPower.regionids;
        state.userPower.schoolid = _userPower.schoolid;
        state.userPower.belong = _userPower.belong;
      } else {
        state.userPower.atype = null;
        state.userPower.regionids = [];
        state.userPower.schoolid = null;
        state.userPower.belong = [];
      }
    },
    // 进入区域设置跳转痕迹
    setEnterRegionPower(state, { regionid, regionName }) {
      if (!state.userPower.regionids.includes(regionid)) {
        state.userPower.regionids.push(regionid);
        state.userPower.belong.push(regionName);
      }
      localStorage.setItem(
        "xh_control_userPower",
        JSON.stringify(state.userPower)
      );
    },
    // 退出当前区域设置跳转痕迹
    setQuitRegionPower(state) {
      if (state.userPower.atype === 1) {
        if (state.userPower.regionids.length > 0) {
          state.userPower.regionids.pop();
          state.userPower.belong.pop();
        }
      } else if (state.userPower.atype === 2) {
        if (state.userPower.regionids.length > 1) {
          state.userPower.regionids.pop();
          state.userPower.belong.pop();
        }
      }
      localStorage.setItem(
        "xh_control_userPower",
        JSON.stringify(state.userPower)
      );
    },
    // 进入学校设置跳转痕迹
    setEnterSchoolPower(state, { schoolid, schoolName }) {
      state.userPower.schoolid = schoolid;
      state.userPower.belong.push(schoolName);
      localStorage.setItem(
        "xh_control_userPower",
        JSON.stringify(state.userPower)
      );
    },
    // 退出当前学校设置跳转痕迹
    setQuitSchoolPower(state) {
      if (state.userPower.atype === 1 || state.userPower.atype === 2) {
        state.userPower.schoolid = null;
        state.userPower.belong.pop();
      }
      localStorage.setItem(
        "xh_control_userPower",
        JSON.stringify(state.userPower)
      );
    }
  },
  getters: {
    powerParams(state) {
      let _powerParams = {
        // 当前所属区域 1平台 2区域 3学校
        rtype: null,
        // 区域ID/学校ID
        objectid: null
      };
      if (
        state.userPower.schoolid &&
        (state.userPower.atype === 1 ||
          state.userPower.atype === 2 ||
          state.userPower.atype === 3)
      ) {
        _powerParams.rtype = 3;
        _powerParams.objectid = state.userPower.schoolid;
      } else if (
        state.userPower.regionids.length > 0 &&
        (state.userPower.atype === 1 || state.userPower.atype === 2)
      ) {
        _powerParams.rtype = 2;
        let _regionids = [...state.userPower.regionids];
        _powerParams.objectid = _regionids.pop();
      } else if (
        state.userPower.regionids.length === 0 &&
        state.userPower.atype === 1
      ) {
        _powerParams.rtype = 1;
        _powerParams.objectid = null;
      }
      return _powerParams;
    }
  }
});
