package nckd.jxccl.swc.mas.plugin.form.incentivemgmt;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.RowDataEntity;
import kd.bos.entity.datamodel.events.AfterAddRowEventArgs;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.ConvertUtil;
import nckd.jxccl.swc.constants.MasConstant;

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

/**
 * 企业负责人任期激励核定表-表单插件
 * 实体标识：nckd_entldrtermbonusappr
 * @author W.Y.C
 * @date 2025/11/30 21:18
 * @version 1.0
 */
public class EntldrTermBonusApprFormPlugin extends AbstractIncentiveApprFormPlugin implements Plugin {


    @Override
    public void afterBindData(EventObject e) {
        DynamicObjectCollection entryCollection = this.getModel().getEntryEntity(FormConstant.NCKD_ENTRYENTITY);
        for (int rowIndex = 0; rowIndex < entryCollection.size(); rowIndex++) {
            DynamicObject entry = entryCollection.get(rowIndex);
            if(entry.get(MasConstant.NCKD_INSTALLMENTYEARS) != null) {
                int installmentYears = entry.getInt(MasConstant.NCKD_INSTALLMENTYEARS);
                initOperateOption(installmentYears, rowIndex);
            }
        }
    }

    @Override
    public void afterAddRow(AfterAddRowEventArgs e) {
        for (RowDataEntity rowDataEntity : e.getRowDataEntities()) {
            int rowIndex = rowDataEntity.getRowIndex();
            initOperateOption(0, rowIndex);
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        
        String fieldKey = e.getProperty().getName();
        ChangeData[] changeSet = e.getChangeSet();
        int rowIndex = changeSet[0].getRowIndex();
        Object oldValue = changeSet[0].getOldValue();
        Object newValue = changeSet[0].getNewValue();
        
        if (MasConstant.NCKD_INSTALLMENTYEARS.equalsIgnoreCase(fieldKey)) {
            if(!Objects.equals(oldValue, newValue)){
                initOperateOption(ConvertUtil.toInt(newValue), rowIndex);
            }
        }
    }

    @Override
    protected String getNotificationMessage() {
        return "未加载到数据，根据【所属二级单位】、【法人组织】及【任期区间】未加载到对应\"任期人员名单\"数据！";
    }

    @Override
    protected void initOperateOption(int installmentYears, int rowIndex) {
        if (rowIndex > -1) {
            for (int i = 1; i <= 10; i++) {
                this.getView().setEnable(false, rowIndex, MasConstant.NCKD_PAYYEAR + i);
                this.getView().setEnable(false, rowIndex, MasConstant.NCKD_PAYAMOUNT + i);
            }
            for (int i = 1; i <= installmentYears; i++) {
                this.getView().setEnable(true, rowIndex, MasConstant.NCKD_PAYYEAR + i);
                this.getView().setEnable(true, rowIndex, MasConstant.NCKD_PAYAMOUNT + i);
            }
        }
    }
}