// components/timePicker/index.js

const dayjs = require("../../utils/dayjs");

function generateYearArray(startYear) {
  const currentYear = new Date().getFullYear();
  const years = [];
  for (let i = startYear; i <= currentYear; i++) {
    years.push(i);
  }
  return years;
}

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    type: String,
    value: Number,
    show: Boolean,
    year: String,
    month: Number,
    day: Number,
    currentDate: Number,
  },
  /**
   * 组件的初始数据
   */
  data: {
    minDate: new Date(2024,0,1).getTime(),
    maxDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if (type === 'day') {
        return `${value}日`;
      }
      return value;
    },
    years: generateYearArray(2024)
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onInput(e) {
      
    },
    onClose() {
      this.triggerEvent("close")
    },
    onCancel() {
      this.triggerEvent("cancel")
    },
    onConfirm(e) {
      const { type } = this.data;
      let time;
      switch(type) {
        case "day":
          time =  dayjs(new Date(e.detail)).format('YYYY-MM-DD');
          break;
        case 'month':
          time =  dayjs(new Date(e.detail)).format('YYYY-MM');
          break;
        case 'year':
          time =  dayjs(new Date(e.detail.value, 0, 1)).format('YYYY');
          break;  
      }
      this.triggerEvent("confirm", { type, time })
    }
  }
})