

/**
 * 布局菜单配置
 * */
import { menusApi } from '@/api/account';
import Setting from '@/setting';

function getMenusName () {
    let storage = window.localStorage, menuList = storage.getItem('menuListStore'), menuData = []
    try {
        menuData = menuList !== undefined ? JSON.parse(menuList) : [];
    } catch (e) {}
    if (typeof menuData !== 'object' || menuData === null) {
        menuData = []
    }
    return menuData
}

function makemenusNav(menuList) {
  for (let i = 0; i < menuList.length; i++) {
    menuList[i].path = `${Setting.routePre}${menuList[i].path}`
    if (menuList[i].children) {
      makemenusNav(menuList[i].children)
    }
  }
}

export default {
    namespaced: true,
    state: {
        menusName: getMenusName()
    },
    mutations: {
        getmenusNav (state, menuList) {
          makemenusNav(menuList)
            state.menusName = menuList;
            let storage = window.localStorage;
            storage.setItem('menuListStore', JSON.stringify(menuList));
        }
    },
    actions: {
        getMenusNavList ({ commit }) {
            return new Promise((resolve, reject) => {
                menusApi().then(async res => {
                    resolve(res);
                    commit('getmenusNav', res.data.menus);
                    let storage = window.localStorage;
                    storage.setItem('menuListStore', JSON.stringify(res.data.menus));
                }).catch(res => {
                    reject(res);
                })
            })
        }
    }
};
