<?php
session_start();
require 'db_conn.php';
// Iegūst rītdienas vārda dienu sarakstu no datubāzes
function getTomorrowNamedays($conn) {
    $tomorrow = new DateTime('tomorrow');

    $month = (int)$tomorrow->format('n');
    $day   = (int)$tomorrow->format('j');

    $stmt = $conn->prepare("SELECT name FROM namedays WHERE month = ? AND day = ?");
    $stmt->bind_param("ii", $month, $day);
    $stmt->execute();

    $result = $stmt->get_result();

    $names = [];
    while ($row = $result->fetch_assoc()) {
        $names[] = trim($row['name']);
    }

    return $names;
}

$tomorrowNamedays = getTomorrowNamedays($conn);

$personalReminder = '';

if (!empty($_SESSION['name']) && !empty($tomorrowNamedays)) {

    $userName = mb_strtolower(trim($_SESSION['name']));
// Izvada paziņojumu, ja vārds sakrīt
    foreach ($tomorrowNamedays as $name) {
        if (mb_strtolower(trim($name)) === $userName) {
            $personalReminder = "🌸 Sveiki, {$_SESSION['name']}! Rīt ir Jūsu vārda diena!";
            break;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="lv">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <!-- Lapas nosaukums -->
  <title>Ziedu internetveikals</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" />

  <link rel="stylesheet" href="main.css" />
  <script src="script.js"></script>
</head>

<body class="page-layout">
 <!-- Lapas galvene -->
<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>
    <!-- Mekleš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">
        <!-- Lietotaja 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">
<?php if (!empty($tomorrowNamedays)): ?>
 <div class="nameday-notification">
    Rītdien vārda dienu svin:
    <strong>
      <?php echo implode(", ", $tomorrowNamedays); ?>
    </strong>
  </div>
<?php endif; ?>
<?php if (!empty($personalReminder)): ?>
 <div class="reminder-personal">
    <?= htmlspecialchars($personalReminder) ?>
  </div>
<?php endif; ?>
<section class="hero">
  <div class="hero-text">
    <h1>Sveicināti Ziedu internetveikalā</h1>
    <p>
      Izvēlies skaistākos augus savām mājām un dārzam.  
      Mēs piedāvājam plašu telpaugu un ziedu klāstu ar piegādi visā Latvijā.
    </p>
    <a href="katalogs.php" class="hero-btn">Skatīt katalogu</a>
  </div>

  <div class="hero-image"></div>
</section>

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