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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
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.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.report.ReportList;
import kd.bos.report.plugin.AbstractReportFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
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.ArrayList;
import java.util.EventObject;
import java.util.List;

/**
* TODO
* 实体标识：TODO
* @author W.Y.C
* @date 2026/1/2 17:35
* @version 1.0
*/
public class UnAnnualAdjustReportFormPlugin extends AbstractReportFormPlugin implements Plugin {


    @Override
    public void registerListener(EventObject e) {
        this.addItemClickListeners(FormConstant.TOOLBARAP);
        this.addItemClickListeners(new String[]{"nckd_tblclose"});
    }

    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 beforeQuery(ReportQueryParam queryParam) {
        ReportList reportList = this.getControl(FormConstant.REPORTLISTAP);
        reportList.setSelectedAll(true);
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        String itemKey = evt.getItemKey();

        if("nckd_new".equals(itemKey) ) {
            ReportList reportList = this.getView().getControl(FormConstant.REPORTLISTAP);
            int[] selectedRowIndexes = reportList.getEntryState().getSelectedRows();
            if (selectedRowIndexes == null || selectedRowIndexes.length == 0) {
                this.getView().showTipNotification("请至少选择一条数据");
                return;
            }
            //任职经历ID
            List<Long> personIds = new ArrayList<>();
            if(selectedRowIndexes != null && selectedRowIndexes.length > 0) {
                for (int selectedRowIndex : selectedRowIndexes) {
                    DynamicObject rowData = reportList.getReportModel().getRowData(selectedRowIndex);
                    personIds.add(rowData.getLong(String.join(".", FormConstant.NCKD_EMPLOYEE,FormConstant.ID_KEY)));
                }
            }

            if (personIds.isEmpty()) {
                this.getView().showErrorNotification("请选择正确的数据");
                return;
            }
            if ("nckd_new".equals(itemKey)) {
                //弹出【批量】在职人员初定窗口
                FormShowParameter showParameter = new FormShowParameter();
                showParameter.setFormId(PositionStructureConstant.NEWANNUALADJUST_ENTITYID);
                showParameter.getOpenStyle().setShowType(ShowType.Modal);
                showParameter.setCaption("生成年度调整");
                showParameter.setSendToClient(true);
                showParameter.setCustomParam(FormConstant.NCKD_PERSON, personIds);
                showParameter.setCloseCallBack(new CloseCallBack(this, PositionStructureConstant.NEWANNUALADJUST_ENTITYID));
                this.getView().showForm(showParameter);

            }
        }
    }
}