<?php
require_once '../includes/auth.php';

if (!isPenguji()) {
    redirect('../login.php');
}

$penguji_id = $_SESSION['user_id'];

// Ambil ujian aktif yang tersedia untuk penguji ini
$stmt = $pdo->prepare("
    SELECT DISTINCT u.* 
    FROM ujian u 
    INNER JOIN ujian_stations us ON u.id = us.ujian_id 
    WHERE u.status = 'aktif'
    ORDER BY u.tanggal_ujian DESC
");
$stmt->execute();
$ujian_aktif = $stmt->fetchAll();

// Hitung statistik - PERBAIKAN: gunakan $pdo bukan $penguji
$stmt = $pdo->prepare("SELECT COUNT(*) as total FROM penilaian WHERE penguji_id = ?");
$stmt->execute([$penguji_id]);
$total_penilaian = $stmt->fetch()['total'];

$stmt = $pdo->prepare("
    SELECT COUNT(DISTINCT peserta_id) as total 
    FROM penilaian 
    WHERE penguji_id = ?
");
$stmt->execute([$penguji_id]);
$total_peserta = $stmt->fetch()['total'];

// Ambil penilaian terbaru
$stmt = $pdo->prepare("
    SELECT p.nama as peserta_nama, s.nama_station, n.nilai, n.created_at
    FROM penilaian n
    INNER JOIN peserta p ON n.peserta_id = p.id
    INNER JOIN stations s ON n.station_id = s.id
    WHERE n.penguji_id = ?
    ORDER BY n.created_at DESC
    LIMIT 5
");
$stmt->execute([$penguji_id]);
$penilaian_terbaru = $stmt->fetchAll();
?>

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

<div class="content">
    <h2>Dashboard Penguji</h2>
    
    <!-- Statistik -->
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-clipboard-check"></i>
            </div>
            <div class="stat-info">
                <h3>Total Penilaian</h3>
                <p class="stat-number"><?php echo $total_penilaian; ?></p>
            </div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-users"></i>
            </div>
            <div class="stat-info">
                <h3>Peserta Dinilai</h3>
                <p class="stat-number"><?php echo $total_peserta; ?></p>
            </div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-calendar-alt"></i>
            </div>
            <div class="stat-info">
                <h3>Ujian Aktif</h3>
                <p class="stat-number"><?php echo count($ujian_aktif); ?></p>
            </div>
        </div>
    </div>

    <!-- Ujian Aktif -->
    <div class="card">
        <h3>Ujian Aktif</h3>
        <?php if (empty($ujian_aktif)): ?>
            <div class="alert alert-info">
                <i class="fas fa-info-circle"></i>
                Tidak ada ujian aktif saat ini.
            </div>
        <?php else: ?>
            <div class="ujian-grid">
                <?php foreach ($ujian_aktif as $ujian): ?>
                    <div class="ujian-card">
                        <div class="ujian-header">
                            <h4><?php echo $ujian['nama_ujian']; ?></h4>
                            <span class="ujian-date">
                                <i class="fas fa-calendar"></i>
                                <?php echo formatTanggal($ujian['tanggal_ujian']); ?>
                            </span>
                        </div>
                        
                        <div class="ujian-actions">
                            <a href="penilaian.php?ujian_id=<?php echo $ujian['id']; ?>" class="btn btn-primary">
                                <i class="fas fa-play-circle"></i> Mulai Penilaian
                            </a>
                            <a href="list_stations.php?ujian_id=<?php echo $ujian['id']; ?>" class="btn btn-secondary">
                                <i class="fas fa-list"></i> Lihat Stations
                            </a>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>

    <!-- Penilaian Terbaru -->
    <div class="card">
        <h3>Penilaian Terbaru</h3>
        <?php if (empty($penilaian_terbaru)): ?>
            <div class="alert alert-info">
                <i class="fas fa-info-circle"></i>
                Belum ada penilaian.
            </div>
        <?php else: ?>
            <div class="table-container">
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>Peserta</th>
                            <th>Station</th>
                            <th>Nilai</th>
                            <th>Waktu</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($penilaian_terbaru as $nilai): ?>
                            <tr>
                                <td><?php echo $nilai['peserta_nama']; ?></td>
                                <td><?php echo $nilai['nama_station']; ?></td>
                                <td>
                                    <span class="nilai-badge nilai-<?php echo $nilai['nilai']; ?>">
                                        <?php echo $nilai['nilai']; ?>
                                    </span>
                                </td>
                                <td><?php echo formatTanggal($nilai['created_at']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

<style>
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.stat-card {
    background: white;
    padding: 1.5rem;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    display: flex;
    align-items: center;
    gap: 1rem;
}

.stat-icon {
    background: #3498db;
    color: white;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
}

.stat-info h3 {
    margin: 0 0 0.5rem 0;
    color: #6c757d;
    font-size: 0.9rem;
    font-weight: normal;
}

.stat-number {
    font-size: 2rem;
    font-weight: bold;
    color: #2c3e50;
    margin: 0;
}

.ujian-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 1.5rem;
}

.ujian-card {
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 8px;
    padding: 1.5rem;
    transition: all 0.3s;
}

.ujian-card:hover {
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    transform: translateY(-2px);
}

.ujian-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 1rem;
}

.ujian-header h4 {
    margin: 0;
    color: #2c3e50;
}

.ujian-date {
    background: #e8f4fd;
    color: #004085;
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.8rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.ujian-actions {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.nilai-badge {
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-weight: bold;
    font-size: 0.8rem;
}

.nilai-0 { background: #e74c3c; color: white; }
.nilai-1 { background: #f39c12; color: white; }
.nilai-2 { background: #27ae60; color: white; }

@media (max-width: 768px) {
    .stats-grid {
        grid-template-columns: 1fr;
    }
    
    .ujian-grid {
        grid-template-columns: 1fr;
    }
    
    .ujian-header {
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .ujian-actions {
        flex-direction: column;
    }
}
</style>

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