package yem.wm.cm.csrm.form;

import kd.bos.dataentity.serialization.SerializationUtils;
import kd.bos.entity.filter.FilterCondition;
import kd.bos.form.IFormView;
import kd.bos.form.control.FilterGrid;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import yem.wm.cm.csrm.enums.PageIdEnum;

import java.util.EventObject;

/**
 * 评分方案动态表单插件
 *
 * @Date 2024/6/13 9:27
 **/
public class ScoringSchemeFormPlugIn extends AbstractFormPlugin implements BeforeF7SelectListener {

    private static final String KEY_SCHECK = "yem_cm_scorecheckrule";//动态表单签署校验规则标识

    Log log = LogFactory.getLog(ScoringSchemeFormPlugIn.class);

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        //规则条件
        this.addClickListeners(new String[]{"yem_filtergridap"});
        // 侦听主菜单按钮点击事件
        this.addItemClickListeners("tbmain");
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        String itemKey = evt.getItemKey();
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {

    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
    }

    @Override
    public void beforeBindData(EventObject e) {
        super.beforeBindData(e);
        FilterGrid filterGrid = this.getView().getControl("yem_filtergridap");
        //设置过滤控件关联单据实体
        filterGrid.setEntityNumber(PageIdEnum.MERCHANT_APPLY.getPageId());
    }

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

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate formOperate = (FormOperate) args.getSource();
        if ("save".equals(formOperate.getOperateKey()) || "submit".equals(formOperate.getOperateKey())) {
            // TODO 在此添加业务逻辑
            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);
            }
            view.getModel().setValue("yem_elaborate_tag", yem_filtergridap);
        }
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent e) {
        super.closedCallBack(e);
        String key = e.getActionId();
    }
}