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

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

import {
  CHANNEL_OPTIONS,
  PAYMENT_OPTIONS,
  STATUS_OPTIONS,
} from '../composables/order-all-page/constants';

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

interface FilterState {
  channel: string;
  endDate: string;
  keyword: string;
  paymentMethod: string;
  startDate: string;
  status: string;
}

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

const props = defineProps<Props>();

const emit = defineEmits<{
  (event: 'export'): void;
  (event: 'search'): void;
  (event: 'update:channel', value: string): void;
  (event: 'update:endDate', value: string): void;
  (event: 'update:keyword', value: string): void;
  (event: 'update:paymentMethod', value: string): void;
  (event: 'update:selectedStoreId', value: string): void;
  (event: 'update:startDate', value: string): void;
  (event: 'update:status', value: string): void;
}>();

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

function handleStartDateChange(value: string | undefined) {
  emit('update:startDate', String(value ?? ''));
  emit('search');
}

function handleEndDateChange(value: string | undefined) {
  emit('update:endDate', String(value ?? ''));
  emit('search');
}

function handleStatusChange(value: unknown) {
  emit('update:status', String(value ?? 'all'));
  emit('search');
}

function handleChannelChange(value: unknown) {
  emit('update:channel', String(value ?? 'all'));
  emit('search');
}

function handlePaymentChange(value: unknown) {
  emit('update:paymentMethod', String(value ?? 'all'));
  emit('search');
}

function handleKeywordChange(value: string | undefined) {
  emit('update:keyword', String(value ?? ''));
}
</script>

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

    <Input
      class="oa-date-input"
      type="date"
      :value="props.filters.startDate"
      @update:value="(value) => handleStartDateChange(value)"
    />
    <span class="oa-date-sep">~</span>
    <Input
      class="oa-date-input"
      type="date"
      :value="props.filters.endDate"
      @update:value="(value) => handleEndDateChange(value)"
    />

    <Select
      class="oa-status-select"
      :value="props.filters.status"
      :options="STATUS_OPTIONS"
      @update:value="(value) => handleStatusChange(value)"
    />

    <Select
      class="oa-channel-select"
      :value="props.filters.channel"
      :options="CHANNEL_OPTIONS"
      @update:value="(value) => handleChannelChange(value)"
    />

    <Select
      class="oa-payment-select"
      :value="props.filters.paymentMethod"
      :options="PAYMENT_OPTIONS"
      @update:value="(value) => handlePaymentChange(value)"
    />

    <Input
      class="oa-search"
      :value="props.filters.keyword"
      placeholder="订单号/手机号"
      allow-clear
      @update:value="(value) => handleKeywordChange(value)"
      @press-enter="emit('search')"
    >
      <template #prefix>
        <IconifyIcon icon="lucide:search" class="oa-search-icon" />
      </template>
    </Input>

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