<?php
session_start(); // Sesijas sākums
require 'db_conn.php';

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
// Iegūst konkrēto produktu
$sql = "
    SELECT p.*, c.name AS category_name
    FROM products p
    LEFT JOIN categories c ON p.category_id = c.id
    WHERE p.id = $id
    LIMIT 1
";

$result = $conn->query($sql);
// Ja produkts nav atrasts, paziņo
if (!$result || $result->num_rows === 0) {
    die("Produkts nav atrasts.");
}

$product = $result->fetch_assoc();

$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 p.category_id != 2
";

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%'
    )";
}

$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><?= htmlspecialchars($product['name']) ?></title>

  <!-- Font Awesome -->
  <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" />

  <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>

<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>


  <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>

<main class="page-content">

  <div class="product-container">

  
    <div class="product-image">
      <img src="<?= PRODUCT_IMAGE_PATH . htmlspecialchars($product['image']) ?>" 
           alt="<?= htmlspecialchars($product['name']) ?>">
    </div>

     <!-- Produkta informācija -->
    <div class="product-info">
      <h1><?= htmlspecialchars($product['name']) ?></h1>

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

      <div class="product-description">
        <?= nl2br(htmlspecialchars($product['description'])) ?>
      </div>

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

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

      <a class="back-btn" href="katalogs.php">
    ← Atpakaļ
      </a>
    </div>

  </div>

</main>
  <!-- Lapas 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>