package nckd.jxccl.hr.psms.plugin.form.contribution;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.ConvertUtil;
import nckd.jxccl.hr.psms.common.ContributionConstant;
import nckd.jxccl.hr.psms.common.PerfRankMgmtConstant;

import java.util.EventObject;
import java.util.Objects;

/**
* 积分项目分数配置插件
* 实体标识：nckd_scoreitemconf
* @author W.Y.C
* @date 2025/10/25 22:13
* @version 1.0
*/
public class ScoreItemConfFormPlugin extends AbstractFormPlugin implements Plugin, BeforeF7SelectListener {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);

        // 为分录中的A字段添加值变化监听
        BasedataEdit scoreItemSubEdit = this.getView().getControl(ContributionConstant.NCKD_SCOREITEMSUB);
        if (scoreItemSubEdit != null) {
            scoreItemSubEdit.addBeforeF7SelectListener(this);
        }

        BasedataEdit scoreItemRankEdit = this.getView().getControl(ContributionConstant.NCKD_SCOREITEMRANK);
        if (scoreItemRankEdit != null) {
            scoreItemRankEdit.addBeforeF7SelectListener(this);
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        this.getModel().setValue(FormConstant.STATUS, "A");
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        String fieldKey = e.getProperty().getName();

        //基础资料联动
        // 仅处理B字段的F7事件
        if (ContributionConstant.NCKD_SCOREITEMSUB.equalsIgnoreCase(fieldKey) || ContributionConstant.NCKD_SCOREITEMRANK.equalsIgnoreCase(fieldKey)) {
            // 获取当前分录行的行号
            int currentRow = e.getRow();

            if (currentRow >= 0) {
                // 获取当前行A字段的值
                DynamicObject fieldAValue = ConvertUtil.toDynamicObjectOrNull(this.getModel().getValue(ContributionConstant.NCKD_SCOREITEM, currentRow));
                if(fieldAValue == null){
                    this.getView().showTipNotification("请先选择“积分项目”");
                    e.setCancel( true);
                    return;
                }
                QFilter filter = new QFilter(ContributionConstant.NCKD_SCOREITEM, QCP.equals, fieldAValue.getLong(FormConstant.ID_KEY));
                e.addCustomQFilter(filter);
            }
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String name = e.getProperty().getName();
        if (ContributionConstant.NCKD_SCOREITEMSUB.equalsIgnoreCase(name) || ContributionConstant.NCKD_SCOREITEM.equalsIgnoreCase(name)) {

            ChangeData[] changeSet = e.getChangeSet();
            Object newValue = changeSet[0].getNewValue();
            Object oldValue = changeSet[0].getOldValue();
            if(newValue != null && !Objects.equals(newValue, oldValue)) {
                if(ContributionConstant.NCKD_SCOREITEMSUB.equalsIgnoreCase(name)){
                    this.getModel().setValue(ContributionConstant.NCKD_SCOREITEMRANK, null,changeSet[0].getRowIndex());
                    this.getView().updateView(ContributionConstant.NCKD_SCOREITEMRANK,changeSet[0].getRowIndex());
                }else{
                    this.getModel().setValue(ContributionConstant.NCKD_SCOREITEMRANK, null,changeSet[0].getRowIndex());
                    this.getView().updateView(ContributionConstant.NCKD_SCOREITEMRANK,changeSet[0].getRowIndex());
                    this.getModel().setValue(ContributionConstant.NCKD_SCOREITEMSUB, null,changeSet[0].getRowIndex());
                    this.getView().updateView(ContributionConstant.NCKD_SCOREITEMSUB,changeSet[0].getRowIndex());
                }
            }
        }
    }

}