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

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.OperateOptionConst;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.ShowType;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.report.ReportShowParameter;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.exception.ValidationException;
import nckd.jxccl.base.entity.helper.EntityHelper;
import nckd.jxccl.opmc.pm.common.PerfManagerFormConstant;

/**
* 人员考评列表
* 实体标识：nckd_perfmanager_query
* @author W.Y.C
* @date 2025/11/14 9:38
* @version 1.0
*/
public class PerfmanagerQueryListPlugin extends AbstractListPlugin implements Plugin {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        String operateKey = ((FormOperate) args.getSource()).getOperateKey();
        if(operateKey.equals("salaryadj")){
            //调档
            OperateOption option = OperateOption.create();
            option.setVariableValue(OperateOptionConst.IGNOREINTERACTION, Boolean.TRUE+"");
            DynamicObject perfManager = EntityHelper.newEntity(PerfManagerFormConstant.PERFMANAGER_ENTITYID);
            OperationResult operationResult =  OperationServiceHelper.executeOperate("salaryadj", PerfManagerFormConstant.PERFMANAGER_ENTITYID, new DynamicObject[]{perfManager}, option);
            if (!operationResult.isSuccess()) {
                int messageType = operationResult.getMessageType();
                if (messageType == 1) {
                    this.getView().showTipNotification(operationResult.getMessage());
                } else {
                    String message = operationResult.getMessage();
                    throw new ValidationException(message);
                }
            }else{
                //刷新列表
                this.getView().invokeOperation(FormConstant.REFRESH_OP);
                this.getView().showConfirm("提示",operationResult.getMessage(), MessageBoxOptions.OK,null,null,null,null);
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        if(e.getOperationResult() != null && e.getOperationResult().isSuccess()){
            String operateKey = e.getOperateKey();
            if("annualperfdetail".equalsIgnoreCase(operateKey)){
                // 打开单据列表
                ReportShowParameter reportShowParameter = new ReportShowParameter();
                reportShowParameter.setFormId("nckd_annualperfdetail");
                reportShowParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage); // 设置打开方式
                reportShowParameter.setCaption("年度绩效结果明细");
                this.getView().showForm(reportShowParameter);

            }else if("updateresult".equalsIgnoreCase(operateKey)){
                this.getView().getParentView().invokeOperation(FormConstant.REFRESH_OP);
            }
        }
    }
}