import { getMenus, getQuickNav,getLeastRecentlyUsed,getPermissions } from '../../api/menu'
import {field5Change3} from '@main/utils/index'
const portal = {
    namespaced: true,
    state: {
        powerList:[],
        menusList:[],
        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 }) {
            await getMenus().then(res => {
                if(res.status==500){
                    if(window.HIVUI_SETTING.loginUrl){
                        location=window.HIVUI_SETTING.loginUrl;
                    }else{
                        location="/"+projectName+"/hivuiLogin/index.html#/";
                    }
                }
                const data = res.dataPack;
                commit('SET_menusList', [...data]);
                console.log(`SET_menusList:`,data);
                let treeData=[...data],defTabs=[];
                
                treeData=field5Change3([...treeData]);
                getActiveItem(treeData);
                
                /*获取默认打开项*/
                function getActiveItem(_arr){
                    _arr.map(item=>{
                        //构建 树 map
                        if(item.children&&item.children.length>0){
                            getActiveItem(item.children);
                        }
                        for(let i = (state.defTabs||[]).length-1;i>=0;i--){

                            if(state.defTabs[i].resId!=item.resId){
                                defTabs.push(item);
                                break;
                            }
                        }
                    });
                }
                commit('SET_defTabs', [...defTabs,...state.defTabs]);
                console.info('menusTree:',treeData);
                commit('SET_menus', treeData);
                return data;
            });
        },
        //取左侧快速
        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;
            await getLeastRecentlyUsed(me.state.user.id).then(res => {
                let data = res.dataPack;
                data=field5Change3([...data]);
                commit('SET_latelyNavs', data);
                return data;
            });
        },
        //取功能权限
        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


