package com.yem.wm.im.orderproreqbill.dynamic;

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

import java.util.EventObject;
import java.util.Map;
import java.util.Set;

/**
 * @author xwudd
 * @date 17:47
 * @Description
 **/
public class SelectManufacturerEdit 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 = this.getModel();
        FormShowParameter fsp = view.getFormShowParameter();
        Map<String,String> suppliers = fsp.getCustomParam("data");
        Set<String> supplierNumbers = suppliers.keySet();
        for(String supplierNumber : supplierNumbers){
            int entryRow = model.createNewEntryRow("yem_selmanufacturer");
            model.setValue("yem_manufacturer",supplierNumber,entryRow);
            model.setValue("yem_manufacturername",suppliers.get(supplierNumber),entryRow);
        }
    }


    @Override
    public void click(EventObject e) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        FormShowParameter fsp = view.getFormShowParameter();
        Control control = (Control)e.getSource();
        String key = control.getKey();
        if("btnok".equals(key)){
            Integer rowIndex = fsp.getCustomParam("rowIndex");
            int currentRowIndex = model.getEntryCurrentRowIndex("yem_selmanufacturer");
            String manufacturer = (String)model.getValue("yem_manufacturer", currentRowIndex);
            String manufacturerName = (String)model.getValue("yem_manufacturername", currentRowIndex);
            parentModel.setValue("yem_manufacturer",manufacturer,rowIndex);
            parentModel.setValue("yem_manufacturername",manufacturerName,rowIndex);
            view.sendFormAction(parentView);
            view.close();
        }
    }
}
