<?php
// debug_login_fix.php
session_start();

// Koneksi database
try {
    $pdo = new PDO("mysql:host=localhost;dbname=soca_osce;charset=utf8", "root", "");
} catch (PDOException $e) {
    die("Koneksi database gagal: " . $e->getMessage());
}

if (isset($_POST['username']) && isset($_POST['password'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    echo "<h3>Login Attempt:</h3>";
    echo "Username: $username<br>";
    echo "Password: $password<br>";
    
    // Cek user
    $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
    $stmt->execute([$username]);
    $user = $stmt->fetch();
    
    if ($user) {
        echo "<h3>User Found:</h3>";
        echo "ID: " . $user['id'] . "<br>";
        echo "Username: " . $user['username'] . "<br>";
        echo "Role: " . $user['role'] . "<br>";
        echo "Password verify: " . (password_verify($password, $user['password']) ? 'TRUE' : 'FALSE') . "<br>";
        
        if (password_verify($password, $user['password'])) {
            // Set session
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['user_name'] = $user['username'];
            $_SESSION['role'] = $user['role'];
            
            echo "<h3 style='color: green;'>SESSION SET:</h3>";
            echo "user_id: " . $_SESSION['user_id'] . "<br>";
            echo "user_name: " . $_SESSION['user_name'] . "<br>";
            echo "role: " . $_SESSION['role'] . "<br>";
            
            // Jika penguji, cari data penguji
            if ($user['role'] === 'penguji') {
                $stmt_penguji = $pdo->prepare("SELECT * FROM penguji WHERE nip = ?");
                $stmt_penguji->execute([$user['username']]);
                $penguji = $stmt_penguji->fetch();
                
                if ($penguji) {
                    $_SESSION['penguji_id'] = $penguji['id'];
                    $_SESSION['penguji_nama'] = $penguji['nama'];
                    echo "penguji_id: " . $_SESSION['penguji_id'] . "<br>";
                    echo "penguji_nama: " . $_SESSION['penguji_nama'] . "<br>";
                } else {
                    echo "penguji_id: NOT FOUND<br>";
                }
            }
            
            // REDIRECT YANG BENAR
            if ($_SESSION['role'] === 'penguji') {
                header("Location: penguji/penguji_dashboard.php");
                exit;
            }
        }
    } else {
        echo "<h3 style='color: red;'>User NOT found</h3>";
    }
}
?>

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

<hr>
<h3>Available Penguji Logins:</h3>
<ul>
    <li>123456 / password (drg. Nur Ariska)</li>
    <li>231456 / password (drg. Endah Maryani)</li>
    <li>35465656456 / password (drg. Citra)</li>
    <li>6767676 / password (drg. Indra)</li>
    <li>123123213 / password (drg. Faradiba)</li>
    <li>123123123 / password (drg. Reisca)</li>
</ul>