"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 {httpUtil} from "../../../util/httpUtil.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 startTask(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    let content = tableViewComponent.getContentMap().get(jsonConfig.pkCode.toString());
    if(content.task_bean_status=='未实例'){
        khcAlert.showWarningMessage("未实例，请检查："+content.task_bean_class);
        return ;
    }
    if(content.task_bean_status=='工作中'){
        khcAlert.showWarningMessage("当前任务在工作中");
        return ;
    }
    let alertData = {};
    alertData.title = '请确认是否启动任务';
    alertData.text = '启动后 "'+content.task_name+'" 将根据表达式开始工作';
    alertData['cancelButtonColor'] = '#181C21E6';
    khcAlert.showConfirm(alertData, ()=>{
        fullMap.getObj('mask').show('即将对'+content['task_name']+'执行操作...')
        fullMap.getObj('ws').khcWSFuncMap.set('wsTask',function (data){
            if(data.status=="ok"){
                if(data.isHideMask=="Y"){
                    Swal.fire('操作成功', data.message,'success' )
                    fullMap.getObj('mask').hide();
                    queryViewComponent.getTableObj().refresh();
                }else{
                    fullMap.getObj('mask').showNewText(data.message)
                }
            }else{
                if(data.status=='40010'){
                    let alertData={title:data.message,text:'标识码：' + data.status}
                    khcAlert.showSystemError(alertData);
                    fullMap.getObj('mask').hide();
                }else{
                    let alertData={title:data.message,text:'标识码：' + data.status}
                    khcAlert.showSystemError(alertData);
                    fullMap.getObj('mask').hide();
                }
            }

        })
        let params =queryViewComponent.getCode();
        params.uuid = content['uuid'];
        params.methodName='startTask';
        let obj={url:config.getConfigJson().myActionUrl,params:params}
        return httpUtil.post(obj, (response)=>{
        })
    });
}
functionMap.set('startTask', startTask);// 将函数添加到 Map 中

function stopTask(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    let content = tableViewComponent.getContentMap().get(jsonConfig.pkCode.toString());
    if(content.task_bean_status=='未实例'){
        khcAlert.showWarningMessage("未实例，请检查："+content.task_bean_class);
        return ;
    }
    if(content.task_bean_status=='未工作'){
        khcAlert.showWarningMessage("当前任务已停止工作");
        return ;
    }
    let alertData = {};
    alertData.title = '请确认是否停止任务';
    alertData.text = '停止后 "'+content.task_name+'" 将不再工作';
    alertData['cancelButtonColor'] = '#181C21E6';
    khcAlert.showConfirm(alertData, ()=>{
        fullMap.getObj('mask').show('即将对'+content['task_name']+'执行操作...')
        fullMap.getObj('ws').khcWSFuncMap.set('wsTask',function (data){
            if(data.status=="ok"){
                if(data.isHideMask=="Y"){
                    Swal.fire('操作成功', data.message,'success' )
                    fullMap.getObj('mask').hide();
                    queryViewComponent.getTableObj().refresh();
                }else{
                    fullMap.getObj('mask').showNewText(data.message)
                }
            }else{
                if(data.status=='40010'){
                    let alertData={title:data.message,text:'标识码：' + data.status}
                    khcAlert.showSystemError(alertData);
                    fullMap.getObj('mask').hide();
                }else{
                    let alertData={title:data.message,text:'标识码：' + data.status}
                    khcAlert.showSystemError(alertData);
                    fullMap.getObj('mask').hide();
                }
            }

        })
        let params =queryViewComponent.getCode();
        params.uuid = content['uuid'];
        params.methodName='stopTask';
        let obj={url:config.getConfigJson().myActionUrl,params:params}
        return httpUtil.post(obj, (response)=>{
        })
    });
}
functionMap.set('stopTask', stopTask);// 将函数添加到 Map 中

function restartTask(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    let content = tableViewComponent.getContentMap().get(jsonConfig.pkCode.toString());
    if(content.task_bean_status=='未实例'){
        khcAlert.showWarningMessage("未实例，请检查："+content.task_bean_class);
        return ;
    }
    let alertData = {};
    alertData.title = '请确认是否重启任务';
    alertData.text = '重启后 "'+content.task_name+'" 将重新根据表达式开始工作';
    alertData['cancelButtonColor'] = '#181C21E6';
    khcAlert.showConfirm(alertData, ()=>{
        fullMap.getObj('mask').show('即将对'+content['task_name']+'执行操作...')
        fullMap.getObj('ws').khcWSFuncMap.set('wsTask',function (data){
            if(data.status=="ok"){
                if(data.isHideMask=="Y"){
                    Swal.fire('操作成功', data.message,'success' )
                    fullMap.getObj('mask').hide();
                    queryViewComponent.getTableObj().refresh();
                }else{
                    fullMap.getObj('mask').showNewText(data.message)
                }
            }else{
                if(data.status=='40010'){
                    let alertData={title:data.message,text:'标识码：' + data.status}
                    khcAlert.showSystemError(alertData);
                    fullMap.getObj('mask').hide();
                }else{
                    let alertData={title:data.message,text:'标识码：' + data.status}
                    khcAlert.showSystemError(alertData);
                    fullMap.getObj('mask').hide();
                }
            }

        })
        let params =queryViewComponent.getCode();
        params.uuid = content['uuid'];
        params.methodName='restartTask';
        let obj={url:config.getConfigJson().myActionUrl,params:params}
        return httpUtil.post(obj, (response)=>{
        })
    });
}
functionMap.set('restartTask', restartTask);// 将函数添加到 Map 中

function startTaskOne(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    let content = tableViewComponent.getContentMap().get(jsonConfig.pkCode.toString());
    if(content.task_bean_status=='未实例'){
        khcAlert.showWarningMessage("未实例，请检查："+content.task_bean_class);
        return ;
    }
    let alertData = {};
    alertData.title = '请确认是否执行';
    alertData.text = '确认后 "'+content.task_name+'" 将立即开始工作（工作一次）';
    alertData['cancelButtonColor'] = '#181C21E6';
    khcAlert.showConfirm(alertData, ()=>{
        fullMap.getObj('mask').show('即将对'+content['task_name']+'执行操作...')
        fullMap.getObj('ws').khcWSFuncMap.set('wsTask',function (data){
            if(data.status=="ok"){
                if(data.isHideMask=="Y"){
                    Swal.fire('操作成功', data.message,'success' )
                    fullMap.getObj('mask').hide();
                    queryViewComponent.getTableObj().refresh();
                }else{
                    fullMap.getObj('mask').showNewText(data.message)
                }
            }else{
                if(data.status=='40010'){
                    let alertData={title:data.message,text:'标识码：' + data.status}
                    khcAlert.showSystemError(alertData);
                    fullMap.getObj('mask').hide();
                }else{
                    let alertData={title:data.message,text:'标识码：' + data.status}
                    khcAlert.showSystemError(alertData);
                    fullMap.getObj('mask').hide();
                }
            }

        })
        let params =queryViewComponent.getCode();
        params.uuid = content['uuid'];
        params.methodName='startTaskOne';
        let obj={url:config.getConfigJson().myActionUrl,params:params}
        return httpUtil.post(obj, (response)=>{
        })
    });
}
functionMap.set('startTaskOne', startTaskOne);// 将函数添加到 Map 中