// pages/clock/clock-tongji/calendar/calendar.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      value: []
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    weeks: ["一", "二", "三", "四", "五", "六", "日"],
    selectedIndex: '',
    selectedDay: {},
    // 日期list 
    calendarDays: [],
    defaultMonth: '',
    defaultDate: '',
    blue: ''
  },
  ready() {

  },
  /**
   * 组件的方法列表
   */
  methods: {
    init(e) {
      this.setData({
        defaultMonth: e.getMonth() + 1,
        defaultDate: e.getDate(),
      })
      this.getMonthDaysCurrent(e)
    },
    // 所选时间对应月份日期
    getMonthDaysCurrent(e) {
      let year = e.getFullYear()
      let month = e.getMonth() + 1
      let date = e.getDate()
      let day = e.getDay() // 周几
      let days = new Date(year, month, 0).getDate() //当月天数(即下个月0号=当月最后一天)

      let firstDayDate = new Date(year, month - 1, 1) // 当月1号
      let firstDay = firstDayDate.getDay() //当月1号对应的星期

      let lastDate = new Date(year, month - 1, days) //当月最后一天日期
      let lastDay = lastDate.getDay() //当月最后一天对应的星期

      let calendarDays = []
      this.setData({
        selectedIndex: '',
        blue: ''
      })
      // 上个月显示的天数及日期
      for (let i = firstDay - 2; i >= 0; i--) {
        let date = new Date(year, month - 1, -i)
        //console.log(date, date.getMonth() + 1)

        calendarDays.push({
          'year': date.getFullYear(),
          'month': date.getMonth() + 1,
          'date': date.getDate(),
          'day': date.getDay(),
          'current': false,
        })
      }

      // 当月显示的日期
      for (let i = 1; i <= days; i++) {
        let index = calendarDays.push({
          'year': year,
          'month': month,
          'date': i,
          'day': new Date(year, month - 1, i).getDay(),
          'current': true,
        })
        if (i == date) {
          this.setData({
            selectedDay: calendarDays[index - 1]
          })
        }
        if (month === this.data.defaultMonth && i === this.data.defaultDate) {
          this.setData({
            selectedIndex: index - 1,
            blue: index - 1,
          })
        }
      }

      // 下个月显示的天数及日期
      for (let i = 1; i <= 7 - lastDay; i++) {
        let date = new Date(year, month, i)
        calendarDays.push({
          'year': date.getFullYear(),
          'month': date.getMonth() + 1,
          'date': date.getDate(),
          'day': date.getDay(),
          'current': false,
        })
      }
      this.setData({
        calendarDays,
        month
      })
    },

    // 手动选中日期
    clickDate(e) {
      let index = e.currentTarget.dataset.index
      let list = this.data.calendarDays
      if (list[index].current) {
        this.triggerEvent('day', list[index])
        this.setData({
          calendarDays: list,
          selectedIndex: index,
          selectedDay: list[index],
          selectedIndex: index,
          blue: index,
          defaultMonth: list[index].month,
          defaultDate: list[index].date
        })
      }
    },
    next() {
      this.getMonthDaysCurrent(new Date(this.data.selectedDay.year, this.data.selectedDay.month, 1))
    },
    last() {
      this.getMonthDaysCurrent(new Date(this.data.selectedDay.year, this.data.selectedDay.month - 2, 1))
    }
  }
})
