package com.yem.wm.es.xsalesorder.from;

import com.yem.wm.utils.DynamicObjectUtil;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.RowDataEntity;
import kd.bos.entity.datamodel.events.AfterAddRowEventArgs;
import kd.bos.entity.datamodel.events.BeforeDeleteRowEventArgs;
import kd.bos.form.IFormView;
import kd.bos.form.control.SplitContainer;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.EventObject;

/**
 * @author zhouc
 * @date 2024/1/19 16:19
 * @className XSalesOrderFromEdit
 * @description 外销合同变更单表单插件
 */
public class XSalesOrderFromEdit extends AbstractBillPlugIn {

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        SplitContainer splitContainer = this.getControl("yem_splitcontainerap");
        splitContainer.setCollapse("yem_splitpanelap1", true);
        Object yemSigndate = this.getModel().getValue("yem_signdate");
        if (yemSigndate == null) {
            this.getModel().setValue("yem_signdate", this.getModel().getValue("createtime"));
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operateKey = e.getOperateKey();
//        if ("newentry1".equals(operateKey)) {
//            int row = this.getView().getModel().getEntryCurrentRowIndex("yem_es_materialinfo");
//            this.getView().getModel().setValue("yem_linetype", "add", row);
//        }
    }

    @Override
    public void afterAddRow(AfterAddRowEventArgs e) {
        IDataModel model = this.getModel();
        String name = e.getEntryProp().getName();
        RowDataEntity[] rowDataEntities = e.getRowDataEntities();
        for (RowDataEntity rowDataEntitie : rowDataEntities) {
            int rowIndex = rowDataEntitie.getRowIndex();
            if ("yem_es_materialinfo".equals(name)) {
                setMaterialType(rowIndex);
            }
            if ("yem_multiconfig".equals(name)){
                this.getModel().setValue("yem_linetypes","add",rowIndex);
            }
        }
    }

    private void setMaterialType(int rowIndex) {
        IDataModel model = this.getModel();
        model.setValue("yem_linetype", "add", rowIndex);
        DynamicObject billType = (DynamicObject) model.getValue("yem_billtype");
        if (billType != null) {
            String number = billType.getString("number");
            if ("yem_es_xsalesorder_zj".equals(number) || "yem_es_xsalesorder_nmzj".equals(number)) {
                QFilter qFilter = new QFilter("number", QCP.equals, "UM-101");
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bd_measureunits", qFilter.toArray());
                if (dynamicObject != null) {
                    model.setValue("yem_unit", dynamicObject, rowIndex);//台
                    model.setValue("yem_baseunit", dynamicObject, rowIndex);
                }
            }
            if ("yem_es_xsalesorder_pj".equals(number) || "yem_es_xsalesorder_nmpj".equals(number)) {
                QFilter qFilter = new QFilter("number", QCP.equals, "PCS");
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bd_measureunits", qFilter.toArray());
                if (dynamicObject != null) {
                    model.setValue("yem_unit", dynamicObject, rowIndex);//个
                    model.setValue("yem_baseunit", dynamicObject, rowIndex);
                }
            }
        }
    }

    @Override
    public void beforeDeleteRow(BeforeDeleteRowEventArgs e) {
        super.beforeDeleteRow(e);
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        String name = e.getEntryProp().getName();
        int[] rowIndexs = e.getRowIndexs();
        if ("yem_es_materialinfo".equals(name)) {
            for(int rowIndex : rowIndexs){
                Long yem_sourceentryid = (Long)model.getValue("yem_sourceentryid",rowIndex);
//                List<BFRow> loadSourceRowIds = BFTrackerServiceHelper.loadSourceRowIds("yem_es_salesorder", name,new Long[]{yem_sourceentryid} );
//                List<BFRow> loadTargetRowIds = BFTrackerServiceHelper.loadTargetRowIds("yem_es_salesorder"
//                        , name,new Long[]{(long)model.getValue("yem_masterid")},new Long[]{yem_sourceentryid} ,null);
                String selectfield = DynamicObjectUtil.getSelectfields("yem_es_salesorder", false);
                String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_es_salesorder", "yem_es_materialinfo", false);
                DynamicObject loadSingle = BusinessDataServiceHelper.loadSingle("yem_es_salesorder", selectfields
                        , new QFilter[]{new QFilter("id", QCP.equals, model.getValue("yem_masterid"))});
                if (loadSingle!=null) {
                    DynamicObjectCollection yemEsMaterialinfo = loadSingle.getDynamicObjectCollection("yem_es_materialinfo");
                    if (!yemEsMaterialinfo.isEmpty()){
                        for (DynamicObject dynamicObject : yemEsMaterialinfo) {
                            long aLong = dynamicObject.getLong("id");
                            if (aLong == yem_sourceentryid){
                                String yemIntauditbusano = dynamicObject.getString("yem_intauditbusano");
                                if (!"".equals(yemIntauditbusano)){
                                    e.setCancel(true);//阻断增行
                                    view.showTipNotification("第"+(rowIndex+1)+"行分录已存在下游，禁止删除。");
                                }
                            }
                        }
                    }
                }
            }
        }
    }

//    @Override
//    public void afterCreateNewData(EventObject e) {
//        super.afterCreateNewData(e);
//        this.getModel().setValue("yem_signdate",new Date());
//    }
}
