package nckd.jxccl.hr.psms.plugin.form.adjust.report;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.events.BeforeItemClickEvent;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.control.events.ItemClickListener;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.report.ReportList;
import kd.bos.report.plugin.AbstractReportFormPlugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

/**
* 未生成动态调整-报表插件
* @author W.Y.C
* @date 2025/9/17 17:04
* @version 1.0
*/
public class UnAdjustedReportFormPlugin extends AbstractReportFormPlugin implements ItemClickListener {

    @Override
    public void initialize() {
        super.initialize();
    }



    @Override
    public void afterBindData(EventObject e) {
        /*super.afterBindData(e);
        getView().refresh();*/
    }

    @Override
    public void registerListener(EventObject e) {
        //监听工具栏
        this.addItemClickListeners(FormConstant.TOOLBARAP);
        this.addItemClickListeners(new String[]{"btnclose"});
    }

    @Override
    public void beforeQuery(ReportQueryParam queryParam) {
        ReportList reportList = this.getControl(FormConstant.REPORTLISTAP);
        reportList.setSelectedAll(true);
    }

    public void beforeItemClick(BeforeItemClickEvent evt) {
        super.beforeItemClick(evt);
        String itemKey = evt.getItemKey();
        if ("nckd_tblclose".equals(itemKey)) {
            this.getView().getParentView().invokeOperation("close");
            this.getView().sendFormAction(this.getView().getParentView());
        }

    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        String itemKey = evt.getItemKey();
        String operationKey = evt.getOperationKey();
        if("nckd_new".equalsIgnoreCase(itemKey) || "nckd_newbatch".equalsIgnoreCase(itemKey)){
            //创建调整
            ReportList reportList = this.getView().getControl(FormConstant.REPORTLISTAP);
            //获取报表选中的行数据
            int[] selectedRowIndexes = reportList.getEntryState().getSelectedRows();
            if(selectedRowIndexes.length > 1 || "nckd_newbatch".equals(itemKey)){
                //多人调整
                List<Long> personIds = new ArrayList<>();
                for (int selectedRowIndex : selectedRowIndexes) {
                    DynamicObject rowData = reportList.getReportModel().getRowData(selectedRowIndex);
                    personIds.add(rowData.getDynamicObject(FormConstant.NCKD_PERSON).getLong(FormConstant.ID_KEY));
                }
                FormShowParameter showParameter = new FormShowParameter();
                showParameter.setFormId(PositionStructureConstant.NEWDYNAMICADJUBATCH_ENTITYID);
                showParameter.getOpenStyle().setShowType(ShowType.Modal);
                showParameter.setCaption("批量调整");
                showParameter.setCustomParam(FormConstant.NCKD_PERSON,personIds);
                showParameter.setCloseCallBack(new CloseCallBack(this, PositionStructureConstant.NEWDYNAMICADJUBATCH_ENTITYID));
                this.getView().showForm(showParameter);
            }else {

                //单人调整
                FormShowParameter showParameter = new FormShowParameter();
                showParameter.setFormId(PositionStructureConstant.NEWDYNAMICADJUDIALOG_ENTITYID);
                showParameter.getOpenStyle().setShowType(ShowType.Modal);
                showParameter.setCaption("新建调整");
                showParameter.setCloseCallBack(new CloseCallBack(this, PositionStructureConstant.NEWDYNAMICADJUDIALOG_ENTITYID));
                if(selectedRowIndexes.length == 1){
                    DynamicObject rowData = reportList.getReportModel().getRowData(selectedRowIndexes[0]);
                    showParameter.setCustomParam(FormConstant.NCKD_PERSON,rowData.getDynamicObject(FormConstant.NCKD_PERSON).getLong(FormConstant.ID_KEY));
                }
                this.getView().showForm(showParameter);
            }
        }
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        String actionId = closedCallBackEvent.getActionId();
        if(StringUtils.equalsAny(actionId,PositionStructureConstant.NEWDYNAMICADJUDIALOG_ENTITYID)){
            Object returnData = closedCallBackEvent.getReturnData();
            if(returnData != null) {
                //刷新列表
                this.getView().invokeOperation(FormConstant.REFRESH_OP);
            }
        }
    }
}