<template>
  <t-checkbox-group
    v-model="valueData"
    :options="options"
    :disabled="!config.writable"
    style="min-height: 32px"
    @change="emitChange"
  ></t-checkbox-group>
</template>

<script setup name="CHECKBOX">
import { ref, computed, watch } from 'vue';
const props = defineProps({
  config: { type: Object },
  modelValue: { type: Array },
});
const emit = defineEmits(['update:modelValue', 'change']);

const valueData = ref([]);
const options = computed(() => {
  const config = props.config || {};
  return !config.options
    ? []
    : typeof config.options === 'string'
    ? JSON.parse(config.options)
    : config.options;
});

const emitChange = () => {
  emit('update:modelValue', valueData.value);
  emit('change', valueData.value);
};

watch(
  () => props.modelValue,
  (val, oldval) => {
    if (val === oldval) return;
    valueData.value = val || [];
  },
  {
    immediate: true,
  }
);
</script>
