export default {
  namespaced: true,
  state: {
    // 旧营收产品$login对象
    login: undefined,
    user: undefined,
    permissions: null,
    roles: null,
    routesConfig: null,
    single: []
  },
  getters: {
    user: state => {
      if (!state.user) {
        try {
          const user = localStorage.getItem(process.env.VUE_APP_USER_KEY)
          state.user = JSON.parse(user)
        } catch (e) {
          console.error(e)
        }
      }
      return state.user
    },
    login: state => {
      if (!state.login) {
        try {
          const user = localStorage.getItem(process.env.VUE_APP_LOGIN_KEY)
          state.login = JSON.parse(user)
        } catch (e) {
          console.error(e)
        }
      }
      return state.login
    },
    permissions: state => {
      if (!state.permissions) {
        try {
          const permissions = localStorage.getItem(process.env.VUE_APP_PERMISSIONS_KEY)
          state.permissions = JSON.parse(permissions)
          state.permissions = state.permissions ? state.permissions : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.permissions
    },
    roles: state => {
      if (!state.roles) {
        try {
          const roles = localStorage.getItem(process.env.VUE_APP_ROLES_KEY)
          state.roles = JSON.parse(roles)
          state.roles = state.roles ? state.roles : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.roles
    },
    routesConfig: state => {
      if (!state.routesConfig) {
        try {
          const routesConfig = localStorage.getItem(process.env.VUE_APP_ROUTES_KEY)
          state.routesConfig = JSON.parse(routesConfig)
          state.routesConfig = state.routesConfig ? state.routesConfig : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.routesConfig
    },
    // 判断是否为调试用户
    isDebugUser: (state, getters) => {
      const currUser = getters.user
      if (!currUser) return false
      return currUser.ename === '1' || (currUser.rolesnames && currUser.rolesnames.indexOf('运维人员') > -1)
    }
  },
  mutations: {
    setUser (state, user) {
      const operInfo = {
        f_operator: user?.name,
        f_operatorid: user?.id,
        f_orgid: user?.orgid,
        f_orgname: user?.orgs,
        f_depid: user?.depids,
        f_depname: user?.deps,
        f_zoneid: user?.zoneid,
        f_zones: user?.zones
      }
      // 使用时请使用 operInfo, operaInfo后续可能移除
      state.user = Object.assign(user, {
        operaInfo: operInfo,
        operInfo
      })
      // 组织组织机构信息
      localStorage.setItem(process.env.VUE_APP_USER_KEY, JSON.stringify(user))
    },
    setSingle (state, page) {
      if (page.path && page.delete) {
        // 删除
        state.single = state.single.filter(item => item.fullPath !== page.path)
      } else if (state.single.filter(item => item.meta.singlePage === page.meta.singlePage).length === 0) {
        // 添加
        state.single.push(page)
      }
    },
    setLogin (state, login) {
      state.login = login
      localStorage.setItem(process.env.VUE_APP_LOGIN_KEY, JSON.stringify(login))
    },
    setPermissions (state, permissions) {
      state.permissions = permissions
      localStorage.setItem(process.env.VUE_APP_PERMISSIONS_KEY, JSON.stringify(permissions))
    },
    setRoles (state, roles) {
      state.roles = roles
      localStorage.setItem(process.env.VUE_APP_ROLES_KEY, JSON.stringify(roles))
    },
    setRoutesConfig (state, routesConfig) {
      state.routesConfig = routesConfig
      localStorage.setItem(process.env.VUE_APP_ROUTES_KEY, JSON.stringify(routesConfig))
    }
  }
}
