<?php
// pengurusan_sijil.php
session_start();
if (!isset($_SESSION['id_admin'])) {
    header('Location: login_admin.php'); exit;
}
require_once 'includes/config.php';
include 'includes/header_admin.php';
include 'includes/sidebar_admin.php';
include 'includes/fpdf.php';
include 'includes/tarikh_my.php';

// Ambil senarai aktiviti utk dropdown
$aktiviti = $conn->query("SELECT id, nama_aktiviti, tarikh_mula, tarikh_tamat FROM aktiviti ORDER BY tarikh_mula DESC");

// Aktiviti dipilih?
$aktiviti_id = isset($_GET['aktiviti_id']) ? (int)$_GET['aktiviti_id'] : 0;
$peserta = null; $butiran_aktiviti = null; $total_hadir = 0;

if ($aktiviti_id > 0) {
    // Butiran aktiviti
    $stmtA = $conn->prepare("SELECT id, nama_aktiviti, penganjur, peringkat, tempat, tarikh_mula, tarikh_tamat 
                             FROM aktiviti WHERE id=? LIMIT 1");
    $stmtA->bind_param("i", $aktiviti_id);
    $stmtA->execute();
    $butiran_aktiviti = $stmtA->get_result()->fetch_assoc();
    $stmtA->close();

    // Peserta: hanya yang HADIR
    $stmt = $conn->prepare("
        SELECT pe.id AS penyertaan_id, pl.id AS pelajar_id, pl.nama, pl.no_ic, pl.program, pe.status_kehadiran
        FROM penyertaan pe
        JOIN pelajar pl ON pe.pelajar_id = pl.id
        WHERE pe.aktiviti_id = ? AND pe.status_kehadiran = 'Hadir'
        ORDER BY pl.nama ASC
    ");
    $stmt->bind_param("i", $aktiviti_id);
    $stmt->execute();
    $peserta = $stmt->get_result();
    $total_hadir = $peserta ? $peserta->num_rows : 0;
    $stmt->close();
}
?>
<style>
.main-content { margin-left:180px; margin-top:100px; padding:20px; }
.card { border-radius:12px; box-shadow:0 6px 18px rgba(0,0,0,.08); border:0; }
.card h5 { margin-bottom: 0; }
.badge-lv { background:#3b0a77; }
.table thead th { background:#3b0a77; color:#fff; }
</style>

<div class="main-content">
  <div class="d-flex align-items-center justify-content-between mb-3">
    <h2 class="mb-0"><i class="bi bi-award-fill"></i> Pengurusan Sijil</h2>
    <div>
      <a class="btn btn-outline-secondary btn-sm" href="dashboard_admin.php"><i class="bi bi-arrow-left"></i> Kembali</a>
    </div>
  </div>

  <!-- Pilih Aktiviti -->
  <div class="card p-4 mb-4">
    <form method="get" class="row g-3 align-items-end">
      <div class="col-md-8">
        <label class="form-label">Pilih Aktiviti</label>
        <select name="aktiviti_id" class="form-select" required onchange="this.form.submit()">
          <option value="">-- Sila Pilih Aktiviti --</option>
          <?php while($a = $aktiviti->fetch_assoc()): ?>
            <option value="<?= $a['id'] ?>"
              <?= ($aktiviti_id == $a['id']) ? 'selected':'' ?>>
              <?= htmlspecialchars($a['nama_aktiviti']) ?> 
              (<?= date('d/m/Y', strtotime($a['tarikh_mula'])) ?> - <?= date('d/m/Y', strtotime($a['tarikh_tamat'])) ?>)
            </option>
          <?php endwhile; ?>
        </select>
      </div>
      <div class="col-md-4">
        <button type="submit" class="btn btn-primary w-100"><i class="bi bi-search"></i> Papar Peserta Hadir</button>
      </div>
    </form>
  </div>

  <?php if ($aktiviti_id > 0 && $butiran_aktiviti): ?>
    <!-- Info Aktiviti -->
    <div class="card p-4 mb-3">
      <div class="row g-3">
        <div class="col-md-6">
          <h5 class="text-primary mb-1"><?= htmlspecialchars($butiran_aktiviti['nama_aktiviti']) ?></h5>
          <div><strong>Penganjur:</strong> <?= htmlspecialchars($butiran_aktiviti['penganjur']) ?></div>
          <div><strong>Peringkat:</strong> <span class="badge badge-lv"><?= htmlspecialchars($butiran_aktiviti['peringkat']) ?></span></div>
        </div>
        <div class="col-md-6">
          <div><strong>Tempat:</strong> <?= htmlspecialchars($butiran_aktiviti['tempat']) ?></div>
          <div><strong>Tarikh:</strong> 
            <?= date('d/m/Y', strtotime($butiran_aktiviti['tarikh_mula'])) ?> - 
            <?= date('d/m/Y', strtotime($butiran_aktiviti['tarikh_tamat'])) ?>
          </div>
        </div>
      </div>
    </div>

    <!-- Senarai Peserta Hadir -->
    <div class="card p-4">
      <div class="d-flex justify-content-between align-items-center mb-3">
        <h5 class="mb-0">Senarai Pelajar Hadir (<?= $total_hadir ?>)</h5>
        <div>
          <?php if ($total_hadir > 0): ?>
            <a href="cetak_sijil_semua.php?aktiviti_id=<?= $aktiviti_id ?>" class="btn btn-success">
              <i class="bi bi-printer"></i> Cetak Semua Sijil
            </a>
          <?php endif; ?>
        </div>
      </div>

      <div class="table-responsive">
        <table class="table table-bordered align-middle">
          <thead>
            <tr>
              <th style="width:55px;">Bil</th>
              <th>Nama Pelajar</th>
              <th>No. IC</th>
              <th>Program</th>
              <th style="width:160px;">Tindakan</th>
            </tr>
          </thead>
          <tbody>
            <?php if ($total_hadir > 0): $bil=1; ?>
              <?php while($row = $peserta->fetch_assoc()): ?>
                <tr>
                  <td><?= $bil++ ?></td>
                  <td><?= htmlspecialchars($row['nama']) ?></td>
                  <td><?= htmlspecialchars($row['no_ic']) ?></td>
                  <td><?= htmlspecialchars($row['program']) ?></td>
                  <td>
                    <a class="btn btn-primary btn-sm"
                       href="sijil_pdf.php?aktiviti_id=<?= $aktiviti_id ?>&pelajar_id=<?= $row['pelajar_id'] ?>">
                       <i class="bi bi-file-earmark-pdf"></i> Cetak Sijil
                    </a>
                  </td>
                </tr>
              <?php endwhile; ?>
            <?php else: ?>
              <tr><td colspan="5" class="text-center">Tiada pelajar yang ditandakan <em>Hadir</em> untuk aktiviti ini.</td></tr>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  <?php elseif ($aktiviti_id > 0): ?>
    <div class="alert alert-warning">Aktiviti tidak ditemui.</div>
  <?php endif; ?>
</div>

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