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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;

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

/**
 * @Classname DeclareDocxBkFilEdit
 * @Description TODO
 * @Date 2023/12/29 17:23
 * @Created by ZZSLL
 */

public class DeclareDocxBkFilEdit extends AbstractFormPlugin
{

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

    @Override
    public void afterCreateNewData(EventObject e)
    {
        super.afterCreateNewData(e);
        IFormView view = this.getView();
        FormShowParameter parameter = view.getFormShowParameter();
        Map<String, Object> params = parameter.getCustomParams();
        DynamicObject dataEntity = this.getModel().getDataEntity();
        IDataModel model = this.getModel();
        if (params != null) {
            if (params.containsKey("yem_bringbillno")) {
                model.setValue("yem_bringbillno", params.get("yem_bringbillno"));
            }
            if (params.containsKey("yem_realbringbillno")) {
                model.setValue("yem_realbringbillno", params.get("yem_realbringbillno"));
            }
            if (params.containsKey("yem_realdeclarationnum")) {
                model.setValue("yem_realdeclarationnum", params.get("yem_realdeclarationnum"));
            }
            if (params.containsKey("yem_realdeclarationdate")) {
                model.setValue("yem_realdeclarationdate", params.get("yem_realdeclarationdate"));
            }
            model.setValue("yem_cyreightamt", params.get("yem_cyreightamt"));
            model.setValue("yem_reightamt", params.get("yem_reightamt"));
        }
    }

    @Override
    public void click(EventObject e)
    {
        super.click(e);
        Control source = (Control) e.getSource();
        String key = source.getKey();
        if ("btncancel".equals(key)) {
            this.getView().returnDataToParent(null);
            this.getView().close();
        }
        if ("btnok".equals(key)) {
            btnOk();
        }
    }

    private void btnOk()
    {
        DynamicObject dataEntity = this.getModel().getDataEntity();
        HashMap<String, Object> map = new HashMap<>();

        map.put("yem_bringbillno", dataEntity.getString("yem_bringbillno"));
        map.put("yem_realbringbillno", dataEntity.getString("yem_realbringbillno"));
        map.put("yem_realdeclarationnum", dataEntity.getString("yem_realdeclarationnum"));
        map.put("yem_realdeclarationdate", dataEntity.getDate("yem_realdeclarationdate"));
        map.put("yem_logisticsbackfiller", dataEntity.getDynamicObject("yem_logisticsbackfiller"));
        map.put("yem_logbackfilltime", dataEntity.getDate("yem_logbackfilltime"));
        map.put("yem_cyreightamt", dataEntity.getLong("yem_cyreightamt.id"));
        map.put("yem_reightamt", dataEntity.getBigDecimal("yem_reightamt"));

        this.getView().returnDataToParent(map);
        this.getView().close();
    }
}
