"use strict";
import { config } from './config.js';
import { khcAlert } from './component/alert/khcAlert.js';
import { util } from './util/util.js'
import { khcMask } from "./component/alert/khcMask.js";
import {home} from "./component/home.js";
import {fullMap} from "./fullMap.js";
import {websocket} from "./component/websocket/websocket.js";
import {khcToastr} from "./component/alert/khcToastr.js";
import { globalErrorHandler } from './util/globalErrorHandler.js';
axios.defaults.baseURL = config.getConfigJson().baseURL;
config.getConfigJson().downFileUrl=config.getConfigJson().baseURL+config.getConfigJson().contextPath+'/api/file/downFile'//这个没走axios，所以补上/api
//全局响应拦截器
axios.interceptors.response.use( (response)=> {
    if(util.isNull(response.data.status)){
        khcAlert.showSystemError({
            title: '系统异常',
            text: '接口返回为空，请检查后端服务'
        });
        return response;
    }
    if (response.data.status == '4010') {//4010,登录状态失效，跳转登录页面重新登录 //todo login
        //let param=JSON.parse(response.config.data)
        //if(param.methodName!='logout'){}
        util.toLogin();
        return response;
    }else if (response.data.status != 'ok') {//非ok状态则根据响应的中文错误信息弹出
        let alertData={title:response.data.message,text:'标识码：' + response.data.status}
        khcAlert.showSystemError(alertData);
        return response;
    }else{
        return response;
    }
},  (error)=> {
    return Promise.reject(error);//错误信息不处理，交给全局异常处理
});

// 初始化全局错误处理（绑定 window.error 和 window.unhandledrejection）
globalErrorHandler.init();

if(config.getConfigJson().loadOutDescription.length>0){
    console.log(config.getConfigJson().loadOutDescription);
}
if(config.getConfigJson().loadOutLink.length>0){
    console.log(config.getConfigJson().loadOutLink,"color:red");
}
let mask = new khcMask();
let ws = new websocket();
ws.init();
fullMap.push('mask',mask);
fullMap.push('ws',ws);
let thisHome=new home({parentObjID: objParentObjID});
let pageCode = config.getPageCode();
let sessionPageCodeValue=util.getSessionStorageItem('pageCode');
if (util.isNull(pageCode)) {
    if(util.isNull(sessionPageCodeValue)){
        thisHome.setPageCode(config.getHomePageCode())
    }
} else {
    thisHome.setPageCode(pageCode);
}
let initHomeFunc=async ()=>{
    await mask.show('正在加载...');
    if(util.isNull(thisHome.pageCode)){
        let url=config.getIndexUrl()+sessionPageCodeValue
        window.location.href = url
    }else{
        await thisHome.render()
        await thisHome.renderPageInfo()
        await mask.hide();
        if(pageCode!='page_component'){
            util.waterMark();
        }
    }
}
initHomeFunc();
window.addEventListener("hashchange", () => {
    $('[data-toggle="popover"]').popover('hide');//popover弹出框组件关闭
    Swal.close();//提示框组件关闭
    mask.show('正在加载...').then(()=>{
        fullMap.getObj('home').menuObj.setDisabled(3000);//禁用菜单,3秒后解除
        let toastDivEle=document.getElementById('toast-container');
        if(toastDivEle!=null){
            document.getElementById('toast-container').remove();//remove toast
        }
        let pageCode = config.getPageCode();
        if (pageCode == 'login') {//跳转登录页面
            config.removeToken();
            util.toLogin();
        }
        if (util.isNull(pageCode)) {
            thisHome.setPageCode(config.getHomePageCode());
        } else {
            thisHome.setPageCode(pageCode);
        }
        thisHome.renderPageInfo().then(()=>{
            mask.hide();
        });
    });
});

$(document).bind("click", function(e){
    let togglePopover=e.target['dataset']['toggle'];
    if(togglePopover=='popover')return ;
    if ($(e.target).closest(".bs-popover-bottom").length == 0&&
        $(e.target).closest(".popover-header").length == 0&&
        $(e.target).closest(".popover-body").length == 0) {//点击了其它区域，关闭 popover 弹窗
        $('[data-toggle="popover"]').popover('hide');
    }
});

