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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.events.PackageDataEvent;
import kd.bos.entity.report.ReportColumn;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.Toolbar;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.events.PreOpenFormEventArgs;
import kd.bos.report.ReportList;
import kd.bos.report.plugin.AbstractReportFormPlugin;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.DateUtil;

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

/**
* 人员考评套打列表
* 实体标识：perfbatchprintlist
* @author W.Y.C
* @date 2025/12/7 21:20
* @version 1.0
*/
public class PerfBatchPrintListPlugin extends AbstractReportFormPlugin implements Plugin {

    @Override
    public void preOpenForm(PreOpenFormEventArgs e) {
        // 1. 获取表单显示参数对象
        FormShowParameter showParameter = (FormShowParameter) e.getSource();
        // 2. 设置页面标题
        showParameter.setCaption("套打列表");
        super.preOpenForm(e);
    }
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        Toolbar toolbar = this.getView().getControl(FormConstant.TOOLBARAP);
        toolbar.addItemClickListener(this);
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        String itemKey = evt.getItemKey();
        String operationKey = evt.getOperationKey();
        if("nckd_baritemap".equalsIgnoreCase(itemKey)){
            ReportList reportList = this.getView().getControl(FormConstant.REPORTLISTAP);
            int[] selectedRowIndexes = reportList.getEntryState().getSelectedRows();
            List<Long> personIds =  new ArrayList<>();
            if(selectedRowIndexes != null && selectedRowIndexes.length > 0) {
                for (int selectedRowIndex : selectedRowIndexes) {
                    DynamicObject rowData = reportList.getReportModel().getRowData(selectedRowIndex);
                    personIds.add(rowData.getDynamicObject(FormConstant.NCKD_PERSON).getLong(FormConstant.ID_KEY));
                }
            }else{
                int rowCount = reportList.getReportModel().getRowCount();
                DynamicObjectCollection rowDataColl = reportList.getReportModel().getRowData(1, rowCount, false);
                for (DynamicObject dynamicObject : rowDataColl) {
                    long personId = dynamicObject.getLong(String.join(".", FormConstant.NCKD_PERSON, FormConstant.ID_KEY));
                    personIds.add(personId);
                }
            }
            FormShowParameter showParameter = new FormShowParameter();
            showParameter.setFormId("nckd_batchmoddocstamppopu");
            showParameter.getOpenStyle().setShowType(ShowType.Modal);
            showParameter.setCloseCallBack(new CloseCallBack(this, "nckd_batchmoddocstamppopu"));
            showParameter.setCaption("批量修改文件印发及盖章时间");
            showParameter.setCustomParam( "personIds", personIds);
            this.getView().showForm(showParameter);
        }
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        String actionId = closedCallBackEvent.getActionId();
        if("nckd_batchmoddocstamppopu".equalsIgnoreCase(actionId)){
            Object returnData = closedCallBackEvent.getReturnData();
            if(returnData != null){
                /*Map<String, Date> returnDate = ConvertUtil.toMap(returnData);
                this.getPageCache().put("fileIssueTime", JSON.toJSONString(returnDate));*/
                this.getView().showSuccessNotification( "文件印发及盖章时间修改成功");
                this.getView().invokeOperation(FormConstant.REFRESH_OP);
            }
        }
    }


   @Override
    public void packageData(PackageDataEvent e) {
        String fileIssueTime = this.getPageCache().get("fileIssueTime");
        if(StringUtils.isNotEmpty(fileIssueTime)){
            JSONObject jsonObject = JSON.parseObject(fileIssueTime);
            String colKey = ((ReportColumn)e.getSource()).getFieldKey();
            if ("nckd_fileissuetime".equalsIgnoreCase(colKey) && jsonObject.containsKey("nckd_fileissuetime")) {
                e.setFormatValue(DateUtil.format(jsonObject.getDate("nckd_fileissuetime"),DateUtil.NORM_DATE_PATTERN));
            } else if ("nckd_stamptime".equalsIgnoreCase(colKey) && jsonObject.containsKey("nckd_stamptime")) {
                e.setFormatValue(DateUtil.format(jsonObject.getDate("nckd_stamptime"),DateUtil.NORM_DATE_PATTERN));
            }

        }


    }
}