<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <a-range-picker
      v-model:value="dateRange"
      @change="search"
      value-format="YYYY-MM-DD"
    />
    <a-tooltip title="实际订单总金额（来自订单表）" class="flex px-4">
      <span class="text-gray-400">实际订单总金额：</span>
      <span class="text-gray-700 font-bold">￥{{ formatNumber(bszxTotalPrice) }}</span>
    </a-tooltip>

    <a-tooltip title="排行榜统计金额（来自排行榜表）" class="flex px-4 ml-4">
      <span class="text-gray-400">排行榜统计金额：</span>
      <span class="text-gray-700 font-bold">￥{{ formatNumber(rankingTotalPrice) }}</span>
    </a-tooltip>
  </a-space>
</template>

<script lang="ts" setup>
  import useSearch from "@/utils/use-search";
  import { watch, ref, computed } from 'vue';
  import { formatNumber } from 'ele-admin-pro/es';
  import { BszxPayRankingParam } from "@/api/bszx/bszxPayRanking/model";
  import { useBszxStatisticsStore } from '@/store/modules/bszx-statistics';

  // 使用百色中学统计数据 store
  const bszxStatisticsStore = useBszxStatisticsStore();

  // 从 store 中获取总金额
  const bszxTotalPrice = computed(() => bszxStatisticsStore.bszxTotalPrice);

  const props = withDefaults(
    defineProps<{
      // 选中的角色
      selection?: [];
      // 保留这个属性以保持向后兼容，但不再使用
      totalPriceAmount?: number;
      // 排行榜统计金额
      rankingTotalPrice?: number;
    }>(),
    {
      rankingTotalPrice: 0
    }
  );

  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);

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

  // 表单数据
  const {where,resetFields} = useSearch<BszxPayRankingParam>({
    id: undefined,
    userId: undefined,
    createTimeStart: undefined,
    createTimeEnd: undefined,
    keywords: ''
  });

  /* 搜索 */
  const search = () => {
    const [d1, d2] = dateRange.value ?? [];
    emit('search', {
      ...where,
      createTimeStart: d1 ? d1 + ' 00:00:00' : '',
      createTimeEnd: d2 ? d2 + ' 23:59:59' : ''
    });
  };

  const onSearch = (text: string) => {
    where.sceneType = text
    search();
  }

  watch(
    () => props.selection,
    () => {}
  );
</script>
