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

import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;

import java.util.EventObject;

/**
 * @author xwudd
 * @date 14:11
 * @Description
 **/
public class BackResourceNo extends AbstractFormPlugin {

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

    @Override
    public void click(EventObject e) {
        Control control = (Control)e.getSource();
        String key = control.getKey();
        switch (key){
            case "btnok" :
                backFill();
                break;
        }
    }


    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        int detailEdInforRows = parentModel.getEntryRowCount("yem_im_detailedinfor");
        for (int i = 0; i < detailEdInforRows; i++) {
            String businessSort = (String)parentModel.getValue("yem_businesssort", i);
            if("Z".equals(businessSort)){
                int entryRow = model.createNewEntryRow("yem_backfillentry");
                model.setValue("yem_businesssort",parentModel.getValue("yem_businesssort",i),entryRow);
                model.setValue("yem_productmodel",parentModel.getValue("yem_productmodel",i),entryRow);
                model.setValue("yem_orderallocation",parentModel.getValue("yem_orderallocation",i),entryRow);
                model.setValue("yem_resourceid",parentModel.getValue("yem_resourceid",i),entryRow);
                model.setValue("yem_materialrow",i,entryRow);
            }
        }
    }

    public void backFill(){
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        int backFillEntryRows = model.getEntryRowCount("yem_backfillentry");
        for (int i = 0; i < backFillEntryRows; i++) {
            Integer materialRow = (Integer)model.getValue("yem_materialrow");
            parentModel.setValue("yem_resourceid",model.getValue("yem_resourceid",i),materialRow);
        }
        view.sendFormAction(parentView);
        view.close();
    }
}
