<template>
  <n-switch v-bind="getSwitchProps" v-model:value="switchVlue" @update:value="changeValue" />
</template>

<script>
import { defineComponent, ref, unref, computed } from 'vue'
export default defineComponent({
  name: 'TableSwitch',
  props: {
    data: {
      type: [Object, String, Number, Boolean],
      required: true
    },
    rowKey: {
      type: String,
      default: ''
    }
  },
  emits: ['change'],
  setup(props, { emit }) {
    const switchVlue = ref()
    const { data, rowKey } = unref(props)
    switchVlue.value = rowKey ? data[rowKey] : data
    const getSwitchProps = computed(() => {
      return {
        ...unref(props)
      }
    })
    function changeValue(value) {
      const { data } = props
      const params = {
        data,
        value
      }
      emit('change', params)
    }
    return {
      switchVlue,
      getSwitchProps,
      changeValue
    }
  }
})
</script>
