"use strict";
import { fullMap } from '../../../fullMap.js';
import { khcAlert } from '../../../component/alert/khcAlert.js'
import { form } from '../../../component/page/form/form.js';
import { util } from '../../../util/util.js'
import { khcModal } from '../../../component/alert/khcModal.js'
import {config} from "../../../config.js";
import {pageInfoMap} from "../../../pageInfoMap.js";
import {httpUtil} from "../../../util/httpUtil.js";
import {khcModalSlide} from "../../../component/alert/khcModalSlide.js";
import {khcToastr} from "../../../component/alert/khcToastr.js";
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);
    }
}
function rePass(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    let content = tableViewComponent.getContentMap().get(jsonConfig.pkCode.toString());
    let alertData = {};
    alertData.title = '请确认是否重置密码';
    alertData.text = '重置后 '+content.user_login_name+' 旧密码立即失效';
    khcAlert.showConfirm(alertData, ()=>{
        let params =queryViewComponent.getCode();
        params.userId=content.id;
        params.methodName='rePassword';
        let obj={url:config.getConfigJson().myActionUrl,params:params}
        return httpUtil.post(obj, (response)=>{
            Swal.fire(
                '重置成功',
                response.data.data.message,
                'success'
            )
        })
    });
}
functionMap.set('rePass', rePass);// 将函数添加到 Map 中

function unbanUser(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    let content = tableViewComponent.getContentMap().get(jsonConfig.pkCode.toString());
    let alertData = {};
    alertData.title = '请确认是否解禁';
    alertData.text = '解禁后 '+content.user_login_name+' 即可继续登录';
    khcAlert.showConfirm(alertData, ()=>{
        let params =queryViewComponent.getCode();
        params.userId=content.id;
        params.methodName='unbanUser';
        let obj={url:config.getConfigJson().myActionUrl,params:params}
        return httpUtil.post(obj, (response)=>{
            Swal.fire(
                '重置成功',
                response.data.data.message,
                'success'
            )
            queryViewComponent.getTableObj().refresh();
        })
    });
}
functionMap.set('unbanUser', unbanUser);// 将函数添加到 Map 中

function updatePass(){
    let jsonConfig=this;
    let formComponent = fullMap.getObj(jsonConfig.objID);
    let eleMap = formComponent.getFormMap();
    let password=eleMap.get('user_password').getValue();
    let rePassword=eleMap.get('re_user_password').getValue()
    if(password.length<=7){
        let alertData={title:'新密码不能为空',text:'至少8个字符、必须包含大小写字母、数字、特殊字符'}
        khcAlert.showSystemError(alertData);
        return ;
    }
    let pwdRegex = new RegExp('(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{8,30}');
    if (!pwdRegex.test(password)) {
        let alertData={title:'您的密码复杂度太低',text:'密码中必须包含大小写字母、数字、特殊字符'}
        khcAlert.showSystemError(alertData);
        return ;
    }
    if(password!=rePassword){
        let alertData={title:'两次密码输入不一致',text:'两次密码输入不一致'}
        khcAlert.showSystemError(alertData);
        return ;
    }
    formComponent.saveData();
}
functionMap.set('updatePass', updatePass);// 将函数添加到 Map 中

