<?php
session_start();
require_once 'db.php';

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

$user_id = $_SESSION['user_id'];
$message = "";
$message_color = "#00bfff"; // default light blue

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $current_password = $_POST['current_password'] ?? '';
  $new_password = $_POST['new_password'] ?? '';
  $confirm_password = $_POST['confirm_password'] ?? '';

  if (empty($current_password) || empty($new_password) || empty($confirm_password)) {
    $message = "❌ All fields are required.";
    $message_color = "red";
  } elseif ($new_password !== $confirm_password) {
    $message = "❌ New passwords do not match.";
    $message_color = "red";
  } else {
    // Check old password
    $stmt = $conn->prepare("SELECT password FROM users WHERE id = ?");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $stmt->bind_result($hashed);
    $stmt->fetch();
    $stmt->close();

    if ($hashed && password_verify($current_password, $hashed)) {
      $new_hashed = password_hash($new_password, PASSWORD_DEFAULT);
      $update = $conn->prepare("UPDATE users SET password = ? WHERE id = ?");
      $update->bind_param("si", $new_hashed, $user_id);
      if ($update->execute()) {
        $message = "✅ Password changed successfully.";
        $message_color = "#00ffcc";
      } else {
        $message = "❌ Failed to update password.";
        $message_color = "red";
      }
    } else {
      $message = "❌ Current password is incorrect.";
      $message_color = "red";
    }
  }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Change Password</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"/>
  <style>
    body {
      background: #0a1826;
      color: #eaf6ff;
      font-family: 'Segoe UI', sans-serif;
      padding: 20px;
    }
    .password-box {
      background: linear-gradient(135deg, #0a1826, #003366);
      border-radius: 15px;
      padding: 25px;
      max-width: 450px;
      margin: auto;
      box-shadow: 0 6px 18px rgba(0,0,0,0.6);
    }
    .password-box h2 {
      color: #66ccff;
      margin-bottom: 20px;
      text-align: center;
      text-shadow: 0 0 6px rgba(102,204,255,0.6);
    }
    .input-group {
      margin-bottom: 18px;
    }
    .input-group label {
      display: block;
      margin-bottom: 6px;
      color: #aad8ff;
    }
    .input-group input {
      width: 93%;
      padding: 10px;
      border: none;
      border-radius: 8px;
      background: #102b40;
      color: #fff;
      outline: none;
    }
    .submit-btn {
      width: 100%;
      padding: 12px;
      background: linear-gradient(to right, #0099ff, #00ffcc);
      color: #000;
      border: none;
      border-radius: 20px;
      font-weight: bold;
      cursor: pointer;
      margin-top: 12px;
      transition: 0.3s ease;
    }
    .submit-btn:hover {
      opacity: 0.9;
    }
    .msg {
      margin-bottom: 20px;
      background: #112233;
      border-left: 5px solid <?= $message_color ?>;
      padding: 12px 15px;
      border-radius: 10px;
      color: <?= $message_color ?>;
      font-weight: 500;
      animation: fadein 0.5s ease-in-out;
    }
    @keyframes fadein {
      from {opacity: 0;}
      to {opacity: 1;}
    }
  </style>
</head>
<body>

  <div class="password-box">
    <h2>Change Password</h2>

    <?php if (!empty($message)): ?>
      <div class="msg"><?= $message ?></div>
    <?php endif; ?>

    <form method="POST">
      <div class="input-group">
        <label for="current_password">Current Password</label>
        <input type="password" name="current_password" id="current_password" required />
      </div>

      <div class="input-group">
        <label for="new_password">New Password</label>
        <input type="password" name="new_password" id="new_password" required />
      </div>

      <div class="input-group">
        <label for="confirm_password">Confirm New Password</label>
        <input type="password" name="confirm_password" id="confirm_password" required />
      </div>

      <button type="submit" class="submit-btn">Update Password</button>
    </form>
  </div>

</body>
</html>
