<?php 
session_start();
require_once '../koneksi.php'; 
include 'layout_header.php';

// 1. Ambil data periode aktif
$q_aktif = mysqli_query($koneksi, "SELECT t.id_tp_tahun, t.tahun_pelajaran, s.id_semester, s.nama_semester 
                                   FROM tahun_pelajaran t, semester s 
                                   WHERE t.status = 'Aktif' AND s.status = 'Aktif' LIMIT 1");
$aktif = mysqli_fetch_array($q_aktif);

$id_tp_aktif = $aktif['id_tp_tahun'] ?? 1;
$id_sem_aktif = $aktif['id_semester'] ?? 1;
$nama_tahun = $aktif['tahun_pelajaran'] ?? 'Belum Set';
$nama_semester = $aktif['nama_semester'] ?? 'Belum Set';

// 2. Filter Status
$status_filter = isset($_GET['status']) ? $_GET['status'] : 'Aktif';
?>

<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h3>Daftar Guru</h3>
        <a href="guru_tambah.php" class="btn btn-success"><i class="fa fa-plus"></i> Tambah Guru</a>
    </div>

    <div class="alert alert-primary shadow-sm">
        <div class="row">
            <div class="col-md-6">Periode: <strong><?= $nama_tahun ?> / <?= $nama_semester ?></strong></div>
        </div>
    </div>

    <div class="mb-3">
        <div class="btn-group">
            <a href="?status=Aktif" class="btn btn-outline-primary <?= ($status_filter=='Aktif')?'active':'' ?>">Aktif</a>
            <a href="?status=Pindah" class="btn btn-outline-warning <?= ($status_filter=='Pindah')?'active':'' ?>">Pindah</a>
            <a href="?status=Non-Aktif" class="btn btn-outline-danger <?= ($status_filter=='Non-Aktif')?'active':'' ?>">Non-Aktif</a>
        </div>
    </div>

    <div class="card shadow-sm p-3">
        <table class="table table-bordered table-hover align-middle">
            <thead class="table-light">
                <tr>
                    <th class="text-center" style="width: 50px;">No</th>
                    <th class="text-center">Foto</th> 
                    <th>Nama & NIP</th>
                    <th>Tugas Mengajar</th>
                    <th class="text-center" style="width: 200px;">Status</th>
                    <th class="text-center" style="width: 150px;">Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                $no = 1;
                $query = "SELECT g.*, 
                    (SELECT GROUP_CONCAT(CONCAT(m.nama_mapel, ' - ', r.kelas) SEPARATOR '<br>') 
                     FROM tugas_guru t 
                     JOIN master_mapel m ON t.id_mapel = m.id_mapel 
                     JOIN rombel r ON t.id_rombel = r.id_rombel 
                     WHERE t.id_guru = g.id_guru AND t.id_semester = '$id_sem_aktif' AND t.id_tp_tahun = '$id_tp_aktif') AS daftar_tugas
                    FROM master_guru g 
                    WHERE g.status = '$status_filter' ORDER BY nama ASC";
                
                $q_guru = mysqli_query($koneksi, $query);

                while($d = mysqli_fetch_array($q_guru)) { 
                    $foto_path = '../assets/img/guru/' . $d['foto'];
                    $foto = (!empty($d['foto']) && file_exists($foto_path)) ? $foto_path : '../assets/img/guru/default.jpg';
                ?>
                <tr>
                    <td class="text-center"><?= $no++ ?></td>
                    <td class="text-center">
                        <img src="<?= $foto ?>" style="width: 50px; height: 50px; object-fit: cover; border-radius: 50%;">
                    </td>
                    <td>
                        <strong><?= htmlspecialchars($d['nama']) ?></strong><br>
                        <small class="text-muted">NIP: <?= htmlspecialchars($d['nip'] ?? '-') ?></small>
                    </td>
                    <td><?= $d['daftar_tugas'] ?? '<em class="text-muted">Tidak ada tugas</em>' ?></td>
                    <td>
                        <form action="aksi_status_guru.php" method="POST">
                            <input type="hidden" name="id_guru" value="<?= $d['id_guru'] ?>">
                            <select name="status" class="form-select form-select-sm" onchange="this.form.submit()">
                                <option value="Aktif" <?= ($d['status'] == 'Aktif') ? 'selected' : '' ?>>Aktif</option>
                                <option value="Pindah" <?= ($d['status'] == 'Pindah') ? 'selected' : '' ?>>Pindah</option>
                                <option value="Non-Aktif" <?= ($d['status'] == 'Non-Aktif') ? 'selected' : '' ?>>Non-Aktif</option>
                            </select>
                            <input type="hidden" name="update_status" value="1">
                        </form>
                    </td>
                    <td class="text-center">
                        <div class="btn-group">
                            <a href="guru_edit.php?id=<?= $d['id_guru'] ?>" class="btn btn-sm btn-info" title="Profil"><i class="fa fa-user"></i></a>
                            <a href="guru_jabatan.php?id=<?= $d['id_guru'] ?>" class="btn btn-sm btn-primary" title="Atur Tugas"><i class="fa fa-briefcase"></i></a>
                            <a href="guru_aksi.php?hapus=<?= $d['id_guru'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin hapus?')"><i class="fa fa-trash"></i></a>
                        </div>
                    </td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
</div>

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