<?php 
include 'koneksi.php';
cek_akses(['Guru']);

$user_id = $_SESSION['user_id'];

// ========== AMBIL DATA DARI TABEL NILAI_SISWA ==========

// Total siswa
$total_siswa = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM students WHERE unit='{$_SESSION['unit']}'"))['total'];

// Total jurnal
$total_jurnal = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM teaching_journals WHERE teacher_id='$user_id'"))['total'];

// Total nilai (dari tabel nilai_siswa)
$total_nilai = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM nilai_siswa WHERE teacher_id='$user_id'"))['total'];

// Total absensi
$total_absensi = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM attendance WHERE teacher_id='$user_id'"))['total'];

// Total supervisi
$total_supervisi = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM supervisions WHERE teacher_id='$user_id'"))['total'];

// Ambil data nilai per siswa (dari tabel nilai_siswa)
$nilai_siswa = mysqli_query($conn, "SELECT n.*, s.name as student_name, c.name as class_name 
    FROM nilai_siswa n 
    JOIN students s ON n.student_id = s.id 
    JOIN classes c ON n.class_id = c.id 
    WHERE n.teacher_id = '$user_id' 
    ORDER BY c.name, s.name");

// Ambil data jurnal terbaru
$jurnal_terbaru = mysqli_query($conn, "SELECT tj.*, c.name as class_name 
    FROM teaching_journals tj 
    JOIN classes c ON tj.class_id = c.id 
    WHERE tj.teacher_id = '$user_id' 
    ORDER BY tj.date DESC LIMIT 5");

// Statistik per jenis nilai
$stat_tugas = mysqli_fetch_assoc(mysqli_query($conn, "SELECT AVG(tugas) as rata, COUNT(tugas) as jumlah FROM nilai_siswa WHERE teacher_id='$user_id' AND tugas IS NOT NULL"));
$stat_ulangan = mysqli_fetch_assoc(mysqli_query($conn, "SELECT AVG(ulangan_harian) as rata, COUNT(ulangan_harian) as jumlah FROM nilai_siswa WHERE teacher_id='$user_id' AND ulangan_harian IS NOT NULL"));
$stat_uts = mysqli_fetch_assoc(mysqli_query($conn, "SELECT AVG(uts) as rata, COUNT(uts) as jumlah FROM nilai_siswa WHERE teacher_id='$user_id' AND uts IS NOT NULL"));
$stat_uas = mysqli_fetch_assoc(mysqli_query($conn, "SELECT AVG(uas) as rata, COUNT(uas) as jumlah FROM nilai_siswa WHERE teacher_id='$user_id' AND uas IS NOT NULL"));
$stat_praktik = mysqli_fetch_assoc(mysqli_query($conn, "SELECT AVG(praktik) as rata, COUNT(praktik) as jumlah FROM nilai_siswa WHERE teacher_id='$user_id' AND praktik IS NOT NULL"));
?>

<!DOCTYPE html>
<html>
<head>
    <title>Rekap Data Guru - SIM ISTIQLAL</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/css/adminlte.min.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="assets/css/theme-istiqlal-green.css">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        .content-wrapper { background: #f1f5f9; }
        .card { border-radius: 20px; box-shadow: 0 5px 20px rgba(0,0,0,0.08); margin-bottom: 25px; border: none; }
        .card-header { border-radius: 20px 20px 0 0; padding: 12px 20px; background: linear-gradient(135deg, #0b5e4e 0%, #10b981 100%); color: white; }
        .card-header h4 { margin: 0; font-size: 16px; font-weight: 600; }
        .table thead th { background: linear-gradient(135deg, #0b5e4e 0%, #10b981 100%); color: white; font-weight: 600; padding: 10px; font-size: 12px; }
        .stats-card { background: white; border-radius: 16px; padding: 20px; text-align: center; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
        .stats-number { font-size: 28px; font-weight: 800; color: #0b5e4e; }
        .stats-label { font-size: 12px; color: #64748b; margin-top: 5px; }
        .table-responsive { overflow-x: auto; }
        .badge-nilai { font-size: 11px; padding: 3px 8px; border-radius: 12px; }
    </style>
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">
    <?php include 'sidebar.php'; ?>
    
    <div class="content-wrapper">
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0" style="font-size: 20px;">
                            <i class="fas fa-chart-pie text-primary"></i> Rekap Data Guru
                        </h1>
                        <small class="text-muted">Ringkasan aktivitas mengajar Anda</small>
                    </div>
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
                            <li class="breadcrumb-item active">Rekap Data</li>
                        </ol>
                    </div>
                </div>
            </div>
        </div>

        <div class="content">
            <div class="container-fluid">
                <!-- Statistik Cards -->
                <div class="row">
                    <div class="col-md-2 col-6">
                        <div class="stats-card">
                            <div class="stats-number"><?= $total_siswa ?></div>
                            <div class="stats-label">Total Siswa</div>
                        </div>
                    </div>
                    <div class="col-md-2 col-6">
                        <div class="stats-card">
                            <div class="stats-number"><?= $total_jurnal ?></div>
                            <div class="stats-label">Jurnal</div>
                        </div>
                    </div>
                    <div class="col-md-2 col-6">
                        <div class="stats-card">
                            <div class="stats-number"><?= $total_nilai ?></div>
                            <div class="stats-label">Nilai</div>
                        </div>
                    </div>
                    <div class="col-md-2 col-6">
                        <div class="stats-card">
                            <div class="stats-number"><?= $total_absensi ?></div>
                            <div class="stats-label">Absensi</div>
                        </div>
                    </div>
                    <div class="col-md-2 col-6">
                        <div class="stats-card">
                            <div class="stats-number"><?= $total_supervisi ?></div>
                            <div class="stats-label">Supervisi</div>
                        </div>
                    </div>
                </div>

                <!-- Statistik Nilai per Jenis -->
                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header">
                                <h4><i class="fas fa-star"></i> Statistik Nilai</h4>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-2 col-6">
                                        <div class="stats-card" style="border-left: 3px solid #10b981;">
                                            <div class="stats-number" style="font-size: 20px;"><?= $stat_tugas['jumlah'] ?? 0 ?></div>
                                            <div class="stats-label">Tugas</div>
                                            <small class="text-muted">Rata-rata: <?= round($stat_tugas['rata'] ?? 0, 2) ?></small>
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-6">
                                        <div class="stats-card" style="border-left: 3px solid #3b82f6;">
                                            <div class="stats-number" style="font-size: 20px;"><?= $stat_ulangan['jumlah'] ?? 0 ?></div>
                                            <div class="stats-label">Ulangan</div>
                                            <small class="text-muted">Rata-rata: <?= round($stat_ulangan['rata'] ?? 0, 2) ?></small>
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-6">
                                        <div class="stats-card" style="border-left: 3px solid #f59e0b;">
                                            <div class="stats-number" style="font-size: 20px;"><?= $stat_uts['jumlah'] ?? 0 ?></div>
                                            <div class="stats-label">UTS</div>
                                            <small class="text-muted">Rata-rata: <?= round($stat_uts['rata'] ?? 0, 2) ?></small>
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-6">
                                        <div class="stats-card" style="border-left: 3px solid #ef4444;">
                                            <div class="stats-number" style="font-size: 20px;"><?= $stat_uas['jumlah'] ?? 0 ?></div>
                                            <div class="stats-label">UAS</div>
                                            <small class="text-muted">Rata-rata: <?= round($stat_uas['rata'] ?? 0, 2) ?></small>
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-6">
                                        <div class="stats-card" style="border-left: 3px solid #8b5cf6;">
                                            <div class="stats-number" style="font-size: 20px;"><?= $stat_praktik['jumlah'] ?? 0 ?></div>
                                            <div class="stats-label">Praktik</div>
                                            <small class="text-muted">Rata-rata: <?= round($stat_praktik['rata'] ?? 0, 2) ?></small>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Daftar Nilai Siswa -->
                <div class="card">
                    <div class="card-header">
                        <h4><i class="fas fa-list"></i> Daftar Nilai Siswa</h4>
                    </div>
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-bordered mb-0">
                                <thead>
                                    <tr>
                                        <th>No</th>
                                        <th>Siswa</th>
                                        <th>Kelas</th>
                                        <th>Mapel</th>
                                        <th>Tugas</th>
                                        <th>Ulangan</th>
                                        <th>UTS</th>
                                        <th>UAS</th>
                                        <th>Praktik</th>
                                        <th>Rata-rata</th>
                                        <th>Grade</th>
                                     </div>
                                </thead>
                                <tbody>
                                    <?php if(mysqli_num_rows($nilai_siswa) > 0): ?>
                                        <?php $no=1; while($row = mysqli_fetch_assoc($nilai_siswa)): 
                                            $rata = 0;
                                            $count = 0;
                                            if($row['tugas']) { $rata += $row['tugas']; $count++; }
                                            if($row['ulangan_harian']) { $rata += $row['ulangan_harian']; $count++; }
                                            if($row['uts']) { $rata += $row['uts']; $count++; }
                                            if($row['uas']) { $rata += $row['uas']; $count++; }
                                            if($row['praktik']) { $rata += $row['praktik']; $count++; }
                                            $rata = $count > 0 ? round($rata / $count, 2) : 0;
                                            
                                            $grade = '';
                                            $grade_class = '';
                                            if($rata >= 85) { $grade = 'A'; $grade_class = 'success'; }
                                            elseif($rata >= 75) { $grade = 'B'; $grade_class = 'info'; }
                                            elseif($rata >= 65) { $grade = 'C'; $grade_class = 'warning'; }
                                            elseif($rata > 0) { $grade = 'D'; $grade_class = 'danger'; }
                                            else { $grade = '-'; $grade_class = 'secondary'; }
                                        ?>
                                        <tr>
                                            <td class="text-center"><?= $no++ ?> </div>
                                            <td><?= htmlspecialchars($row['student_name']) ?> </div>
                                            <td><?= htmlspecialchars($row['class_name']) ?> </div>
                                            <td><?= htmlspecialchars($row['mata_pelajaran']) ?> </div>
                                            <td class="text-center"><?= $row['tugas'] ?? '-' ?> </div>
                                            <td class="text-center"><?= $row['ulangan_harian'] ?? '-' ?> </div>
                                            <td class="text-center"><?= $row['uts'] ?? '-' ?> </div>
                                            <td class="text-center"><?= $row['uas'] ?? '-' ?> </div>
                                            <td class="text-center"><?= $row['praktik'] ?? '-' ?> </div>
                                            <td class="text-center"><strong><?= $rata ?></strong> </div>
                                            <td class="text-center"><span class="badge badge-nilai bg-<?= $grade_class ?>"><?= $grade ?></span> </div>
                                         </div>
                                        <?php endwhile; ?>
                                    <?php else: ?>
                                        <td><td colspan="11" class="text-center">Belum ada data nilai</div>
                                    <?php endif; ?>
                                </tbody>
                             </div>
                        </div>
                    </div>
                </div>

                <!-- Jurnal Terbaru -->
                <div class="card">
                    <div class="card-header">
                        <h4><i class="fas fa-history"></i> Jurnal Terbaru</h4>
                    </div>
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-bordered mb-0">
                                <thead>
                                    <tr>
                                        <th>Tanggal</th>
                                        <th>Kelas</th>
                                        <th>Mapel</th>
                                        <th>Materi</th>
                                     </div>
                                </thead>
                                <tbody>
                                    <?php if(mysqli_num_rows($jurnal_terbaru) > 0): ?>
                                        <?php while($row = mysqli_fetch_assoc($jurnal_terbaru)): ?>
                                        <tr>
                                            <td><?= date('d-m-Y', strtotime($row['date'])) ?> </div>
                                            <td><?= htmlspecialchars($row['class_name']) ?> </div>
                                            <td><?= htmlspecialchars($row['subject']) ?> </div>
                                            <td><?= substr(htmlspecialchars($row['material']), 0, 50) ?>...</div>
                                         </div>
                                        <?php endwhile; ?>
                                    <?php else: ?>
                                        <td><td colspan="4" class="text-center">Belum ada jurnal</div>
                                    <?php endif; ?>
                                </tbody>
                             </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <footer class="main-footer">
        <strong>&copy; <?= date('Y') ?> SIM ISTIQLAL - Sistem Administrasi Guru</strong>
    </footer>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/js/adminlte.min.js"></script>
</body>
</html>