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

/* Pārbauda, vai lietotājs ir pieslēdzies */
if (!isset($_SESSION['user_id'])) {
    header("Location: login.html");
    exit();
}

$user_id = $_SESSION['user_id'];

/* Iegūst lietotāju datus */
$stmt = $conn->prepare("SELECT name, email FROM users WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();

$name = $user['name'];
$email = $user['email'];

$success = isset($_GET['success']);
?>
<!DOCTYPE html>
<html lang="lv">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Profils</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 stils-->
  <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>
 <!-- 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>

  <div class="icons">
    <a href="cart.php" class="fas fa-shopping-cart"></a>
    <button type="button" class="fas fa-search mobile-search-toggle"></button>
    <!-- Izkrītošais saraksts -->
    <div class="dropdown">
      <?php if (isset($_SESSION['email'])): ?>
        <!-- Pieslēgušies lietotāji dodas uz profile.php -->
        <a href="profile.php" class="fas fa-user dropbtn"></a>
      <?php else: ?>
        <!-- Vieši nevar neko nospiest -->
        <a href="#" class="fas fa-user dropbtn"></a>
      <?php endif; ?>
      <!-- Lietotāja statuss -->
      <div class="dropdown-content">
        <?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">

  <div class="profile-card">
    <div class="avatar">
      <i class="fas fa-user-circle"></i>
    </div>
     <!-- Paziņojums par veiksmīgu datu atjaunināšanu -->
    <?php if($success): ?>
      <div class="success-msg">Jūsu profils tika atjaunināts!</div>
    <?php endif; ?>
     
    <h2>Sveiki, <?php echo htmlspecialchars($name ?? ""); ?>!</h2>

    <form action="update_profile.php" method="POST">
      <label>Vārds</label>
      <input type="text" name="name" value="<?php echo htmlspecialchars($name ?? ""); ?>" placeholder="Jūsu vārds (neobligāti)">

      <label>E-pasts</label>
      <input type="email" value="<?php echo htmlspecialchars($email); ?>" readonly>

      <button type="submit">Saglabāt izmaiņas</button>
    </form>
  </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>
