package yem.base.common.module.meta.report;


import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.entity.report.FilterInfo;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.form.CloseCallBack;
import kd.bos.form.ShowFormHelper;
import kd.bos.form.control.Control;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.list.ListShowParameter;
import kd.bos.report.plugin.AbstractReportFormPlugin;
import yem.base.common.utils.YEM;

import java.util.EventObject;

/**
 * 数据比对
 *
 * @Description: 报表表单插件
 * @Date: 2024/4/11 23:40
 * @Auther: longh
 */
public class DataCompareRptEdit extends AbstractReportFormPlugin {
    private static final String entryKey = "yem_entryentity";

    @Override
    public boolean verifyQuery(ReportQueryParam queryParam) {
        DynamicObjectCollection yem_entryentity = this.getModel().getEntryEntity("yem_entryentity");
        Object[] pkArray = yem_entryentity.stream().map(x -> x.get("yem_fdataid")).toArray();
        FilterInfo filterInfo = queryParam.getFilter();
        filterInfo.addFilterItem("yem_entryentity", pkArray);
        return super.verifyQuery(queryParam);
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("yem_fdata");
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        String key = ((Control) evt.getSource()).getKey();
        if ("yem_fdata".equals(key)) {
            dataClick(key);
        }
    }

    private void dataClick(String key) {
        DynamicObject yem_fentityobj = (DynamicObject) this.getModel().getValue("yem_fentityobj");
        if (yem_fentityobj != null) {
            ListShowParameter createShowListForm = ShowFormHelper.createShowListForm(yem_fentityobj.getString("id"), true, 0);
            createShowListForm.setCloseCallBack(new CloseCallBack(this, key));
            this.getView().showForm(createShowListForm);
        } else {
            this.getView().showTipNotification("请先选择主实体。");
        }
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent event) {
        String actionId = event.getActionId();
        if (actionId != null) {
            if ("yem_fdata".equals(actionId)) {
                listCallBack(event);
            }
        }
    }

    private void listCallBack(ClosedCallBackEvent event) {
        IDataModel model = this.getModel();
        ListSelectedRowCollection selectedRows = (ListSelectedRowCollection) event.getReturnData();
        if (selectedRows != null) {
            for (int i = 0; i < selectedRows.size(); ++i) {
                ListSelectedRow info = selectedRows.get(i);
                String billNo = info.getBillNo();
                String number = info.getNumber();
                Object primaryKeyValue = info.getPrimaryKeyValue();
                int index;
                if (i == 0) {
                    index = model.getEntryCurrentRowIndex(entryKey);
                } else {
                    index = model.createNewEntryRow(entryKey);
                }
                model.setValue("yem_fdata", YEM.isNotEmpty(billNo) ? billNo : number, index);
                model.setValue("yem_fdataid", primaryKeyValue, index);
            }
        }
    }

}
