package com.yem.wm.bd.costs.from;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
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.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;

import java.util.EventObject;

/**
 * @author power
 * @ClassName CostsEdit
 * @description: 机型拆装费用表单插件
 * @date 2023年07月20日
 * @version: 1.0
 */
public class CostsEdit extends AbstractBillPlugIn implements BeforeF7SelectListener {
    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String propertyName = e.getProperty().getName();
        boolean stopClear;
        ChangeData cd = e.getChangeSet()[0];
        ChangeData[] changeData = e.getChangeSet();
        int rowIndex = changeData[0].getRowIndex();
        switch (propertyName) {
            case "yem_bd_mechanical" :
                this.mechanical();
                this.getModel().setValue("yem_tearamount",null,rowIndex);
                break;
            case "yem_products" :
               this.getModel().deleteEntryData("yem_detail");
                this.getModel().batchCreateNewEntryRow("yem_detail", 1);
                break;

            default:
                break;
        }

    }
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        //给物品基础资料添加beforef7Select事件监听
        BasedataEdit supplies = this.getControl("yem_bd_mechanical");
        supplies.addBeforeF7SelectListener(this);
    }
    /**
     * 基础资料按表头条件过滤
     */
    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        DynamicObject datas = this.getModel().getDataEntity(true);
        Control control = (Control) e.getSource();
        String key = control.getKey();
        if ("yem_bd_mechanical".equals(key)) {

            DynamicObject yem_products = datas.getDynamicObject("yem_products");
            if (yem_products!=null){
                Long id = yem_products.getLong("id");
                // 设置列表过滤条件
//                ListShowParameter formShowParameter = (ListShowParameter)e.getFormShowParameter();
//                formShowParameter.getListFilterParameter().setFilter(new QFilter("group.parent.id", QCP.equals,id));
                QFilter qFilter = new QFilter("group.parent.id", QCP.equals, yem_products.getLong("id"));
                e.addCustomQFilter(qFilter);
            }
        }
    }
    /**
     * 提示选择产品类型
     */
    private void mechanical(){
        //产品类型
        DynamicObject yem_products = (DynamicObject)this.getModel().getValue("yem_products");
        if (yem_products==null){
            this.getView().showErrorNotification("产品类型未选择，请先选择产品类型");
        }
    }




}
