// packageE/healthy/component/healthy_age/healthy_age.js


const date = new Date();
const years = [];
const months = [];
const days = [];

for (let i = 1970; i <= date.getFullYear(); i++) {
  years.push(i);
}

for (let i = 1; i <= 12; i++) {
  if(i<10){
    months.push("0"+i);
  }else  months.push(i);
  
}

for (let i = 1; i <= 31; i++) {
  if(i<10){
    days.push("0"+i);
  }else  days.push(i);
}


const isLeapYear = (year) => {
  return (year % 400 === 0) || (year % 100 !== 0 && year % 4 === 0);
};
// 2.获得每个月的日期有多少，注意 month - [0-11]
const getMonthCount = (year, month) => {
  let arr = [
    31, null, 31, 30, 
    31, 30, 31, 31,
    30, 31, 30, 31
  ];
  let count = arr[month] || (isLeapYear(year) ? 29 : 28);
  //return Array.from(new Array(count), (item, value) => value + 1);
  return count;
};

Component({
  /**
     * 组件的属性列表
     */
  properties: {
    question:{
      type:Object,
      value:{}
    },
    answer:{
      type:Object,
      value:{}
    },
    site:{
      type:Number,
      value:0
    }
  },

  /**
     * 组件的初始数据
     */
  data: {
    years,months,days,
    selected:[5,5,5]
  },
  ready(){
    let answer = this.data.answer.answer;
    if(answer){
      console.log("zhix",answer);
      let arr = answer.split("-");
      console.log(arr);
      let selectArr = [];
      arr[0] = parseInt(arr[0]);//arr[1] = parseInt(arr[1]),arr[2] = parseInt(arr[2]);
      if(parseInt(arr[1])>=10)  arr[1] = parseInt(arr[1]);
      if(parseInt(arr[2])>=10)  arr[2] = parseInt(arr[2]);
      console.log(arr);
      selectArr[0] = years.indexOf(arr[0])>-1 ? years.indexOf(arr[0]) : 0;
      selectArr[1] = months.indexOf(arr[1])>-1 ? months.indexOf(arr[1]) : 0;
      selectArr[2] = days.indexOf(arr[2])>-1 ? days.indexOf(arr[2]) : 0;
      console.log(selectArr);
      this.setData({
        selected:selectArr
        //currentSex:answer
      });
          
    }else {
      this.triggerEvent("quesCallback",{
        question:this.data.question,
        site:this.data.site,
        answer:years[this.data.selected[0]]+"-"+months[this.data.selected[1]]+"-"+days[this.data.selected[2]]
      });
    }
  },
  /**
     * 组件的方法列表
     */
  methods: {
    change(event){
      let arr = event.detail.value;
      console.log(arr);
      console.log(months[arr[1]]);
      let month = parseInt(months[arr[1]]);
      console.log(month);
      if(arr[0] != this.data.selected[0] || arr[1] != this.data.selected[1]){
        let daysCount = getMonthCount(years[arr[0]],month-1);
        let tempDays = [];
        for (let i = 1; i <= daysCount; i++) {
          if(i<10){
            tempDays.push("0"+i);
          }else  tempDays.push(i);
        }
        this.setData({days:tempDays});
      }
      this.setData({selected:arr});

      this.triggerEvent("quesCallback",{
        question:this.data.question,
        site:this.data.site,
        answer:years[this.data.selected[0]]+"-"+months[this.data.selected[1]]+"-"+days[this.data.selected[2]]
      });


    }
  }
});
