<template>
  <vxe-switch
    v-model="dataValue"
    open-value="Y"
    close-value="N"
    :disabled="props.readonly || props.disabled"
    @change="dataValueChange"
    @click="dataValueClick"
    @focus="dataValueFocus"
    @blur="dataValueBlur"
  ></vxe-switch>
</template>

<script setup name="FormSwitch">
import { ref, onMounted, watch } from 'vue';

const props = defineProps({
  value: {
    type: String,
    default: 'N'
  },
  //是否禁用
  disabled: {
    type: Boolean,
    default: false
  },
  // 当前行数据
  record: {
    type: Object,
    default: {}
  },
  // 是否只读
  readonly: {
    type: Boolean,
    default: false
  }
});

const emits = defineEmits(['update:value', 'onBlur', 'onClick', 'onChange', 'onFocus']);

onMounted(() => {
  dataValue.value = props.value;
});

// 选中的值
const dataValue = ref('');

watch(
  () => props.value,
  val => {
    dataValue.value = val;
  },
  { deep: true }
);

// 更改值
const dataValueChange = () => {
  emits('update:value', dataValue.value);
  emits('onChange', props.record);
};

// 点击
const dataValueClick = () => {
  emits('onClick', props.record);
};
// 失去焦点
const dataValueBlur = () => {
  emits('onBlur', props.record);
};
// 获取焦点
const dataValueFocus = () => {
  emits('onFocus', props.record);
};
</script>

<style></style>
