var util = require('../../utils/util.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: { 
    dateData: {
      type: Array
    },
    day: {
      type: String
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    week: ['日', '一', '二', '三', '四', '五', '六'],
    priceData: [],
    DaysInMonth: [],
    daysData: [],
    isSelect: -1,
    mydate: -1,
    today_after7: util.AddDays(new Date().Format("yyyy-MM-dd"), 7),
    dateIndex: -1,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    selectDate(e) {
      let item = e.currentTarget.dataset.item;
      let index = e.currentTarget.dataset.index;
      if (this.data.daysData[index].price == "0.00" || this.data.daysData[index].price == "") {
        return;
      } else {
        this.setData({
          dateIndex: index
        })
        this.triggerEvent('childrenEvent', this.data.daysData[index]);
      }
    },
    getYearMonthDay(list) {
      this.setData({
        priceData: list[0]
      })
      let day = this.data.day;
      let currentYear = day.substring(0, 4); //当前年份
      let currentMonth = day.substring(5, 7); //当前月份	
      let date = new Date();
      let strDate = date.getDate();
      let strMonth = (date.getMonth() + 1).toString();
      // alert(typeof strMonth)
      //判断是否是闰年
      if (this.isleapYears(currentYear)) {
        this.setData({
          DaysInMonth: [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        }) 
      } else {
        this.setData({
          DaysInMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        })
      }
      let monthDay = this.data.DaysInMonth[Number(currentMonth) - 1];//当前月的天数			
      let daysData = [];
      //给数据源赋值
      for (let i = 0; i < Number(monthDay); i++) {
        var priceDict = { 'day': String(i + 1), 'price': 0, 'dis': false, date_str: '', priceId: "" };
        daysData.push(priceDict);
      }
      console.log(monthDay)
      this.setData({
        daysData: daysData
      }) 
      var currentDay = `${currentYear}-${currentMonth}-01`;
      var dateObject = new Date(currentDay);
      var firstDay = dateObject.getDay();//得到每个月1号是周几
      let today = new Date().Format("yyyy-MM-dd");
      for (let i = 0; i < this.data.priceData.length; i++) {
        let price = this.data.priceData[i];
        let dateStr = new Date(price.dateStr).Format("yyyy-MM-dd");
        var dayIndex = price.dateStr.substring(price.dateStr.length - 2, price.dateStr.length);
        var dayDict = daysData[Number(dayIndex) - 1]; 
        if (price.dateStr < this.data.today_after7) {
          dayDict.price = '';
        } else {
          dayDict.price = price.price;
          dayDict.priceId = price.priceId;
        }
        dayDict.date_str = price.dateStr;
        if (dayIndex < strDate && strMonth == currentMonth) {
          dayDict.price = '';
        }
      }
      
      if (firstDay > 0) {
        var firstDayData = [];
        for (var i = 0; i < firstDay; i++) {
          var dict = { 'day': ' ', price: '', 'dis': true };
          firstDayData.push(dict);
        }
        this.setData({
          daysData: firstDayData.concat(daysData)
        }) 
      } else {
        this.setData({
          daysData: daysData
        }) 
        
      }
      
    },
    isleapYears(year) {
      if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
        return true;
      } else {
        return false;
      }
    },
  }
})