// packageH/turmaroundTime/timeMonthYear/monthYear.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    monthYearShow: {
      type: ''
    },
    selectMonth: {
      type: ''
    },
    navTaps: {
      type: ''
    },
    monthIndex: {
      type: ''
    },
    arr: {
      type: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    monthArr: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
    yearArr: [],
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      console.log(this.properties.selectMonth, '11111111111111111111');
    },
    moved() {},
    detached() {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 关闭月，年，选择
    closeMonthYearShow() {
      this.setData({
        monthYearShow: false
      });
    },
    // 月份和日历的向右选择
    monthYearRight(e) {
      if (this.data.navTaps == 3) {
        if (this.data.monthIndex == this.data.arr[9]) {
          console.log('该更新年份数组了');

          let arr = this.data.arr;
          for (let i = 0; i < arr.length; i++) {
            console.log(arr[i]);
            arr[i] = arr[i] - 1;
          }
          this.setData({
            arr: arr,
            monthIndex: Number(this.data.monthIndex) - 1,
            selectMonth: Number(this.data.monthIndex) - 1
          });
        } else {
          this.setData({
            monthIndex: Number(this.data.monthIndex) - 1,
            selectMonth: Number(this.data.monthIndex) - 1
          });
        }

      } else {
        let selectMonth = this.data.selectMonth;
        let year = selectMonth.slice(0, 4);
        let month = selectMonth.slice(5, 7);
        if (month == 12) {
          year = Number(year) + 1;
          month = 1;
        } else {
          month = Number(month) + 1;
        }
        if (month < 10) {
          month = `0${month}`;
        }
        this.setData({
          selectMonth: `${year}-${month}`,
          monthIndex: month - 1
        });
      }

    },
    // 月份和日历的向左选择
    monthYearLeft(e) {
      if (this.data.navTaps == 3) {
        if (this.data.monthIndex == new Date().getFullYear()) {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          });
          return;
        } else {
          if (this.data.monthIndex == this.data.arr[9]) {
            console.log('该更新年份数组了');
            if (this.data.monthIndex == new Date().getFullYear()) {
              wx.showToast({
                title: '没有更多了',
                icon: 'none',
                duration: 1000
              });
              return;
            }
            this.setData({
              monthIndex: Number(this.data.monthIndex) + 1,
            });
            if (this.data.monthIndex < new Date().getFullYear()) {
              let arr = this.data.arr;
              for (let i = 0; i < arr.length; i++) {
                arr[i] = arr[i] + 1;
              }
              this.setData({
                arr: arr,
                monthIndex: Number(this.data.monthIndex) + 1,
                selectMonth: Number(this.data.monthIndex) + 1
              });
            }
          } else {
            this.setData({
              monthIndex: Number(this.data.monthIndex) + 1,
              selectMonth: Number(this.data.monthIndex) + 1
            });
          }

        }
      } else {
        let selectMonth = this.data.selectMonth;
        let year = selectMonth.slice(0, 4);
        let month = selectMonth.slice(5, 7);
        if (month == 1) {
          year = year - 1;
          month = 12;
        } else {
          month = month - 1;
        }
        if (month < 10) {
          month = `0${month}`;
        }
        this.setData({
          selectMonth: `${year}-${month}`,
          monthIndex: month - 1
        });
      }

    },
    // monthYearShowConfirm() {
    //   console.log(this.data.selectMonth)
    //   this.setData({
    //     textTime_class: this.data.selectMonth
    //   })
    // },
    // 年份点击选择
    selectItemYear(e) {
      let ind = e.currentTarget.dataset.item;
      let year = this.data.arr[ind];
      this.setData({
        monthIndex: year
      });
    },
    // 月份点击选择
    selectItem(e) {
      console.log(e);
      this.setData({
        monthIndex: e.currentTarget.dataset.item
      });
      console.log(this.data.selectMonth);
      let month = (e.currentTarget.dataset.item + 1);
      let year = this.data.selectMonth.slice(0, 4);
      if (month < 10) {
        month = `0${month}`;
      }
      let dateInit = `${year}-${month}-01`;
      dateInit = Date.parse(dateInit);
      console.log(dateInit);
      this.setData({
        month: month,
        selectMonth: `${year}-${month}`
      });
      // console.log(year)
      // console.log(month)
    },
    monthYearShowConfirm() {
      if (this.data.navTaps == 3) {
        let year = this.data.monthIndex;
        let dateInit = `${year}-01-01`;
        dateInit = Date.parse(dateInit);
        this.triggerEvent('confirmBtn', {
          postTime: dateInit,
          textTime_class: this.data.monthIndex
        });
      } else {
        let month;
        if (this.data.month) {
          month = this.data.month;
        } else {
          month = this.data.selectMonth.slice(5);
        }
        let year = this.data.selectMonth.slice(0, 4);
        let dateInit = `${year}-${month}-01`;
        dateInit = Date.parse(dateInit);
        console.log(dateInit);
        this.triggerEvent('confirmBtn', {
          postTime: dateInit,
          textTime_class: this.data.selectMonth
        });
      }

    }
  }
});