<?php require_once __DIR__ . '/config/db.php'; require_once __DIR__ . '/includes/tracker.php'; ?>
<?php
$pdo = getDB();
$categories = $pdo->query("
    SELECT * FROM categories
    WHERE is_active = 1
    ORDER BY sort_order ASC
")->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Amryl - Product Details & Specifications</title>
  <meta content="width=device-width, initial-scale=1.0" name="viewport" />
  <meta content="amryl water, alkaline water, ro water, mineral water, bottled water, pure hydration, safe drinking water" name="keywords" />
  <meta content="Amryl delivers premium alkaline, RO, and mineral water with advanced 5-step purification. Trusted for purity since 2008." name="description" />
  <link href="img/pure-water-drop.png" rel="icon" />

  <link rel="preconnect" href="https://fonts.googleapis.com" />
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
  <link href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wdth,wght@0,75..100,300..800;1,75..100,300..800&family=Playfair+Display:ital,wght@0,400..900;1,400..900&display=swap" rel="stylesheet" />
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/all.css" />
  <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.1/font/bootstrap-icons.css" rel="stylesheet" />
  <link href="lib/animate/animate.min.css" rel="stylesheet" />
  <link href="lib/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet" />
  <link href="css/bootstrap.min.css" rel="stylesheet" />
  <link href="css/style.css" rel="stylesheet" />

  <style>
    .hero {
      background: linear-gradient(rgba(0,0,0,0.45), rgba(0,0,0,0.45)),
        url("img/page-header-bg.jpg");
      background-size: cover;
      background-position: center;
      color: white;
      padding: 130px 0 80px;
      text-align: center;
    }
    .hero h1 {
      font-size: 3.2rem;
      font-weight: 700;
      color: #ffffff;
      font-family: 'Playfair Display', serif;
    }
    .hero p { font-size: 1.15rem; opacity: .85; }

    /* Product Card */
    .product-card {
      background: white;
      border-radius: 2rem;
      overflow: hidden;
      box-shadow: 0 20px 60px rgba(0,0,0,0.10);
      transition: all 0.4s ease;
      margin-bottom: 4rem;
    }
    .product-card:hover {
      transform: translateY(-10px);
      box-shadow: 0 35px 80px rgba(0,0,0,0.18);
    }

    /* Image side */
    .product-img {
      height: 100%;
      min-height: 560px;
      overflow: hidden;
      position: relative;
    }
    .product-img img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: transform 0.8s ease;
    }
    .product-img:hover img { transform: scale(1.07); }

    /* Content side */
    .content-area {
      padding: 4rem 4rem;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    .content-area * { text-align: left !important; }
    .content-area ul {
      padding-left: 1.5rem !important;
      margin-bottom: 1.2rem;
    }
    .content-area li { margin-bottom: .35rem; }

    .icon-title {
      font-size: 4.5rem;
      background: linear-gradient(135deg, #0d6efd, #6610f2);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      margin-bottom: .8rem;
      display: inline-block;
    }
    .product-title {
      font-family: 'Playfair Display', serif;
      font-weight: 900;
      font-size: 2.4rem;
      margin-bottom: .5rem;
      line-height: 1.2;
      color: #111;
    }
    .product-tagline {
      color: #6c757d;
      font-size: 1rem;
      margin-bottom: 1.2rem;
      font-style: italic;
    }
    .sizes-badge {
      display: inline-block;
      background: linear-gradient(135deg, #0d6efd15, #6610f215);
      border: 1px solid #0d6efd30;
      color: #0d6efd;
      font-weight: 600;
      font-size: .82rem;
      padding: .25rem .7rem;
      border-radius: 50px;
      margin: .15rem .1rem;
      letter-spacing: .02em;
    }

    .info-section-title {
      color: #0d6efd;
      font-weight: 700;
      font-size: 1.05rem;
      margin: 1.8rem 0 .7rem;
      position: relative;
      padding-bottom: 8px;
      text-transform: uppercase;
      letter-spacing: .04em;
    }
    .info-section-title::after {
      content: "";
      position: absolute;
      left: 0; bottom: 0;
      width: 50px; height: 3px;
      background: linear-gradient(90deg, #0d6efd, #6610f2);
      border-radius: 2px;
    }

    /* Delivery / storage pill boxes */
    .info-pill-box {
      background: #f8f9fa;
      border-left: 3px solid #0d6efd;
      border-radius: 0 .6rem .6rem 0;
      padding: .8rem 1.1rem;
      font-size: .92rem;
      color: #444;
      margin-bottom: .5rem;
      line-height: 1.6;
    }

    /* Section wrapper bg */
    .cat-section {
      padding: 0 0 0.5rem 0;
    }

    @media (max-width: 991.98px) {
      .product-img { min-height: 320px; }
      .content-area { padding: 2.5rem 2rem !important; }
      .product-title { font-size: 1.9rem; }
      .icon-title { font-size: 3.5rem; }
      .hero h1 { font-size: 2.2rem; }
    }
  </style>
</head>
<body>
<?php require_once __DIR__ . '/includes/navbar.php'; ?>

<!-- Hero -->
<div class="hero text-center">
  <div class="container">
    <h1 class="wow fadeInDown">Complete Product Range</h1>
    <p class="lead wow fadeInUp">Pure hydration crafted for every lifestyle</p>
  </div>
</div>

<div class="container my-4"></div>

<?php if (empty($categories)): ?>
  <div class="container my-5 text-center py-5">
    <i class="fas fa-box-open fa-3x text-muted mb-3"></i>
    <h4 class="text-muted">No products available at the moment.</h4>
    <p class="text-muted">Please check back soon.</p>
  </div>
<?php else: ?>
  <?php foreach ($categories as $index => $cat):
    $isReverse    = (bool)$cat['layout_reverse'];
    $bgColor      = htmlspecialchars($cat['bg_color'] ?? '#ffffff');
    $imgSrc       = !empty($cat['image_path'])
                    ? htmlspecialchars($cat['image_path'])
                    : 'img/pure-water-drop.png';

    // Parse newline-delimited additional_info into array
    $additionalLines = array_filter(
      array_map('trim', explode("\n", $cat['additional_info'] ?? ''))
    );

    // Parse available_sizes — comma separated
    $sizes = array_filter(
      array_map('trim', explode(',', $cat['available_sizes'] ?? ''))
    );
  ?>
  <div class="cat-section" style="background:<?= $bgColor ?>;">
    <div class="container-fluid px-4 px-lg-5">
      <div class="product-card wow fadeInUp">
        <div class="row g-0 align-items-stretch">

          <!-- Content -->
          <div class="col-lg-6 order-lg-<?= $isReverse ? 2 : 1 ?>">
            <div class="content-area">

              <i class="<?= htmlspecialchars($cat['icon'] ?? 'fas fa-tint') ?> icon-title"></i>

              <h2 class="product-title"><?= htmlspecialchars($cat['name']) ?></h2>

              <?php if (!empty($cat['tagline'])): ?>
                <p class="product-tagline"><?= htmlspecialchars($cat['tagline']) ?></p>
              <?php endif; ?>

              <?php if (!empty($sizes)): ?>
                <div class="mb-2">
                  <?php foreach ($sizes as $sz): ?>
                    <span class="sizes-badge"><?= htmlspecialchars($sz) ?></span>
                  <?php endforeach; ?>
                </div>
              <?php endif; ?>

              <!-- Description -->
              <div class="info-section-title">Product Description</div>
              <p><?= nl2br(htmlspecialchars($cat['description'] ?? '')) ?></p>

              <!-- Additional Info -->
              <?php if (!empty($additionalLines)): ?>
                <div class="info-section-title">Additional Information</div>
                <ul>
                  <?php foreach ($additionalLines as $line): ?>
                    <li><?= htmlspecialchars($line) ?></li>
                  <?php endforeach; ?>
                </ul>
              <?php endif; ?>

              <!-- Storage -->
              <?php if (!empty(trim($cat['storage_info'] ?? ''))): ?>
                <div class="info-section-title">Storage Instructions</div>
                <div class="info-pill-box">
                  <?= nl2br(htmlspecialchars($cat['storage_info'])) ?>
                </div>
              <?php endif; ?>

              <!-- Delivery -->
              <?php if (!empty(trim($cat['delivery_info'] ?? ''))): ?>
                <div class="info-section-title">Delivery Instructions</div>
                <div class="info-pill-box">
                  <?= nl2br(htmlspecialchars($cat['delivery_info'])) ?>
                </div>
              <?php endif; ?>

            </div>
          </div>

          <!-- Image -->
          <div class="col-lg-6 order-lg-<?= $isReverse ? 1 : 2 ?>">
            <div class="product-img">
              <img src="<?= $imgSrc ?>"
                   alt="<?= htmlspecialchars($cat['name']) ?>"
                   onerror="this.src='img/pure-water-drop.png'" />
            </div>
          </div>

        </div>
      </div>
    </div>
  </div>
  <?php endforeach; ?>
<?php endif; ?>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="lib/wow/wow.min.js"></script>
<script src="lib/easing/easing.min.js"></script>
<script src="lib/waypoints/waypoints.min.js"></script>
<script src="lib/counterup/counterup.min.js"></script>
<script src="lib/owlcarousel/owl.carousel.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>