package yem.base.common.module.attach.form;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.sdk.plugin.Plugin;
import yem.base.common.utils.YEM;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

/**
 * 单据界面插件
 */
public class PDFMergeFormPlugin extends AbstractBillPlugIn implements Plugin {


    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        Control control = this.getView().getControl("yem_pdf_merge");
        if (control != null){
            this.addClickListeners("yem_pdf_merge");
        }

    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control) evt.getSource();
        String key = source.getKey();
        if ("yem_pdf_merge".equals(key)){
            FormShowParameter showParameter = new FormShowParameter();
            showParameter.setStatus(OperationStatus.ADDNEW);
            showParameter.getOpenStyle().setShowType(ShowType.Modal);
            showParameter.setFormId("yem_base_pdfmerge");
            Map<String, Object> map = new HashMap<>();
            map.put("srcAttachKey", "attachmentpanel");
            map.put("targetAttachKey", "signattachment");
            showParameter.setCustomParams(map);
            showParameter.setCloseCallBack(new CloseCallBack(this.getClass().getName(),"yem_base_pdfmerge"));
            this.getView().showForm(showParameter);
        }
    }


    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operateKey = e.getOperateKey();
        OperationResult operationResult = e.getOperationResult();
        if (!operationResult.isSuccess())return;
        switch(operateKey){
            case "filemerge":// 附件合并
                showFileMergePage();
                break;
            default :
                break;
        }
    }

    // 打开附件合并界面
    private void showFileMergePage(){
        FormShowParameter showParameter = new FormShowParameter();
        showParameter.setStatus(OperationStatus.ADDNEW);
        showParameter.getOpenStyle().setShowType(ShowType.Modal);
        showParameter.setFormId("yem_base_pdfmerge");
        showParameter.setCloseCallBack(new CloseCallBack(this.getClass().getName(),"yem_base_pdfmerge"));
        this.getView().showForm(showParameter);
    }


    @Override
    public void closedCallBack(ClosedCallBackEvent e) {
        super.closedCallBack(e);
        String actionId = e.getActionId();
        if ("yem_base_pdfmerge".equals(actionId)) {
            // 返回目标附件面板key
            String returnData = (String) e.getReturnData();
            if (!YEM.isEmpty(returnData)){
                // 刷新附件面板
                this.getView().updateView(returnData);
                this.getView().showSuccessNotification(ResManager.loadKDString("合并PDF文件成功！", "PDFMergeFormPlugin_0", "yem-base"));
            }
        }
    }

}