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

    return {
      state
    }
  }
})
</script>
