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

import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;

import java.util.Date;
import java.util.EventObject;

/**
 * @Description: TODO
 * @Date: 2024/8/29 10:54
 * @Created: by ZZSLL
 */

public class VoidReasonEdit 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();
        IDataModel model = view.getModel();
        IFormView parentView = view.getParentView();
        IDataModel parentViewModel = parentView.getModel();
        String voidreson = (String) parentViewModel.getValue("yem_voidreson");
        model.setValue("yem_voidreson", voidreson);
    }

    @Override
    public void click(EventObject e) {
        super.click(e);
        IFormView view = this.getView();
        IDataModel model = view.getModel();
        Control control = (Control) e.getSource();
        String key = control.getKey();
        IFormView parentView = view.getParentView();
        IDataModel parentViewModel = parentView.getModel();
        if ("btnok".equals(key)) {
            String voidreson = (String) model.getValue("yem_voidreson");
            if (YEM.isNotEmpty(voidreson.trim())) {
                DynamicObject dataEntity = parentViewModel.getDataEntity();
                String name = dataEntity.getDynamicObjectType().getName();
                if (name.equals("yem_es_salesorder")) {
//                    parentViewModel.setValue("yem_canceluser",YEM.getCurrentUserId());
//                    parentViewModel.setValue("yem_canceldate",new Date());
                    parentViewModel.setValue("yem_voidreson", voidreson);
//                    parentViewModel.setValue("yem_cancelstatus", "B");
                } else {
                    parentViewModel.setValue("yem_voidreson", voidreson);
                    parentViewModel.setValue("yem_voidstatus", "B");
                }
                view.close();
            } else {
                view.showErrorNotification("请填写作废原因！！");
            }
        }
        if ("btncancel".equals(key)) {
            view.close();
        }
    }
}
