package nckd.jimin.jyyy.fi.plugin.form;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.cardentry.CardEntry;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import nckd.base.helper.CommonHelperUtils;

import java.util.Date;
import java.util.EventObject;

/**
 * 表单标识：出差申请单(nckd_er_tripreqbill_ext)
 * @author wanghaiwu_kd
 * @date 2025/05/24
 */
public class TripReqBillCusFormPlugin extends AbstractBillPlugIn {
    private static final Log log = LogFactory.getLog(TripReqBillCusFormPlugin.class);

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

        String key = e.getProperty().getName();
        ChangeData[] changeData = e.getChangeSet();
        Object newValue = changeData[0].getNewValue();
        Object oldValue = changeData[0].getOldValue();
        int rowIndex = changeData[0].getRowIndex();

        switch (key) {
            case "startdate":
                if(rowIndex == 0){
                    this.getModel().setValue("nckd_amends", 0);

                    if(newValue != null) {
                        //计算字段【候补判断】
                        Date startDate = CommonHelperUtils.getDateStringFormat((Date) newValue, "yyyy-MM-dd");
                        Date curDate = CommonHelperUtils.getDateStringFormat(new Date(), "yyyy-MM-dd");

                        int diffDays = CommonHelperUtils.diffDateD(startDate, curDate);

                        if (diffDays > 0) {
                            this.getModel().setValue("nckd_amends", diffDays);
                        }
                    }

                    this.getView().updateView("nckd_amends");
                }
                break;
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);

        boolean isChange = (boolean)this.getModel().getValue("ischange");
        //如果是变更行程，隐藏分录删除、新增按钮
        if(isChange) {
            this.getView().setVisible(Boolean.FALSE, new String[]{"cardentryfixrowap1"});
            CardEntry cardEntry = (CardEntry) this.getControl("tripentry");
            int count = this.getModel().getEntryRowCount("tripentry");

            for (int index = 0; index < count; ++index) {
                cardEntry.setChildVisible(Boolean.FALSE, index, new String[]{"deltripentry"});
            }
        }
    }
}
