"use strict";
import {khcModalSlide} from "../../kanhouchu/component/alert/khcModalSlide.js";
import {config} from "../../kanhouchu/config.js";
import {form} from "../../kanhouchu/component/page/form/form.js";
import {pageInfoMap} from "../../kanhouchu/pageInfoMap.js";
import {fullMap} from "../../kanhouchu/fullMap.js";
import {httpUtil} from "../../kanhouchu/util/httpUtil.js";
import {util} from "../../kanhouchu/util/util.js";
import {khcAlert} from "../../kanhouchu/component/alert/khcAlert.js";

export function main(jsonConfig) {
    eval(jsonConfig.button_method).apply(jsonConfig);
}

//用户任务表单
function flwodesignEditUserTaskConfig(){
    let jsonConfig=this;
    let lf=jsonConfig.lf;
    let lfwidth=jsonConfig.lfwidth;
    let node=jsonConfig.node;
    let setDocumentationFieldArray=['opinion_add','opinion_show','is_form_edit','show_field','can_null','read_only','show_button_name','show_buttons','child_page_json']
    let setAssigneeFieldArray=['bpm_user','role','depart'];
    let params = {};
    params.dsCode='ds_template';
    params.formCode='form_template_bpm_config';
    let khcModalForForm = new khcModalSlide({ modalAreaID: config.getConfigJson().modalAreaID });
    khcModalForForm.renderWidthBefore=function (){
        return 600;
    }
    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')
        let bpmParams = {};
        bpmParams.dsCode='ds_form';
        bpmParams.methodName='getFormToDict';
        bpmParams.bpmFormCode=fullMap.getObj('bpmFormCode');
        let obj={url:config.getConfigJson().myActionUrl,params:bpmParams}
        let formConfig={};
        httpUtil.post(obj,(response)=>{
            formConfig=response.data.data.data
            formComponent.renderBeforeFunc=function (){
                let formData = formComponent.formStructureData.formData;
                for (let i = 0; i < formData.length; i++) {
                    if(formData[i].name=='show_field'||formData[i].name=='can_null'||formData[i].name=='read_only'){
                        let dictData={data:formConfig.formInfoDict}
                        formData[i].data.dict_code=dictData;
                    }
                    if(formData[i].name=='show_buttons'){
                        let dictData={data:formConfig.buttonDict}
                        formData[i].data.dict_code=dictData;
                    }
                }
            }
            formComponent.render().then(() => {
                let eleMap = formComponent.getFormMap();
                let nodesMap=lf.graphModel.nodesMap;
                let model=nodesMap[node.id]['model'];
                let textJson=JSON.parse(JSON.stringify(model.text));
                let childShapeJson=fullMap.getObj(node.id)
                let propertiesJson=childShapeJson['properties'];
                let documentationJson;
                if(util.isNull(propertiesJson['documentation'])){
                    documentationJson={x:textJson.x,y:textJson.y}
                }else{
                    documentationJson=JSON.parse(propertiesJson['documentation']);
                    documentationJson.x=textJson.x;
                    documentationJson.y=textJson.y;
                }
                for (let key in documentationJson) {
                    if(setDocumentationFieldArray.includes(key)){
                        if(key=='child_page_json'){//child_page_json 需要特殊处理
                            eleMap.get(key).setValue(JSON.stringify(documentationJson[key]));
                        }else{
                            eleMap.get(key).setValue(documentationJson[key]);
                        }
                    }
                }
                let assigneeStr='';
                if(propertiesJson['usertaskassignment']!=''){
                    assigneeStr=propertiesJson['usertaskassignment']['assignment']['assignee'];//派遣任务配置
                }
                let assigneeJson={};
                if(!util.isNull(assigneeStr)){
                    assigneeJson=JSON.parse(assigneeStr);
                    for (let key in assigneeJson) {
                        if(setAssigneeFieldArray.includes(key)){
                            eleMap.get(key).setValue(assigneeJson[key]);
                        }
                    }
                }
                eleMap.get('text').setValue(node.text.value)
                formComponent.formEdit();
                khcModalForForm.show();
            });
        });
        formComponent.setSaveBefore(()=> {
            let eleMap=formComponent.getFormMap();
            let nodesMap=lf.graphModel.nodesMap;
            let model=nodesMap[node.id]['model'];
            let textJson=JSON.parse(JSON.stringify(model.text));
            let childShapeJson=fullMap.getObj(node.id)
            let propertiesJson=childShapeJson['properties'];
            let documentation;
            if(util.isNull(propertiesJson['documentation'])){
                documentation={}
            }else{
                documentation=JSON.parse(propertiesJson['documentation']);
            }
            for (let i = 0; i < setDocumentationFieldArray.length; i++) {
                let key=setDocumentationFieldArray[i];
                if(key=='child_page_json'){//child_page_json 需要特殊处理
                    if(!util.isNull(eleMap.get(key).getValue())){
                        documentation[key]=JSON.parse(eleMap.get(key).getValue());
                    }
                }else{
                    documentation[key]=eleMap.get(key).getValue();
                }
            }
            if(!util.isNull(documentation['x'])){
                node.x=documentation['x'];
            }
            if(!util.isNull(documentation['y'])){
                node.y=documentation['y'];
            }
            propertiesJson['documentation']=JSON.stringify(documentation);
            let assigneeStr='';
            if(propertiesJson['usertaskassignment']!=''){
                assigneeStr=propertiesJson['usertaskassignment']['assignment']['assignee'];//派遣任务配置
            }else{
                propertiesJson['usertaskassignment']= {'assignment':{'assignee':''}}
            }
            let assigneeJson={};
            if(!util.isNull(assigneeStr)){
                assigneeJson=JSON.parse(assigneeStr);
            }
            for (let i = 0; i < setAssigneeFieldArray.length; i++) {
                let key=setAssigneeFieldArray[i];
                assigneeJson[key]=eleMap.get(key).getValue();
            }
            propertiesJson['usertaskassignment']['assignment']['assignee']=JSON.stringify(assigneeJson)
            childShapeJson['properties']=propertiesJson;
            fullMap.push(node.id,childShapeJson);
            textJson.value=eleMap.get('text').getValue();
            model.text=textJson;
            khcModalForForm.hide();
            khcAlert.showSuccessMessage('保存成功');
            return false;
        })
        khcModalForForm.hideAfterFunc=function (){
            lf.setProperties(node.id, {
                isActived: false,
            });
        }
        khcModalForForm.showAfterFunc=function (){
            let slideLeftButton = document.getElementById(khcModalForForm.objID+'left');
            let slideRightButton = document.getElementById(khcModalForForm.objID+'right');
            let slideModalDialogDiv = document.getElementById(khcModalForForm.objID+'modalDialog');
            let showLeft=function (){
                slideLeftButton.style.display = 'none';
                slideRightButton.style.display = 'block';
                slideModalDialogDiv.style.right = 'auto';
            }
            let showRight=function (){
                slideLeftButton.style.display = 'block';
                slideRightButton.style.display = 'none';
                slideModalDialogDiv.style.right = '';
            }
            let nodex = node.x;
            if (nodex > (lfwidth / 2)) {
                showLeft();//如果节点在屏幕右边，属性面板就显示在左边，反之依然
            } else {
                showRight();
            }
        }
    })
}

