<?php 
include 'koneksi.php';
cek_akses(['Kepala', 'Pimpinan', 'Super Admin']);

$guru_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if($guru_id == 0) {
    header("Location: monitoring.php");
    exit();
}

// Ambil data guru
$query_guru = mysqli_query($conn, "SELECT full_name, unit FROM users WHERE id = $guru_id AND role = 'Guru'");
$guru = mysqli_fetch_assoc($query_guru);

if(!$guru) {
    echo "<script>alert('Guru tidak ditemukan!'); window.location='monitoring.php';</script>";
    exit();
}

// Ambil jurnal guru
$query_jurnal = 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 = $guru_id 
    ORDER BY tj.date DESC LIMIT 20");

$jurnal_list = [];
while($row = mysqli_fetch_assoc($query_jurnal)) {
    $jurnal_list[] = $row;
}

// Ambil nilai guru (dari tabel nilai_siswa)
$query_nilai = mysqli_query($conn, "SELECT n.*, c.name as class_name, s.name as student_name 
    FROM nilai_siswa n 
    JOIN classes c ON n.class_id = c.id 
    JOIN students s ON n.student_id = s.id 
    WHERE n.teacher_id = $guru_id 
    ORDER BY n.updated_at DESC LIMIT 20");

$nilai_list = [];
while($row = mysqli_fetch_assoc($query_nilai)) {
    $nilai_list[] = $row;
}

// Ambil absensi guru
$query_absensi = mysqli_query($conn, "SELECT a.*, c.name as class_name, s.name as student_name 
    FROM attendance a 
    JOIN classes c ON a.class_id = c.id 
    JOIN students s ON a.student_id = s.id 
    WHERE a.teacher_id = $guru_id 
    ORDER BY a.date DESC LIMIT 20");

$absensi_list = [];
while($row = mysqli_fetch_assoc($query_absensi)) {
    $absensi_list[] = $row;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Detail 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">
    <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; }
        .table tbody td { padding: 8px 10px; vertical-align: middle; font-size: 12px; }
        .nav-tabs .nav-link { color: #0b5e4e; font-weight: 500; }
        .nav-tabs .nav-link.active { color: #0b5e4e; border-bottom: 2px solid #0b5e4e; font-weight: 600; }
        .info-card { background: white; border-radius: 16px; padding: 20px; margin-bottom: 20px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
        .link-jurnal { color: #0b5e4e; text-decoration: none; }
        .link-jurnal:hover { text-decoration: underline; color: #10b981; }
        .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-user-circle text-primary"></i> Detail Aktivitas Guru
                        </h1>
                    </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"><a href="monitoring.php">Monitoring</a></li>
                            <li class="breadcrumb-item active">Detail Guru</li>
                        </ol>
                    </div>
                </div>
            </div>
        </div>

        <div class="content">
            <div class="container-fluid">
                <!-- Info Guru -->
                <div class="info-card">
                    <div class="d-flex align-items-center">
                        <div class="me-3">
                            <i class="fas fa-user-circle fa-4x text-success"></i>
                        </div>
                        <div>
                            <h3 class="mb-0"><?= htmlspecialchars($guru['full_name']) ?></h3>
                            <p class="text-muted mb-0">Unit: <?= htmlspecialchars($guru['unit']) ?></p>
                        </div>
                        <div class="ms-auto">
                            <a href="monitoring.php" class="btn btn-secondary">
                                <i class="fas fa-arrow-left"></i> Kembali
                            </a>
                        </div>
                    </div>
                </div>

                <!-- Tab Navigasi -->
                <ul class="nav nav-tabs mb-4" id="detailTab" role="tablist">
                    <li class="nav-item" role="presentation">
                        <button class="nav-link active" data-bs-toggle="tab" data-bs-target="#tabJurnal" type="button" role="tab">
                            <i class="fas fa-book"></i> Jurnal (<?= count($jurnal_list) ?>)
                        </button>
                    </li>
                    <li class="nav-item" role="presentation">
                        <button class="nav-link" data-bs-toggle="tab" data-bs-target="#tabNilai" type="button" role="tab">
                            <i class="fas fa-star"></i> Nilai (<?= count($nilai_list) ?>)
                        </button>
                    </li>
                    <li class="nav-item" role="presentation">
                        <button class="nav-link" data-bs-toggle="tab" data-bs-target="#tabAbsensi" type="button" role="tab">
                            <i class="fas fa-user-check"></i> Absensi (<?= count($absensi_list) ?>)
                        </button>
                    </li>
                </ul>

                <div class="tab-content">
                    <!-- Tab Jurnal -->
                    <div class="tab-pane fade show active" id="tabJurnal" role="tabpanel">
                        <div class="card">
                            <div class="card-header">
                                <h4><i class="fas fa-book"></i> Jurnal Mengajar</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>
                                                <th>Jenis Materi</th>
                                                <th>Link</th>
                                                <th>Target</th>
                                             </div>
                                        </thead>
                                        <tbody>
                                            <?php if(count($jurnal_list) > 0): ?>
                                                <?php foreach($jurnal_list as $row): ?>
                                                <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>
                                                    <td><span class="badge bg-info"><?= htmlspecialchars($row['jenis_materi']) ?></span> </div>
                                                    <td class="text-center">
                                                        <?php if(!empty($row['link_url'])): ?>
                                                            <a href="<?= $row['link_url'] ?>" target="_blank" class="link-jurnal">
                                                                <i class="fas fa-external-link-alt"></i> Buka
                                                            </a>
                                                        <?php else: ?>
                                                            <span class="text-muted">-</span>
                                                        <?php endif; ?>
                                                     </div>
                                                    <td><?= substr(htmlspecialchars($row['keterangan_target']), 0, 30) ?>...</div>
                                                 </div>
                                                <?php endforeach; ?>
                                            <?php else: ?>
                                                <td><td colspan="7" class="text-center">Belum ada data jurnal</div>
                                            <?php endif; ?>
                                        </tbody>
                                     </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Tab Nilai -->
                    <div class="tab-pane fade" id="tabNilai" role="tabpanel">
                        <div class="card">
                            <div class="card-header">
                                <h4><i class="fas fa-star"></i> Nilai Siswa</h4>
                            </div>
                            <div class="card-body p-0">
                                <div class="table-responsive">
                                    <table class="table table-bordered mb-0">
                                        <thead>
                                            <tr>
                                                <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>
                                             </div>
                                        </thead>
                                        <tbody>
                                            <?php if(count($nilai_list) > 0): ?>
                                                <?php foreach($nilai_list as $row): 
                                                    $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;
                                                ?>
                                                <tr>
                                                    <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>
                                                 </div>
                                                <?php endforeach; ?>
                                            <?php else: ?>
                                                <td><td colspan="9" class="text-center">Belum ada data nilai</div>
                                            <?php endif; ?>
                                        </tbody>
                                     </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Tab Absensi -->
                    <div class="tab-pane fade" id="tabAbsensi" role="tabpanel">
                        <div class="card">
                            <div class="card-header">
                                <h4><i class="fas fa-user-check"></i> Absensi Siswa</h4>
                            </div>
                            <div class="card-body p-0">
                                <div class="table-responsive">
                                    <table class="table table-bordered mb-0">
                                        <thead>
                                            <tr>
                                                <th>Siswa</th>
                                                <th>Kelas</th>
                                                <th>Tanggal</th>
                                                <th>Status</th>
                                             </div>
                                        </thead>
                                        <tbody>
                                            <?php if(count($absensi_list) > 0): ?>
                                                <?php foreach($absensi_list as $row): 
                                                    $status_class = 'secondary';
                                                    if($row['status'] == 'Hadir') $status_class = 'success';
                                                    elseif($row['status'] == 'Sakit') $status_class = 'warning';
                                                    elseif($row['status'] == 'Izin') $status_class = 'info';
                                                    elseif($row['status'] == 'Alfa') $status_class = 'danger';
                                                ?>
                                                <tr>
                                                    <td><?= htmlspecialchars($row['student_name']) ?> </div>
                                                    <td><?= htmlspecialchars($row['class_name']) ?> </div>
                                                    <td><?= date('d-m-Y', strtotime($row['date'])) ?> </div>
                                                    <td><span class="badge bg-<?= $status_class ?>"><?= $row['status'] ?></span> </div>
                                                 </div>
                                                <?php endforeach; ?>
                                            <?php else: ?>
                                                <td><td colspan="4" class="text-center">Belum ada data absensi</div>
                                            <?php endif; ?>
                                        </tbody>
                                     </div>
                                </div>
                            </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>