<?php
session_start();
require 'db_conn.php';

$search = $_GET['q'] ?? '';
$search = trim($search);

$sql = "
    SELECT p.id, p.name, p.description, p.price, p.image, c.name AS category_name
    FROM products p
    LEFT JOIN categories c ON p.category_id = c.id
    WHERE 1
";

if (!empty($search)) {
    $safe = $conn->real_escape_string($search);
    $sql .= " AND (
        p.name LIKE '%$safe%'
        OR p.description LIKE '%$safe%'
        OR c.name LIKE '%$safe%'
    )";
}
function shortText($text, $maxLength = 120) {
    if (mb_strlen($text) <= $maxLength) {
        return $text;
    }

    $short = mb_substr($text, 0, $maxLength);

    $short = mb_substr($short, 0, mb_strrpos($short, ' '));

    return $short . '...';
}
$sql .= " ORDER BY p.id DESC";

$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="lv">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Katalogs</title>

  <!-- Font Awesome fonts -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"/>

  <!-- Google Fonts -->
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700;800&display=swap" rel="stylesheet" />

  <!-- Meklēšanas joslas CSS-->
  <link rel="stylesheet" href="main.css">
  <script src="script.js"></script>
</head>

<body class="page-layout">

<header>
  <input type="checkbox" id="toggler" />
  <label for="toggler" class="fa fa-bars"></label>

  <a href="index.php" class="logo">Ziedu internetveikals<span>🌷</span></a>
  <!-- Navigācijas josla -->
<nav class="navbar">
  <div class="dropdown-nav">
    <a href="katalogs.php" class="dropbtn-nav">Katalogs</a>

    <div class="dropdown-content-nav">
      <a href="ziedi.php">Ziedi</a>
      <a href="telpaugi.php">Telpaugi</a>
      <a href="eksotiskieaugi.php">Eksotiskie augi</a>
    </div>
  </div>
</nav>
    <!-- Meklēšanas josla -->
    <div class="search-container">
      <form action="katalogs.php" method="GET">
        <input type="text" name="q" placeholder="Meklēt">
        <button type="submit">
          <i class="fa fa-search"></i>
        </button>
      </form>
    </div>

  <!-- Ikonas -->
  <div class="icons">
    <a href="cart.php" class="fas fa-shopping-cart"></a>
    <button type="button" class="fas fa-search mobile-search-toggle"></button>


    <div class="dropdown">
      <?php if (isset($_SESSION['email'])): ?>
      <!-- Noklikšķināmā profila lapa autenficētiem lietotājiem-->
        <a href="profile.php" class="fas fa-user dropbtn"></a>
       <?php else: ?>
      <!-- Viesiem profila lapa neko nedara, nav noklikšķināma-->
         <a href="#" class="fas fa-user dropbtn"></a>
       <?php endif; ?>

      <div class="dropdown-content">
        <!-- Lietotāja statuss -->
        <?php if (isset($_SESSION['email'])): ?>
          <a href="logout.php" class="logout">Izrakstīties</a>
        <?php else: ?>
          <a href="login.html">Pieslēgties</a>
          <a href="register.html">Reģistrēties</a>
        <?php endif; ?>
      </div>
    </div>
  </div>
</header>
<!-- Galvenais saturs -->
<main class="page-content">
<!-- Parāda meklēšanas rezultātus -->
  <?php if (!empty($search)): ?>
    <p style="text-align:center;">
      Meklēšanas rezultāti: <strong><?= htmlspecialchars($search) ?></strong>
    </p>
  <?php endif; ?>

  <?php if ($result && $result->num_rows > 0): ?>
    <div class="catalog-grid">
      <!-- Cikls caur visiem produktiem -->
      <?php while ($row = $result->fetch_assoc()): ?>
        <div class="catalog-card">
          
          <a href="product.php?id=<?= $row['id'] ?>" target="_blank">
          <img
            src="<?= !empty($row['image']) 
                  ? PRODUCT_IMAGE_PATH . htmlspecialchars($row['image']) 
                  : PRODUCT_IMAGE_PATH . 'placeholder.jpg' ?>"
            alt="<?= htmlspecialchars($row['name']) ?>"
          >

          <div class="catalog-body">
            <h3><?= htmlspecialchars($row['name']) ?></h3>

            <div class="catalog-category">
              <?= htmlspecialchars($row['category_name'] ?? 'Bez kategorijas') ?>
            </div>

            <div class="catalog-desc">
              <?= htmlspecialchars(shortText($row['description'])) ?>
            </div>

            <div class="catalog-price">
              <?= htmlspecialchars($row['price']) ?> €
            </div>

            <a href="cart.php?add=<?= $row['id'] ?>" class="catalog-btn">
              Pievienot grozam 🛒
            </a>
          </div>

        </div>
      <?php endwhile; ?>

    </div>
  <?php else: ?>
    <p style="text-align:center;">Produkts netika atrasts.</p>
  <?php endif; ?>

</main>
  <!-- Kājene -->
<footer>
  <div class="footer-container">
    <div class="footer-left">
      <a href="parmums.php">Par mums</a>
      <a href="kontakti.php">Kontakti</a>
    </div>
  </div>
</footer>


</body>
</html>
