import { getItem, setItem } from "@/utils/storage";
import { TAGS_VIEW } from "@/constant/index.js";
export default {
  namespaced: true,
  state: () => ({
    isBig: false,
    sidebarOpened: true,
    tagsViewList: getItem(TAGS_VIEW) || [],
    urlMd5: null,
    timer: null,
    timec: null,//时间戳

  }),
  mutations: {
    //修改变大状态
    editBig(state, value) {
      state.isBig = value;
    },
    triggerSidebarOpened(state) {
      state.sidebarOpened = !state.sidebarOpened;
    },

    //添加tags
    addTagsViewList(state, tag) {
      const isFind = state.tagsViewList.find((item) => {
        return item.path === tag.path;
      });
      //处理重复
      if (!isFind) {
        state.tagsViewList.push(tag);
        const other = state.tagsViewList.filter((item) => {
          return item.path !== '/dutyBook/home/workbench';
        });
        const tagList = [{
          fullPath: "/dutyBook/home/workbench",
          meta: {
            title: "工作台",
            icon: "icon_ywyy",
            iconH: "homeH"
          },
          name: "dutyBook/home/workbench",
          params: {},
          path: "/dutyBook/home/workbench",
          query: {},
          title: "工作台"
        }, ...other]
        setItem(TAGS_VIEW, tagList);
      }
    },

    /**
     * 删除 tag
     */
    removeTagsView(state, payload) {
      const tagsViewList = state.tagsViewList;
      if (payload.type == "index") {
        tagsViewList.splice(payload.index, 1);
      } else {

        tagsViewList.splice(tagsViewList.indexOf(payload.data), 1);
      }
      setItem(TAGS_VIEW, tagsViewList);
    },
    /**
     * 清空 tag
     */
    clearTag(state, menu) {
      if (menu != null) {
        if (menu.fullPath != '/dutyBook/home/workbench') {
          state.tagsViewList = [{
            fullPath: "/dutyBook/home/workbench",
            meta: {
              meta: {
                title: "工作台"
                // icon: "gerenzhongxin"
              }
            },
            name: "dutyBook/home/workbench",
            params: {},
            path: "/dutyBook/home/workbench",
            query: {},
            title: "工作台"
          }, menu];
        } else {
          state.tagsViewList = [menu];
        }

      } else {
        state.tagsViewList = [{
          fullPath: "/dutyBook/home/workbench",
          meta: {
            meta: {
              title: "工作台"
              // icon: "gerenzhongxin"
            }
          },
          name: "dutyBook/home/workbench",
          params: {},
          path: "/dutyBook/home/workbench",
          query: {},
          title: "工作台"
        }];
      }

      setItem(TAGS_VIEW, state.tagsViewList);
    },
    // 保存请求md5
    addurlMd5(state, url, timec) {
      state.urlMd5 = url;
      state.timec = timec;
      if (state.timer) {

        clearTimeout(state.timer)
      }
      state.timer = setTimeout(() => {
        state.urlMd5 = null
        state.timer = null
      }, 2000)
    },

  }
};
