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

import com.yem.wm.utils.FunctionalCommon;
import com.yem.wm.utils.YEM;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.botp.runtime.BFRow;
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 kd.bos.servicehelper.botp.BFTrackerServiceHelper;

import java.util.EventObject;
import java.util.List;

/**
 * @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);
    }

    @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);
            }
        }
    }

    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[]{yem_sourceentryid} );
                if(loadTargetRowIds.size() > 0) {
                    e.setCancel(true);//阻断增行
                    view.showTipNotification("该分录已存在下游，禁止删除。");
                }
            }
        }
    }
}
