<template>
  <el-radio-group v-model="state.hour.cronEvery" class="el-radio-flex">
    <el-radio :label="1">{{ text.Hours.every }}</el-radio>
    <el-radio :label="2">
      {{ text.Hours.interval[0] }}
      <el-input-number
        type="number"
        :min="1"
        :max="60"
        :size="size"
        v-model="state.hour.incrementIncrement"
      />
      {{ text.Hours.interval[1] || '' }}
      <el-input
        type="number"
        class="input-width-number"
        min="0"
        max="59"
        :size="size"
        v-model="state.hour.incrementStart"
      />
      {{ text.Hours.interval[2] || '' }}
    </el-radio>
    <el-radio :label="3">
      {{ text.Hours.cycle[0] }}
      <el-input-number
        type="number"
        v-model="state.hour.rangeStart"
        :min="0"
        :max="60"
        :size="size"
      />
      {{ text.Hours.cycle[1] || '' }}
      <el-input-number
        type="number"
        v-model="state.hour.rangeEnd"
        :min="1"
        :max="59"
        :size="size"
      />
      {{ text.Hours.cycle[2] || '' }}
    </el-radio>
    <el-radio :label="4">
      {{ text.Hours.specific }}
      <el-select multiple v-model="state.hour.specificSpecific" :size="size">
        <el-option :value="index" v-for="(item, index) in 24" :key="index">{{
          index
        }}</el-option>
      </el-select>
    </el-radio>
  </el-radio-group>
</template>
<script>
import { reactive, computed, defineComponent, watch } from 'vue'
import {
  ElSelect,
  ElOption,
  ElInputNumber,
  ElInput,
  ElRadioGroup,
  ElRadio
} from 'element-plus'
export default defineComponent({
  name: 'hour',
  components: {
    ElSelect,

    ElOption,
    ElInputNumber,
    ElInput,
    ElRadioGroup,
    ElRadio
  },
  props: {
    text: {},
    size: {
      type: String,
      default: 'small'
    }
  },
  setup(props, { emit }) {
    const dealData = () => {
      let hours = ''
      state.hour.specificSpecific.map((val) => {
        hours += val + ','
      })
      hours = hours.slice(0, -1)
      return hours
    }
    const state = reactive({
      hour: {
        cronEvery: 1,
        incrementStart: 3,
        incrementIncrement: 5,
        rangeStart: 1,
        rangeEnd: 0,
        specificSpecific: []
      },
      hoursText: computed(() => {
        const cronEvery = state.hour.cronEvery
        const hoursObj = {
          1: '*',
          2: state.hour.incrementStart + '/' + state.hour.incrementIncrement,
          3: state.hour.rangeStart + '-' + state.hour.rangeEnd,
          4: dealData()
        }
        return hoursObj[cronEvery]
      })
    })
    watch(
      () => state.hoursText,
      (v) => {
        const result = {
          textData: v,
          type: 'hoursText'
        }
        emit('postResult', result)
      },
      { immediate: true, deep: true }
    )

    return {
      state
    }
  }
})
</script>
