<script setup lang="ts">
import { IconifyIcon } from '@vben/icons';

import { Button, Input, Select } from 'ant-design-vue';

interface OptionItem {
  label: string;
  value: string;
}

interface FilterState {
  keyword: string;
  tierId: string;
}

interface Props {
  filters: FilterState;
  isExporting: boolean;
  isStoreLoading: boolean;
  selectedStoreId: string;
  showExport: boolean;
  storeOptions: OptionItem[];
  tierOptions: OptionItem[];
}

const props = defineProps<Props>();

const emit = defineEmits<{
  (event: 'export'): void;
  (event: 'reset'): void;
  (event: 'search'): void;
  (event: 'update:keyword', value: string): void;
  (event: 'update:selectedStoreId', value: string): void;
  (event: 'update:tierId', value: string): void;
}>();

function handleStoreChange(value: unknown) {
  if (typeof value === 'number' || typeof value === 'string') {
    emit('update:selectedStoreId', String(value));
    return;
  }

  emit('update:selectedStoreId', '');
}
</script>

<template>
  <div class="mm-toolbar">
    <Select
      class="mm-store-select"
      :value="props.selectedStoreId"
      placeholder="全部门店"
      :loading="props.isStoreLoading"
      :options="props.storeOptions"
      :disabled="props.isStoreLoading || props.storeOptions.length === 0"
      @update:value="(value) => handleStoreChange(value)"
    />

    <Select
      class="mm-tier-select"
      :value="props.filters.tierId"
      :options="props.tierOptions"
      @update:value="(value) => emit('update:tierId', String(value ?? ''))"
    />

    <Input
      class="mm-search-input"
      :value="props.filters.keyword"
      placeholder="搜索姓名 / 手机号"
      allow-clear
      @update:value="(value) => emit('update:keyword', String(value ?? ''))"
      @press-enter="emit('search')"
    >
      <template #prefix>
        <IconifyIcon icon="lucide:search" class="mm-search-icon" />
      </template>
    </Input>

    <Button class="mm-query-btn" type="primary" @click="emit('search')">
      查询
    </Button>
    <Button class="mm-reset-btn" @click="emit('reset')">重置</Button>

    <div class="mm-toolbar-right">
      <Button
        v-if="props.showExport"
        class="mm-export-btn"
        :loading="props.isExporting"
        @click="emit('export')"
      >
        <template #icon>
          <IconifyIcon icon="lucide:download" />
        </template>
        导出
      </Button>
    </div>
  </div>
</template>
