<template>
  <div>
    <h1>月份选择器</h1>
    <h2>绑定dayjs对象</h2>
    <div class="demo-component">
      <vue-monthly-picker
        v-model="selectedMonth"
        :input-class="{'input': isDisplayInput}"
        :disabled="isDisable"
        :month-labels="locale"
        :clear-option="clearOption"
        :min="min"
        :max="max"
        :alignment="alignment"
        :selected-background-color="selectedBackgroundColor"
        @selected="handleSelect"
      />
    </div>

    <div>选择的月份: {{ selectedMonthStr }}</div>

    
    <h2>绑定String</h2>
    <div class="demo-component">
      <vue-monthly-picker
        v-model="selectedMonth1"
        :input-class="{'input': isDisplayInput}"
        :disabled="isDisable"
        :month-labels="locale"
        :clear-option="clearOption"
        :min="min"
        :max="max"
        :alignment="alignment"
        :selected-background-color="selectedBackgroundColor"
        @selected="handleSelect"
      />
    </div>

    <div>选择的月份: {{ selectedMonth1 }}</div>

    <div>
      <div class="form-group">
        <label>只读</label>
        <div class="input-group">
          <Switches v-model="isDisable" />
          {{ isDisable ? 'Disabled': 'Enable' }}
        </div>
      </div>
      <div class="form-group">
        <label>限制范围</label>
        <div class="input-group">
          <Switches v-model="isLimitRange" />
          {{ rangeDisplay }}
        </div>
      </div>
      <div class="form-group">
        <label>显示</label>
        <div class="input-group">
          <Switches
            v-model="isDisplayInput"
            true-value="Input"
            false-value="Label"
          />
          {{ isDisplayInput? 'Input': 'Label' }}
        </div>
      </div>
      <div class="form-group">
        <label>清空</label>
        <div class="input-group">
          <Switches v-model="clearOption" />
          {{ clearOption ? 'Enable': 'Disabled' }}
        </div>
      </div>
      <div class="form-group">
        <label>国际化</label>
        <select
          v-model="locale"
          placeholder="Select a language"
          class="form-control"
        >
          <option
            v-for="option in options"
            :key="option.id"
            :value="option.monthLabels"
          >
            {{ option.title }}
          </option>
        </select>
      </div>
      <div class="form-group">
        <label>对齐</label>
        <select
          v-model="alignment"
          placeholder="Select an alignment"
          class="form-control"
        >
          <option
            v-for="alignment1 in alignments"
            :key="alignment1"
            :value="alignment1"
          >
            {{ alignment1 }}
          </option>
        </select>
      </div>
      <div class="form-group">
        <label>背景色</label>
        <select
          v-model="selectedBackgroundColor"
          placeholder="Select an color"
          class="form-control"
        >
          <option
            v-for="color in colorExamples"
            :key="color"
            :value="color"
          >
            {{ color }}
          </option>
        </select>
      </div>
    </div>
  </div>
</template>

<script>
import dayjs from 'dayjs';
import VueMonthlyPicker from '@/vue-monthly-picker/index.js';
import Switches from '@/switches/index.js';

export default {
  name: 'App',
  components: {
    VueMonthlyPicker, Switches,
  },
  data() {
    return {
      selectedMonth: dayjs(),
      selectedMonth1: '2024-05',
      isDisable: false,
      isDisplayInput: true,
      locale: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
      alignment: 'left',
      selectedBackgroundColor: 'blue',
      options: [
        {
          id: 1,
          title: 'English',
          monthLabels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        },
        {
          id: 2,
          title: '中文',
          monthLabels: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
        },
        {
          id: 3,
          title: 'Korean',
          monthLabels: ['1월', '2월', '3월', '4월', '5월', '6월', '7월', '8월', '9월', '10월', '11월', '12월'],
        },
      ],
      alignments: ['left', 'center', 'right'],
      colorExamples: ['blue', 'red', 'black'],
      min: null,
      max: null,
      isLimitRange: false,
      clearOption: true,
    };
  },
  computed: {
    rangeDisplay() {
      if (!this.min || !this.max) {
        return 'Disabled';
      }
      return this.min.format('YYYY/MM') + '-' + this.max.format('YYYY/MM');
    },

    selectedMonthStr(){
      if(typeof this.selectedMonth === 'string'){
        return this.selectedMonth;
      }else{
        return this.selectedMonth.format('YYYY-MM');
      }
    },
  },
  watch: {
    isLimitRange(newValue) {
      if (newValue) {
        this.setSelectRange(dayjs().subtract(6, 'months'), dayjs().add(6, 'months'));
      } else {
        this.setSelectRange(null, null);
      }
    },
    
  },

  mounted(){
    this.testDayJs();
  },
  methods: {
    handleSelect(value) {
      console.log('Select', value);
    },
    setSelectRange(min, max) {
      this.min = min;
      this.max = max;
    },
    // 测试DayJs
    testDayJs(){
      // exist bug
      console.log(dayjs('00:00:00', 'HH:mm:ss'));


      console.log(dayjs('2024-05-09').format('YYYY-MM-DD'));
      console.log(dayjs('2024-05-09 10:45').format('YYYY-MM-DD'));
      console.log(dayjs('2024-05-09 10:45:01').format('YYYY-MM-DD'));
      console.log(dayjs().format('YYYYMMDD_hhmmss'));

      const nowDate = new Date();
      const startDate = dayjs(nowDate).subtract(1, 'M').format('YYYY-MM-DD HH:mm:ss');
      const endDate = dayjs(nowDate).add(1, 'd').format('YYYY-MM-DD HH:mm:ss');
      console.log(startDate, endDate);
      
      const startDate1 = '2024-01-02';
      const endDate1 = '2024-01-10';
      let currentYearMonth = dayjs(startDate1).add(1, 'M').format('YYYY-MM');
      console.log(endDate1, 'isBefore', currentYearMonth, dayjs(endDate1).isBefore(currentYearMonth));

      console.log(dayjs('2024-01-25').format('YYYY-MM-DD HH:mm'));
      console.log(dayjs('2024-01-25').format('YYYY-MM-DD'));
      console.log(dayjs('2024-01-25').format('YYYY-MM'));
      console.log(dayjs().format('_YYYYMMDD_hhmmss'));
    },
  },
};
</script>

<style scoped>
#app {
    font-family: "Avenir", Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
}

.demo-component {
    width: 250px;
    margin: auto;
}
.control-group {
    margin-top: 100px;
}
.control {
    text-align: center;
}
.option-list {
    margin-top: 20px;
}
</style>
