/*
 * @Author: WangQiBiao
 * @Date: 2019-09-18 15:59:59
 * @LastEditors: WangQiBiao
 * @LastEditTime: 2019-09-20 16:28:25
 * @Description:
 */

export default {
  namespaced: true,
  state: {
    isCollapse: false, // 菜单展开收缩
    permisList: JSON.parse(window.sessionStorage.getItem('PERMIS_LIST')) || [] // 权限路由
  },
  mutations: {
    /**
     * 设置伸缩菜单
     */
    SET_IS_COLLAPSE (state, newValue) {
      state.isCollapse = newValue
    },
    /**
     * 设置权限路由
     */
    SET_PERMIS_LIST (state, newValue) {
      state.permisList = newValue
    }
  },
  actions: {
    /**
     * 操作菜单伸缩
     */
    handleIsCollapse ({ commit }, newValue) {
      commit('SET_IS_COLLAPSE', newValue)
    },
    /**
     * 格式化菜单路由
     */
    handlePermisList ({ commit }, routesList = []) {
      const newRoutesList = routesList.map(item => {
        return {
          path: `${item.path}`,
          meta: {
            title: item.meta.title,
            icon: item.meta.icon
          },
          children: item.children && item.children.map(child => {
            return {
              path: child.path,
              meta: {
                title: child.meta.title,
                icon: child.meta.icon
              }
            }
          })
        }
      })
      window.sessionStorage.setItem('PERMIS_LIST', JSON.stringify(newRoutesList))
      commit('SET_PERMIS_LIST', newRoutesList)
    },
    /**
     * 清除权限路由表
     */
    handleClearPermisStorage () {
      window.sessionStorage.removeItem('PERMIS_LIST')
    }
  }
}
