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

import com.yem.em.utils.SubRelationUtils;
import com.yem.wm.utils.StringUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
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.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.IFormPlugin;
import kd.bos.list.ListFilterParameter;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author zhouc
 * @date 2023/11/1 23:07
 * @className CompSubFormEdit
 * @description 部件替换关系表单插件
 */
public class CompSubFormEdit extends AbstractBillPlugIn {
    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        String yem_sourcetype = dataEntity.getString("yem_sourcetype");
        if (!yem_sourcetype.equals("Add")) {
            this.getView().setEnable(false, "yem_entryentity");
        } else {
            DynamicObjectCollection yem_entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
            for (int i = 0; i < yem_entryentity.size(); i++) {
                DynamicObject object = yem_entryentity.get(i);
                String yem_replace = object.getString("yem_replace");
                //新增删除锁定替换相关信息
                if ("D".equals(yem_replace)) {
                    this.getView().setEnable(false, i,
                            "yem_praonumber", "yem_praoname", "yem_onumber", "yem_oname", "yem_oenname", "yem_oqty");
                }
                if (!"D".equals(yem_replace)) {
                    this.getView().setEnable(false, i,
                            "yem_oenname", "yem_oqty");
                }
                if ("E".equals(yem_replace)) {
                    this.getView().setEnable(false, i,
                            "yem_repno", "yem_repnoname", "yem_repnumber", "yem_repname", "yem_repenname", "yem_repenqty");
                }
            }
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("number", "yem_parno");
        this.addClickListeners("yem_praonumber", "yem_onumber");
        this.addClickListeners("yem_repno", "yem_repnumber");
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        ChangeData[] changeData = e.getChangeSet();
        Object newValue = changeData[0].getNewValue();
        int row = changeData[0].getRowIndex();
        String key = e.getProperty().getName();
        int enrow = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        if ("yem_onumber".equals(key)) {
            String yem_onumber = (String) model.getValue("yem_onumber", enrow);
            if (StringUtils.isEmpty(yem_onumber)) {
                model.setValue("yem_onumber", "", enrow);
                model.setValue("yem_oname", "", enrow);
                model.setValue("yem_oenname", "", enrow);
                model.setValue("yem_oqty", new BigDecimal(0), enrow);
            }
        }

        if ("yem_repnumber".equals(key)) {
            String yem_onumber = (String) model.getValue("yem_repnumber", enrow);
            if (StringUtils.isEmpty(yem_onumber)) {
                model.setValue("yem_repnumber", "", enrow);
                model.setValue("yem_repname", "", enrow);
                model.setValue("yem_repenname", "", enrow);
                model.setValue("yem_repenqty", new BigDecimal(0), enrow);
            }
        }
        if ("yem_praonumber".equals(key)) {
            String yem_praonumber = (String) model.getValue("yem_praonumber");
            if (StringUtils.isEmpty(yem_praonumber)) {
                model.setValue("yem_praonumber", "");
                model.setValue("yem_praoname", "");
            }
        }
        if ("yem_repno".equals(key)) {
            String yem_repno = (String) model.getValue("yem_repno");
            if (StringUtils.isEmpty(yem_repno)) {
                model.setValue("yem_repno", "");
                model.setValue("yem_repnoname", "");
            }
        }
        if ("yem_parno".equals(key)) {
            String yem_repno = (String) model.getValue("yem_parno");
            if (StringUtils.isEmpty(yem_repno)) {
                model.setValue("yem_parno", "");
                model.setValue("yem_parname", "");
            }
        }
        if ("number".equals(key)) {
            String yem_repno = (String) model.getValue("number");
            if (StringUtils.isEmpty(yem_repno)) {
                model.setValue("number", "");
                model.setValue("name", "");
            }
        }
        //替换关系
        if ("yem_replace".equals(key)) {
            String yem_replace = (String) model.getValue("yem_replace", row);
            //新增删除锁定替换相关信息
            if ("D".equals(yem_replace)) {
                this.getView().setEnable(false, row,
                        "yem_praonumber", "yem_praoname", "yem_onumber", "yem_oname", "yem_oenname", "yem_oqty");
            }
            if (!"D".equals(yem_replace)) {
                this.getView().setEnable(false, row,
                        "yem_oenname", "yem_oqty");
            }
            if ("E".equals(yem_replace)) {
                this.getView().setEnable(false, row,
                        "yem_repno", "yem_repnoname", "yem_repnumber", "yem_repname", "yem_repenname", "yem_repenqty");
            }
            if("A".equals(yem_replace)||"B".equals(yem_replace)||"C".equals(yem_replace)){
                this.getView().setEnable(true, row, "yem_onumber");
            }
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        super.beforeDoOperation(e);
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        FormOperate operate = (FormOperate) e.getSource();
        String OperateKey = operate.getOperateKey();
        if (OperateKey.equals("newentry")) {
            String number = dataEntity.getString("number");
            if (number.length() == 0) {
                view.showErrorNotification("未选择部件编码，请选择后重试！");
                e.cancel = true;
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        String operateKey = e.getOperateKey();
        if ("newentry".equals(operateKey)) {
            int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
            model.setValue("yem_praonumber", dataEntity.getString("number"), row);
            model.setValue("yem_praoname", dataEntity.getString("name"), row);
            model.setValue("yem_repno", dataEntity.getString("number"), row);
            model.setValue("yem_repnoname", dataEntity.getString("name"), row);
        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control c = (Control) evt.getSource();
        String key = c.getKey().toLowerCase();
        IDataModel model = this.getModel();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        //表头编码
        if (key.equals("number")) {
            String yem_replacement = dataEntity.getString("yem_replacement");
            if (yem_replacement.equals("Par")) {
                showfromlist("yem_em_cmmptable", "number");
            }
            if (yem_replacement.equals("Sub")) {
                showfromlist("yem_em_assatlasdet", "number");
            }
        }
        //表头零件编码
        if (key.equals("yem_parno")) {
            Map<String, Object> entryInfo = new HashMap<String, Object>();
            String yem_parentass = dataEntity.getString("number");
            if (yem_parentass.length() > 0) {
                entryInfo.put("number", yem_parentass);
                showpartselection("yem_parno", entryInfo);
            }
        }
        //原部件编码
        if (key.equals("yem_praonumber")) {
            String number = dataEntity.getString("number");
            setlist(number, "yem_praonumber");
        }
        //原零件编码
        if (key.equals("yem_onumber")) {
            int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
            String yem_replace = (String) model.getValue("yem_replace", row);
            if (yem_replace != null) {
                if (yem_replace.equals("D")) {
                    showfromlist("bd_material", "bd_material");
                } else {
                    Map<String, Object> entryInfo = new HashMap<String, Object>();
                    String yem_parentass = (String) model.getValue("yem_praonumber", row);
                    if (yem_parentass.length() > 0) {
                        entryInfo.put("number", yem_parentass);
                        entryInfo.put("yem_replacement", dataEntity.getString("yem_replacement"));
                        showpartselection("yem_onumber", entryInfo);
                    }
                }
            } else {
                getView().showErrorNotification("请先填写替换关系！");
            }
        }
        //替换部件编码
        if (key.equals("yem_repno")) {
            String number = dataEntity.getString("number");
            setlist(null, "yem_repno");

        }
        //替换零件编码
        if (key.equals("yem_repnumber")) {
            showfromlist("bd_material", "yem_repnumber");
        }

    }


    @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 ("number".equals(actionId)) {
            String yem_replacement = dataEntity.getString("yem_replacement");
            ListSelectedRowCollection listData = (ListSelectedRowCollection) e.getReturnData();
            if (listData != null && listData.size() > 0) {
                long fid = (long) listData.get(0).getPrimaryKeyValue();
                if (yem_replacement.equals("Par")) {
                    DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle(fid, "yem_em_cmmptable");
                    if (yem_em_assatlasdet != null) {
                        this.getView().getModel().setValue("number", yem_em_assatlasdet.getString("number"));
                        this.getView().getModel().setValue("name", yem_em_assatlasdet.getString("name"));
                    }
                }
                if (yem_replacement.equals("Sub")) {
                    DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle(fid, "yem_em_assatlasdet");
                    if (yem_em_assatlasdet != null) {
                        this.getView().getModel().setValue("number", yem_em_assatlasdet.getString("number"));
                        this.getView().getModel().setValue("name", yem_em_assatlasdet.getString("name"));
                    }
                }

            }
        }
        //表头零件编码
        if ("yem_parno".equals(actionId)) {
            Object returnData = e.getReturnData();
            if (returnData == null) {
                return;
            }
            Map<String, Object> map = (Map) returnData;
            model.setValue("yem_parno", map.get("yem_number"));
            model.setValue("yem_parname", map.get("yem_name"));
        }
        //原部件编码
        if ("yem_praonumber".equals(actionId)) {
            int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
            ListSelectedRowCollection listData = (ListSelectedRowCollection) e.getReturnData();
            if (listData != null && listData.size() > 0) {
                long fid = (long) listData.get(0).getPrimaryKeyValue();
                DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle(fid, "yem_em_assatlasdet");
                if (yem_em_assatlasdet != null) {
                    this.getView().getModel().setValue("yem_praonumber", yem_em_assatlasdet.getString("number"), row);
                    this.getView().getModel().setValue("yem_praoname", yem_em_assatlasdet.getString("name"), row);
                }
            }
        }
        //原零件编码
        if ("yem_onumber".equals(actionId)) {
            int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
            Object returnData = e.getReturnData();
            if (returnData == null) {
                return;
            }
            Map<String, Object> map = (Map) returnData;
            model.setValue("yem_onumber", map.get("yem_number"), row);
            model.setValue("yem_oname", map.get("yem_name"), row);
            model.setValue("yem_oenname", map.get("yem_enname"), row);
            model.setValue("yem_oqty", map.get("yem_qty"), row);
        }
        if ("bd_material".equals(actionId)) {
            int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
            ListSelectedRowCollection listData = (ListSelectedRowCollection) e.getReturnData();
            if (listData != null && listData.size() > 0) {
                long fid = (long) listData.get(0).getPrimaryKeyValue();
                DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle(fid, "bd_material");
                if (yem_em_assatlasdet != null) {
                    this.getView().getModel().setValue("yem_onumber", yem_em_assatlasdet.getString("number"));
                    this.getView().getModel().setValue("yem_oname", yem_em_assatlasdet.getString("name"));
                    this.getView().getModel().setValue("yem_oenname", yem_em_assatlasdet.getString("yem_nameen"));
                }
            }
        }
        //替换部件编码
        if ("yem_repno".equals(actionId)) {
            int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
            ListSelectedRowCollection listData = (ListSelectedRowCollection) e.getReturnData();
            if (listData != null && listData.size() > 0) {
                long fid = (long) listData.get(0).getPrimaryKeyValue();
                DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle(fid, "yem_em_assatlasdet");
                if (yem_em_assatlasdet != null) {
                    this.getView().getModel().setValue("yem_repno", yem_em_assatlasdet.getString("number"), row);
                    this.getView().getModel().setValue("yem_repnoname", yem_em_assatlasdet.getString("name"), row);
                }
            }
        }
        //替换零件编码
        if ("yem_repnumber".equals(actionId)) {
            int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
//            Object returnData = e.getReturnData();
//            if (returnData == null) {
//                return;
//            }
//            Map<String, Object> map = (Map) returnData;
//            model.setValue("yem_repnumber", map.get("yem_number"), row);
//            model.setValue("yem_repname", map.get("yem_name"), row);
//            model.setValue("yem_repenname", map.get("yem_enname"), row);
//            model.setValue("yem_repenqty", map.get("yem_qty"), row);

            ListSelectedRowCollection listData = (ListSelectedRowCollection) e.getReturnData();
            if (listData != null && listData.size() > 0) {
                long fid = (long) listData.get(0).getPrimaryKeyValue();
                DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle(fid, "bd_material");
                if (yem_em_assatlasdet != null) {
                    this.getView().getModel().setValue("yem_repnumber", yem_em_assatlasdet.getString("number"), row);
                    this.getView().getModel().setValue("yem_repname", yem_em_assatlasdet.getString("name"), row);
                    //根据编码携带英文名称
                    DynamicObject number = SubRelationUtils.querypartenname(yem_em_assatlasdet.getString("number"));
                    if (number != null) {
                        this.getView().getModel().setValue("yem_repenname", number.getString("yem_entryentity.yem_partenname"), row);
                    }
                }
            }
        }
    }

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

        FormShowParameter showParameter = new FormShowParameter();
        showParameter.setFormId("yem_em_partselection");
        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);
    }

    public void setlist(String number, String CallBack) {
        ListShowParameter listpara = new ListShowParameter();
        listpara.setBillFormId("yem_em_assatlasdet");
        listpara.setParentPageId(this.getView().getPageId());
        listpara.setMultiSelect(false);
        listpara.setStatus(OperationStatus.VIEW);
        listpara.getOpenStyle().setShowType(ShowType.Modal);
        ListFilterParameter listFilterParameter = new ListFilterParameter();
        if (number != null) {
            List list = new ArrayList<>();
            QFilter qFilter = new QFilter("number", QCP.equals, number);
            DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle("yem_em_assatlasdet", "id", qFilter.toArray());
            if (yem_em_assatlasdet != null) {
                String pkId = yem_em_assatlasdet.getString("id");
                DynamicObject assatlasdet = BusinessDataServiceHelper.loadSingle(pkId, "yem_em_assatlasdet");
                if (assatlasdet != null) {
                    DynamicObjectCollection yem_entryentity = assatlasdet.getDynamicObjectCollection("yem_entryentity");
                    for (int i = 0; i < yem_entryentity.size(); i++) {
                        DynamicObject dynamicObject = yem_entryentity.get(i);
                        boolean yem_iscomp = dynamicObject.getBoolean("yem_iscomp");
                        if (yem_iscomp) {
                            list.add(dynamicObject.getString("yem_partnumber"));
                        }
                    }
                }
            }
            QFilter qFilter1 = new QFilter("number", QCP.in, list);
            listFilterParameter.setFilter(qFilter1);
        }
        listpara.setListFilterParameter(listFilterParameter);
        listpara.getOpenStyle().setCacheId(listpara.getPageId());
        listpara.setLookUp(true);
        //设置弹出子单据页面的样式
        StyleCss inlineStyleCss = new StyleCss();
        inlineStyleCss.setHeight("800");
        inlineStyleCss.setWidth("1400");
        listpara.getOpenStyle().setInlineStyleCss(inlineStyleCss);
        CloseCallBack closeCallBack = new CloseCallBack(this.getClass().getName(), CallBack);
        listpara.setCloseCallBack(closeCallBack);
        this.getView().showForm(listpara);
    }

    /**
     * 弹出单据列表
     *
     * @param fromid 表单标识
     */
    public void showfromlist(String fromid, String actionId) {
        ListShowParameter listpara = new ListShowParameter();
        listpara.setBillFormId(fromid);
        listpara.setParentPageId(this.getView().getPageId());
        listpara.setMultiSelect(false);
        listpara.setStatus(OperationStatus.VIEW);
        listpara.getOpenStyle().setShowType(ShowType.Modal);
        ListFilterParameter listFilterParameter = new ListFilterParameter();
        listpara.setListFilterParameter(listFilterParameter);
        listpara.getOpenStyle().setCacheId(listpara.getPageId());
        listpara.setLookUp(true);
        //设置弹出子单据页面的样式
        StyleCss inlineStyleCss = new StyleCss();
        inlineStyleCss.setHeight("800");
        inlineStyleCss.setWidth("1400");
        listpara.getOpenStyle().setInlineStyleCss(inlineStyleCss);
        CloseCallBack closeCallBack = new CloseCallBack(this.getClass().getName(), actionId);
        listpara.setCloseCallBack(closeCallBack);
        this.getView().showForm(listpara);
    }
}
