let date = new Date()
let times = []
for (let i = 0 ; i < 15; i++) {
  let stamp = date.setDate(date.getDate()+ (i == 0 ? 0 : 1))
  let year = new Date(stamp).getFullYear()
  let month = new Date(stamp).getMonth()+1 < 10 ? '0'+(new Date(stamp).getMonth()+1) : new Date(stamp).getMonth()+1
  let day = new Date(stamp).getDate() < 10 ? '0'+new Date(stamp).getDate() : new Date(stamp).getDate()
  times.push(`${year+'-'+month+'-'+day}`)
}
import {
  get,
} from '../../../../utils/http';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    projectData: {
      type: Object,
      value: {},
      observer(newVal,oldVal) {
        if(newVal) {
          this.getTime().then(res => {
            this.triggerEvent("change",this.data.hours[0])
          })
        }
      }
    }
  },

  lifetimes: {
    attached: function() {
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    hours: [],
    times,
    day: 2,
    current: 0,
    value: [0,0],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindChange: function (e) {
      const value = e.detail.value
      if(e.currentTarget.dataset.index != value[0]) {
        this.setData({ value: [e.detail.value[0],0],current:-0,hours: [] })
        this.getTime().then(res => {
          this.triggerEvent("change", this.data.hours[value[1]])
        })
      } else if(e.currentTarget.dataset.index == value[0]) {
        this.setData({
          current: value[1]
        })
        this.triggerEvent("change",this.data.hours[value[1]])
      }
    },

    /* 选择当前时间段 */
    onChangeHours(e) {
      if(this.data.hours[e.currentTarget.dataset.index].status == 0) {
        return;
      }
      this.setData({
        current: e.currentTarget.dataset.index
      })
      this.triggerEvent("change",this.data.hours[e.currentTarget.dataset.index])
    },
    
    /* 获取可预约时间 */
    // api/project/time
    getTime() {
      let { times,value } = this.data;
      return new Promise((resolve,reject) => {
        get('api/project/time',{
          store_id: this.data.projectData.store_id,
          project_id: this.data.projectData.id,
          order_day: times[value[0]]
        },(res) => {
          this.setData({
            hours: res.data
          })
          resolve(res)
        })
      })
    }
  },

})
