<template>
  <div class="pl-3 pr-3">
    <a-radio-group :value="modelValue" @change="handleRadioChange">
      <a-radio v-for="item in dictOptions" :key="item.value" :value="item.value">{{ item.text }}</a-radio>
    </a-radio-group>
  </div>
</template>

<script>
import { ajaxGetDictItems } from '@/api/services/base'

export default {
  name: 'HDictRadio',
  props: {
    value: {
      type: String,
      required: false,
      default: ''
    },
    dictCode: {
      type: String,
      required: false,
      default: ''
    }
  },
  data() {
    return {
      modelValue: this.value,
      dictOptions: []
    }
  },
  watch: {
    value: {
      handler(val) {
        this.modelValue = val
      }
    },
    dictCode: {
      handler() {
        this.initDictData()
      },
      immediate: true
    }
  },
  methods: {
    initDictData() {
      if (this.options && this.options.length > 0) {
        this.dictOptions = [...this.options]
      } else {
        //根据字典Code, 初始化字典数组
        ajaxGetDictItems(this.dictCode, null).then(res => {
          this.dictOptions = res.value
        })
      }
    },
    handleRadioChange(e) {
      this.$emit('input', e.target.value)
      this.$emit('change', e.target.value)
    }
  }
}
</script>
