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

if (!isset($_GET['token'])) {
    die("Invalid request!");
}
$token = $_GET['token'];

// Check if the token is valid
$stmt = $pdo->prepare("SELECT email FROM password_resets WHERE token = ?");
$stmt->execute([$token]);
$reset_request = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$reset_request) {
    die("<h3 style='text-align:center; color:red; margin-top:50px; font-family: sans-serif;'>This link has expired or is invalid. <a href='forgot_password.php'>Try again</a></h3>");
}

// If form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['new_password'])) {
    $new_password = password_hash($_POST['new_password'], PASSWORD_DEFAULT);
    $email = $reset_request['email'];

    // Update the password
    $update = $pdo->prepare("UPDATE students SET password = ? WHERE email = ?");
    $update->execute([$new_password, $email]);

    // Delete the used token
    $pdo->prepare("DELETE FROM password_resets WHERE email = ?")->execute([$email]);

    echo "<script>alert('Your password has been changed successfully!'); window.location.href='login.php';</script>";
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Set New Password | Ahmad Academy</title>
    
    <link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">

    <style>
        :root {
            --primary: #064e3b;
            --primary-dark: #022c22;
            --accent: #d4af37; /* Golden */
            --bg-light: #f8fafc;
        }

        body {
            /* Premium Dark Green Gradient Background */
            background: linear-gradient(135deg, var(--primary-dark) 0%, var(--primary) 100%);
            font-family: 'Outfit', sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
            padding: 20px;
        }

        .login-card {
            background: #ffffff;
            border-radius: 20px;
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.4);
            width: 100%;
            max-width: 450px;
            padding: 45px 40px;
            position: relative;
            overflow: hidden;
            border-top: 6px solid var(--accent); /* Golden Top Border */
        }

        .brand-icon {
            width: 75px; height: 75px;
            background: rgba(212, 175, 55, 0.1); /* Light Golden Background */
            border-radius: 20px;
            display: flex; align-items: center; justify-content: center;
            margin: 0 auto 20px;
            font-size: 35px; 
            color: var(--accent);
            box-shadow: 0 10px 20px rgba(212, 175, 55, 0.15);
        }

        .login-title { 
            font-weight: 800; 
            color: var(--primary-dark); 
            text-align: center; 
            margin-bottom: 10px; 
            font-size: 26px; 
        }
        
        .login-subtitle { 
            color: #64748b; 
            text-align: center; 
            font-size: 15px; 
            margin-bottom: 35px; 
            line-height: 1.6;
            font-weight: 500; 
        }

        .form-label { 
            font-weight: 700; 
            color: var(--primary); 
            font-size: 14px; 
            margin-bottom: 8px; 
        }

        .input-group-custom {
            position: relative;
            margin-bottom: 25px;
        }

        .input-group-custom .icon-left {
            position: absolute;
            left: 18px;
            top: 50%;
            transform: translateY(-50%);
            color: #94a3b8;
            z-index: 10;
            transition: 0.3s;
            font-size: 18px;
        }

        .input-group-custom .toggle-password {
            position: absolute;
            right: 18px;
            top: 50%;
            transform: translateY(-50%);
            color: #94a3b8;
            z-index: 10;
            cursor: pointer;
            transition: 0.3s;
            font-size: 18px;
        }
        
        .input-group-custom .toggle-password:hover { color: var(--primary); }

        .form-control {
            height: 55px;
            padding-left: 50px !important;
            padding-right: 50px !important; /* Space for right icon */
            border-radius: 12px;
            background-color: #f8fafc;
            border: 2px solid #e2e8f0;
            font-weight: 500;
            font-size: 15px;
            color: #334155;
            transition: all 0.3s ease;
        }

        .form-control:focus {
            background-color: #fff;
            border-color: var(--accent);
            box-shadow: 0 0 0 4px rgba(212, 175, 55, 0.15);
            outline: none;
        }

        .form-control:focus + .icon-left { color: var(--accent); }

        /* Save Button - Golden Color */
        .btn-login {
            background-color: var(--accent);
            color: var(--primary-dark);
            width: 100%;
            height: 55px;
            border-radius: 12px;
            font-weight: 800;
            font-size: 16px;
            border: none;
            margin-top: 5px;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            box-shadow: 0 5px 15px rgba(212, 175, 55, 0.3);
        }

        .btn-login:hover {
            background-color: #cda832;
            transform: translateY(-3px);
            box-shadow: 0 12px 25px rgba(212, 175, 55, 0.4);
            color: var(--primary-dark);
        }
    </style>
</head>
<body>

    <div class="login-card">
        <div class="brand-icon">
            <i class="fas fa-shield-alt"></i>
        </div>
        
        <h3 class="login-title">Set New Password</h3>
        <p class="login-subtitle">Please enter your new password below to regain access to your account.</p>

        <form method="POST">
            <div class="mb-3">
                <label class="form-label">New Password</label>
                <div class="input-group-custom">
                    <input type="password" id="password" name="new_password" class="form-control" placeholder="Enter new password" required minlength="6">
                    <i class="fas fa-lock icon-left"></i>
                    <i class="fas fa-eye toggle-password" id="togglePassword" onclick="togglePassVisibility()"></i>
                </div>
            </div>

            <button type="submit" class="btn btn-login">
                Save Password <i class="fas fa-check-circle"></i>
            </button>
        </form>
    </div>

    <script>
        function togglePassVisibility() {
            const passwordInput = document.getElementById('password');
            const toggleIcon = document.getElementById('togglePassword');

            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                toggleIcon.classList.remove('fa-eye');
                toggleIcon.classList.add('fa-eye-slash');
            } else {
                passwordInput.type = 'password';
                toggleIcon.classList.remove('fa-eye-slash');
                toggleIcon.classList.add('fa-eye');
            }
        }
    </script>
</body> 
</html>