function editUserRole(){
    let jsonConfig=this;
    let code={dsCode:'ds_system_roleuser',formCode:'form_roleuser_user',queryCode:''}
    jsonConfig.button_method='editOneByModalSlide';
    jsonConfig.paramsAfterFunc=function (params){
        return util.clone(code);
    }
    jsonConfig.getKhcModalForFormAfterFunc=function (pageComponent){
        return pageComponent.editUserRoleByModalSlideForm
    }
    jsonConfig.setKhcModalForFormAfterFunc=function (pageComponent,khcModalForForm){
        pageComponent.editUserRoleByModalSlideForm=khcModalForForm;
    }
    jsonConfig.renderThenFunc=function (content){
        let queryViewComponent = fullMap.getObj(jsonConfig.objID);
        let khcModalForForm=fullMap.getObj(queryViewComponent.pageID).editUserRoleByModalSlideForm;
        let formComponent=khcModalForForm.formComponent;
        let eleMap = formComponent.getFormMap();
        let params=util.clone(code);
        params.userId=content.id;
        params.methodName='getAllRoleByUserId';
        let obj={url:config.getConfigJson().myActionUrl,params:params}
        httpUtil.post(obj, (response)=>{
            eleMap.get('user_id').setValue(content.id)
            eleMap.get('role_id').setValue(response.data.data.data)
        })
        formComponent.setSaveAfter( ()=> {
            queryViewComponent.getTableObj().refresh();
            khcModalForForm.hide();
        })
        formComponent.formEdit();
        khcModalForForm.show();
    }
    util.import2023(config.getConfigJson().tableActionPath,jsonConfig);
}
functionMap.set('editUserRole', editUserRole);// 将函数添加到 Map 中

//借调，复制一个用户到借调部门
function secondmentUser(){
    let jsonConfig=this;
    jsonConfig.button_method='editOneByModalSlide'
    let code={};
    jsonConfig.paramsAfterFunc=function (params){
        params.formCode='form_user_secondment';
        code=params;
        return params;
    }
    jsonConfig.getKhcModalForFormAfterFunc=function (pageComponent){
        return pageComponent.editSecondmentUserByModalSlideForm;
    }
    jsonConfig.setKhcModalForFormAfterFunc=function (pageComponent,khcModalForForm){
        pageComponent.editSecondmentUserByModalSlideForm=khcModalForForm;
    }
    jsonConfig.renderThenFunc=function (content){
        let queryViewComponent = fullMap.getObj(jsonConfig.objID);
        let khcModalForForm=fullMap.getObj(queryViewComponent.pageID).editSecondmentUserByModalSlideForm;
        let formComponent=khcModalForForm.formComponent;
        let params=util.clone(code);
        formComponent.renderValue(content).then(()=>{
            let eleMap = formComponent.getFormMap();
            eleMap.get('parent_id').setValue(content['id'])
            eleMap.get('user_login_name').setValue(content['user_login_name']+'_'+util.getUUID())
            let fieldList=['id','org_id','uuid','create_time','create_user'];
            for (let i = 0; i <fieldList.length; i++) {
                if(eleMap.get(fieldList[i])!=undefined){
                    eleMap.get(fieldList[i]).setValue('');
                }
            }
            //同理可以实现saveBefore
            formComponent.setSaveAfter( ()=> {
                queryViewComponent.getTableObj().refresh();
                khcModalForForm.hide();
            })
            formComponent.formEdit();//一些高级属性在新增时大部分是不需要设置所以隐藏，在编辑时再展示
            formComponent.setButtonParams(jsonConfig.buttonParams);
            khcModalForForm.show();
        })
    }
    util.import2023(config.getConfigJson().tableActionPath,jsonConfig);
}
functionMap.set('secondmentUser', secondmentUser);// 将函数添加到 Map 中

//身份切换
function switchUser(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    let content = tableViewComponent.getContentMap().get(jsonConfig.pkCode.toString());
    let alertData = {};
    alertData.title = '请确认是否切换至此用户';
    alertData.text = '';
    alertData['confirmButtonColor'] = '#3085d6';
    alertData['cancelButtonColor'] = '#181C21E6';
    khcAlert.showConfirm(alertData, ()=>{
        let params =queryViewComponent.getCode();
        params.userId=content.id;
        params.methodName='switchUser';
        let obj={url:config.getConfigJson().myActionUrl,params:params}
        return httpUtil.post(obj, (response)=>{
            Swal.fire(
                '正在切换',
                response.data.data.message,
                'success'
            )
            config.setToken(response.data.data.token);
            util.setLocalStorageItem('userLoginName',content['user_login_name']);
            window.location.href=config.getIndexUrl();
        })
    });
}
functionMap.set('switchUser', switchUser);// 将函数添加到 Map 中
