<template>
  <el-form size='small'>
    <el-form-item>
      <el-radio v-model='radioValue' :label="1">
        月，允许的通配符[, - * /]
      </el-radio>
    </el-form-item>

    <el-form-item>
      <el-radio v-model='radioValue' :label="2">
        周期从
        <el-input-number v-model='cycle01' :max="11" :min="1"/>
        -
        <el-input-number v-model='cycle02' :max="12" :min="cycle01 ? cycle01 + 1 : 2"/>
        月
      </el-radio>
    </el-form-item>

    <el-form-item>
      <el-radio v-model='radioValue' :label="3">
        从
        <el-input-number v-model='average01' :max="11" :min="1"/>
        月开始，每
        <el-input-number v-model='average02' :max="12 - average01 || 0" :min="1"/>
        月执行一次
      </el-radio>
    </el-form-item>

    <el-form-item>
      <el-radio v-model='radioValue' :label="4">
        指定
        <el-select 
          v-model="checkboxList" 
          placeholder="可多选" 
          clearable
          multiple 
          style="width:100%"
        >
          <el-option v-for="item in 12" :key="item" :value="item">{{ item }}</el-option>
        </el-select>
      </el-radio>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref, computed, watch } from 'vue'

// 定义组件名称
defineOptions({
  name: 'crontab-month'
})

// 定义props
const props = defineProps({
  check: {
    type: Function,
    required: true
  },
  cron: {
    type: Object,
    required: true
  }
})

// 定义emits
const emit = defineEmits(['update'])

// 数据定义
const radioValue = ref(1)
const cycle01 = ref(1)
const cycle02 = ref(2)
const average01 = ref(1)
const average02 = ref(1)
const checkboxList = ref([])

// 计算属性
// 计算两个周期值
const cycleTotal = computed(() => {
  const cycle01Val = props.check(cycle01.value, 1, 11)
  const cycle02Val = props.check(cycle02.value, cycle01Val ? cycle01Val + 1 : 2, 12)
  return cycle01Val + '-' + cycle02Val
})

// 计算平均用到的值
const averageTotal = computed(() => {
  const average01Val = props.check(average01.value, 1, 11)
  const average02Val = props.check(average02.value, 1, 12 - average01Val || 0)
  return average01Val + '/' + average02Val
})

// 计算勾选的checkbox值合集
const checkboxString = computed(() => {
  let str = checkboxList.value.join()
  return str == '' ? '*' : str
})

// 方法定义
// 单选按钮值变化时
const radioChange = () => {
  switch (radioValue.value) {
    case 1:
      emit('update', 'month', '*')
      break
    case 2:
      emit('update', 'month', cycleTotal.value)
      break
    case 3:
      emit('update', 'month', averageTotal.value)
      break
    case 4:
      emit('update', 'month', checkboxString.value)
      break
  }
}

// 周期两个值变化时
const cycleChange = () => {
  if (radioValue.value == '2') {
    emit('update', 'month', cycleTotal.value)
  }
}

// 平均两个值变化时
const averageChange = () => {
  if (radioValue.value == '3') {
    emit('update', 'month', averageTotal.value)
  }
}

// checkbox值变化时
const checkboxChange = () => {
  if (radioValue.value == '4') {
    emit('update', 'month', checkboxString.value)
  }
}

// Watchers
watch(radioValue, radioChange)
watch(cycleTotal, cycleChange)
watch(averageTotal, averageChange)
watch(checkboxString, checkboxChange)
</script>