<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <a-select
      v-model:value="specId"
      placeholder="筛选规格组"
      allow-clear
      style="width: 160px"
      :loading="specLoading"
      @change="search"
    >
      <a-select-option
        v-for="spec in specList"
        :key="spec.specId"
        :value="spec.specId"
      >
        {{ spec.specName }}
      </a-select-option>
    </a-select>
    <a-input-search
      v-model:value="keywords"
      placeholder="搜索规格值"
      allow-clear
      style="width: 180px"
      @search="search"
    />
    <a-button type="primary" class="ele-btn-icon" @click="add">
      <template #icon><PlusOutlined /></template>
      <span>添加规格值</span>
    </a-button>
    <a-button
      danger
      class="ele-btn-icon"
      :disabled="!selection?.length"
      @click="remove"
    >
      <template #icon><DeleteOutlined /></template>
      <span>批量删除</span>
    </a-button>
  </a-space>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { PlusOutlined, DeleteOutlined } from '@ant-design/icons-vue';
  import type { ShopSpecValueParam } from '@/api/shop/shopSpecValue/model';
  import { listShopSpec } from '@/api/shop/shopSpec';
  import type { ShopSpec } from '@/api/shop/shopSpec/model';

  const props = withDefaults(
    defineProps<{
      selection?: any[];
    }>(),
    {}
  );

  const emit = defineEmits<{
    (e: 'search', where?: ShopSpecValueParam): void;
    (e: 'add'): void;
    (e: 'remove'): void;
  }>();

  const keywords = ref('');
  const specId = ref<number | undefined>(undefined);
  const specList = ref<ShopSpec[]>([]);
  const specLoading = ref(false);

  const loadSpecList = async () => {
    specLoading.value = true;
    try {
      specList.value = await listShopSpec() || [];
    } catch (e) {
      // ignore
    } finally {
      specLoading.value = false;
    }
  };

  const search = () => {
    emit('search', {
      keywords: keywords.value || undefined,
      specId: specId.value
    } as any);
  };

  const add = () => emit('add');
  const remove = () => emit('remove');

  onMounted(() => {
    loadSpecList();
  });
</script>
