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

import java.util.*;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;

/**
 * @author GX
 * 集装箱信息动态表单插件
 */
public class PackingInforTcEdit extends AbstractBillPlugIn {

    /**
     * 获取到父页面（装箱单）传来的数据放入动态表单
     */
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        Map<String, Object> map = this.getView().getFormShowParameter().getCustomParams();
        JSONArray tpv_personinfor = (JSONArray) map.get("yem_im_encasement_ci");//集装箱信息数据
        DynamicObjectCollection newpersoninfor = this.getModel().getEntryEntity("yem_entryentity");
        newpersoninfor.clear();
        Map map1 = new HashMap<>();
        if (map.containsKey("yem_encasenum")) {
            String yem_containernumber = (String) map.get("yem_encasenum");
            String[] split = yem_containernumber.split(";");
            if (split.length > 0) {
                for (int j = 0; j < split.length; j++) {
                    map1.put(split[j], split[j]);
                }
            }
        }

        for (int i = 0; i < tpv_personinfor.size(); i++) {
            JSONObject yemEncasenum = (JSONObject) tpv_personinfor.get(i);
            String yem_encasenum = yemEncasenum.getString("yem_encasenum");
            if (!map1.containsKey(yem_encasenum)) {
                JSONObject parseObject = JSONObject.parseObject(JSONObject.toJSONString(tpv_personinfor.get(i)));
                int index = this.getModel().createNewEntryRow("yem_entryentity");
                this.getModel().setValue("yem_encasenum", parseObject.getString("yem_encasenum"), index);
                this.getModel().setValue("yem_carnums", parseObject.getString("yem_carnums"), index);
            }
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners(new String[]{"btnok", "btncancel"});
    }

    /**
     * 点击事件，点击确认回填数据
     */
    @Override
    public void click(EventObject evt) {
        super.click(evt);
        String key = ((Control) evt.getSource()).getKey();
        switch (key) {
            case "btnok":
                DynamicObject dataEntity = this.getModel().getDataEntity(true);
                List <DynamicObject> list = new ArrayList<>();
                DynamicObjectCollection dataEntitys = dataEntity.getDynamicObjectCollection("yem_entryentity");
                EntryGrid entryGrid = this.getControl("yem_entryentity");
                int[] selectRows = entryGrid.getSelectRows();
                for (DynamicObject data : dataEntitys) {
                    int i = data.getInt("Seq");
                    for (int selectRow : selectRows) {
                        if ((i - 1) == selectRow) {
                            list.add(data);
                        }
                    }
                }
                this.getView().returnDataToParent(list);
                this.getView().close();
                break;
            case "btncancel":
                this.getView().close();
                break;
        }
    }

}
