package yem.wm.cm.htqs.form;

import com.alibaba.fastjson.JSONObject;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import yem.base.common.module.meta.propselect.helper.MetaPropertyHelper;

import java.util.EventObject;

public class SignRuleFormPlugIn  extends AbstractBillPlugIn implements BeforeF7SelectListener {

    private static final String KEY_SCHECK = "yem_cm_signcheckrule";//动态表单签署校验规则标识

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        //匹配规则
        this.addClickListeners(new String[]{"yem_matchingrule"});

        //签署单据编码匹配校验规则的单据编码
        ((BasedataEdit) this.getControl("yem_signaturenum")).addBeforeF7SelectListener(this);
    }


    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
//        String key = e.getProperty().getName();
//        switch (key) {
//            case "yem_requirebill":
//            case "yem_changebill":
//                //this.beforeF7SelectBill(e);
//                break;
//            case "yem_changebilltype":
//                //this.beforeF7BillType(e);
//                break;
//        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        String key = ((Control) evt.getSource()).getKey();
        switch (key) {
            case "yem_matchingrule":
                this.clickCheckRule(evt, key);
                break;
            default:
                break;
        }
    }

    private void clickCheckRule(EventObject evt, String key) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();

//        int rowIndex = model.getEntryCurrentRowIndex("yem_checkmap");
//        DynamicObject entryRow = model.getEntryRowEntity("yem_checkmap", rowIndex);

//        DynamicObject matchingrule = null;
//
//        if (model.getValue("yem_matchingrule") instanceof DynamicObject) {
//            matchingrule = (DynamicObject)model.getValue("yem_matchingrule");
//        }
//
//        if (matchingrule == null) {
//            view.showMessage("请先填写签章编码");
//            return;
//        }

        DynamicObject matchingrule = null;

        if (model.getValue("yem_signaturenum") instanceof DynamicObject) {
            matchingrule = (DynamicObject)model.getValue("yem_signaturenum");
        }

        if (matchingrule == null) {
            view.showMessage("请先填写签章编码");
            return;
        }

        FormShowParameter showParameter = new FormShowParameter();
        showParameter.setStatus(OperationStatus.ADDNEW);
        showParameter.getOpenStyle().setShowType(ShowType.Modal);
        showParameter.setFormId(KEY_SCHECK);
        //添加演示页面
//        DynamicObject signaturenum = MetaPropertyHelper.getAndCheckF7Value(view, "yem_signaturenum");
//        showParameter.setCustomParam("entityNumber", signaturenum.getString("number"));
//        showParameter.setCustomParam("data", matchingrule);

        showParameter.setCustomParam("entityNumber", matchingrule.getPkValue());
        showParameter.setCloseCallBack(new CloseCallBack(this, KEY_SCHECK));
        view.showForm(showParameter);

    }


    @Override
    public void closedCallBack(ClosedCallBackEvent e) {
        super.closedCallBack(e);
        String key = e.getActionId();
        switch (key) {
            case KEY_SCHECK:
                this.callBack4CheckRule(e);
                break;

        }
    }

    private void callBack4CheckRule(ClosedCallBackEvent e) {
        String actionId = e.getActionId();
        Object returnData = e.getReturnData();
        if (actionId != null && returnData != null) {
            IDataModel model = this.getModel();
            JSONObject jsonObj = (JSONObject) returnData;

            DynamicObject data = model.getDataEntity();

            model.setValue("yem_matchingrule", jsonObj.getString("yem_desc"));

            data.set("yem_checkcondition_tag",jsonObj.toJSONString());
        }
    }


}
