<?php
session_start();
require_once 'includes/config.php';

if (isset($_SESSION['penguji_id'])) {
    header('Location: penguji/dashboard_penguji.php');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nip = trim($_POST['nip']);
    
    if (empty($nip)) {
        $error = "NIP harus diisi";
    } else {
        try {
            // Cek apakah penguji ada di database penguji
            $stmt = $pdo->prepare("SELECT * FROM penguji WHERE nip = ?");
            $stmt->execute([$nip]);
            $penguji = $stmt->fetch();
            
            if ($penguji) {
                // Cek apakah penguji di-assign ke ujian aktif
                $stmt = $pdo->prepare("
                    SELECT ps.*, u.nama_ujian, u.tanggal_ujian, s.nama_station
                    FROM penguji_station ps
                    JOIN ujian u ON ps.ujian_id = u.id
                    JOIN stations s ON ps.station_id = s.id
                    WHERE ps.penguji_id = ? AND u.status = 'aktif'
                    ORDER BY ps.created_at DESC
                    LIMIT 1
                ");
                $stmt->execute([$penguji['id']]);
                $assignment = $stmt->fetch();
                
                if ($assignment) {
                    // Login berhasil, simpan session
                    $_SESSION['penguji_id'] = $penguji['id'];
                    $_SESSION['penguji_nama'] = $penguji['nama'];
                    $_SESSION['penguji_nip'] = $penguji['nip'];
                    $_SESSION['assignment_id'] = $assignment['id'];
                    $_SESSION['ujian_id'] = $assignment['ujian_id'];
                    $_SESSION['station_id'] = $assignment['station_id'];
                    $_SESSION['peran'] = $assignment['peran'];
                    $_SESSION['ujian_nama'] = $assignment['nama_ujian'];
                    $_SESSION['station_nama'] = $assignment['nama_station'];
                    
                    header('Location: penguji/dashboard_penguji.php');
                    exit;
                } else {
                    $error = "Anda belum di-assign ke ujian aktif manapun";
                }
            } else {
                $error = "NIP tidak ditemukan";
            }
        } catch (PDOException $e) {
            $error = "Error sistem: " . $e->getMessage();
        }
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Penguji - SOCA OSCE</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .login-container {
            max-width: 400px;
            margin: 100px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="login-container">
            <h2 class="text-center mb-4">Login Penguji</h2>
            
            <?php if ($error): ?>
                <div class="alert alert-danger"><?php echo $error; ?></div>
            <?php endif; ?>
            
            <form method="POST" action="">
                <div class="mb-3">
                    <label for="nip" class="form-label">NIP</label>
                    <input type="text" class="form-control" id="nip" name="nip" required 
                           placeholder="Masukkan NIP Anda">
                </div>
                
                <button type="submit" class="btn btn-primary w-100">Login</button>
            </form>
            
            <div class="text-center mt-3">
                <a href="../index.php" class="text-decoration-none">Kembali ke Halaman Utama</a>
            </div>
        </div>
    </div>
</body>
</html>