package com.yem.em.ass.ReplacementGroup.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.*;
import kd.bos.form.control.Control;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.events.MessageBoxClosedEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.form.plugin.IFormPlugin;

import java.math.BigDecimal;
import java.util.EventObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhouc
 * @date 2024/1/2 23:07
 * @className replacementgroupFormEdit
 * @description 替换组表单插件
 */
public class replacementgroupFormEdit extends AbstractFormPlugin {
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("yem_repbeforenumber", "yem_repafternumber");
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control c = (Control) evt.getSource();
        String key = c.getKey().toLowerCase();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        if (key.equals("yem_repbeforenumber")) {
            int row = this.getModel().getEntryCurrentRowIndex("yem_repbefore");
            Map<String, Object> entryInfo = new HashMap<String, Object>();
            DynamicObject yem_parentass = dataEntity.getDynamicObject("yem_parentass");
            if (yem_parentass != null) {
                entryInfo.put("yem_parentass", yem_parentass.getString("id"));
                showpartselection("repbeforenumber", entryInfo);
            } else {
                this.getView().showErrorNotification("请选择对应的总成！");
            }
        }
        if (key.equals("yem_repafternumber")) {
            int row = this.getModel().getEntryCurrentRowIndex("yem_repfater");
            Map<String, Object> entryInfo = new HashMap<String, Object>();
            DynamicObject yem_parentass = dataEntity.getDynamicObject("yem_parentass");
            if (yem_parentass != null) {
                entryInfo.put("yem_parentass", yem_parentass.getString("id"));
                showpartselection("repafternumber", entryInfo);
            }
        }
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent e) {
        super.closedCallBack(e);
        String actionId = e.getActionId();
        IDataModel model = this.getModel();
        Object returnData = e.getReturnData();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        if (actionId.equals("repbeforenumber")) {
            if (returnData != null) {
                List<DynamicObject> list = (List) returnData;
                if (list.size() > 0) {
                    int row = this.getModel().getEntryCurrentRowIndex("yem_repbefore");
                    for (int i = 0; i < list.size(); i++) {
                        if (i > 0) {
                            row = this.getModel().createNewEntryRow("yem_repbefore");
                        }
                        DynamicObject object = list.get(i);
                        model.setValue("yem_repbeforenumber", object.get("yem_number"), row);
                        model.setValue("yem_repbeforename", object.get("yem_name"), row);
                        model.setValue("yem_repbeforeenname", object.get("yem_enname"), row);
                        model.setValue("yem_repbeforeqty", object.get("yem_qty"), row);
                        model.setValue("yem_repbeforeremark", object.get("yem_remark"), row);
                    }
                }
            }
        }
        if (actionId.equals("repafternumber")) {
            if (returnData != null) {
                List<DynamicObject> list = (List) returnData;
                if (list.size() > 0) {
                    int row = this.getModel().getEntryCurrentRowIndex("yem_repfater");
                    for (int i = 0; i < list.size(); i++) {
                        if (i > 0) {
                            row = this.getModel().createNewEntryRow("yem_repfater");
                        }
                        DynamicObject object = list.get(i);
                        model.setValue("yem_repafternumber", object.get("yem_number"), row);
                        model.setValue("yem_repaftername", object.get("yem_name"), row);
                        model.setValue("yem_repafterenname", object.get("yem_enname"), row);
                        model.setValue("yem_repafterqty", object.get("yem_qty"), row);
                        model.setValue("yem_repafterremark", object.get("yem_remark"), row);
                    }
                }
            }
        }
    }

    public void showpartselection(String closeCallBack, Map<String, Object> entryInfo) {

        FormShowParameter showParameter = new FormShowParameter();
        showParameter.setFormId("yem_em_multpartselect");
        if (closeCallBack != null) {
            showParameter.setCloseCallBack(new CloseCallBack((IFormPlugin) this, closeCallBack));
        }
        if (entryInfo.size() > 0) {
            showParameter.setCustomParams(entryInfo);
        }
        showParameter.getOpenStyle().setShowType(ShowType.Modal);
        this.getView().showForm(showParameter);
    }

    @Override
    public void beforePropertyChanged(PropertyChangedArgs e) {
        super.beforePropertyChanged(e);
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        String name = e.getProperty().getName();
        ChangeData changeData = e.getChangeSet()[0];
        int rowIndex = changeData.getRowIndex();
        Object newValue = changeData.getNewValue();
        Object oldValue = changeData.getOldValue();
        IPageCache pageCache = this.getPageCache();
        if (name.equals("yem_repbeforenumber")) {
            if (newValue != null && ((String) newValue).length() == 0) {
                model.setValue("yem_repbeforename", null, rowIndex);
                model.setValue("yem_repbeforeenname", null, rowIndex);
                model.setValue("yem_repbeforeqty", new BigDecimal(0), rowIndex);
            }
        }
        if (name.equals("yem_repafternumber")) {
            if (newValue != null && ((String) newValue).length() == 0) {
                model.setValue("yem_repaftername", null, rowIndex);
                model.setValue("yem_repafterenname", null, rowIndex);
                model.setValue("yem_repafterqty", new BigDecimal(0), rowIndex);
            }
        }
        if (name.equals("yem_parentass")) {
            long newlong = 0l;
            long oldlong = 0l;
            if (newValue instanceof Long) {
                newlong = (Long) newValue;
                String yem_parentass = pageCache.get("yem_parentass");
                if (yem_parentass != null) {
                    long l = Long.parseLong(yem_parentass);
                    if (newlong == l) {
                        return;
                    }
                }
            } else {
                DynamicObject yem_contract = (DynamicObject) newValue;
                if (yem_contract != null) {
                    newlong = yem_contract.getLong("id");
                }
            }
            if (oldValue instanceof Long) {
                oldlong = (Long) oldValue;
            } else {
                DynamicObject yem_contract = (DynamicObject) oldValue;
                if (yem_contract != null) {
                    oldlong = yem_contract.getLong("id");
                }
            }
            if (newlong > 0 && oldlong > 0) {
                pageCache.put("yem_parentass", String.valueOf(oldlong));
                pageCache.put("yem_newlong", String.valueOf(newlong));
                if (newlong != oldlong) {
                    view.showConfirm("选择部件后，将清除单据体信息，是否确认切换？", MessageBoxOptions.YesNo, ConfirmTypes.Wait,
                            new ConfirmCallBackListener(name, this));
                }
            }
        }
    }

    @Override
    public void confirmCallBack(MessageBoxClosedEvent e) {
        String callBackId = e.getCallBackId();
        MessageBoxResult result = e.getResult();
        IFormView view = (IFormView) e.getSource();
        IDataModel model = view.getModel();
        IPageCache pageCache = view.getPageCache();
        if (result == MessageBoxResult.Yes) {
            if ("yem_parentass".equals(callBackId)) {
                model.deleteEntryData("yem_repfater");
                model.deleteEntryData("yem_repbefore");
                pageCache.put("yem_parentass", pageCache.get("yem_newlong"));
            }
        }
        if (result == MessageBoxResult.No) {
            if ("yem_parentass".equals(callBackId)) {
                String productsOldValue = pageCache.get("yem_parentass");
                model.setValue("yem_parentass", Long.parseLong(productsOldValue));
            }
        }
    }
}
