<template>
  <el-radio-group v-model="state.year.cronEvery" class="el-radio-flex">
    <el-radio :label="1">{{ text.Year.every }}</el-radio>
    <el-radio :label="5">{{ text.Year.noTouch }}</el-radio>
    <el-radio :label="2">
      {{ text.Year.interval[0] }}
      <el-input-number
        :size="size"
        type="number"
        v-model="state.year.incrementIncrement"
        :min="1"
        :max="99"
      />
      {{ text.Year.interval[1] }}
      <el-input-number
        :size="size"
        type="number"
        v-model="state.year.incrementStart"
        :min="currYear"
        :max="currYear + 10"
      />
    </el-radio>
    <el-radio :label="3">
      {{ text.Year.cycle[0] }}
      <el-input-number
        :size="size"
        type="number"
        v-model="state.year.rangeStart"
        :min="currYear"
        :max="currYear + 10"
      />
      {{ text.Year.cycle[1] }}
      <el-input-number
        :size="size"
        type="number"
        v-model="state.year.rangeEnd"
        :min="currYear"
        :max="currYear + 10"
      />
    </el-radio>
    <el-radio :label="4">
      {{ text.Year.specific }}
      <el-select multiple v-model="state.year.specificSpecific" :size="size">
        <el-option
          v-for="(val, index) in 100"
          :key="index"
          :value="currYear + val"
        >
          {{ currYear + val }}
        </el-option>
      </el-select>
    </el-radio>
  </el-radio-group>
</template>
<script>
import { reactive, computed, defineComponent, watch } from 'vue'
import {
  ElSelect,
  ElOption,
  ElInputNumber,
  ElRadioGroup,
  ElRadio
} from 'element-plus'
export default defineComponent({
  name: 'year',
  components: {
    ElSelect,
    ElOption,
    ElInputNumber,
    ElRadioGroup,
    ElRadio
  },
  props: {
    text: {},
    size: {
      type: String,
      default: 'small'
    }
  },
  setup(props, { emit }) {
    const dealData = () => {
      let years = ''
      state.year.specificSpecific.map((val) => {
        years += val + ','
      })
      years = years.slice(0, -1)
      return years
    }
    const currYear = new Date().getFullYear() - 1
    const state = reactive({
      year: {
        cronEvery: 6,
        incrementStart: 2022,
        incrementIncrement: 1,
        rangeStart: 1,
        rangeEnd: 1,
        specificSpecific: []
      },
      yearsText: computed(() => {
        const cronEvery = state.year.cronEvery
        const yearsObj = {
          1: '*',
          2: state.year.incrementStart + '/' + state.year.incrementIncrement,
          3: state.year.rangeStart + '-' + state.year.rangeEnd,
          4: dealData(),
          5: ''
        }
        return yearsObj[cronEvery]
      })
    })
    watch(
      () => state.yearsText,
      (v) => {
        const result = {
          textData: v,
          type: 'yearsText'
        }
        emit('postResult', result)
      },
      { immediate: true, deep: true }
    )

    return {
      state,
      currYear
    }
  }
})
</script>
