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

import kd.bos.form.FormShowParameter;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.events.AfterDoOperationEventArgs;
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 org.apache.commons.lang3.StringUtils;

import java.util.EventObject;
import java.util.List;
import java.util.Map;

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

    @Override
    public void afterCreateNewData(EventObject e) {
        // 获取当前页面的FormShowParameter对象
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        //获取列表选择的人员
        Object personIds = showParameter.getCustomParam(FormConstant.NCKD_PERSON);
        if(personIds != null) {
            initValue(ConvertUtil.toList(personIds));
        }
    }

    /**
     * 初始化表单信息
     * @param personIds 人员ID
     * @return: void
     * @author W.Y.C
     * @date: 2025/09/25 21:40
     */
    private void initValue(List<Long> personIds){
        //多选基础资料赋值
        this.getModel().setValue("nckd_mulselepersonposfile", personIds.toArray());
        this.getView().updateView(FormConstant.NCKD_ENTRYENTITY);
    }

    @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)){
            Map<String, String> customData = afterDoOperationEventArgs.getOperationResult().getCustomData();
            String message = customData.get("message");
            if(StringUtils.isNotBlank( message)){
                this.getView().showConfirm("提示","生效成功!",message, MessageBoxOptions.OK,null,null,null,null);
            }else{
                this.getView().getParentView().showSuccessNotification("生效成功!");
            }
            this.getView().returnDataToParent("true");
        }
    }
}