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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.form.ShowType;
import kd.bos.form.events.HyperLinkClickArgs;
import kd.bos.form.events.HyperLinkClickEvent;
import kd.bos.list.IListView;
import kd.bos.list.ListShowParameter;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;

/**
 * @ClassName: ImportPlanListPlugin
 * @Description: 绩效导入任务列表插件
 * @Author: PJJ
 * @Date: 2026/1/6 下午6:11
 * @Version: 1.0
 */
public class ImportPlanListPlugin extends AbstractListPlugin {

    @Override
    public void billListHyperLinkClick(HyperLinkClickArgs args) {
        super.billListHyperLinkClick(args);
        HyperLinkClickEvent hyperLinkClickEvent = args.getHyperLinkClickEvent();
        String fieldName = hyperLinkClickEvent.getFieldName();
        if (StringUtils.equals(fieldName, "name")) {
            //拦截导入任务超链接事件
            args.setCancel(true);
            IListView listView = (IListView) this.getView();
            ListSelectedRow selectRow = listView.getCurrentSelectedRowInfo();
            Long importTaskId = (Long) selectRow.getPrimaryKeyValue();
            //打开绩效结果列表
            this.openTempDataPage(importTaskId);
        }
    }

    private void openTempDataPage(Long ImportPlanId) {
        DynamicObject data = BusinessDataServiceHelper.loadSingle(ImportPlanId,"nckd_importplan");
        if (ObjectUtils.isNotEmpty(data)) {
            ListShowParameter parameter = new ListShowParameter();
            parameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
            parameter.setBillFormId("nckd_examinproject_result");
            parameter.setFormId("bos_templatetreelist");//epa_perfresultreelist
            String caption = MessageFormat.format("绩效导入任务 - {0}", data.getString("name"));
            parameter.setCaption(caption);
            parameter.setCustomParam("importplan", ImportPlanId);
            parameter.setCustomParam("template", data.getDynamicObject("nckd_import_kpi_template").getPkValue() );
            parameter.getListFilterParameter().getQFilters().add(new QFilter("nckd_importplan", QCP.equals, ImportPlanId));
            String pageId = this.getView().getMainView() == null ? this.getView().getPageId() : this.getView().getMainView().getPageId();
            pageId = pageId + '_' + ImportPlanId;
            parameter.setPageId(pageId);
            this.getView().showForm(parameter);
        } else {
            String msg = ResManager.loadKDString("数据已不存在。", "ImportPlan_0", "opmc-formplugin");
            this.getView().showErrorNotification(msg);
        }
    }
}
