package com.yem.em.ass.dynamic;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.FormShowParameter;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.*;

/**
 * @author zhouc
 * @date 2024/1/3 19:35
 * @className MultPartSelectDynamicEdit
 * @description 零件选择动态表单插件
 */
public class MultPartSelectDynamicEdit extends AbstractFormPlugin {
    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        IDataModel model = this.getModel();
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        Map<String, Object> customParas = showParameter.getCustomParams();
        if (customParas != null) {
            String pkId = "";
            if (customParas.containsKey("yem_parentass")) {
                pkId = customParas.get("yem_parentass").toString();
            }
            if (customParas.containsKey("number")) {
                QFilter qFilter = new QFilter("number", QCP.equals, customParas.get("number"));
                DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle("yem_em_assatlasdet", "id", qFilter.toArray());
                if (yem_em_assatlasdet != null) {
                    pkId = yem_em_assatlasdet.getString("id");
                }
            }
            if (pkId.length() == 0) {
                return;
            }
            DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle(pkId, "yem_em_assatlasdet");
            if (yem_em_assatlasdet != null) {
                DynamicObjectCollection yem_entryentity = yem_em_assatlasdet.getDynamicObjectCollection("yem_entryentity");
                for (int i = 0; i < yem_entryentity.size(); i++) {
                    DynamicObject dynamicObject = yem_entryentity.get(i);
                    boolean yem_isdelete = dynamicObject.getBoolean("yem_isdelete");
                    if (!yem_isdelete) {
                        int row = model.createNewEntryRow("yem_entryentity");
                        model.setValue("yem_prao", yem_em_assatlasdet, row);
                        model.setValue("yem_praonumber", yem_em_assatlasdet.getString("number"), row);
                        model.setValue("yem_praoname", yem_em_assatlasdet.getString("name"), row);
                        model.setValue("yem_number", dynamicObject.getString("yem_partnumber"), row);
                        model.setValue("yem_name", dynamicObject.getString("yem_partname"), row);
                        model.setValue("yem_enname", dynamicObject.getString("yem_partenname"), row);
                        model.setValue("yem_qty", dynamicObject.getBigDecimal("yem_partqty"), row);
                        model.setValue("yem_remark", dynamicObject.getString("yem_partremark"), row);
                    }
                }
                this.getView().updateView("yem_entryentity");
            }
        }
    }

    @Override
    public void registerListener(EventObject e) {
        this.addClickListeners("btnok");
    }

    @Override
    public void click(EventObject e) {
        super.click(e);
        Control control = (Control) e.getSource();
        String key = control.getKey();
        IDataModel model = this.getModel();
        if ("btnok".equals(key)) {
            List list = new ArrayList<>();
//            Map map = new HashMap();
            //获取单据体控件
            EntryGrid entryGrid = this.getControl("yem_entryentity");
            int selectRows[] = entryGrid.getSelectRows();
            DynamicObjectCollection entity = this.getModel().getEntryEntity("yem_entryentity");
            if (selectRows != null && selectRows.length > 0) {
                for (int selectRow : selectRows) {
                    DynamicObject dynamicObject = entity.get(selectRow);  //获取选中行的单据体数据
                    list.add(dynamicObject);
                }
            }
//            int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
//            map.put("yem_number", model.getValue("yem_number", row));//零件编码
//            map.put("yem_name", model.getValue("yem_name", row));//零件名称
//            map.put("yem_enname", model.getValue("yem_enname", row));//零件英文名称
//            map.put("yem_qty", model.getValue("yem_qty", row));//零件数量
//            map.put("yem_remark", model.getValue("yem_remark", row));//备注
            this.getView().returnDataToParent(list);
            this.getView().close();
        }
    }
}
