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

    return {
      state
    }
  }
})
</script>
