<?php include 'include/header.php';?>
<?php include 'include/navbar.php';?>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
  <style>
    body {
      background: #f7f7f7;
    }
    .login-box {
      max-width: 400px;
      margin: 80px auto;
      padding: 30px;
      background: #fff;
      box-shadow: 0 0 10px rgba(0,0,0,0.1);
      border-radius: 10px;
    }
    .login-box h2 {
      margin-bottom: 25px;
    }
    .form-control:focus {
      box-shadow: none;
      border-color: #00796B;
    }
    .btn-warning {
      width: 100%;
    }
  </style>

<div class="login-box">



<?php
$code = $_GET['code'];

$check = mysqli_query($conn, "SELECT * FROM users WHERE verification_code = '$code'");
if (mysqli_num_rows($check) === 0) {
    echo "<p style='text-align:center;'>Invalid or expired reset link.</p>";
    exit;
}
?>

<?php

$msg = "";
$success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $code = mysqli_real_escape_string($conn, $_POST['code']);
    $new = mysqli_real_escape_string($conn, $_POST['new_password']);
    $confirm = mysqli_real_escape_string($conn, $_POST['confirm_password']);

    if ($new !== $confirm) {
        $msg = "<p class='text-center text-white bg-danger rounded p-2'>Passwords do not match.</p>";
    } else {
        $hashed = password_hash($new, PASSWORD_DEFAULT);
        $update = mysqli_query($conn, "UPDATE users SET password='$hashed' WHERE verification_code='$code'");

        if ($update) {
            $success = true;
        } else {
            $msg = "<p class='text-center text-white bg-danger rounded p-2'>Something went wrong. Please try again.</p>";
        }
    }
}

?>





    <h2 style="text-align: center;">Set New Password</h2>
    <?php echo $msg;?>
  <form action="" method="POST" style="max-width: 400px; margin: auto;">
    <input type="hidden" name="code" value="<?= htmlspecialchars($code) ?>">
    <input type="password" class="form-control" name="new_password" placeholder="New Password" required ><br><br>
    <input type="password" class="form-control" name="confirm_password" placeholder="Confirm Password" required ><br><br>
    <button type="submit"  class="btn my-btn btn-block">Update Password</button>
  </form>

</div>





<?php if ($success): ?> 
<script>
    Swal.fire({
        title: 'Success!',
        text: 'Your password has been updated successfully.',
        icon: 'success',
        confirmButtonText: 'OK'
    }).then((result) => {
        if (result.isConfirmed) {
            window.location.href = 'login.php';
        }
    });
</script>
<?php endif; ?>










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