package com.yem.wm.bc.companytitle.dynamic;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.dataentity.metadata.clr.DataEntityPropertyCollection;
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.EventObject;

/**
 * @author xwudd
 * @date 16:25
 * @Description
 **/
public class TplCallBackEdit  extends AbstractFormPlugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        IFormView parentView = this.getView().getParentView();
        IDataModel parentModel = parentView.getModel();
        IDataModel model = this.getModel();
        DataEntityPropertyCollection properties = model.getDataEntityType().getProperties();
        for (IDataEntityProperty property : properties) {
            String propName = property.getName();
            if (parentModel.getProperty(propName) != null) {
                Object propValue = parentModel.getValue(propName);
                if (propValue instanceof DynamicObject) {
                    DynamicObject obj = (DynamicObject) propValue;
                    model.setValue(propName, obj.getPkValue());
                } else {
                    model.setValue(propName, propValue);
                }
            }
        }
        DynamicObject company = (DynamicObject) parentModel.getValue("yem_company");
        if (company != null && "yem_addcustomertitle".equals(model.getDataEntityType().getName())) {
            model.setValue("yem_orgfield", company.get("yem_erporg"));
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners(new String[]{"btnok"});
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        String key = ((Control) evt.getSource()).getKey();
        switch (key) {
            case "btnok" :
                this.btnOk();
        }
    }

    private void btnOk() {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        DataEntityPropertyCollection properties = model.getDataEntityType().getProperties();
        for (IDataEntityProperty property : properties) {
            String propName = property.getName();
            if (parentModel.getProperty(propName) != null) {
                Object propValue = model.getValue(propName);
                if (propValue instanceof DynamicObject) {
                    DynamicObject obj = (DynamicObject) propValue;
                    parentModel.setValue(propName, obj.getPkValue());
                } else {
                    parentModel.setValue(propName, propValue);
                }
            }
        }
        view.sendFormAction(parentView);
        view.close();
    }


}