//线条表单
function flwodesignEditGatewayConfig(){
    let jsonConfig=this;
    let lf=jsonConfig.lf;
    let lfwidth=jsonConfig.lfwidth;
    let node=jsonConfig.node;
    let params = {};
    params.dsCode='ds_template';
    params.formCode='form_template_bpm_gateway_config';
    let khcModalForForm = new khcModalSlide({ modalAreaID: config.getConfigJson().modalAreaID });
    khcModalForForm.renderWidthBefore=function (){
        return 600;
    }
    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')
        let bpmParams = {};
        bpmParams.dsCode='ds_form';
        bpmParams.methodName='getFormToDict';
        bpmParams.bpmFormCode=fullMap.getObj('bpmFormCode');
        let obj={url:config.getConfigJson().myActionUrl,params:bpmParams}
        let formConfig={};
        httpUtil.post(obj,(response)=>{
            formConfig=response.data.data.data
            formComponent.render().then(() => {
                let eleMap = formComponent.getFormMap();
                let edgesMap=lf.graphModel.edgesMap;
                let model=edgesMap[node.id]['model'];
                let textJson=JSON.parse(JSON.stringify(model.text));
                let childShapeJson=fullMap.getObj(node.id)
                let propertiesJson=childShapeJson['properties'];
                eleMap.get('conditionsequenceflow').setValue(propertiesJson['conditionsequenceflow'])
                formComponent.formEdit();
                khcModalForForm.show();
            });
        });
        formComponent.setSaveBefore(()=> {
            let eleMap=formComponent.getFormMap();
            let edgesMap=lf.graphModel.edgesMap;
            let model=edgesMap[node.id]['model'];
            let textJson=JSON.parse(JSON.stringify(model.text));
            let childShapeJson=fullMap.getObj(node.id)
            let propertiesJson=childShapeJson['properties'];
            propertiesJson['conditionsequenceflow']=eleMap.get('conditionsequenceflow').getValue();
            childShapeJson['properties']=propertiesJson;
            fullMap.push(node.id,childShapeJson);
            khcModalForForm.hide();
            khcAlert.showSuccessMessage('保存成功');
            return false;
        })
        khcModalForForm.hideAfterFunc=function (){
            lf.setProperties(node.id, {
                isActived: false,
            });
        }
        khcModalForForm.showAfterFunc=function (){
            let slideLeftButton = document.getElementById(khcModalForForm.objID+'left');
            let slideRightButton = document.getElementById(khcModalForForm.objID+'right');
            let slideModalDialogDiv = document.getElementById(khcModalForForm.objID+'modalDialog');
            let showLeft=function (){
                slideLeftButton.style.display = 'none';
                slideRightButton.style.display = 'block';
                slideModalDialogDiv.style.right = 'auto';
            }
            let showRight=function (){
                slideLeftButton.style.display = 'block';
                slideRightButton.style.display = 'none';
                slideModalDialogDiv.style.right = '';
            }
            let nodex = node.x;
            if (nodex > (lfwidth / 2)) {
                showLeft();//如果节点在屏幕右边，属性面板就显示在左边，反之依然
            } else {
                showRight();
            }
        }
    })
}