import setting from '@/setting.js'
import util from '@/libs/util'
import menu from '@/menu'
import { getMenuAll } from '@/api/menu'

export default {
  namespaced: true,
  state: {
    // 顶栏菜单
    header: [],
    // 侧栏菜单
    aside: [],
    // 侧边栏收缩
    asideCollapse: setting.menu.asideCollapse,
    // 侧边栏折叠动画
    asideTransition: setting.menu.asideTransition,
    // 权限字典 { '/path': '菜单名' }
    authKey: {},
    // 菜单源数据
    sourceData: []
  },
  actions: {
    async asideCollapseSet ({ state, dispatch }, collapse) {
      state.asideCollapse = collapse
      await dispatch('d2admin/db/set', {
        dbName: 'sys',
        path: 'menu.asideCollapse',
        value: state.asideCollapse,
        user: true
      }, { root: true })
    },
    async asideCollapseToggle ({ state, dispatch }) {
      state.asideCollapse = !state.asideCollapse
      await dispatch('d2admin/db/set', {
        dbName: 'sys',
        path: 'menu.asideCollapse',
        value: state.asideCollapse,
        user: true
      }, { root: true })
    },
    async asideTransitionSet ({ state, dispatch }, transition) {
      state.asideTransition = transition
      await dispatch('d2admin/db/set', {
        dbName: 'sys',
        path: 'menu.asideTransition',
        value: state.asideTransition,
        user: true
      }, { root: true })
    },
    async asideTransitionToggle ({ state, dispatch }) {
      state.asideTransition = !state.asideTransition
      await dispatch('d2admin/db/set', {
        dbName: 'sys',
        path: 'menu.asideTransition',
        value: state.asideTransition,
        user: true
      }, { root: true })
    },
    async asideLoad ({ state, dispatch }) {
      const menuSetting = await dispatch('d2admin/db/get', {
        dbName: 'sys',
        path: 'menu',
        defaultValue: setting.menu,
        user: true
      }, { root: true })
      state.asideCollapse = menuSetting.asideCollapse !== undefined ? menuSetting.asideCollapse : setting.menu.asideCollapse
      state.asideTransition = menuSetting.asideTransition !== undefined ? menuSetting.asideTransition : setting.menu.asideTransition
    },
    async sourceDataLoad ({ state, commit, dispatch }) {
      // 优先从 localStorage 读取上次缓存的菜单数据
      state.sourceData = await dispatch('d2admin/db/get', {
        dbName: 'database',
        path: '$menu.sourceData',
        defaultValue: [],
        user: true
      }, { root: true })

      // 缓存为空且已登录时，从后端获取菜单
      if (!state.sourceData.length && util.cookies.get('token')) {
        const res = await getMenuAll()
        state.sourceData = res || []
        await dispatch('d2admin/db/set', {
          dbName: 'database',
          path: '$menu.sourceData',
          value: state.sourceData,
          user: true
        }, { root: true })
      }

      // 构建菜单树和权限字典
      menu.install(this, state.sourceData)
    },
    async menuReload ({ state, dispatch }) {
      if (!util.cookies.get('token')) {
        return
      }
      await dispatch('d2admin/db/set', {
        dbName: 'database',
        path: '$menu.sourceData',
        value: [],
        user: true
      }, { root: true })
      state.sourceData = []
      await dispatch('sourceDataLoad')
    }
  },
  mutations: {
    headerAuth (state, source) {
      if (!source || source.length <= 0) {
        return
      }
      const auth = {}
      source.forEach(value => {
        if (value.url) {
          auth[value.url] = value.name
        }
      })
      state.authKey = auth
    },
    headerSet (state, menu) {
      state.header = menu
    },
    asideSet (state, menu) {
      state.aside = menu
    }
  }
}
