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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.BeforeItemClickEvent;
import kd.bos.form.control.events.RowClickEvent;
import kd.bos.form.control.events.RowClickEventListener;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListShowParameter;
import kd.bos.list.events.BackPressedEvent;
import kd.bos.list.events.EndSelectEvent;
import kd.bos.list.events.ItemSelectEvent;
import kd.bos.list.events.ListSelectedListener;
import kd.bos.list.events.QueryListEvent;
import kd.bos.list.events.SelectEvent;
import kd.bos.plugin.sample.dynamicform.pcform.entrygrid.template.EntryRowClick;
import kd.bos.report.ReportList;
import kd.bos.report.ReportShowParameter;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import scala.Int;

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

/**
 * 动态表单插件
 */
public class KeyBeHavEvalReptQueryFromPlugin extends AbstractFormPlugin implements Plugin {

    @Override
    public void registerListener(EventObject e) {
        ReportList reportList = getControl("reportlistap");
        Integer currentPageIndex = reportList.getEntryState().getCurrentPageIndex();
        Integer pageRows = reportList.getEntryState().getPageRows();
        //报表行点击事件
        reportList.addRowClickListener(new RowClickEventListener() {
            //列表选择行事件监听
            @Override
            public void entryRowClick(RowClickEvent evt) {
                int rowCount = reportList.getReportModel().getRowCount();
                int row = evt.getRow();
                int startRow = row - 3;
                if(startRow <= 1){
                    startRow = 1;
                }
                int endRow = row + 3;
                if(endRow >= rowCount){
                    endRow = rowCount;
                }

                DynamicObject selectRowData = reportList.getReportModel().getRowData(row);
                if(selectRowData != null) {
                    reportList.clearSelection();
                    long selectPerson = selectRowData.getLong(String.join(".", FormConstant.NCKD_PERSON, FormConstant.ID_KEY));
                    List<Integer> selectRows = new ArrayList<>();
                    for (int i = startRow; i <= endRow; i++) {
                        DynamicObject rowDate = reportList.getReportModel().getRowData(i);
                        long personid = rowDate.getLong(String.join(".", FormConstant.NCKD_PERSON, FormConstant.ID_KEY));
                        if(personid == selectPerson){
                            selectRows.add( i);
                        }
                    }
                    if(!selectRows.isEmpty()){
                        int[] selectRowsArray = selectRows.stream().mapToInt(Integer::intValue).toArray();
                        reportList.selectRows(selectRowsArray, selectRows.get(selectRows.size() - 1));
                    }

                }
            }
        });
        this.addItemClickListeners(FormConstant.TOOLBARAP);
        this.addItemClickListeners(new String[]{"btnclose"});
    }

    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 afterDoOperation(AfterDoOperationEventArgs e) {
        String operateKey = e.getOperateKey();
        if(e.getOperationResult() != null && e.getOperationResult().isSuccess()){
            if("newappointment".equalsIgnoreCase(operateKey)) {
                ReportList reportList = this.getView().getControl("reportlistap");
                int[] selectedRows = reportList.getEntryState().getSelectedRows();
                if(selectedRows != null && selectedRows.length > 0) {
                    DynamicObject rowData = reportList.getReportModel().getRowData(selectedRows[0]);
                    FormShowParameter showParameter = new FormShowParameter();
                    showParameter.setFormId("nckd_newseniorappoint");
                    showParameter.getOpenStyle().setShowType(ShowType.Modal);
                    showParameter.setCaption("新建聘任");
                    showParameter.setCustomParam("selectRow", rowData.getLong(FormConstant.ID_KEY));
                    showParameter.setCloseCallBack(new CloseCallBack(this, "nckd_newseniorappoint"));
                    this.getView().showForm(showParameter);
                }
            }
        }
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        String actionId = closedCallBackEvent.getActionId();
        if("nckd_newseniorappoint".equalsIgnoreCase(actionId)){
            Object returnData = closedCallBackEvent.getReturnData();
            if(returnData != null) {
                this.getView().showSuccessNotification("聘任成功！");
            }
        }
    }
}