<?php
$pageTitle = '全部香水 - FOLIBIZ香水商城';
include __DIR__ . '/../includes/functions.php';
include __DIR__ . '/../includes/ProductRepository.php';

$repo = new ProductRepository();

$category = isset($_GET['category']) ? $_GET['category'] : '全部';
$gender = isset($_GET['gender']) ? $_GET['gender'] : '全部';
$brand = isset($_GET['brand']) ? $_GET['brand'] : '全部';
$sort = isset($_GET['sort']) ? $_GET['sort'] : '';
$keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';

$products = $repo->filter($category, $gender, $brand, $keyword);
$products = $repo->sort($products, $sort);

$categories = $repo->getCategories();
$brands = $repo->getBrands();
$genders = $repo->getGenders();

$total = count($products);
?>

<?php include '../includes/header.php'; ?>

<div class="container">
  <div class="list-page">
    <aside class="filter-sidebar">
      <div class="filter-group">
        <div class="filter-title">香调</div>
        <a href="/list.php?gender=<?php echo urlencode($gender); ?>&brand=<?php echo urlencode($brand); ?>&sort=<?php echo urlencode($sort); ?>" class="filter-option <?php echo $category === '全部' ? 'active' : ''; ?>">全部</a>
        <?php foreach ($categories as $c): ?>
          <a href="/list.php?category=<?php echo urlencode($c); ?>&gender=<?php echo urlencode($gender); ?>&brand=<?php echo urlencode($brand); ?>&sort=<?php echo urlencode($sort); ?>" class="filter-option <?php echo $c === $category ? 'active' : ''; ?>"><?php echo escape($c); ?></a>
        <?php endforeach; ?>
      </div>
      <div class="filter-group">
        <div class="filter-title">性别</div>
        <a href="/list.php?category=<?php echo urlencode($category); ?>&brand=<?php echo urlencode($brand); ?>&sort=<?php echo urlencode($sort); ?>" class="filter-option <?php echo $gender === '全部' ? 'active' : ''; ?>">全部</a>
        <?php foreach ($genders as $g): ?>
          <a href="/list.php?category=<?php echo urlencode($category); ?>&gender=<?php echo urlencode($g); ?>&brand=<?php echo urlencode($brand); ?>&sort=<?php echo urlencode($sort); ?>" class="filter-option <?php echo $g === $gender ? 'active' : ''; ?>"><?php echo escape($g); ?></a>
        <?php endforeach; ?>
      </div>
      <div class="filter-group">
        <div class="filter-title">品牌</div>
        <a href="/list.php?category=<?php echo urlencode($category); ?>&gender=<?php echo urlencode($gender); ?>&sort=<?php echo urlencode($sort); ?>" class="filter-option <?php echo $brand === '全部' ? 'active' : ''; ?>">全部</a>
        <?php foreach ($brands as $b): ?>
          <a href="/list.php?category=<?php echo urlencode($category); ?>&gender=<?php echo urlencode($gender); ?>&brand=<?php echo urlencode($b); ?>&sort=<?php echo urlencode($sort); ?>" class="filter-option <?php echo $b === $brand ? 'active' : ''; ?>"><?php echo escape($b); ?></a>
        <?php endforeach; ?>
      </div>
    </aside>
    <div class="list-main">
      <div class="list-toolbar">
        <div class="sort-options">
          <a href="/list.php?category=<?php echo urlencode($category); ?>&gender=<?php echo urlencode($gender); ?>&brand=<?php echo urlencode($brand); ?>" class="sort-btn <?php echo !$sort ? 'active' : ''; ?>">综合</a>
          
          <a href="/list.php?category=<?php echo urlencode($category); ?>&gender=<?php echo urlencode($gender); ?>&brand=<?php echo urlencode($brand); ?>&sort=price-asc" class="sort-btn <?php echo $sort === 'price-asc' ? 'active' : ''; ?>">价格升序</a>
          <a href="/list.php?category=<?php echo urlencode($category); ?>&gender=<?php echo urlencode($gender); ?>&brand=<?php echo urlencode($brand); ?>&sort=price-desc" class="sort-btn <?php echo $sort === 'price-desc' ? 'active' : ''; ?>">价格降序</a>
        </div>
        <span class="result-count">共 <?php echo $total; ?> 件商品</span>
      </div>
      <div class="product-grid">
        <?php if ($products): ?>
          <?php foreach ($products as $p): ?>
            <div class="product-card" onclick="window.location.href='/detail.php?id=<?php echo $p['id']; ?>'">
              <div class="product-card-img">
                <?php if ($p['isHot']): ?>
                  <span class="product-tag">热销</span>
                <?php endif; ?>
                <img src="<?php echo escape($p['image']); ?>" alt="<?php echo escape($p['name']); ?>" loading="lazy">
                <div class="product-card-overlay">
                  <button class="btn btn-primary btn-sm" onclick="event.stopPropagation();addToCart(<?php echo $p['id']; ?>)">快速加入购物车</button>
                </div>
              </div>
              <div class="product-card-info">
                <div class="product-card-brand"><?php echo escape($p['brand']); ?></div>
                <div class="product-card-name"><?php echo escape($p['name']); ?></div>
                <div class="product-card-price">
                  <span class="price-current">&yen;<?php echo $p['price']; ?></span>
                  <?php if ($p['originalPrice']): ?>
                    <span class="price-original">&yen;<?php echo $p['originalPrice']; ?></span>
                  <?php endif; ?>
                </div>
                <div class="product-card-rating">
                  <?php echo stars($p['rating']); ?> <span><?php echo $p['rating']; ?> (<?php echo $p['reviewCount']; ?>)</span>
                </div>
              </div>
            </div>
          <?php endforeach; ?>
        <?php else: ?>
          <p style="text-align:center;color:#999;padding:40px;grid-column:1/-1">暂无符合条件的商品</p>
        <?php endif; ?>
      </div>
    </div>
  </div>
</div>

<?php include '../includes/footer.php'; ?>
