package nckd.jxccl.wtc.wtabm.vaapply;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.AfterDeleteRowEventArgs;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.operate.AbstractOperate;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.hr.hbp.formplugin.web.HRCoreBaseBillEdit;
import nckd.jxccl.wtc.wtabm.web.common.util.WtcUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * Tyx
 * 2025-09-29
 * 休假申请单扩展表单插件
 */
public class VaApplyBillEditEx extends HRCoreBaseBillEdit {

    private static final Log log = LogFactory.getLog(VaApplyBillEditEx.class);

    /**
     * 获取历史休假明细信息，填充到单据上nckd_hisentryentity单据体上
     * @param closedCallBackEvent
     */
    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        super.closedCallBack(closedCallBackEvent);
        String actionId = closedCallBackEvent.getActionId();
        IDataModel model = this.getModel();
        DynamicObjectCollection cols = model.getEntryEntity("entryentity");
        if (StringUtils.equalsAny(actionId, new CharSequence[]{"newentry", "editrow"})) {
            DynamicObject entry = cols.get(0);
            // 当前休假人员
            Long employeeId = Long.valueOf(model.getValue("attfilebasef7.employee.id").toString());

            // 休假开始日期
            Date date = entry.getDate("entrystartdate");
            // 休假类型编码

            String typeNumber = entry.getString("entryvacationtype.number");

            log.info("-------- 开始查询历史休假明细 empId：{}, typeNumber：{}", employeeId, typeNumber);
            DynamicObjectCollection historyBillCols = WtcUtils.getHistoryVaApplyInfos(employeeId, date, typeNumber);
            log.info("-------- 结束查询历史休假明细 条数：{}", historyBillCols.size());

            for (DynamicObject historyDyn : historyBillCols) {
                int row = model.createNewEntryRow("nckd_hisentryentity");
                model.setValue("nckd_hisvaltype", historyDyn.getString("entryentity.entryvacationtype.name"), row);
                model.setValue("nckd_hisstartdate", historyDyn.getDate("startdate"), row);
                model.setValue("nckd_hisenddate", historyDyn.getDate("enddate"), row);
                model.setValue("nckd_hisapplytime", historyDyn.getBigDecimal("applytime"), row);
            }
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        AbstractOperate op = (AbstractOperate)args.getSource();
        String operateKey = op.getOperateKey();
        if (StringUtils.equals(operateKey, "opaddnewpage")) {
            args.setCancel(true);
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
    }

    /**
     * 删除行后清除历史休假明细单据体
     * @param e
     */
    @Override
    public void afterDeleteRow(AfterDeleteRowEventArgs e) {
        super.afterDeleteRow(e);
        if("entryentity".equals(e.getEntryProp().getName())) {
            IDataModel model = this.getModel();
            model.deleteEntryData("nckd_hisentryentity");
        }
    }
}
