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

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.entity.operate.OperateOptionConst;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.ShowType;
import kd.bos.form.StyleCss;
import kd.bos.form.control.events.ItemClickEvent;
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.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.common.utils.ConvertUtil;
import nckd.jxccl.base.entity.helper.EntityHelper;
import nckd.jxccl.opmc.pm.common.PerfManagerFormConstant;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 考核周期列表插件
 * 实体标识：nckd_perfmanager
 * @author W.Y.C
 * @date 2025/11/4 13:07
 * @version 1.0
 */
public class PerfManagerListPlugin extends AbstractListPlugin implements Plugin {

    @Override
    public void itemClick(ItemClickEvent evt) {
        String itemKey = evt.getItemKey();
        if ("nckd_new".equals(itemKey)) {
            FormShowParameter showParameter = new FormShowParameter();
            showParameter.setFormId(PerfManagerFormConstant.BATCHEVALCYCLE_ENTITYID);
            showParameter.getOpenStyle().setShowType(ShowType.Modal);
            showParameter.setCaption("新增考核周期");
            showParameter.setSendToClient(true);
            showParameter.setCloseCallBack(new CloseCallBack(this, PerfManagerFormConstant.BATCHEVALCYCLE_ENTITYID));
            this.getView().showForm(showParameter);
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        String operateKey = ((FormOperate) args.getSource()).getOperateKey();
        if (operateKey.equals("list_cyclegenerate")) {
            //周期生成
            /*OperateOption option = OperateOption.create();
            option.setVariableValue(OperateOptionConst.IGNOREINTERACTION, Boolean.TRUE+"");
            DynamicObject perfManager = EntityHelper.newEntity(PerfManagerFormConstant.PERFMANAGER_ENTITYID);
            OperationResult operationResult =  OperationServiceHelper.executeOperate("cyclegenerate", 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);
            }*/
            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 ("batchupdate".equals(operateKey)) {

                this.getView().setVisible(Boolean.FALSE, "nckd_advconbaritemap","nckd_advconbaritemap1");
                ListSelectedRowCollection selectedRows = this.getSelectedRows();
                FormShowParameter showParameter = new FormShowParameter();
                showParameter.setFormId(PerfManagerFormConstant.BATCHEVALCYCLE_ENTITYID);
                showParameter.getOpenStyle().setShowType(ShowType.Modal);
                showParameter.setCaption("批量修改考核周期");
                showParameter.setSendToClient(true);
                showParameter.setCloseCallBack(new CloseCallBack(this, PerfManagerFormConstant.BATCHEVALCYCLE_ENTITYID));
                showParameter.setCustomParam("nckd_isaddorupdate","2");
                if(!selectedRows.isEmpty()) {
                    List<Long> selectedIds = selectedRows.stream()
                            .map(row -> ConvertUtil.toLong(row.getPrimaryKeyValue()))
                            .collect(Collectors.toList());
                    showParameter.setCustomParam("selectedRows", selectedIds);
                }
                StyleCss styleCss = new StyleCss();
               /* styleCss.setWidth("1100");
                styleCss.setHeight("600");*/
                showParameter.getOpenStyle().setInlineStyleCss(styleCss);
                this.getView().showForm(showParameter);
            }
        }
    }
}