<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
include 'db.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$userId = $_SESSION['user_id'];
$message = '';

// Fetch current user info
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$userId]);
$user = $stmt->fetch();

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $newUsername = $_POST['username'];
    $newEmail = $_POST['email'];
    $newPassword = $_POST['password'];
    $confirmPassword = $_POST['confirm_password'];

    $uploadDir = 'images/user/';
    $profileImage = $user['profile_image'];

    // Handle new image upload
    if (isset($_FILES['profile_image']) && $_FILES['profile_image']['error'] === UPLOAD_ERR_OK) {
        $tmpName = $_FILES['profile_image']['tmp_name'];
        $originalName = basename($_FILES['profile_image']['name']);
        $ext = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
        $allowed = ['jpg', 'jpeg', 'png', 'webp'];

        if (in_array($ext, $allowed)) {
            $newName = uniqid('profile_', true) . '.' . $ext;
            move_uploaded_file($tmpName, $uploadDir . $newName);
            $profileImage = $newName;
        } else {
            $message = "<span style='color:red;'>Invalid image format. Use JPG, PNG, or WEBP.</span>";
        }
    }

    if (!$message) {
        if (!empty($newPassword)) {
            if ($newPassword === $confirmPassword) {
                $hashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);
                $stmt = $pdo->prepare("UPDATE users SET username=?, email=?, password=?, profile_image=? WHERE id=?");
                $stmt->execute([$newUsername, $newEmail, $hashedPassword, $profileImage, $userId]);
                $message = "<span style='color:green;'>Profile and password updated successfully.</span>";
            } else {
                $message = "<span style='color:red;'>Passwords do not match.</span>";
            }
        } else {
            $stmt = $pdo->prepare("UPDATE users SET username=?, email=?, profile_image=? WHERE id=?");
            $stmt->execute([$newUsername, $newEmail, $profileImage, $userId]);
            $message = "<span style='color:green;'>Profile updated successfully.</span>";
        }
    }

    // Reload updated data
    $stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
    $stmt->execute([$userId]);
    $user = $stmt->fetch();
}
?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>User Settings - Review Stream</title>
  <link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<!-- Sans-serif fonts -->
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Raleway&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto+Slab&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Noto+Serif&display=swap" rel="stylesheet">

<!-- Favicon -->
<link rel="icon" href="/favicon.png" type="image/png" />
  
  
  <style>
  
  body{
      margin:0; 
      font-family: 'Roboto', sans-serif;
      line-height: 1.6;
      padding-bottom: 260px;
}
  
    .container { max-width: 500px; margin: 2rem auto; font-family: Arial, sans-serif; }
    form { display: flex; flex-direction: column; gap: 1rem; }
    input[type="text"], input[type="email"], input[type="password"], input[type="file"] {
        padding: 0.6rem; border-radius: 6px; border: 1px solid #ccc;
    }
    .update-settings { padding: 0.6rem; background: #007BFF; color: #fff; border: none; border-radius: 6px; cursor: pointer; }
    .update-settings:hover { background: #1e7e34; }
    .profile-pic {
        width: 100px; height: 100px;
        border-radius: 50%;
        object-fit: cover;
        border: 2px solid #ddd;
        display: block;
        margin: 0 auto 1rem;
    }
    
    
    
    
    
    
      .fixed-footer {
      position: fixed;
      left: 0;
      bottom: 0;
      width: 100%;
      background: #222;
      text-align: center;
      padding: 10px 0;
      color: #fff;
      z-index: 1000;
    }

   .fixed-footer p{
		font-family: 'Poppins', sans-serif !important;
		font-size: 13px !important;
	}

    .footer-menu a {
      margin: 0 10px;
      text-decoration: none;
      color: #fff;
	  font-family: 'Poppins', sans-serif !important;
	  font-size: 13px !important;
    }

    .footer-menu a:hover {
      text-decoration: underline;
    }
	
	
	
.topbar {
  background-color: #007BFF; /* Blue */
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 15px;
  position: relative;
}

.logo {
  display: flex;
  align-items: center;
  font-weight: bold;
  font-size: 18px;
}

.logo-icon {
  background: linear-gradient(135deg, #a2d4f5, #fefb72);
  border-radius: 50%;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 8px;
}

.menu-toggle {
  font-size: 1.5em;
  color: #fff;
  cursor: pointer;
  display: none;
}

.menu {
  display: flex;
  gap: 15px;
}

.menu a, .menu button {
  color: #fff;
  text-decoration: none;
  background: none;
  border: none;
  cursor: pointer;
}

/* Mobile styles */
@media (max-width: 768px) {
  .menu-toggle {
    display: block;
  }

  .menu {
    flex-direction: column;
    background-color: #007BFF;
    position: absolute;
    top: 60px;
    left: 0;
    right: 0;
    display: none;
    padding: 10px 0;
    z-index: 1000;
  }

  .menu.active {
    display: flex;
  }

  .menu a, .menu button {
    padding: 10px 15px;
  }
}
  </style>
</head>
<body>
<div class="topbar">
  <div class="logo">
    <div class="logo-icon">
      <i class="fas fa-shield-alt" style="color:#4A90E2; font-size: 25px; box-shadow: 0 1px 3px rgba(0,0,0,0.2); margin-left: 8px"></i>
    </div>
    REVIEW STREAM
  </div>

  <div class="menu-toggle" onclick="toggleMenu()">
    <i class="fas fa-bars"></i>
  </div>

  <div class="menu" id="topMenu">
    <a href="index.php">Home</a>
    <a href="companies.php">Companies</a>
    <a href="blog.php">Newsroom</a>
    
    <?php if(isset($_SESSION['user_id'])): ?>
      <a href="user-settings.php">My Settings</a>
      <a href="logout.php">Logout</a>
    <?php else: ?>
      <a href="login.php">Login</a>
    <?php endif; ?>
    
    <button class="dark-mode-toggle" onclick="document.body.classList.toggle('dark-mode')">🌓</button>
  </div>
</div>




<div class="container">
  <h2 style="text-align:center;">Update Your Profile</h2>
  <?php if ($message): ?>
    <div style="text-align:center;"><?= $message ?></div>
  <?php endif; ?>

  <form method="post" enctype="multipart/form-data">
    <img src="images/user/<?= htmlspecialchars($user['profile_image']) ?>" alt="Profile Image" class="profile-pic">
    
    <label>Change profile image:</label>
    <input type="file" name="profile_image" accept="image/*">
    
    <label>Username:</label>
    <input type="text" name="username" value="<?= htmlspecialchars($user['username']) ?>" required>
    
    <label>Email:</label>
    <input type="email" name="email" value="<?= htmlspecialchars($user['email']) ?>" required>

    <label>New Password (leave blank if not changing):</label>
    <input type="password" name="password">

    <label>Confirm New Password:</label>
    <input type="password" name="confirm_password">

    <button class="update-settings" type="submit">Update Profile</button>
  </form>
</div>


<?php include 'footer.php'; ?>







<script>
  function toggleMenu() {
    document.getElementById('topMenu').classList.toggle('active');
  }
</script>

</body>
</html>
