import { getMenus, getQuickNav,getLeastRecentlyUsed,getPermissions } from '../../api/menu'
import {turnMenuTree,field5Change3} from '@main/utils/index'
const portal = {
    namespaced: true,
    state: {
        powerList:[],
        menusList:[],
        hideMenuItemList:[],//隐藏的文件夹类型菜单节点ID
        menus:[],
        defTabs: [],
        navs: [],
        latelyNavs:[],
        config:[],
    },
    mutations: {
        SET_powerList: (state, powerList) => {
            state.powerList = powerList
        },
        SET_menusList: (state, menusList) => {
            state.menusList = menusList
        },
        SET_menus: (state, menus) => {
            state.menus = menus
        },
        SET_defTabs: (state, data) => {
            state.defTabs = data
        },
        SET_navs: (state, navs) => {
            state.navs = navs
        },
        SET_latelyNavs: (state, latelyNavs) => {
            state.latelyNavs = latelyNavs
        },
        SET_config: (state, config) => {
            state.config = config
        },
    },
    actions: {
        //取功能菜单
        async getMenus({ commit, state }) {
            function menuHandle(data){
                commit('SET_menusList', [...data]);
                console.log(`SET_menusList:`,[...data]);
                let treeData=[...data],defTabs=[];
                
                treeData=turnMenuTree(field5Change3([...treeData]));
                getActiveItem(treeData);
                
                /*获取默认打开项*/
                function getActiveItem(_arr){
                    _arr.map(item=>{
                        //构建 树 map
                        let flag=false;
                        if(item.children&&item.children.length>0){
                            if(!item.isShow||state.hideMenuItemList.indexOf(item.parentId)!=-1){
                                state.hideMenuItemList.push(item.resId);
                            }
                            getActiveItem(item.children);
                        }
                        
                        for(let i = (state.defTabs||[]).length-1;i>=0;i--){

                            if(state.defTabs[i].resId!=item.resId){
                                flag=true;
                                break;
                            }
                        }
                        if(item.isActive&&!flag) defTabs.push(item);
                    });
                }
                commit('SET_defTabs', [...defTabs,...state.defTabs]);
                console.info('menusTree:',[...treeData]);
                console.info('隐藏菜单ID:',[...state.hideMenuItemList]);
                commit('SET_menus', treeData);
                return data;
            }
            /*if(window.__menus&&window.__menus.length>0){
                menuHandle(window.__menus);
            }*/
            return new Promise((resolve, reject) => {
                getMenus().then(res => {
                    if(res&&res.status==500){
                        if(window.HIVUI_SETTING.loginUrl){
                            location=window.HIVUI_SETTING.loginUrl;
                        }else{
                            location="/"+projectName+"/hivuiLogin/index.html#/";
                        }
                    }
                    resolve(menuHandle(res.dataPack));
                }).catch(error=>{
                    reject(error)
                });
            });
        },
        //取左侧快速
        async getQuickNav({ commit, state }) {
            await getQuickNav().then(res => {
                const data = res.data.dataPack.rows;
                console.log(`getQuickNav`)
                let defTabs=[],flag=false;
                data.map(item=>{ 
                    flag=false;
                    for(let i = state.defTabs.length-1;i>=0;i--){
                        if(state.defTabs[i].resId==item.resId){
                            flag = true;
                            break;
                        }
                    }
                    if(item.isActive&&!flag) defTabs.push(item);
                });   
                commit('SET_defTabs', [...defTabs,...state.defTabs]);
                commit('SET_navs', data);
                return data;
            });
        },
        setQuickNav({ commit, state },data){
            commit('SET_navs', data);
        },
        //最近使用
        async getLatelyUse({ commit, state }){
            let me =this;
            return new Promise((resolve, reject) => {
                getLeastRecentlyUsed(me.state.user.id).then(res => {
                    let data = res.dataPack;
                    data=field5Change3([...data]);
                    commit('SET_latelyNavs', data);
                    return data;
                }).catch(error=>{
                    reject(error)
                });
            });
        },
        //取功能权限
        async getPowers({ commit, state }) {
            await getPermissions(this.state.user.id).then(res => {
                const data = res.data.dataPack.rows;
                commit('SET_powerList', [...data]);  
            });
        },
        getSysConfig({ commit, state }){
            commit('SET_config', window.__sysConfig);
        }
    }
}

export default portal


