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

import kd.bos.bill.AbstractMobBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.ConfirmCallBackListener;
import kd.bos.form.IFormView;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.control.Control;
import kd.bos.form.field.DateTimeEdit;
import kd.bos.form.field.MulBasedataEdit;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.fi.er.business.servicehelper.CommonServiceHelper;
import kd.fi.er.business.utils.ErCommonUtils;
import kd.fi.er.business.utils.SystemParamterUtil;
import kd.fi.er.business.utils.TripBillTravelerUtils;
import kd.fi.er.formplugin.budget.BudgetCommonUtil;
import kd.macc.cad.common.utils.DateUtils;
import nckd.base.helper.CommonHelperUtils;
import org.apache.commons.lang.StringUtils;

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

/**
 * 出差申请移动端添加行程
 * 表单标识：添加行程（nckd_er_tripreqsectio_ext）
 * @author wanghaiwu_kd
 *
 */
public class TripReqMobFormPlugin extends AbstractMobBillPlugIn {
    private static final Log log = LogFactory.getLog(TripReqMobFormPlugin.class);

    public void registerListener(EventObject e) {
        this.addClickListeners(new String[]{"btn_save"});
    }

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

    @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();

        //处理开始时间、结束时间选择范围问题
        DateTimeEdit startTime = (DateTimeEdit) this.getView().getControl("nckd_startdatetime");
        DateTimeEdit endTime = (DateTimeEdit) this.getView().getControl("nckd_enddatetime");

        if("nckd_startdatetime".equals(key) || "nckd_enddatetime".equals(key)){
            switch (key) {
                case "nckd_startdatetime":
                    setStandardDateFieldValue("nckd_startdatetime", "startdate");
                    break;
                case "nckd_enddatetime":
                    setStandardDateFieldValue("nckd_enddatetime", "enddate");
                    break;
            }

            startTime.setIsStartDate(true);
            startTime.setRelatedDate("nckd_enddatetime");

            endTime.setRelatedDate("nckd_startdatetime");

            if(this.getModel().getValue("nckd_startdatetime") != null) {
                endTime.setMinDate((Date) this.getModel().getValue("nckd_startdatetime"));
            } else {
                endTime.setMinDate(CommonHelperUtils.getDateStringFormat("2000-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss"));
            }

            if(this.getModel().getValue("nckd_enddatetime") != null) {
                startTime.setMaxDate((Date) this.getModel().getValue("nckd_enddatetime"));
            } else {
                startTime.setMaxDate(CommonHelperUtils.getDateStringFormat("2078-12-31 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            }
        }
    }

    /**
     * 根据二开的开始时间、结束时间设置标准的开始日期、结束日期的字段值
     */
    public void setStandardDateFieldValue(String fieldName, String standardFieldName){
        if(this.getModel().getValue(fieldName) == null){
            this.getModel().setValue(standardFieldName, null);
            return;
        }

        Date dateValue = (Date) this.getModel().getValue(fieldName);
        this.getModel().setValue(standardFieldName, dateValue);
    }

    public void click(EventObject evt) {
        Control source = (Control)evt.getSource();
        String key = source.getKey();

        IDataModel model = this.getModel();
        IDataModel parentModel = this.getView().getParentView().getModel();
        IFormView parentView = this.getView().getParentView();

        switch (key) {
            case "btn_save":
                DynamicObject dataEntity = model.getDataEntity(true);
                DynamicObjectCollection tripentry = dataEntity.getDynamicObjectCollection("tripentry");
                DynamicObjectCollection parentTripEntry = parentModel.getEntryEntity("tripentry");

                //根据第一行程开始时间判断是否后补单据
                int diffDays = 0;
                if(tripentry.size() > 0){
                    DynamicObject entry = tripentry.get(0);
                    int index = entry.getInt("index");
                    if(index > 1){
                        entry = parentTripEntry.get(0);
                    }

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

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

                parentModel.setValue("nckd_amends", diffDays);
                parentView.updateView("nckd_amends");
                this.getView().sendFormAction(parentView);

                break;
        }

    }
}
