// import {
//     getAllProject
//   } from "@/api/scan/request";
// import {
//   getUserInfo,getUserInfoByUserId
// } from "@/api/system/persagy-sso-server";
import tools from "@/utils/old-adm/scan/tools";
// import storage from '@/framework/utils/storage'
import storage from "@/utils/storageUtil";
import lStorage from "@/utils/old-adm/localStorage";
import store from "@/store";

const KEY_MENU_SELECTED = "menu_selected";
const KEY_PROJECT_SELECTED = "global_project_selected";
const KEY_PAGE_BRANDCRUMB = "page_brandcrumb";

export default {
  namespaced: true,
  state: {
    sidebarClosed: false,
    sidebarSelected: "", // sidebar选中的选项
    // userInfo: null, //{ username: 'admin' },
    userInfo: {
      userName: "yanruolan",
      userId: "06328a53c69a41bb8f5bb1a552c6e8d6",
    },
    permissions: {
      "system:role:delete": true,
      "system:role:create": true,
      "system:role:query": true,
      "system:role:setOpts": true,
    },
    menus: [], //系统菜单列表
    projectId: "",
    projectName: "",
    group_code: "",
    // projects: project["14"],
    projects: [],
    breadcrumb: [],
    uploaderList: [], //当前上传文件列表
    secret: "", //项目密码
    userId: "", //用户id
    // userId: "06328a53c69a41bb8f5bb1a552c6e8d6", //用户id
    rowEdit: false, //表格数据变化
    errorReport: [], //扫楼错误报告
  },
  getters: {
    sidebarClosed: (state) => state.sidebarClosed,
    secret: (state) => state.secret,
    userId: (state) => state.userId,
    sidebarSelected: (state) => {
      // if (!state.pageSidebarSelected) {
      //     let menu = storage.get(KEY_MENU_SELECTED)
      //     if (menu) {
      //         state.pageSidebarSelected = menu
      //     }
      // }
      // return state.pageSidebarSelected
      return state.sidebarSelected;
    },
    userInfo: (state) => state.userInfo,
    permissions: (state) => state.permissions,
    menus: (state) => state.menus,
    projects: (state) => state.projects,
    uploaderList: (state) => state.uploaderList,
    projectId: (state) => {
      if (!state.projectId) {
        let pid = storage.get(KEY_PROJECT_SELECTED);
        if (pid) {
          state.projectId = pid;
        } else if (state.projects && state.projects.length) {
          state.projectId = state.projects[0].id;
        }
      }
      return state.projectId;
    },
    projectName: (state) => state.projectName,
    group_code: (state) => state.group_code,
    breadcrumb: (state) => {
      if (!state.breadcrumb) {
        let arr = storage.get(KEY_PAGE_BRANDCRUMB);
        if (arr) {
          state.breadcrumb = arr;
        }
      }
      return state.breadcrumb;
    },
  },
  mutations: {
    setRowEdit: (state, val) => (state.rowEdit = val),
    setErrorReport: (state, val) => (state.errorReport = val),
    setSidebarClosed: (state, val) => (state.sidebarClosed = val),
    setUserInfo: (state, val) => (state.userInfo = val),
    setGroupCode: (state, val) => (state.group_code = val),
    setUserId: (state, val) => (state.userId = val),
    setSidebarSelected: (state, val) => {
      state.sidebarSelected = val;
      storage.set(KEY_MENU_SELECTED, val);
      lStorage.set("screen_data", {
        path: val,
        data: {},
      });
    },
    setProjects: (state, val) => (state.projects = val),
    setprojectId: (state, val) => {
      lStorage.remove("cacheInfo"); //待删除(删除用户浏览器无用缓存)
      let cacheInfo = lStorage.get("historyInfo")
        ? lStorage.get("historyInfo")
        : {};
      state.projectId = val;
      lStorage.set("projectId", val);
      if (cacheInfo[state.userInfo.userName]) {
        // cacheInfo[state.userInfo.userName].projectId = val
        cacheInfo[state.userInfo.userName] = [
          ...new Set([val, ...cacheInfo[state.userInfo.userName]]),
        ].slice(0, 3);
        lStorage.set("historyInfo", cacheInfo);
      } else {
        cacheInfo[state.userInfo.userName] = [val];
        lStorage.set("historyInfo", cacheInfo);
      }
      storage.set(KEY_PROJECT_SELECTED, val);
      state.projects.map((item) => {
        if (item.id == val) {
          state.secret = item.pwd;
          state.projectName = item.name;
          state.group_code = item.group_code;
          lStorage.set("group_code", item.group_code);
          lStorage.set("secret", item.pwd);
        }
      });
    },
    setMenus: (state, val) => (state.menus = val),
    setUploaderList: (state, val) => {
      state.uploaderList = val ? val : [];
    },
  },
  actions: {
    setRowEdit(contentx, value) {
      contentx.commit("setRowEdit", value);
    },
    setErrorReport(contentx, value) {
      contentx.commit("setErrorReport", value);
    },
  },
};
