<script setup>
import { ref, computed } from 'vue'
import VRender from '../VRender/index.vue'
import useStyle from '../hooks/useStyle'

defineOptions({
  name: 'VRadioGroup'
})

const root = ref(null)

const props = defineProps({
  field: {
    type: Object,
    required: true
  }
})

const model = defineModel({ type: [String, Number] })

const { style } = useStyle(props)

const fieldInfo = computed(() => {
  let { options, ...rest } = props.field || {}
  return rest
})

defineExpose({
  root
})
</script>

<template>
  <el-radio-group ref="root" v-model="model" v-bind="fieldInfo" :style="style">
    <template v-if="field.options && field.options.length">
      <template v-for="(item, index) in field.options" :key="index">
        <el-radio
          v-if="!field.button"
          :value="item[field.valueField || 'value']"
          :size="item.size || field.size || 'default'"
          :disabled="item.disabled || field.disabled || false"
          :border="item.border || field.border || false"
        >
          <VRender :render="item[field.labelField || 'label']"></VRender>
        </el-radio>
        <el-radio-button
          v-else
          :value="item[field.valueField || 'value']"
          :size="item.size || field.size || 'default'"
          :disabled="item.disabled || field.disabled || false"
        >
          <VRender :render="item[field.labelField || 'label']"></VRender>
        </el-radio-button>
      </template>
    </template>
  </el-radio-group>
</template>

<style scoped>
</style>