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

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;

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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.events.ClosedCallBackEvent;

/**
 * @author GX
 * 装柜明细动态表单回填
 */
public class PackingInforEdit extends AbstractBillPlugIn {

    /**
     * 获取到父页面（装箱单）传来的数据放入动态表单
     */
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        Map<String, Object> map = this.getView().getFormShowParameter().getCustomParams();
        if (!map.containsKey("yem_im_packinginfor_pi")) {
            return;
        }
        JSONArray tpv_personinfor = (JSONArray) map.get("yem_im_packinginfor_pi");//装柜信息分录数据
        DynamicObjectCollection newpersoninfor = this.getModel().getEntryEntity("yem_entryentity");
        newpersoninfor.clear();
        for (int i = 0; i < tpv_personinfor.size(); i++) {
            JSONObject parseObject = JSONObject.parseObject(JSONObject.toJSONString(tpv_personinfor.get(i)));
            int index = this.getModel().createNewEntryRow("yem_entryentity");
            JSONObject yem_boxgoodsid = parseObject.getJSONObject("yem_boxgoodsid");
            if (yem_boxgoodsid != null) {
                this.getModel().setValue("yem_boxgoodsid", yem_boxgoodsid.getLong("id"), index);
            }
            JSONObject yem_resourceidps = parseObject.getJSONObject("yem_resourceidps");
            if (yem_resourceidps != null) {
                this.getModel().setValue("yem_resourceidps", yem_resourceidps.getLong("id"), index);
            }
            JSONObject yem_cabinettype = parseObject.getJSONObject("yem_cabinettype");
            if (yem_cabinettype != null) {
                this.getModel().setValue("yem_cabinettype", yem_cabinettype.getLong("id"), index);
            }
            this.getModel().setValue("yem_goodescription", parseObject.getString("yem_goodescription"), index);
            this.getModel().setValue("yem_contnum", parseObject.getString("yem_contnum"), index);
            this.getModel().setValue("yem_containernumber", parseObject.getString("yem_containernumber"), index);
            this.getModel().setValue("yem_carnum", parseObject.getString("yem_carnum"), index);
            this.getModel().setValue("yem_machinenumber", parseObject.getString("yem_machinenumber"), index);
            this.getModel().setValue("yem_numberunits", parseObject.getBigDecimal("yem_numberunits"), index);
            this.getModel().setValue("yem_weightsum", parseObject.getBigDecimal("yem_weightsum"), index);
            this.getModel().setValue("yem_cube", parseObject.getBigDecimal("yem_cube"), index);
            this.getModel().setValue("yem_numberparts", parseObject.getBigDecimal("yem_numberparts"), index);
            this.getModel().setValue("yem_mountingweight", parseObject.getBigDecimal("yem_mountingweight"), index);
            this.getModel().setValue("yem_mountingcube", parseObject.getBigDecimal("yem_mountingcube"), index);
            this.getModel().setValue("yem_wholenum", parseObject.getBigDecimal("yem_wholenum"), index);
            this.getModel().setValue("yem_wholew", parseObject.getBigDecimal("yem_wholew"), index);
            this.getModel().setValue("yem_wholecube", parseObject.getBigDecimal("yem_wholecube"), index);
            this.getModel().setValue("yem_comment", parseObject.getString("yem_comment"), index);
        }
    }

    /**
     * 注册按钮
     */
    @Override
    public void registerListener(EventObject e) {
        // TODO Auto-generated method stub
        super.registerListener(e);
        this.addClickListeners(new String[]{"btnok", "btncancel", "yem_containernumber", "yem_carnum", "yem_machinenumber"});
    }

    /**
     * 点击事件，点击集装箱，车号，弹出（集装箱信息）动态表单，点击确认回填数据
     */
    @Override
    public void click(EventObject evt) {
        // TODO Auto-generated method stub
        super.click(evt);
        Map<String, Object> map = this.getView().getFormShowParameter().getCustomParams();
        String key = ((Control) evt.getSource()).getKey();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        switch (key) {
            case "yem_containernumber"://集装箱号
            case "yem_carnum"://车号
                FormShowParameter showParameter = new FormShowParameter();
                showParameter.setStatus(OperationStatus.ADDNEW);
                showParameter.getOpenStyle().setShowType(ShowType.Modal);
                showParameter.setFormId("yem_im_encasement_ci");
                StringBuffer ret = new StringBuffer();
                DynamicObjectCollection yem_entryentitys = dataEntity.getDynamicObjectCollection("yem_entryentity");
                for (int i = 0; i < yem_entryentitys.size(); i++) {
                    DynamicObject object = yem_entryentitys.get(i);
                    String yem_containernumber = object.getString("yem_containernumber");
                    if (yem_containernumber != null) {
                        if (yem_containernumber.length() > 0) {
                            ret.append(yem_containernumber).append(";");
                        }
                    }
                }
                if (ret.length() > 0) {
                    map.put("yem_encasenum", ret.deleteCharAt(ret.length() - 1));
                }
                showParameter.setCustomParams(map);
                showParameter.setCloseCallBack(new CloseCallBack(this, "yem_containernumber"));
                this.getView().showForm(showParameter);
                break;
            case "yem_machinenumber"://整机编号
                FormShowParameter parameter = new FormShowParameter();
                parameter.setStatus(OperationStatus.ADDNEW);
                parameter.getOpenStyle().setShowType(ShowType.Modal);
                parameter.setFormId("yem_im_encasement_bh");
                StringBuffer retData = new StringBuffer();
                DynamicObjectCollection yem_entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
                for (int i = 0; i < yem_entryentity.size(); i++) {
                    DynamicObject object = yem_entryentity.get(i);
                    retData.append(object.getString("yem_machinenumber")).append(";");
                }
                map.put("yem_machinenumber", retData.deleteCharAt(retData.length() - 1));
                parameter.setCustomParams(map);
                parameter.setCloseCallBack(new CloseCallBack(this, "machineNumCallback"));
                this.getView().showForm(parameter);
                break;
            case "btnok"://确认
                this.getView().returnDataToParent(dataEntity);
                this.getView().close();
                break;
            case "btncancel"://取消
                this.getView().close();
                break;
        }
    }

    /**
     * 回调，将数据返回父页面
     */
    @Override
    public void closedCallBack(ClosedCallBackEvent e) {
        String actionId = e.getActionId();
        Object returnData = e.getReturnData();
        switch (actionId) {
            case "yem_containernumber":
                List<DynamicObject> data = ( List <DynamicObject>) returnData;
                setValue(data);
                break;
            case "machineNumCallback":
                String retData = (String) returnData;
                setMachineValue(retData);
                break;
            default:
                break;
        }
    }

    private void setMachineValue(String retData) {
        if (retData != null) {
            int rowIndex = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
            String yem_machinenumber = (String) this.getModel().getValue("yem_machinenumber", rowIndex);
            this.getModel().setValue("yem_machinenumber", yem_machinenumber + ";" + retData, rowIndex);
        }
    }

    /**
     * 将子页面拿到的值赋入
     */
    private void setValue( List <DynamicObject> data) {
        if (data != null) {
            int rowIndex = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
            DynamicObjectCollection yemEntryentity = this.getModel().getEntryEntity("yem_entryentity");
            String yem_encasenum = new String();
            String yem_carnums = "";
            for (DynamicObject datum : data) {
                String yemEncasenum = datum.getString("yem_encasenum");
                yem_encasenum = yem_encasenum + yemEncasenum ;
                String yemCarnums = datum.getString("yem_carnums");
                yem_carnums = yem_carnums + yemCarnums;
            }
            this.getModel().setValue("yem_containernumber", yem_encasenum, rowIndex);
            this.getModel().setValue("yem_carnum", yem_carnums, rowIndex);
        }
    }
}
