<script setup lang="ts">
import {appSearchListPost} from '@/service'
import SearchSkeleton from "@/pages/search/components/search-skeleton.vue";
const {t} = useI18n()
const props = defineProps<{
  id?: string;
}>();

const keyword = ref('')
function handleSearch() {
  nextTick(() => {
    if (!keyword.value) {
      return uni.showToast({title: t('common.prompt.please-enter-keyword-search'), icon: 'none'})
    }
    uni.navigateTo({
      url: `/pages-store/pages/store/search/result?keyword=${keyword.value}&id=${props.id}`,
    })
    keyword.value = ''
  })
}

const loading = ref(true)
onMounted(() => {
  loading.value = true
  // 查询热门搜索词列表
  getHotSearchList()
})

const hotSearchList = ref([])
function getHotSearchList() {
  appSearchListPost({}).then(res=> {
    console.log('热门搜索词列表', res)
    hotSearchList.value = res.data
  }).finally(() => {
    loading.value = false
  })
}
function handleHotSearch(item: any) {
  nextTick(() => {
    uni.navigateTo({
      url: `/pages-store/pages/store/search/result?keyword=${item.name}&id=${props.id}`,
    })
  })
}
</script>

<template>
  <view class="">
    <view
        class="animate-in fade-in animate-ease-out animate-duration-300"
        v-show="loading"
    >
      <search-skeleton />
    </view>
    <view
        class="animate-in fade-in animate-ease-in animate-duration-300"
        v-show="!loading"
    >
      <header-search focus class="" v-model="keyword" :placeholder="t('components.search.placeholder')" @search="handleSearch"/>
      <view class="pl-30rpx mt-52rpx">
        <view class="text-40rpx lh-40rpx text-#333 font-bold mb-24rpx tracking-[.04em]">
          {{ t('pages.search.hot-title') }}
        </view>
        <template v-for="item in hotSearchList">
          <view  @click="handleHotSearch(item)" class="w-full h-144rpx flex items-center">
            <image :src="item.logoUrl" class="w-64rpx h-64rpx mr-28rpx rounded-50%" mode="aspectFill"></image>
            <view class="flex-1 border-b-solid border-b-1rpx border-b-#DFDFDF h-full flex items-center text-30rpx text-primary font-500 tracking-[.06em]">
              {{  item.name }}
            </view>
          </view>
        </template>
      </view>
    </view>
  </view>
</template>

<style>
page {
  background-color: #fff;
}
</style>