<script setup lang="ts">
/**
 * 文件职责：到账查询筛选栏（门店、日期、渠道、导出）。
 */
import type { FinanceSettlementFilterState, OptionItem } from '../types';

import { IconifyIcon } from '@vben/icons';

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

import { SETTLEMENT_CHANNEL_OPTIONS } from '../composables/settlement-page/constants';

interface Props {
  canExport: boolean;
  filters: FinanceSettlementFilterState;
  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:selectedStoreId', value: string): void;
  (event: 'update:startDate', 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 handleChannelChange(value: unknown) {
  emit('update:channel', String(value ?? 'all'));
  emit('search');
}
</script>

<template>
  <div class="fst-toolbar">
    <Select
      class="fst-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="fst-date-input"
      type="date"
      :value="props.filters.startDate"
      @update:value="(value) => handleStartDateChange(value)"
    />
    <span class="fst-date-sep">~</span>
    <Input
      class="fst-date-input"
      type="date"
      :value="props.filters.endDate"
      @update:value="(value) => handleEndDateChange(value)"
    />

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

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