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

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.field.ComboEdit;
import kd.bos.form.field.ComboItem;
import kd.bos.form.plugin.AbstractFormPlugin;

import java.math.BigDecimal;
import java.util.*;

/**
 * ljw
 * 跟单任务控制台分录弹窗表单插件
 */
public class QueryCopytasksEdit extends AbstractFormPlugin  {

    @Override
    public void registerListener(EventObject e) {// 列表监听事件栏
        super.registerListener(e);
        final Button btOK = (Button) this.getView().getControl("btnok");// 获取子表单模型//确认键
        if (btOK != null) {
            this.addClickListeners(new String[]{"btnok"});// 监听子表单确认键
        }
        this.addItemClickListeners("yem_advcontoolbarap");
    }
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        IFormView view = this.getView();
        Map<String, Object> customParams = view.getFormShowParameter().getCustomParams();
        String yemText = (String) customParams.get("yem_text");
        IDataModel model = this.getModel();
        model.setValue("yem_text",yemText);
    }
    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
            String billtype = dataEntity.getString("yem_text");
            //分录配件生产信息 yem_es_salesorder_r
            // yem_stockway
            // 整机显示 现产 A 改制 B 外购	D
            //配件显示 自制 C 外购 D 调拨	E
            Map map = new HashMap<>();
            if (billtype.equals("yem_tracktaskconsole_zj")) {//整机
                map.put("现产", "A");
                map.put("改制", "B");
                map.put("外购", "D");
                fillEntityCombo(map);
            }
            if (billtype.equals("yem_tracktaskconsole_pj")) {
                map.put("自制", "C");
                map.put("外购", "D");
                map.put("调拨", "E");
                map.put("库存", "F");
                fillEntityCombo(map);
            }
    }
    /**
     * @param map 下拉项值
     */
    private void fillEntityCombo(Map<String, Object> map) {
        ComboEdit mainentry = (ComboEdit) this.getView().getControl("yem_stockway");
        if (map.size() > 0) {
            List<ComboItem> entryItems = this.buildEntryComboItems(map);
            mainentry.setComboItems(entryItems);
        } else {
            mainentry.setComboItems(null);
        }
    }
    /**
     * @param entitys
     * @return
     */
    private List<ComboItem> buildEntryComboItems(Map<String, Object> entitys) {
        ArrayList<ComboItem> entryItems = new ArrayList<>();
        ComboItem blankItem = new ComboItem();
        blankItem.setValue("");
        blankItem.setCaption(new LocaleString());
        entryItems.add(blankItem);
        if (entitys != null && entitys.size() > 0) {
            for (Map.Entry entry : entitys.entrySet()) {
                String mapKey = (String) entry.getKey();
                String mapValue = (String) entry.getValue();
                ComboItem entryItem = new ComboItem();
                entryItem.setValue(mapValue);
                entryItem.setCaption(new LocaleString(mapKey));
                entryItems.add(entryItem);
            }
        }
        return entryItems;
    }
    @Override
    public void click(EventObject evt) {
        final Control c = (Control) evt.getSource();
        if ("btnok".equals(c.getKey().toLowerCase())) {
            this.confirm(evt);// 执行evt
        } else if (StringUtils.equals(c.getKey(), "btncancel")) {
            this.getView().returnDataToParent(null);
            this.getView().close();
        }
    }
    /**
     * 返回父页面
     *
     * @param evt
     */
    public void confirm(EventObject evt) {// 构造确认取消对话框
        Map<String, Object> map = new HashMap<String, Object>();//// 获取Map接口对象
        map.put("yem_stockway",this.getModel().getValue("yem_stockway"));
        map.put("yem_isvmi",this.getModel().getValue("yem_isvmi"));
        this.getView().returnDataToParent(map);// 设置返回单据表单的值
        this.getView().close();// 关闭动态表单
    }

}
