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

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

import java.math.BigDecimal;
import java.util.Date;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname OrderProreqBillEdit
 * @Description TODO
 * @Date 2023/12/27 11:19
 * @Created by ZZSLL
 */

public class OrderProreqBillEdit extends AbstractFormPlugin
{

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

    @Override
    public void click(EventObject e)
    {
        super.click(e);
        Control source = (Control) e.getSource();
        String key = source.getKey();
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        DynamicObject dataEntity = model.getDataEntity(true);
        if ("btncancel".equals(key)) {
            view.returnDataToParent(null);
            view.close();
        }
        if ("btnok".equals(key)) {

            BigDecimal quantity = dataEntity.getBigDecimal("yem_quantity");
            BigDecimal closequantity = dataEntity.getBigDecimal("yem_closequantity");
            if (quantity.compareTo(closequantity) < 0) {
                this.getView().showErrorNotification("关闭数量不允许超出数量");
                return;
            }

            Map<String, Object> map = new HashMap<>();
            map.put("yem_closequantity", dataEntity.getBigDecimal("yem_closequantity"));
            map.put("yem_closereson", dataEntity.getString("yem_closereson"));
            map.put("yem_closepeople", dataEntity.getLong("yem_closepeople.id"));
            map.put("yem_closingtime", dataEntity.getDate("yem_closingtime"));
            view.returnDataToParent(map);
            view.close();
        }
    }

    @Override
    public void afterCreateNewData(EventObject e)
    {
        super.afterCreateNewData(e);
        FormShowParameter parameter = this.getView().getFormShowParameter();
        Map<String, Object> map = parameter.getCustomParams();
        IDataModel model = this.getModel();
        model.setValue("yem_materials", map.get("yem_materiel"));
        model.setValue("yem_quantity", map.get("yem_qty"));
    }
}
