var app = getApp()
var that;
Component({
  mixins: [],
  data: {
    dateIndex: 1,
    month: app.globalData.month,//月
    startDay: app.globalData.day,
    endDay: app.globalData.day,
  },
  props: {
    onClose: (data) => console.log(data),
    onConfirm:(data) => console.log(data),
    defaultDate:{type:"1",date:app.globalData.month},
  },
  didMount() {
    if(this.props.defaultDate.type==1){
          this.setData({
              dateIndex:"1",
              month:this.props.defaultDate.date,
              isShow:this.props.isShow
          })
    }else{
          this.setData({
              dateIndex:"2",
              startDay:this.props.defaultDate.date[0],
              endDay:this.props.defaultDate.date[1],
              isShow:this.props.isShow
          })
    }
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    onConfirm(){
        var obj;
        if (this.data.startDay > this.data.endDay) {
          app.globalData.showToast("开始时间不能大于结束时间")
          return false;
        }else{
            if(this.data.dateIndex==1){
                obj={
                  type:this.data.dateIndex,
                  date:this.data.month
                }
            }else{
                obj={
                  type:this.data.dateIndex,
                  date:[this.data.startDay,this.data.endDay]
                }
            }
            this.props.onConfirm(obj);
            this.props.onClose();
        }
    },
    onClose(){
      this.props.onClose();
    },
    selectDay() {
      dd.datePicker({
        format: 'yyyy-MM-dd',
        currentDate: this.data.startDay,
        success: (res) => {
          this.setData({
            startDay: res.date,
            //  endDay: res.date
          })
        },
      });
    },
    selectDay2() {
      dd.datePicker({
        format: 'yyyy-MM-dd',
        currentDate: this.data.startDay,
        success: (res) => {
          if (res.date < this.data.startDay) {
            app.globalData.showToast("结束时间不能小于开始时间")
            this.setData({
              endDay: this.data.startDay
            })
            return false;
          }
          this.setData({
            endDay: res.date
          })
        },
      });
    },
    activeDate(e) {
      this.setData({ dateIndex: e.target.dataset.index })
    },
    selectDate() {
      dd.datePicker({
        format: 'yyyy-MM',
        currentDate: this.data.month,
        success: (res) => {
          this.setData({
            month: res.date
          })
        },
      });
    },

  },
});
