<script setup lang="ts">
import { ref, watch, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getHotProductsApi, getNewProductsApi } from '@/api/product'
import type { ProductListItem } from '@/types'
import { Search, Sunny, Star } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()

const products = ref<ProductListItem[]>([])
const loading = ref(false)
const mode = ref<'hot' | 'new' | 'search'>(
  route.query.hot ? 'hot' : route.query.new ? 'new' : 'search'
)
const keyword = ref<string>((route.query.q as string) || '')

async function loadHot() {
  loading.value = true
  try { products.value = (await getHotProductsApi(50)) || [] } finally { loading.value = false }
}
async function loadNew() {
  loading.value = true
  try { products.value = (await getNewProductsApi(50)) || [] } finally { loading.value = false }
}

function search() {
  if (keyword.value.trim()) {
    ElMessage.warning('后端暂未提供关键词搜索，请浏览热门/新品')
    return
  }
  mode.value = 'hot'
  loadHot()
}

watch(
  () => route.query,
  (q) => {
    if (q.hot) { mode.value = 'hot'; loadHot() }
    else if (q.new) { mode.value = 'new'; loadNew() }
  }
)

onMounted(() => {
  if (mode.value === 'hot') loadHot()
  else if (mode.value === 'new') loadNew()
})
</script>

<template>
  <div class="page container">
    <!-- Search Bar -->
    <div class="search-hero">
      <div class="search-box">
        <el-input
          v-model="keyword"
          placeholder="搜索美味零食..."
          size="large"
          class="search-input"
          @keyup.enter="search"
        >
          <template #append>
            <el-button class="search-btn" @click="search">
              <el-icon><Search /></el-icon>
            </el-button>
          </template>
        </el-input>
      </div>
      <div class="search-tags">
        <span class="tag-label">热门搜索：</span>
        <span class="tag-item" @click="keyword = '坚果'; search()">坚果</span>
        <span class="tag-item" @click="keyword = '巧克力'; search()">巧克力</span>
        <span class="tag-item" @click="keyword = '肉干'; search()">肉干</span>
        <span class="tag-item" @click="keyword = '饼干'; search()">饼干</span>
      </div>
    </div>

    <!-- Results -->
    <div v-loading="loading" class="results-section">
      <div class="results-header">
        <div class="header-left">
          <div class="section-icon" :class="mode">
            <el-icon v-if="mode === 'hot'"><Sunny /></el-icon>
            <el-icon v-else><Star /></el-icon>
          </div>
          <h2>
            <span v-if="mode === 'hot'">热门推荐</span>
            <span v-else-if="mode === 'new'">新品上市</span>
            <span v-else>搜索结果</span>
          </h2>
          <span class="count">共 {{ products.length }} 件商品</span>
        </div>
      </div>

      <div v-if="products.length" class="product-grid">
        <div
          v-for="p in products"
          :key="p.id"
          class="product-card hover-card"
          @click="router.push({ name: 'product-detail', params: { id: p.id } })"
        >
          <div class="card-img">
            <img :src="p.mainImage || 'https://placehold.co/300x300?text=SNACK'" />
            <span v-if="p.isHot" class="tag hot">热销</span>
            <span v-if="p.isNew" class="tag new">新品</span>
          </div>
          <div class="card-body">
            <h4>{{ p.name }}</h4>
            <p class="desc">月销 {{ p.sales }}</p>
            <div class="price-row">
              <span class="price-lg">¥{{ p.minPrice }}</span>
            </div>
          </div>
        </div>
      </div>

      <el-empty v-else description="暂无商品">
        <el-button type="primary" @click="router.push('/')">去首页</el-button>
      </el-empty>
    </div>
  </div>
</template>

<style scoped>
.page {
  padding: 24px 0 60px;
}

/* Search Hero */
.search-hero {
  background: linear-gradient(135deg, #FFF0EB 0%, #FFF7ED 100%);
  border-radius: var(--radius-xl);
  padding: 40px;
  margin-bottom: 32px;
  border: 1px solid rgba(255, 107, 53, 0.1);
}
.search-box {
  max-width: 600px;
  margin: 0 auto 16px;
}
.search-input :deep(.el-input__wrapper) {
  border-radius: var(--radius-full) 0 0 var(--radius-full);
  box-shadow: 0 0 0 1px var(--color-border) inset;
  padding-left: 20px;
}
.search-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px var(--color-primary) inset, var(--shadow-glow) !important;
}
.search-input :deep(.el-input-group__append) {
  border-radius: 0 var(--radius-full) var(--radius-full) 0;
  background: var(--color-primary-gradient);
  border-color: var(--color-primary);
  padding: 0 24px;
}
.search-btn {
  color: #fff !important;
  background: transparent !important;
  border: none !important;
  font-size: 18px;
}
.search-tags {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  font-size: 13px;
}
.tag-label {
  color: var(--color-text-muted);
}
.tag-item {
  padding: 4px 14px;
  border-radius: var(--radius-full);
  background: #fff;
  color: var(--color-text-secondary);
  cursor: pointer;
  transition: all var(--transition-fast);
  border: 1px solid var(--color-border-light);
}
.tag-item:hover {
  border-color: var(--color-primary);
  color: var(--color-primary);
  background: var(--color-primary-light);
}

/* Results Section */
.results-section {
  background: #fff;
  border-radius: var(--radius-xl);
  padding: 28px;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--color-border-light);
  min-height: 400px;
}
.results-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}
.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}
.section-icon {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
}
.section-icon.hot {
  background: linear-gradient(135deg, #FF6B35, #EF4444);
}
.section-icon.new {
  background: linear-gradient(135deg, #06D6A0, #3B82F6);
}
.results-header h2 {
  font-size: 20px;
  font-weight: 700;
}
.count {
  font-size: 13px;
  color: var(--color-text-muted);
  margin-left: 8px;
}

/* Product Grid */
.product-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 20px;
}
.product-card {
  background: #fff;
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--color-border-light);
  transition: all var(--transition-base);
}
.card-img {
  position: relative;
  aspect-ratio: 1;
  overflow: hidden;
}
.card-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform var(--transition-slow);
}
.product-card:hover .card-img img {
  transform: scale(1.08);
}
.tag {
  position: absolute;
  top: 10px;
  left: 10px;
  padding: 4px 12px;
  border-radius: var(--radius-full);
  font-size: 12px;
  color: #fff;
  font-weight: 700;
  box-shadow: var(--shadow-sm);
}
.tag.hot {
  background: linear-gradient(135deg, #FF6B35, #EF4444);
}
.tag.new {
  background: linear-gradient(135deg, #06D6A0, #3B82F6);
}
.card-body {
  padding: 14px;
}
.card-body h4 {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--color-text);
}
.desc {
  font-size: 13px;
  color: var(--color-text-muted);
  margin-bottom: 10px;
}
.price-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Responsive */
@media (max-width: 1280px) {
  .product-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}
@media (max-width: 1024px) {
  .product-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}
@media (max-width: 768px) {
  .search-hero {
    padding: 24px 20px;
  }
  .product-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }
  .results-section {
    padding: 16px;
  }
}
</style>
