/**
 * 主要针对固定按钮的事件操作，main函数作为固定的入口，
 */

"use strict";
import { util } from '../../util/util.js'
import { fullMap } from '../../fullMap.js';
import { khcAlert } from '../alert/khcAlert.js'
import {khcModal} from "../alert/khcModal.js";
import {form} from "../page/form/form.js";
import {config} from "../../config.js";
import {khcMask} from "../alert/khcMask.js";
import {pageInfoMap} from "../../pageInfoMap.js";
import {httpUtil} from "../../util/httpUtil.js";
import {khcModalSlide} from "../alert/khcModalSlide.js";
//objID 是按钮所在的组件的id，可以根据id获取到组件对象本身，然后展开对组件的各项操作。
//method 真正的执行方法
let functionMap=new Map();// 使用 Map 来存储多个函数。每个函数都通过 set 方法添加到 Map 中，键是函数名，值是对应的函数。
export function main(jsonConfig) {
    try {
        functionMap.get(jsonConfig.button_method).apply(jsonConfig);
    } catch (error) {
        khcAlert.showSystemError(error.message);
        console.log(error);
    }
}
let khcModalForForm=null;
function toUserinfo(){
    let jsonConfig=this;
    /*let params = {};
    params.dsCode='ds_system_user';
    params.formCode='form_user_info';
    khcModalForForm = new khcModalSlide({ modalAreaID: config.getConfigJson().modalAreaID });
    khcModalForForm.render().then(() => {
        let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
        khcModalForForm.formObjID=formComponent.getObjID()
        let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
        pageInfoMap.push(khcModalForFormKey,khcModalForForm);
        formComponent.setFormShowType('modals')
        formComponent.render().then(() => {
            params.methodName='findOneUserInfo';
            let obj={url:config.getConfigJson().myActionUrl,params:params}
            httpUtil.post(obj,(response)=>{
                let eleMap = formComponent.getFormMap();
                let content=response.data.data.data;
                for (let key in content) {
                    eleMap.get(key).setValue(content[key])
                }
            })
            formComponent.formEdit();
            khcModalForForm.show();
        });
        //同理可以实现saveBefore
        formComponent.setSaveAfter(()=> {
            khcModalForForm.hide();
        })
    })*/
    window.location.href=config.getIndexUrl()+'#page_my_info';
}
functionMap.set('toUserinfo', toUserinfo);// 将函数添加到 Map 中
function rePassword(){
    /*let params = {};
    params.dsCode='ds_system_user';
    params.formCode='form_user_update_pass';
    khcModalForForm = new khcModalSlide({ modalAreaID: config.getConfigJson().modalAreaID });
    khcModalForForm.render().then(() => {
        let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
        khcModalForForm.formObjID=formComponent.getObjID()
        let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
        pageInfoMap.push(khcModalForFormKey,khcModalForForm);
        formComponent.setFormShowType('modals')
        formComponent.render().then(() => {
            let eleMap = formComponent.getFormMap();
            params.methodName='findOneUserInfo'
            let obj={url:config.getConfigJson().myActionUrl,params:params}
            httpUtil.post(obj,(response)=>{
                let eleMap = formComponent.getFormMap();
                let content=response.data.data.data;
                for (let key in content) {
                    eleMap.get(key).setValue(content[key])
                }
                eleMap.get('user_password').setValue('')
                document.getElementById(eleMap.get('user_password').eleID).setAttribute('type','password')
                document.getElementById(eleMap.get('re_user_password').eleID).setAttribute('type','password')
            })
            formComponent.formEdit();
            khcModalForForm.show();
        });
        //同理可以实现saveBefore
        formComponent.setSaveAfter(()=> {
            khcModalForForm.hide();
        })
    })*/
    window.location.href=config.getIndexUrl()+'#page_my_info?component_code=password';
}
functionMap.set('rePassword', rePassword);// 将函数添加到 Map 中
function toMessage(){
    alert('我的消息(待开发)');
}
functionMap.set('toMessage', toMessage);// 将函数添加到 Map 中
function toUserSettings(){
    alert('账号设置(待开发)');
}
functionMap.set('toUserSettings', toUserSettings);// 将函数添加到 Map 中
function logout(){
    let pageParam = {version: '1.0.0',dsCode:'ds_system_user', methodName: 'logout'}
    let obj={url:config.getConfigJson().myActionUrl,params:pageParam}
    fullMap.getObj('mask').show('正在退出...');
    httpUtil.post(obj, (response)=>{
        /*//首窗需要跳转单点登录地址
        if(config.getEnv()!='prd'){
            config.removeToken();
            util.toLogin();
        }else{
            fullMap.getObj('mask').show('正在退出...');
            util.removeCookie('token')
            window.location.href = 'http://192.141.252.2:8080/cas/logout?service=http://192.141.25.169/';
        }*/
        config.removeToken();
        util.toLogin();
    })
}
functionMap.set('logout', logout);// 将函数添加到 Map 中
function toSecondmentUser(){
    let jsonConfig=this;
    let params = {};
    params.dsCode='ds_system_user';
    params.formCode='form_show_secondment';
    khcModalForForm = new khcModalSlide({ modalAreaID: config.getConfigJson().modalAreaID });
    khcModalForForm.render().then(() => {
        let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
        khcModalForForm.formObjID=formComponent.getObjID()
        let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
        pageInfoMap.push(khcModalForFormKey,khcModalForForm);
        formComponent.setFormShowType('modals')
        formComponent.render().then(() => {
            let eleMap = formComponent.getFormMap();
            params.methodName='findOneUserInfo';
            let obj={url:config.getConfigJson().myActionUrl,params:params}
            httpUtil.post(obj,(response)=>{
                let eleMap = formComponent.getFormMap();
                let content=response.data.data.data;
                for (let key in content) {
                    eleMap.get(key).setValue(content[key])
                }
            })
            formComponent.formEdit();
            khcModalForForm.show();
        });
        //同理可以实现saveBefore
        formComponent.setSaveAfter(()=> {
            khcModalForForm.hide();
        })
    })
}
functionMap.set('toSecondmentUser', toSecondmentUser);// 将函数添加到 Map 中