package nckd.jxccl.hrmp.hbpm.plugin.form.hr;

import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import nckd.jxccl.hrmp.hbpm.common.hr.PositionHRConstant;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 岗位信息维护列表插件
 * @entity: hbpm_positionhr
 * @author: jtd
 * @date: 2026/1/4 21:37
 */
public class PositionHRListPlugin  extends AbstractListPlugin {

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);

        switch(afterDoOperationEventArgs.getOperateKey()) {
            case PositionHRConstant.POSITIONASSIGN_OP:
                Object[] selectedRowIds = getSelectedRows().stream().map(ListSelectedRow::getPrimaryKeyValue).toArray();
                if (selectedRowIds.length > 0) {
                    showPositionAssignFrom(selectedRowIds);
                } else {
                    getView().showTipNotification("请选择要执行的数据。");
                }
        }

    }

    private void showPositionAssignFrom(Object[] selectedRowIds) {
        FormShowParameter formShowParameter = new FormShowParameter();
        formShowParameter.setFormId(PositionHRConstant.NCKD_POSITIONASSIGN_ENTITY);
        formShowParameter.getOpenStyle().setShowType(ShowType.Modal);
        formShowParameter.setCustomParam(PositionHRConstant.SELECTED_IDS_CUSTOM_PARAM, Arrays.stream(selectedRowIds).map(String::valueOf).collect(Collectors.joining(",")));
        formShowParameter.setCloseCallBack(new CloseCallBack(getPluginName(), PositionHRConstant.POSITIONASSIGN_CALLBACK));
        getView().showForm(formShowParameter);
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        super.closedCallBack(closedCallBackEvent);

        switch (closedCallBackEvent.getActionId()) {
            case PositionHRConstant.POSITIONASSIGN_CALLBACK:
                Object returnData = closedCallBackEvent.getReturnData();
                if (returnData != null) {
                    getView().showSuccessNotification("操作成功");
                }
        }
    }
}
