<template>
  <el-radio-group v-model="state.month.cronEvery" class="el-radio-flex">
    <el-radio :label="1">{{ text.Month.every }}</el-radio>
    <el-radio :label="5">{{ text.Month.noTouch }}</el-radio>
    <el-radio :label="2">
      {{ text.Month.interval[0] }}
      <el-input-number
        :size="size"
        type="number"
        v-model="state.month.incrementIncrement"
        :min="0"
        :max="12"
      />
      {{ text.Month.interval[1] }}
      <el-input-number
        :size="size"
        type="number"
        v-model="state.month.incrementStart"
        :min="0"
        :max="12"
      />
    </el-radio>
    <el-radio :label="3">
      {{ text.Month.cycle[0] }}
      <el-input-number
        :size="size"
        type="number"
        v-model="state.month.rangeStart"
        :min="1"
        :max="12"
      />
      {{ text.Month.cycle[1] }}
      <el-input-number
        :size="size"
        type="number"
        v-model="state.month.rangeEnd"
        :min="1"
        :max="12"
      />
    </el-radio>
    <el-radio :label="4">
      {{ text.Month.specific }}
      <el-select multiple v-model="state.month.specificSpecific" :size="size">
        <el-option v-for="(val, index) in 12" :key="index" :value="val">{{
          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: 'month',
  components: {
    ElSelect,
    ElOption,
    ElInputNumber,
    ElRadioGroup,
    ElRadio
  },
  props: {
    text: {},
    size: {
      type: String,
      default: 'small'
    }
  },
  setup(props, { emit }) {
    const dealData = () => {
      let months = ''
      state.month.specificSpecific.map((val) => {
        months += val + ','
      })
      months = months.slice(0, -1)
      return months
    }
    const state = reactive({
      month: {
        cronEvery: 1,
        incrementStart: 3,
        incrementIncrement: 5,
        rangeStart: 1,
        rangeEnd: 1,
        specificSpecific: []
      },
      monthsText: computed(() => {
        const cronEvery = state.month.cronEvery
        const monthsObj = {
          1: '*',
          2: state.month.incrementStart + '/' + state.month.incrementIncrement,
          3: state.month.rangeStart + '-' + state.month.rangeEnd,
          4: dealData(),
          5: '?'
        }
        return monthsObj[cronEvery]
      })
    })
    watch(
      () => state.monthsText,
      (v) => {
        const result = {
          textData: v,
          type: 'monthsText'
        }
        emit('postResult', result)
      },
      { immediate: true, deep: true }
    )

    return {
      state
    }
  }
})
</script>
