package com.yem.wm.es.salesorder.dynamic;

import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.plugin.AbstractFormPlugin;

import java.util.EventObject;

/**
 * @Description: TODO
 * @Date: 2024/5/24 17:44
 * @Created: by ZZSLL
 */

public class InCompLineEdit extends AbstractFormPlugin {

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

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        IFormView view = this.getView();
        IDataModel model = view.getModel();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        DynamicObject parentDataEntity = parentModel.getDataEntity(true);
        DynamicObjectCollection collection = parentDataEntity.getDynamicObjectCollection("yem_es_materialinfo");
        model.deleteEntryData("yem_entry");
        for (DynamicObject object : collection) {
            String businesssort = object.getString("yem_businesssort");
            if ("Z".equals(businesssort)) {
                int idx = model.createNewEntryRow("yem_entry");
                Long pkValue = (Long) object.getPkValue();
                model.setValue("yem_sourceid", pkValue, idx);
                model.setValue("yem_seq", object.getString("seq"), idx);
                model.setValue("yem_basetype", object.getString("yem_productmodel.name"), idx);
                model.setValue("yem_resourceno", object.getString("yem_resourcenog.number"), idx);
            }
        }
    }

    @Override
    public void click(EventObject e) {
        super.click(e);
        IFormView view = this.getView();
        IDataModel model = view.getModel();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        Control control = (Control) e.getSource();
        String key = control.getKey();
        if ("btnok".equals(key)) {
            EntryGrid entry = this.getControl("yem_entry");
            int[] rows = entry.getSelectRows();
            if (rows != null && rows.length > 0) {
                int idx = rows[0];
                Long sourceid = (Long) model.getValue("yem_sourceid", idx);
                int seq = Integer.parseInt((String) model.getValue("yem_seq", idx));
                String basetype = (String) model.getValue("yem_basetype", idx);
                String resourceno = (String) model.getValue("yem_resourceno", idx);
                String KEY = seq + "_" + basetype + "_" + resourceno;
                int rowIndex = parentModel.getEntryCurrentRowIndex("yem_es_materialinfo");
                parentModel.setValue("yem_incompid", sourceid, rowIndex);
                parentModel.setValue("yem_incomptext", KEY, rowIndex);
                DynamicObject resourcenog = (DynamicObject) parentModel.getValue("yem_resourcenog", (seq - 1));
                if (YEM.isNotEmpty(resourcenog)) {
                    parentModel.setValue("yem_resourcenog", resourcenog.getPkValue(), rowIndex);
                }
            }
        }
        view.sendFormAction(parentView);
        view.close();
    }
}
