<?php
/**
 * SisPen Snesaka - Laporan Absensi
 */
require_once '../../includes/functions.php';
requireLogin();

$pageTitle  = 'Laporan Absensi';
$activeMenu = 'absensi';
$pengaturan = getPengaturan();
$ta         = $pengaturan['tahun_ajaran_aktif'];
$sem        = $pengaturan['semester_aktif'];
$isAdmin    = $_SESSION['role'] === 'admin';

$kelasId = intval($_GET['kelas_id'] ?? 0);

if ($isAdmin) {
    $kelasList = db_fetchAll(
        "SELECT * FROM kelas WHERE tahun_ajaran=? AND semester=? ORDER BY tingkat, nama_kelas",
        [$ta, $sem]
    );
} else {
    $guruId    = $_SESSION['guru_id'] ?? 0;
    $kelasList = $guruId ? db_fetchAll(
        "SELECT DISTINCT k.* FROM pengampu p JOIN kelas k ON k.id=p.kelas_id
         WHERE p.guru_id=? AND p.tahun_ajaran=? AND p.semester=? ORDER BY k.tingkat, k.nama_kelas",
        [$guruId, $ta, $sem]
    ) : array();
}

$absensiList   = array();
$selectedKelas = null;

if ($kelasId) {
    $selectedKelas = db_fetch("SELECT * FROM kelas WHERE id=?", [$kelasId]);
    $absensiList   = db_fetchAll("
        SELECT s.nis, s.nama, s.jenis_kelamin,
               COALESCE(a.sakit,0) as sakit,
               COALESCE(a.izin,0)  as izin,
               COALESCE(a.tanpa_keterangan,0) as alpha
        FROM siswa s
        LEFT JOIN absensi a ON a.siswa_id=s.id AND a.kelas_id=? AND a.tahun_ajaran=? AND a.semester=?
        WHERE s.kelas_id=? AND s.status='aktif'
        ORDER BY s.nama
    ", [$kelasId, $ta, $sem, $kelasId]);
}

include '../../templates/header.php';
?>

<div class="card" style="margin-bottom:16px;">
  <div class="card-body" style="padding:14px 20px;">
    <form method="GET" style="display:flex;gap:12px;align-items:flex-end;">
      <div style="min-width:220px;flex:1;">
        <label class="form-label">Kelas</label>
        <select name="kelas_id" class="form-control" onchange="this.form.submit()">
          <option value="">-- Pilih Kelas --</option>
          <?php foreach ($kelasList as $k): ?>
          <option value="<?= $k['id'] ?>" <?= $kelasId==$k['id']?'selected':'' ?>>
            <?= htmlspecialchars($k['nama_kelas']) ?>
          </option>
          <?php endforeach; ?>
        </select>
      </div>
    </form>
  </div>
</div>

<?php if ($kelasId && $selectedKelas && !empty($absensiList)):
  $totS = array_sum(array_column($absensiList,'sakit'));
  $totI = array_sum(array_column($absensiList,'izin'));
  $totA = array_sum(array_column($absensiList,'alpha'));
?>

<!-- Summary cards -->
<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:14px;margin-bottom:16px;">
  <?php foreach ([
    ['fas fa-users','Total Siswa',count($absensiList),'#1a4a7a','#e0f0ff'],
    ['fas fa-thermometer-half','Total Sakit',$totS,'#1d4ed8','#dbeafe'],
    ['fas fa-file-alt','Total Izin',$totI,'#b45309','#fef3c7'],
    ['fas fa-times-circle','Total Alpha',$totA,'#dc2626','#fee2e2'],
  ] as $c): ?>
  <div style="background:<?= $c[4] ?>;border-radius:10px;padding:16px;display:flex;align-items:center;gap:12px;border:1px solid <?= $c[3] ?>22;">
    <div style="width:44px;height:44px;border-radius:10px;background:<?= $c[3] ?>;color:#fff;display:flex;align-items:center;justify-content:center;font-size:18px;flex-shrink:0;">
      <i class="<?= $c[0] ?>"></i>
    </div>
    <div>
      <div style="font-size:11px;color:<?= $c[3] ?>;font-weight:600;opacity:.8;"><?= $c[1] ?></div>
      <div style="font-size:28px;font-weight:800;color:<?= $c[3] ?>;line-height:1.1;"><?= $c[2] ?></div>
    </div>
  </div>
  <?php endforeach; ?>
</div>

<div class="card">
  <div class="card-header">
    <span class="card-title">
      <i class="fas fa-table" style="color:var(--primary);margin-right:8px;"></i>
      Rekap Absensi &mdash; <?= htmlspecialchars($selectedKelas['nama_kelas']) ?>
      <span style="font-size:12px;font-weight:400;color:var(--text-muted);margin-left:8px;">TA <?= $ta ?> Sem <?= $sem ?></span>
    </span>
    <div style="display:flex;gap:8px;">
      <button onclick="window.print()" class="btn btn-accent btn-sm">
        <i class="fas fa-print"></i> Cetak
      </button>
      <a href="input.php?kelas_id=<?= $kelasId ?>" class="btn btn-primary btn-sm">
        <i class="fas fa-edit"></i> Edit
      </a>
    </div>
  </div>
  <div style="overflow-x:auto;">
    <table style="width:100%;border-collapse:collapse;font-size:13px;">
      <thead>
        <tr style="background:var(--primary-dark);color:#fff;">
          <th style="padding:10px 12px;text-align:center;width:36px;">No</th>
          <th style="padding:10px 12px;text-align:left;">Nama Siswa</th>
          <th style="padding:10px 8px;text-align:center;width:80px;">NIS</th>
          <th style="padding:10px 8px;text-align:center;width:70px;background:rgba(59,130,246,.3);">Sakit</th>
          <th style="padding:10px 8px;text-align:center;width:70px;background:rgba(245,158,11,.3);">Izin</th>
          <th style="padding:10px 8px;text-align:center;width:70px;background:rgba(239,68,68,.3);">Alpha</th>
          <th style="padding:10px 8px;text-align:center;width:70px;">Total</th>
          <th style="padding:10px 8px;text-align:center;width:90px;">Keterangan</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($absensiList as $idx => $a):
          $total = $a['sakit'] + $a['izin'] + $a['alpha'];
          $ket   = $total === 0 ? '<span style="color:#16a34a;font-weight:600;">Baik</span>'
                 : ($total <= 3 ? '<span style="color:#d97706;font-weight:600;">Cukup</span>'
                 : '<span style="color:#dc2626;font-weight:600;">Perlu Perhatian</span>');
        ?>
        <tr style="<?= $idx%2===0?'':'background:var(--surface-2);' ?>">
          <td style="padding:8px 12px;text-align:center;color:var(--text-muted);"><?= $idx+1 ?></td>
          <td style="padding:8px 12px;font-weight:600;">
            <?= htmlspecialchars($a['nama']) ?>
            <span class="badge <?= $a['jenis_kelamin']==='L'?'badge-info':'badge-warning' ?>" style="font-size:10px;margin-left:4px;"><?= $a['jenis_kelamin'] ?></span>
          </td>
          <td style="padding:8px 6px;text-align:center;"><code style="font-size:11px;"><?= $a['nis'] ?></code></td>
          <td style="padding:8px 6px;text-align:center;font-weight:700;color:#1d4ed8;"><?= $a['sakit'] ?: '-' ?></td>
          <td style="padding:8px 6px;text-align:center;font-weight:700;color:#b45309;"><?= $a['izin']  ?: '-' ?></td>
          <td style="padding:8px 6px;text-align:center;font-weight:700;color:#dc2626;"><?= $a['alpha'] ?: '-' ?></td>
          <td style="padding:8px 6px;text-align:center;font-weight:800;font-size:15px;"><?= $total ?: '-' ?></td>
          <td style="padding:8px 6px;text-align:center;"><?= $ket ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
      <tfoot>
        <tr style="background:var(--bg);border-top:2px solid var(--border);font-weight:700;">
          <td colspan="3" style="padding:10px 12px;color:var(--text-muted);font-size:12px;">Total</td>
          <td style="padding:10px 6px;text-align:center;color:#1d4ed8;"><?= $totS ?></td>
          <td style="padding:10px 6px;text-align:center;color:#b45309;"><?= $totI ?></td>
          <td style="padding:10px 6px;text-align:center;color:#dc2626;"><?= $totA ?></td>
          <td style="padding:10px 6px;text-align:center;"><?= $totS+$totI+$totA ?></td>
          <td></td>
        </tr>
      </tfoot>
    </table>
  </div>
</div>

<?php else: ?>
<div class="card">
  <div class="card-body" style="text-align:center;padding:48px;color:var(--text-muted);">
    <i class="fas fa-calendar-check" style="font-size:48px;display:block;margin-bottom:14px;opacity:.2;"></i>
    <p><?= $kelasId ? 'Belum ada data absensi untuk kelas ini.' : 'Pilih kelas untuk melihat laporan absensi.' ?></p>
    <?php if ($kelasId): ?>
    <a href="input.php?kelas_id=<?= $kelasId ?>" class="btn btn-primary" style="margin-top:12px;">
      <i class="fas fa-edit"></i> Input Absensi Sekarang
    </a>
    <?php endif; ?>
  </div>
</div>
<?php endif; ?>

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