package nckd.jxccl.wtc.wtte.report;

import com.google.common.collect.Sets;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.entity.report.FilterInfo;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.form.events.HyperLinkClickEvent;
import kd.bos.form.events.HyperLinkClickListener;
import kd.bos.orm.util.CollectionUtils;
import kd.bos.report.ReportList;
import kd.bos.report.ReportShowParameter;
import kd.bos.report.plugin.AbstractReportFormPlugin;
import kd.hr.hbp.common.util.HRStringUtils;
import nckd.jxccl.wtc.wtabm.web.common.util.WtcUtils;

import java.util.EventObject;
import java.util.Iterator;
import java.util.Set;

/**
 * Tyx 2025-10-15
 * 出勤率统计报表界面插件 nckd_attendanceraterpt
 * 缺勤明细报表也用这个插件 nckd_absentdetailrpt
 */
public class AttendanceRateReportFormPlugin extends AbstractReportFormPlugin implements HyperLinkClickListener {

    /**
     * 校验考勤期间和归属日期其中之一必填
     * @param queryParam
     * @return
     */
    @Override
    public boolean verifyQuery(ReportQueryParam queryParam) {
        FilterInfo filterInfo = queryParam.getFilter();
        boolean mustFlag = filterInfo.getDynamicObject("nckd_attperiodentry") == null && (filterInfo.getDate("nckd_owndatestart") == null || filterInfo.getDate("nckd_owndateend") == null);
        if (mustFlag) {
            this.getView().showTipNotification(ResManager.loadKDString("“考勤期间”与“归属日期”至少一个必填。", "RecordSumDayListRFPlugin_1", "wtc-wtte-formplugin", new Object[0]));
            return false;
        } else {
            return super.verifyQuery(queryParam);
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String name = e.getProperty().getName();
        clearViewKey(name, this.getModel());
        switch (name) {
            case "nckd_owndatestart":
                WtcUtils.checkQwndate("nckd_owndatestart", "nckd_owndateend", this.getView(), name);
                break;
            case "nckd_owndateend":
                WtcUtils.checkQwndate("nckd_owndatestart", "nckd_owndateend", this.getView(), name);
        }
    }

    public static void clearViewKey(String name, IDataModel model) {
        if (HRStringUtils.equals("nckd_con_org", name)) {
            Set<String> clearSet = Sets.newHashSetWithExpectedSize(16);
            clearSet.add("nckd_attperiodentry");
            clearViewKey((Set)clearSet, model);
        }

    }

    public static void clearViewKey(Set<String> clearSet, IDataModel model) {
        if (!CollectionUtils.isEmpty(clearSet) && model != null) {
            Iterator it = clearSet.iterator();
            while(it.hasNext()) {
                String clearKet = (String)it.next();
                model.setValue(clearKet, (Object)null);
            }
        }

    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        ReportList list = this.getControl("reportlistap");
        list.addHyperClickListener(this);
    }

    /**
     * 点击缺勤天数后 携带参数跳转到缺勤明细报表nckd_absentdetailrpt
     * @param hyperLinkClickEvent
     */
    @Override
    public void hyperLinkClick(HyperLinkClickEvent hyperLinkClickEvent) {
        String fieldName = hyperLinkClickEvent.getFieldName();
        if(fieldName.equals("nckd_absentday")) {
           // 把当前页面的参数传过去
            ReportShowParameter param = new ReportShowParameter();
            param.setFormId("nckd_absentdetailrpt");
            param.getOpenStyle().setTargetKey(getView().getFormShowParameter().getOpenStyle().getTargetKey());
            param.getOpenStyle().setShowType(getView().getFormShowParameter().getFormConfig().getShowType());
            param.setQueryParam(this.getQueryParam());
            this.getView().showForm(param);
        }
    }
}