// 需登出重置的state
const initStates = {
  // 选项卡菜单
  tabControlMenus: [{
    index: '0',
    route: { name: 'home' },
    name: '首页',
  }],
}
const state = {
  // 主题色
  theme: window.localStorage.getItem('themeColor') || '#4992fc', // default color, 默认主题色，蜂网蓝

  // 面包屑
  breadcrumb: [{ name: '首页' }],
  // 页面名称
  pageName: '首页',
  excludeAliveComps: [], // 排除缓存的组件名

  // 用户名
  userName: window.localStorage.getItem('userName'),

  // 仓库名
  projectName: window.localStorage.getItem('projectName'),

  // 菜单
  activeMenu: window.localStorage.getItem('activeMenu'),
  openMenuId: window.localStorage.getItem('openMenuId'),
  rootMenu: {}, // 根菜单对象
  restoredSideMenuItem: {}, // 关闭选项卡时恢复的菜单项

  // 按钮权限
  btnPermission: window.localStorage.getItem('btnPermission') || '{}',

  // 上传文件
  uploadSuccess: true,

  // 用户状态, 其他页面条件判断需求
  sysParamConfig: {},

  // 批次属性配置
  lotAttrsConfigs: {},

  tableColumns: [],
  storeKey: '',
  newLables: [],
  newColumns: [],

  // 登出需重置的state
  ...initStates,
}
const stateCopy = { ...state } // 存副本

const mutations = {
  /**
   * 更新表格
   *
   */
  changeNewColumns (state, value) {
    state.newColumns = value
  },
  /**
   * 小圆圈选中的
   *
   */
  changeStoreLables (state, value) {
    state.newLables = value
  },
  /**
   * 用户状态, 其他页面条件判断需求
   */
  changeStoreKey (state, value) {
    state.storeKey = value
  },
  /**
   * 用户状态, 其他页面条件判断需求
   */
  sysParamConfig (state, data) {
    state.sysParamConfig = data
  },
  /**
   * 改变主题色
  */
  changeTheme (state, color) {
    state.theme = color
    window.localStorage.setItem('themeColor', color)
  },
  /**
   * 登录名称
  */
  changeUserName (state, value) {
    state.userName = value
    window.localStorage.setItem('userName', value)
  },
  /**
   * 页面名称
   */
  changePageName (state, value) {
    state.pageName = value
    window.localStorage.setItem('pageName', value)
  },
  /**
   * 修改当前页面的路径
   */
  changeBreadcrumb (state, value) {
    state.breadcrumb = value
    window.localStorage.setItem('breadcrumb', JSON.stringify(value))
  },
  /**
   * 修改排除缓存的组件名
   */
  changeExcludeAliveComps (state, value) {
    state.excludeAliveComps = value
  },

  /**
   * 切换仓库
   */
  changeProjectName (state, value) {
    state.projectName = value
    window.localStorage.setItem('projectName', value)
  },
  /**
   * 表头
   */
  changeTableColumns (state, value) {
    state.tableColumns = value
  },
  /**
   * 选择菜单
   */
  changeActiveMenu (state, value) {
    state.activeMenu = value
    window.localStorage.setItem('activeMenu', value)
  },
  changeOpenMenuId (state, value) {
    state.openMenuId = value
    window.localStorage.setItem('openMenuId', value)
  },
  // 设置根菜单对象
  setRootMenu (state, value) {
    state.rootMenu = value
  },
  // 改变按钮权限
  changeBtnPermission (state, value) {
    state.btnPermission = value
    window.localStorage.setItem('btnPermission', value)
  },
  // 改变上传文件成功标识
  changeUploadSuccess (state) {
    state.uploadSuccess = !state.uploadSuccess
  },
  // 添加标签页菜单
  addTabControlMenu (state, value) {
    if (state.tabControlMenus.some(item => item.index === value.index)) return
    // const copy = [...state.tabControlMenus]
    // copy.push(value)
    // state.tabControlMenus = [...copy]
    state.tabControlMenus.push(value)
  },
  // 删除标签页菜单
  closeTabControlMenu (state, value) {
    const copy = [...state.tabControlMenus]
    copy.splice(value, 1)
    state.tabControlMenus = [...copy]
  },
  // 重置侧边菜单信息
  restoreSideMenu (state, value) {
    state.restoredSideMenuItem = { ...value }
  },
  // 更改批次属性配置
  changeLotAttrsConfig (state, value) {
    state.lotAttrsConfigs = value
  },

  /**
   * 重置相关
   * */
  // 重置指定state
  resetState (state, opts) {
    if (opts === undefined) {
      // 重置全部
      for (const item in stateCopy) {
        state[item] = stateCopy[item]
      }
    } else if (opts instanceof Array) {
      // 重置部分
      opts.map(item => {
        state[item] = stateCopy[item]
      })
    } else if (typeof opts === 'string') {
      // 重置一个
      state[opts] = stateCopy[opts]
    } else {
      console.log('重置类型不符, 为', Object.prototype.toString.call(opts))
    }
  },
  // 登出需重置
  resetForLogout (state) {
    // 登录重置
    for (const item in initStates) {
      state[item] = initStates[item]
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
}
