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 nckd.jxccl.hr.psms.common.PositionStructureConstant;

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

/**
* 批量新建年度调整
* @author W.Y.C
* @date 2025/10/13 20:33
* @version 1.0
*/
public class NewAnnualAdjustFormPlugin extends AbstractFormPlugin implements Plugin {

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

    /**
     * 初始化表单信息
     * @param personIds 人员ID
     * @return: void
     * @author W.Y.C
     * @date: 2025/09/25 21:40
     */
    private void initValue(List<Long> personIds){
        if(personIds.size() >1) {
            this.getModel().batchCreateNewEntryRow(FormConstant.NCKD_ENTRYENTITY, personIds.size() - 1);
        }
        for (int i = 0; i < personIds.size(); i++) {
            this.getModel().setValue(FormConstant.NCKD_PERSON,personIds.get(i), i);
        }

        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 && PositionStructureConstant.OP_CONFIRMADJUST.equalsIgnoreCase(operateKey)){
            Map<String, String> customData = afterDoOperationEventArgs.getOperationResult().getCustomData();
            String jobLeveStr = customData.get(PositionStructureConstant.NCKD_JOBLEVELHR);
            //刷新父级页面
            this.getView().getParentView().invokeOperation(FormConstant.REFRESH_OP);
            this.getView().returnDataToParent("true");
            this.getView().showConfirm("提示","新建调整成功，职位档案已生成。",jobLeveStr, MessageBoxOptions.OK,null,null,null,null);

        }
    }
}