<?php
// debug_login.php
session_start();
echo "<h2>Debug Session</h2>";
echo "<pre>";
print_r($_SESSION);
echo "</pre>";

echo "<h2>Debug POST</h2>";
echo "<pre>";
print_r($_POST);
echo "</pre>";

if (isset($_POST['username']) && isset($_POST['password'])) {
    // Simulate login
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    // Koneksi database
    try {
        $host = 'localhost';
        $dbname = 'soca_osce';
        $username_db = 'root';
        $password_db = '';
        
        $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username_db, $password_db);
        
        // Cek user
        $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
        $stmt->execute([$username]);
        $user = $stmt->fetch();
        
        if ($user) {
            echo "<h3>User ditemukan:</h3>";
            echo "Username: " . $user['username'] . "<br>";
            echo "Role: " . $user['role'] . "<br>";
            echo "Password verify: " . (password_verify($password, $user['password']) ? 'TRUE' : 'FALSE');
            
            if (password_verify($password, $user['password'])) {
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['user_name'] = $user['username'];
                $_SESSION['role'] = $user['role'];
                echo "<br><strong style='color: green;'>LOGIN BERHASIL! Redirecting...</strong>";
                echo "<script>setTimeout(() => window.location.href = 'admin/sync_penguji.php', 2000);</script>";
            }
        } else {
            echo "<h3 style='color: red;'>User tidak ditemukan</h3>";
        }
        
    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
}
?>

<h2>Test Login Form</h2>
<form method="POST">
    Username: <input type="text" name="username" value="admin"><br>
    Password: <input type="password" name="password" value="admin123"><br>
    <button type="submit">Login</button>
</form>