<script setup lang="ts">
import { useSearchStore } from "@/store";
import TabSwitcher from "./components/tab-switcher.vue";
import SwiperList from "./components/swiper-list/swiper-list.vue";
import Score from "@/components/filtrate-tool/components/score.vue";
import PriceChoose from "@/components/filtrate-tool/components/price-choose.vue";
import SortPopup from "@/pages/search/components/sort-popup.vue";

const props = defineProps<{
  keyword?: string;
}>();

const { t } = useI18n();

const searchStore = useSearchStore();

const keyword = ref(props.keyword || "");

function handleSearch() {
  nextTick(() => {
    if (keyword.value) {
      searchStore.setHistoryList(keyword.value);
    }
    if (swiperListRef.value) {
      swiperListRef.value[activeTab.value].refresh()
    }
  });
}

const swiperListRef = ref<InstanceType<typeof SwiperList>>()
const activeTab = ref(0);
const tabs = [
  { label: t('pages.search.result.food'), value: "food" },
  { label: t('pages.search.result.recipe'), value: "recipe" },
];
function handleTabChange(index: number) {
  console.log("切换到标签:", index);
}

const scoreRef = ref<InstanceType<typeof Score>>()
const priceChooseRef = ref<InstanceType<typeof PriceChoose>>()
const sortPopupRef = ref<InstanceType<typeof SortPopup>>()
function toggleScore() {
  if (scoreRef.value) {
    scoreRef.value.onOpen()
  }
}
const scoreRange = ref(''); // 星级评分
const price = ref(''); // 价格范围
function applyScore(value: string) {
  console.log('applyScore', value)
  scoreRange.value = value;
  nextTick(()=> {
    if (swiperListRef.value) {
      swiperListRef.value[0].refresh()
    }
  })
}
function togglePrice() {
  if (priceChooseRef.value) {
    priceChooseRef.value.onOpen()
  }
}
function applyPrice(value: string) {
  console.log('applyPrice', value)
  price.value = value;
  nextTick(()=> {
    if (swiperListRef.value) {
      swiperListRef.value[0].refresh()
    }
  })
}
function toggleSort() {
  if (sortPopupRef.value) {
    sortPopupRef.value.onOpen()
  }
}

const currentSort = ref(1); // 默认排序方式
function handleApply(sort: number) {
  console.log('sort', sort)
  currentSort.value = sort;
  nextTick(()=> {
    if (swiperListRef.value) {
      swiperListRef.value[1].refresh()
    }
  })
}

function recipeReset() {
  handleApply(1)
}

onMounted(()=> {
  if (swiperListRef.value) {
    swiperListRef.value[0].refresh()
  }
})
</script>

<template>
  <view class="">
    <z-paging-swiper>
      <template #top>
        <header-search
          class="pb-42rpx"
          v-model="keyword"
          @search="handleSearch"
        />
        <!-- <tab-switcher
          v-model="activeTab"
          :tabs="tabs"
          @change="handleTabChange"
        /> -->
      </template>
      <swiper
          class="h-full"
          :current="activeTab"
          disable-touch
        >
          <swiper-item
            class="swiper-item"
            v-for="(item, index) in tabs"
            :key="index"
          >
            <swiper-list ref="swiperListRef"
                         :activeTab="activeTab"
                         :currentIndex="index"
                         :currentSort="currentSort"
                         :keyword="keyword"
                         :scoreRange="scoreRange"
                         :price="price"
                         @toggleScore="toggleScore"
                         @togglePrice="togglePrice"
                         @toggleSort="toggleSort"
                         @reset="recipeReset"
            ></swiper-list>
          </swiper-item>
        </swiper>
    </z-paging-swiper>

    <!-- 星级筛选弹窗 -->
    <score @applyScore="applyScore" ref="scoreRef" />
    <price-choose @applyPrice="applyPrice" ref="priceChooseRef" />
    <!-- 菜谱筛选弹窗 -->
    <sort-popup ref="sortPopupRef" @apply="handleApply" />
  </view>
</template>

<style scoped lang="scss">
:deep(.wd-tabs__nav-item) {
  font-size: 40rpx;
  color: #666666;
}
:deep(.wd-tabs) {
  .wd-tabs__line {
    bottom: 0 !important;
    z-index: 99 !important;
    border-radius: 0 !important;
  }
}
</style>
