import Vue from "vue";
import Vuex from "vuex";
import { normalCallPost } from "./assets/utils";

Vue.use(Vuex);

let userData = {};
let currentParentName = "";
let headBar = [];
let CurrentThirdArr = [];
let ThirdPath = "";
let CurrentSecondPath = "";
try {
  userData = JSON.parse(window.sessionStorage.userData || "{}");
  currentParentName = window.sessionStorage.currentParentName || "";
  headBar = JSON.parse(window.sessionStorage.headBar || "{}");
  CurrentThirdArr = JSON.parse(window.sessionStorage.CurrentThirdArr || "{}");
  ThirdPath = window.sessionStorage.ThirdPath || "";
  CurrentSecondPath = window.sessionStorage.CurrentSecondPath || "";
} catch (error) {
  console.log("未从session中获取到userData");
  console.log("未从session中获取到currentParentName");
  console.log("未从session中获取到headBar");
  console.log("未从session中获取到CurrentThirdArr");
  console.log("未从session中获取到ThirdPath");
  console.log("未从session中获取到CurrentSecondPath");
}

function convertTreeToList(root, array) {
  if (!root || !root.length) return array;
  root.forEach((item) => {
    let data = JSON.parse(JSON.stringify(item));
    const url = data.url
      ? data.url
      : item.childList[0]
      ? item.childList[0].url || ""
      : "";
    delete data.childList;
    array.push({
      name: data.name,
      path: data.url,
      id: data.id,
      parentId: data.parentId,
      icon: data.imgPath,
    });
    if (item.childList) {
      convertTreeToList(item.childList, array);
    }
  });
  return array;
}

function isPc() {
  var userAgentInfo = navigator.userAgent;
  var Agents = new Array(
    "Android",
    "iPhone",
    "SymbianOS",
    "Windows Phone",
    "iPad",
    "iPod"
  );
  var flag = true;
  for (var v = 0; v < Agents.length; v++) {
    if (userAgentInfo.indexOf(Agents[v]) > 0) {
      flag = false;
      break;
    }
  }
  return flag;
}

export default new Vuex.Store({
  state: {
    isLogin: false,
    isMobile: !isPc(),
    userData, //用户数据
    group: "",
    headBar,
    currentParentName, //当前选择一级菜单
    CurrentSecondPath, //当前二级菜单路径
    CurrentThirdArr,
    ThirdPath,
  },
  // 同步存储数据
  mutations: {
    setUserData(state, data) {
      data.flat = convertTreeToList(data.barList, []);
      state.userData = {};
      state.userData = Object.assign({}, state.userData, data);
      state.isLogin = true;
      window.sessionStorage.userData = JSON.stringify(data);
      window.sessionStorage.token = data.token;
    },
    // 头部导航栏数据
    setHeadBar(state, data) {
      state.headBar = data;
      window.sessionStorage.headBar = JSON.stringify(data);
    },
    logout(state) {
      state.userData = {};
      state.isLogin = false;
      // window.sessionStorage.userData = '';
      // window.sessionStorage.token = '';
      // window.sessionStorage.headBar = ''
      // window.sessionStorage.currentParentName = ''
      // window.sessionStorage.CurrentSecondPath = ''
      // window.sessionStorage.CurrentThirdArr = ''
      window.sessionStorage.clear();
    },
    setGroup(state, data) {
      state.group = data;
    },
    // 当前以及导航名
    setCurrentParentName(state, data) {
      state.currentParentName = data;
      window.sessionStorage.currentParentName = data;
    },
    setCurrentSecondPath(state, data) {
      state.CurrentSecondPath = data;
      window.sessionStorage.CurrentSecondPath = data;
    },
    // 当前三级导航栏
    setCurrentThirdArr(state, data) {
      state.CurrentThirdArr = data;
      window.sessionStorage.CurrentThirdArr = JSON.stringify(data);
    },
    // 当前三级导航栏路径
    setThirdPath(state, data) {
      state.ThirdPath = data;
      window.sessionStorage.ThirdPath = data;
    },
  },
  // 异步任务
  actions: {
    login({ commit }) {
      normalCallPost("/login/index")
        .then(({ data }) => {
          commit("setUserData", data);
        })
        .catch((error) => {});
    },
    logout({ commit }) {
      normalCallPost("/login/logout")
        .then((data) => {})
        .catch((error) => {})
        .then(() => {
          commit("logout");
        });
    },
  },
});
