package nckd.jxccl.opmc.pm.plugin.form.print;

import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.field.DateEdit;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.ConvertUtil;
import nckd.jxccl.base.common.utils.DateUtil;
import nckd.jxccl.opmc.pm.common.PerfManagerFormConstant;

import java.util.Calendar;
import java.util.Date;
import java.util.EventObject;

/**
* 年度调整生效表单插件
* @author W.Y.C
* @date 2025/10/15 10:14
* @version 1.0
*/
public class SelectDatePrintFormPlugin extends AbstractFormPlugin implements Plugin {

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        String operateKey = afterDoOperationEventArgs.getOperateKey();
        boolean success = afterDoOperationEventArgs.getOperationResult() != null && afterDoOperationEventArgs.getOperationResult().isSuccess();
        if(success && FormConstant.AFFIRM_OP.equalsIgnoreCase(operateKey)){
            Object value = this.getModel().getValue("nckd_printdate");
            this.getView().returnDataToParent(value);
            this.getView().close();
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        // 调用方法，设置日期控件的可选范围
        setDateFieldLimit();
    }

    /**
     * 设置日期字段的可选范围为近5年（例如2026年时，可选2021-2025年）
     */
    private void setDateFieldLimit() {
        // 1. 获取日期控件对象
        DateEdit dateEdit = this.getView().getControl("nckd_printdate");
        if (dateEdit == null) {
            return;
        }

        // 2. 计算日期范围
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);

        Date beginYear = DateUtil.minusYears(DateUtil.nowDate(), 5);
        Date endYear = DateUtil.minusYears(DateUtil.nowDate(), 1);
        // 3. 设置控件的可选范围

        dateEdit.setMinDate(beginYear);
        dateEdit.setMaxDate(endYear);

    }
}