<?php
// Aktifkan session dan koneksi
session_start();
include 'koneksi.php';

$email = isset($_GET['email']) ? $_GET['email'] : '';
$status = '';
$message = '';

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $status = 'error';
    $message = "Email tidak valid.";
} else {
    // Cek apakah user dengan email ini ada
    $stmt = mysqli_prepare($conn, "SELECT id, is_verified FROM users WHERE email = ?");
    mysqli_stmt_bind_param($stmt, "s", $email);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);

    if ($result && mysqli_num_rows($result) > 0) {
        $user = mysqli_fetch_assoc($result);
        if ($user['is_verified'] == 1) {
            $status = 'info';
            $message = "Akun sudah pernah diverifikasi sebelumnya.";
        } else {
            // Verifikasi akun
            $update = mysqli_prepare($conn, "UPDATE users SET is_verified = 1 WHERE email = ?");
            mysqli_stmt_bind_param($update, "s", $email);
            if (mysqli_stmt_execute($update)) {
                $status = 'success';
                $message = "Akun berhasil diverifikasi! Pengguna sudah dapat login.";
            } else {
                $status = 'error';
                $message = "Gagal memperbarui status verifikasi. Silakan coba lagi.";
            }
        }
    } else {
        $status = 'error';
        $message = "Email tidak ditemukan dalam database.";
    }
}
?>

<!DOCTYPE html>
<html lang="id">

<head>
    <meta charset="UTF-8">
    <title>Verifikasi Akun</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <style>
        body {
            display: flex;
            height: 100vh;
            background-color: #f9f9f9;
            align-items: center;
            justify-content: center;
            font-family: 'Poppins', sans-serif;
        }

        .alert {
            max-width: 500px;
            padding: 20px;
            border-radius: 8px;
        }
    </style>
</head>

<body>
    <div class="alert alert-<?= $status === 'success' ? 'success' : ($status === 'info' ? 'info' : 'danger') ?>" role="alert">
        <h4 class="alert-heading"><?= ucfirst($status) ?>!</h4>
        <p><?= htmlspecialchars($message) ?></p>
        <hr>
        <a href="login2.php" class="btn btn-primary">Ke Halaman Login</a>
    </div>
</body>

</html>