<script>
export default {
  name: 'Index',
  inject: ['FormItemContext'],
  data () {
    return {
      optionsV: [],
      findIndex: 0
    }
  },
  methods: {
    btnClick () {
      // 获取 下一个下标
      this.findIndex = this.findIndex + 1
      if (this.findIndex >= this.FormItemContext.option.length) {
        this.findIndex = 0
      }
      // 修改选项
      this.optionsV = [this.FormItemContext.option[this.findIndex]]
      // 赋值表单值
      this.FormItemContext.form[this.FormItemContext.attr.model] = this.FormItemContext.option[this.findIndex].value
    },
    initOptionsV () {
      if (this.FormItemContext.option.length && this.FormItemContext.form[this.FormItemContext.attr.model]) {
        this.findIndex = this.FormItemContext.option.findIndex(item => item.value === this.FormItemContext.form[this.FormItemContext.attr.model])
        if (this.findIndex === -1) {
          this.findIndex = 0
        }
      }
      this.optionsV = [this.FormItemContext.option[this.findIndex]]
    }
  },
  mounted () {
    this.initOptionsV()
  }
}
</script>

<template>
  <a-radio-group v-model="this.FormItemContext.form[this.FormItemContext.attr.model]">
    <a-radio-button v-for="modeItem in optionsV" :key="modeItem.value" :value="modeItem.value" @click="btnClick">
      {{ modeItem.label }}
    </a-radio-button>
  </a-radio-group>
</template>

<style scoped lang="less">

</style>
