import sa from '/@/sa-frame/sa';
import {useUserStore} from "../store/user";
import router from "../router";
import {addRoutesByMenuList} from "../router/routes-dynamic";
import {defineMenu} from "../router/router-util";
import {useSettingStore} from "../store/setting";
import {useAppStore} from "../store/app";
import {menuList} from "../router/menu-list";


// Admin 模板初始化函数 
export default function(jumpPath) {
    
    const appStore = useAppStore();
    let settingStore = useSettingStore();
    
    // 获取当前会话信息 
    sa.ajax("/AccAdmin/getLoginInfo", function (res) {

        // 校验当前登录账号，是否有权限进入后台 
        if(res.data.perList === undefined || res.data.perList.indexOf('in-system') === -1) {
            sa.$sys.setCurrUser(res.data.admin);
            return sa.alert('当前账号暂无进入后台权限', function (){
                router.push('/login');
            });
        }

        // 把账号相关信息，保存到本地 
        sa.$sys.setCurrUser(res.data.admin);
        useUserStore().setUserInfo({
            name: res.data.admin.name,
            avatar: res.data.admin.avatar
        });

        // 配置信息
        sa.$sys.setAppCfg(res.data.appCfg);

        // 保存权限数据
        sa.setAuth(res.data.perList);

        // 初始化右上角的菜单选项 
        appStore.setAppInfo(res.data.appCfg);
        
        // 初始化菜单, isServerMenu=菜单加载模式, true=从后端请求获取菜单, false=前端定义菜单 (menu-list.js 文件里定义)
        const beMenuList = settingStore.isServerMenu ? defineMenu(res.data.menuList) : menuList;
        addRoutesByMenuList(beMenuList);
        appStore.setMenuList(beMenuList, res.data.perList);
        
        if(jumpPath) {
            router.push(jumpPath);
        }
        
        // 初始化右上角的菜单选项 
        initDropList();

        // 初始化模板，一定要调用 
        appStore.init();
    }, {msg: '加载初始数据...', msgType: 'layer'})
        
}

// 初始化右上角的菜单选项 
const initDropList = function () {
    const layer = sa.layer;
    useUserStore().setDropList([
        {
            name: '我的资料',
            click: function (){
                sa.$page.openAdminInfo();
            }
        },
        {
            name: '修改资料',
            click: function () {
                sa.showModel("修改我的资料", () => import('../sp-views/sp-admin/admin-add'), {
                    id: sa.$sys.getCurrUser().id,
                    curr: true
                })
            }
        },
        {
            name: '修改密码',
            click: function () {
                sa.showModel('修改密码', import('@/sp-views/sp-admin/update-password'))
                // sa.showIframe('修改密码', 'sa-view-sp/sp-admin/update-password.html', '550px', '350px');
            }
        },
        {
            name: '退出登录',
            click: function (){
                layer.confirm('退出登录？', function() {
                    sa.ajax('/AccAdmin/doExit', function(res) {
                        layer.alert('注销成功', function() {
                            layer.closeAll()
                            router.push(`/login?redirect=${encodeURIComponent(router.currentRoute.value.fullPath)}`)
                        })
                    })
                });
            }
        }
    ])
}
