package com.yem.wm.im.transportCosts.form;

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.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.ksql.util.StringUtil;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.sdk.plugin.Plugin;

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

/**
 * @Description 装箱核算运输费用
 */
public class TransportCostsFormPlugIn extends AbstractBillPlugIn implements BeforeF7SelectListener, Plugin {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        BasedataEdit product = this.getControl("yem_bd_products");
        product.addBeforeF7SelectListener(this);
//        BasedataEdit feeItem = this.getControl("yem_feeitem");
//        feeItem.addBeforeF7SelectListener(this);
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        IDataModel model = this.getModel();
        Control control = (Control) e.getSource();
        String key = control.getKey();
//        if ("yem_bd_products".equals(key)) {
////            DynamicObject products = (DynamicObject) model.getValue("yem_bd_products");
////            if (products != null) {
//            QFilter QFilter = new QFilter("parent", QCP.equals, null);
//            QFilter.or(new QFilter("parent", QCP.equals, 0));
//            e.addCustomQFilter(QFilter);
////            }
////            String accountingType = (String)model.getValue("yem_accountingtype");
////            if(!StringUtil.isEmpty(accountingType)){
////                e.addCustomQFilter(new QFilter("yem_classification", QCP.equals,accountingType));
////            }
//        }
//        if("yem_feeitem".equals(key)){
//            List<Long> ids = new ArrayList<>();
//            DynamicObjectCollection logistics = model.getEntryEntity("yem_bd_logistics");
//            for(DynamicObject logistic : logistics){
//                DynamicObject feeItem = logistic.getDynamicObject("yem_feeitem");
//                if(feeItem != null){
//                    ids.add(feeItem.getLong("id"));
//                }
//            }
//            e.addCustomQFilter(new QFilter("id", QCP.not_in,ids));
//        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        IDataModel model = this.getModel();
        String name = e.getProperty().getName();
        ChangeData[] changeSet = e.getChangeSet();
        int rowIndex = changeSet[0].getRowIndex();
        switch (name) {
            case "yem_bd_products":
                DynamicObject products = (DynamicObject) model.getValue("yem_bd_products");
                if (products != null) {
                    model.setValue("yem_accountingtype", products.getString("yem_businesssort"));
                }
                break;
//            case "yem_accountingtype":
//                model.setValue("yem_bd_products", null);
//                break;
            case "yem_feeitem":
                DynamicObject newValue = (DynamicObject) changeSet[0].getNewValue();
                if (newValue != null) {
                    Long newValueId = newValue.getLong("id");
                    int rows = model.getEntryRowCount("yem_bd_logistics");
                    for (int i = 0; i < rows; i++) {
                        if (i == rowIndex) {
                            continue;
                        }
                        DynamicObject logistic = model.getEntryRowEntity("yem_bd_logistics", i);
                        DynamicObject feeItem = logistic.getDynamicObject("yem_feeitem");
                        if (feeItem != null) {
                            Long id = feeItem.getLong("id");
                            if (newValueId.equals(id)) {
                                this.getView().showErrorNotification("物流费用明细，费用项目不允许重复！");
                                model.setValue("yem_feeitem", null, rowIndex);
                            }
                        }
                    }
                }
                break;
        }
    }

}