package com.yem.wm.im.tracktaskconsole.form;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;

import java.util.HashMap;
import java.util.Map;

public class TracktaskconsoleEdit extends AbstractBillPlugIn {

    private static final String KEY_INWARDINFO = "yem_copytasks";// 回填表单标识

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String key = e.getOperateKey();
        if (key.equals("donothing")) {
            donothing();
        }
    }

    /**
     * 回填销售信息
     */
    private void donothing() {
        DynamicObject yemBilltype =(DynamicObject) this.getModel().getValue("yem_billtype");
        FormShowParameter showParameter = new FormShowParameter();// 设置弹出页面标识
        // 设置弹出页面的标识
        showParameter.setFormId(KEY_INWARDINFO);
        Map<String, Object> entryInfo = new HashMap<String, Object>();
        if (yemBilltype!=null){
            String number = yemBilltype.getString("number");
            entryInfo.put("yem_text", number);
            showParameter.setCustomParams(entryInfo);
        }
        // CloseCallBack参数：回调插件，回调标识
        showParameter.setCloseCallBack(new CloseCallBack(this, KEY_INWARDINFO));
        // 状态
        showParameter.setStatus(OperationStatus.ADDNEW);
        // 设置页面关闭回调方法
        // 设置弹出页面打开方式
        showParameter.getOpenStyle().setShowType(ShowType.Modal);
        // 打开动态表单
        this.getView().showForm(showParameter);
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent e) {
        super.closedCallBack(e);
        String actionId = e.getActionId();
        DynamicObject dynamicObjectONe = this.getModel().getDataEntity(true);
        if (KEY_INWARDINFO.equals(actionId)) {
            Object returnData = e.getReturnData();
            if (null != returnData) {
                HashMap<String, Object> values = (HashMap<String, Object>) returnData;
                String yemStockway = (String) values.get("yem_stockway");
                String yemIsvmi = (String) values.get("yem_isvmi");
                DynamicObjectCollection value = (DynamicObjectCollection) this.getModel().getValue("yem_es_salesorder_r");
                EntryGrid entryGrid = this.getControl("yem_es_salesorder_r");
                int[] selectRows = entryGrid.getSelectRows();
                if (value.size() > 0) {
                    for (int selectRow : selectRows) {
                        this.getModel().setValue("yem_stockway", yemStockway, selectRow);
                        this.getModel().setValue("yem_isvmi", yemIsvmi, selectRow);
                    }
                }
            }
        }
    }
}
