package yem.wm.cm.csrm.form;

import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.serialization.SerializationUtils;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.BizDataEventArgs;
import kd.bos.entity.filter.FilterCondition;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.control.FilterGrid;
import kd.bos.form.plugin.AbstractFormPlugin;

import java.util.EventObject;

/**
 * 评分方案启动规则动态表单插件
 *
 * @Date 2024/6/13 9:29
 **/
public class ScoringWriterBackPlugin extends AbstractFormPlugin {

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

    @Override
    public void beforeBindData(EventObject e) {
        super.beforeBindData(e);
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        String entityId = showParameter.getCustomParam("entityId");
        FilterGrid filterGrid = this.getView().getControl("yem_filtergridap");
        //设置过滤控件关联单据实体
        filterGrid.setEntityNumber(entityId);
        IDataModel model = this.getModel();
        String yemRule = showParameter.getCustomParam("yem_rule");
        if (!yemRule.isEmpty()) {
            model.setValue("yem_desc", showParameter.getCustomParam("yem_rule"));
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        IFormView view = this.getView();
        FilterGrid filterGrid = view.getControl("yem_filtergridap");
        FormShowParameter formShowParameter = this.getView().getFormShowParameter();
        String yemElaborateTag = formShowParameter.getCustomParam("yem_elaborate_tag");
        if (!yemElaborateTag.isEmpty()) {
            FilterCondition filterCondition = SerializationUtils.fromJsonString(yemElaborateTag, FilterCondition.class);
            if (filterCondition != null) {
                filterGrid = view.getControl("yem_filtergridap");
                filterGrid.SetValue(filterCondition);
            }
        }
        super.afterBindData(e);
    }

    @Override
    public void createNewData(BizDataEventArgs e) {
        super.createNewData(e);
    }

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


    private void afterWriteData() {
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        FilterGrid filterGrid = view.getControl("yem_filtergridap");
        FilterCondition filterCondition = filterGrid.getFilterGridState().getFilterCondition();
        String yem_filtergridap = "";
        if (filterCondition != null) {
            yem_filtergridap = SerializationUtils.toJsonString(filterCondition);
        }
        JSONObject jsonObj = new JSONObject();
        //规则条件
        jsonObj.put("yem_rule", model.getValue("yem_desc"));
        //详细说明
        jsonObj.put("yem_elaborate", yem_filtergridap);
        view.returnDataToParent(jsonObj);
        view.close();
    }
}