package yem.base.common.module.version.dynamic;

import kd.bos.dataentity.serialization.SerializationUtils;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.filter.FilterCondition;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.control.FilterGrid;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import yem.base.common.utils.YEM;

import java.util.EventObject;

/**
 * 条件设置-表单插件
 *
 * @Description:
 * @author: longh
 * @date: 2023年11月7日
 */
public class RuleSettingsEdit extends AbstractFormPlugin {

    @Override
    public void beforeBindData(EventObject e) {
        super.beforeBindData(e);
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        String entityNumber = showParameter.getCustomParam("formKey");
        FilterGrid filterGrid = this.getView().getControl("yem_filtergridap");
        // 设置过滤控件关联单据实体
        filterGrid.setEntityNumber(entityNumber);
    }

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

    @Override
    public void afterCreateNewData(EventObject evt) {
        super.afterCreateNewData(evt);
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        FormShowParameter showParameter = view.getFormShowParameter();
        String filterDesc = showParameter.getCustomParam("filterDesc");
        if (filterDesc != null) {
            model.setValue("yem_desc", filterDesc);
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operateKey = e.getOperateKey();
        switch (operateKey) {
            case "ok":
                this.afterDoOK();
                break;
        }
    }

    /**
     * 点击确定回传数据
     */
    private void afterDoOK() {
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        FormShowParameter showParameter = view.getFormShowParameter();
        String colDesc = showParameter.getCustomParam("colDesc");
        String colJson = showParameter.getCustomParam("colJson");
        String entryKey = showParameter.getCustomParam("entryKey");
        String subEntryKey = showParameter.getCustomParam("subEntryKey");
        FilterGrid filterGrid = view.getControl("yem_filtergridap");
        FilterCondition filterCondition = filterGrid.getFilterGridState().getFilterCondition();
        String filterGridJson = "";
        if (filterCondition != null) {
            filterGridJson = SerializationUtils.toJsonString(filterCondition);
        }
        String filterDesc = (String) model.getValue("yem_desc");
        if (YEM.isEmpty(filterDesc)) {
            this.getView().showErrorNotification("请填写描述！");
            return;
        }
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        if (subEntryKey != null) {
            int entryRowIndex = parentModel.getEntryCurrentRowIndex(entryKey);
            int subEntryRowIndex = parentModel.getEntryCurrentRowIndex(subEntryKey);
            parentModel.setValue(colDesc, filterDesc, subEntryRowIndex, entryRowIndex);
            parentModel.setValue(colJson, filterGridJson, subEntryRowIndex, entryRowIndex);
        } else if (entryKey != null) {
            int entryRowIndex = parentModel.getEntryCurrentRowIndex(entryKey);
            parentModel.setValue(colDesc, filterDesc, entryRowIndex);
            parentModel.setValue(colJson, filterGridJson, entryRowIndex);
        } else {
            parentModel.setValue(colDesc, filterDesc);
            parentModel.setValue(colJson, filterGridJson);
        }
        view.sendFormAction(parentView);
        view.close();
    }

}
