<template>
  <div>
    <a-input-group compact>
      <a-input
        disabled
        style="width: calc(100% - 32px)"
        v-model:value="content"
        :placeholder="placeholder"
      />
      <a-button @click="openEdit">
        <template #icon><BulbOutlined class="ele-text-warning" /></template>
      </a-button>
    </a-input-group>
    <!-- 选择弹窗 -->
    <SelectData
      v-model:visible="showEdit"
      :data="current"
      :title="placeholder"
      :customer-type="customerType"
      @done="onChange"
    />
  </div>
</template>

<script lang="ts" setup>
  import { BulbOutlined } from '@ant-design/icons-vue';
  import { ref, watch } from 'vue';
  import SelectData from './components/select-data.vue';
  import { Spec } from '@/api/shop/spec/model';

  const props = withDefaults(
    defineProps<{
      value?: any;
      customerType?: string;
      placeholder?: string;
    }>(),
    {
      placeholder: '请选择商户'
    }
  );

  const emit = defineEmits<{
    (e: 'done', Spec): void;
    (e: 'clear'): void;
  }>();

  // 是否显示编辑弹窗
  const showEdit = ref(false);
  // 当前编辑数据
  const current = ref<Spec | null>(null);
  const content = ref<any>(props.value);

  // 监听外部传入的值变化，确保编辑回显时能正确显示
  watch(() => props.value, (newVal) => {
    content.value = newVal;
  });

  /* 打开编辑弹窗 */
  const openEdit = (row?: Spec) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  const onChange = (row: Spec) => {
    content.value = row.specName;
    emit('done', row);
  };
</script>
