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

import com.alibaba.fastjson.JSONArray;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.tmc.cim.common.util.StringUtils;

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

/**
 * @author 张立攀
 * @version 1.0
 * @description   整机编号动态表单
 * @date 2023-08-31 16:02
 */
public class ZNumberEdit extends AbstractBillPlugIn {
    @Override
    public void registerListener(EventObject e) {
        this.addClickListeners("btnok");
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        Map<String, Object> map = this.getView().getFormShowParameter().getCustomParams();
        if(map != null){
            int row = 0;
            JSONArray jSONArray= (JSONArray) map.get("key");
            while (jSONArray.iterator().hasNext()){
//                JSONArray entryJson = (JSONArray)jSONArray.get(0);
                this.getModel().createNewEntryRow("yem_entryentity");
//                if(entryJson.size() >= 1){
                this.getModel().setValue("yem_vehicleno",jSONArray.get(0),row);
//                }
                jSONArray.remove(0);
                row ++;
            }
        }
    }

    @Override
    public void click(EventObject evt) {
        Control control = (Control)evt.getSource();
        if(StringUtils.equals(control.getKey(),"btnok")){
            IFormView parentView = this.getView().getParentView();
            IDataModel model = parentView.getModel();
            int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
            int parRow = model.getEntryCurrentRowIndex("yem_im_packinginfor_pi");
            String vehicleno = (String) this.getModel().getValue("yem_vehicleno",row);
            model.setValue("yem_machinenumber",vehicleno,parRow);
            this.getView().sendFormAction(parentView);
            this.getView().close();
        }
    }
}
