// packageE/member/component/month/month.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    maxYear: 0,
    maxMonth: 0,
    currentYear: 0,
    currentSelectedYear: 0,
    currentMonth: 0,
    monthList: []
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      let date = new Date();
      this.setData({
        currentSelectedYear: date.getFullYear(),
        maxYear: date.getFullYear(),
        currentYear: date.getFullYear(),
        maxMonth: date.getMonth() + 1,
        currentMonth: date.getMonth() + 1,
      });
      this.setMonthItem();
    },
    moved() {},
    detached() {},
  },
  ready(){
    
  },
  /**
   * 组件的方法列表
   */
  methods: {
    prevYear() {
      this.setData({
        currentSelectedYear: this.data.currentSelectedYear-1
      });
      this.setMonthItem();
    },
    nextYear() {
      this.setData({
        currentSelectedYear: this.data.currentSelectedYear+1
      });
      this.setMonthItem();
    },
    selectedMonth(e) {
      let item = e.currentTarget.dataset.item;
      if (item.enabled == true) return;
      this.setData({
        currentYear:this.data.currentSelectedYear,
        currentMonth:item.num
      });
      this.triggerEvent('selectedMonth',{
        year: this.data.currentYear,
        month: this.data.currentMonth
      });
    },
    setMonthItem() {
      this.monthList = [];
      // let {
      //   maxYear,
      //   maxMonth,
      //   currentYear,
      //   currentMonth,
      //   currentSelectedYear
      // } = this;
      let maxYear = this.data.maxYear,
        maxMonth = this.data.maxMonth,
        currentYear = this.data.currentYear,
        currentMonth = this.data.currentMonth,
        currentSelectedYear = this.data.currentSelectedYear;
      console.log(maxYear, maxMonth, currentYear, currentMonth);
      let arr = [];
      for (let i = 1; i <= 12; i++) {
        let o = {};
        o.num = i;
        o.enabled = false;
        if (currentSelectedYear >= maxYear) {
          if (i > maxMonth) {
            o.enabled = true;
          }
        }
        arr.push(o);
      }
      this.setData({
        monthList:arr
      });
      console.log(this.data.monthList);
    },

  }
});