<template>
  <el-radio-group v-model="state.week.cronEvery" class="el-radio-flex">
    <el-radio :label="1">{{ text.Weeks.every }}</el-radio>
    <el-radio :label="6">{{ text.Weeks.noTouch }}</el-radio>
    <el-radio :label="2">
      {{ text.Weeks.cycleWeek[0] }}
      <el-input-number
        :size="size"
        type="number"
        v-model="state.week.cycleStart"
        :min="1"
        :max="7"
      />
      {{ text.Weeks.cycleWeek[1] }}
      <el-input-number
        :size="size"
        type="number"
        v-model="state.week.cycleEnd"
        :min="1"
        :max="7"
      />
    </el-radio>
    <el-radio :label="3">
      {{ text.Weeks.intervalWeek[0] }}
      <el-input-number
        :size="size"
        type="number"
        v-model="state.week.intervalStart"
        :min="1"
        :max="7"
      />
      {{ text.Weeks.intervalWeek[1] }}
      <el-input-number
        :size="size"
        type="number"
        v-model="state.week.intervalEnd"
        :min="1"
        :max="7"
      />
    </el-radio>
    <el-radio :label="4">
      {{ text.Weeks.lastWeek }}
      <el-input-number
        :size="size"
        type="number"
        v-model="state.week.lastWeek"
        :min="1"
        :max="7"
      />
    </el-radio>
    <el-radio :label="5">
      {{ text.Weeks.specificWeek }}
      <el-select multiple v-model="state.week.specificSpecific" :size="size">
        <el-option v-for="(val, index) in 7" :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: 'week',
  components: {
    ElSelect,
    ElOption,
    ElInputNumber,
    ElRadioGroup,
    ElRadio
  },
  props: {
    text: {},
    size: {
      type: String,
      default: 'small'
    }
  },
  setup(props, { emit }) {
    const dealData = () => {
      let weeks = ''
      state.week.specificSpecific.map((val) => {
        weeks += val + ','
      })
      weeks = weeks.slice(0, -1)
      return weeks
    }
    const state = reactive({
      week: {
        cronEvery: 6,
        cycleStart: 1,
        cycleEnd: 2,
        loopStart: 1,
        loopEnd: 1,
        intervalStart: 1,
        intervalEnd: 1,
        lastWeek: 0,
        specificSpecific: []
      },
      weeksText: computed(() => {
        const cronEvery = state.week.cronEvery
        const weeksObj = {
          1: '*',
          2: state.week.cycleStart + '-' + state.week.cycleEnd,
          3: state.week.intervalStart + '#' + state.week.intervalEnd,
          4: state.week.lastWeek + 'L',
          5: dealData(),
          6: '?'
        }
        return weeksObj[cronEvery]
      })
    })
    watch(
      () => state.weeksText,
      (v) => {
        const result = {
          textData: v,
          type: 'weeksText'
        }
        emit('postResult', result)
      },
      { immediate: true, deep: true }
    )

    return {
      state
    }
  }
})
</script>
