<?php
require_once '../../config/config.php';
require_once '../../config/database.php';
require_once '../../core/helpers.php';
requireLogin();

$pageTitle  = 'Pengembangan Diri';
$activePage = 'pengembangan';

$search = sanitize($_GET['q'] ?? '');
$jenis  = sanitize($_GET['jenis'] ?? '');
$tahun  = sanitize($_GET['tahun'] ?? '');
$page   = max(1,(int)($_GET['page'] ?? 1));

$where  = ['1=1'];
$params = [];
if ($search) { $where[] = '(judul_kegiatan LIKE ? OR penyelenggara LIKE ?)'; $params[] = "%$search%"; $params[] = "%$search%"; }
if ($jenis)  { $where[] = 'jenis_kegiatan = ?'; $params[] = $jenis; }
if ($tahun)  { $where[] = 'YEAR(tanggal_mulai) = ?'; $params[] = $tahun; }

$whereStr = implode(' AND ', $where);
$sql = "SELECT * FROM pengembangan_diri WHERE {$whereStr} ORDER BY tanggal_mulai DESC";
$pag = paginate($sql, $params, 15, $page);
$baseUrl = BASE_URL.'/modules/pengembangan_diri/index.php?q='.urlencode($search).'&jenis='.urlencode($jenis).'&tahun='.urlencode($tahun);

$jenisList = ['Diklat','Seminar','Workshop','Webinar','Bimtek','Lokakarya','Lainnya'];

// Total JP
$totalJP = Database::count("SELECT SUM(jumlah_jp) FROM pengembangan_diri");

require_once ROOT_PATH . '/views/layouts/header.php';
?>

<div class="page-header">
  <div class="page-header-title">
    <h1><i class="bi bi-award me-2 text-primary"></i>Pengembangan Diri</h1>
    <nav><ol class="breadcrumb"><li class="breadcrumb-item"><a href="<?= BASE_URL ?>/modules/dashboard/index.php">Dashboard</a></li><li class="breadcrumb-item active">Pengembangan Diri</li></ol></nav>
  </div>
  <?php if(isAdmin()): ?>
  <a href="form.php" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i>Tambah Kegiatan</a>
  <?php endif; ?>
</div>

<!-- Stat -->
<div class="row g-3 mb-3">
  <div class="col-6 col-md-3">
    <div class="card text-center p-3">
      <div class="fw-800 fs-3 text-primary"><?= $pag['total'] ?></div>
      <div class="text-muted small">Total Kegiatan</div>
    </div>
  </div>
  <div class="col-6 col-md-3">
    <div class="card text-center p-3">
      <div class="fw-800 fs-3 text-success"><?= $totalJP ?></div>
      <div class="text-muted small">Total Jam Pelajaran</div>
    </div>
  </div>
</div>

<!-- Filter -->
<div class="card mb-3">
  <div class="card-body py-2">
    <form method="GET" class="row g-2 align-items-end">
      <div class="col-sm-4"><input type="text" name="q" class="form-control form-control-sm" placeholder="Cari judul, penyelenggara…" value="<?= e($search) ?>"></div>
      <div class="col-sm-3">
        <select name="jenis" class="form-select form-select-sm">
          <option value="">Semua Jenis</option>
          <?php foreach($jenisList as $j): ?>
          <option value="<?= $j ?>" <?= $jenis===$j?'selected':'' ?>><?= $j ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-sm-2"><input type="number" name="tahun" class="form-control form-control-sm" placeholder="Tahun" value="<?= e($tahun) ?>" min="2015" max="2030"></div>
      <div class="col-auto d-flex gap-2">
        <button type="submit" class="btn btn-primary btn-sm"><i class="bi bi-search"></i></button>
        <a href="<?= BASE_URL ?>/modules/pengembangan_diri/index.php" class="btn btn-outline-secondary btn-sm">Reset</a>
      </div>
    </form>
  </div>
</div>

<div class="card">
  <div class="card-header">
    <span class="card-header-title"><i class="bi bi-award me-2"></i>Daftar Pengembangan Diri</span>
    <span class="badge bg-primary-subtle text-primary"><?= $pag['total'] ?> kegiatan</span>
  </div>
  <div class="table-responsive">
    <table class="table table-hover mb-0">
      <thead>
        <tr><th>#</th><th>Judul Kegiatan</th><th>Jenis</th><th>Penyelenggara</th><th>Pelaksanaan</th><th>JP</th><th>No. Sertifikat</th><th>Aksi</th></tr>
      </thead>
      <tbody>
        <?php if(empty($pag['data'])): ?>
        <tr><td colspan="8" class="text-center py-4 text-muted">Tidak ada data</td></tr>
        <?php else: ?>
        <?php $no=($pag['current']-1)*$pag['per_page']+1; ?>
        <?php foreach($pag['data'] as $row): ?>
        <tr>
          <td class="text-muted"><?= $no++ ?></td>
          <td>
            <div class="fw-600" style="font-size:.87rem"><?= e(mb_strimwidth($row['judul_kegiatan'],0,45,'…')) ?></div>
            <?php if($row['tempat']): ?><div class="text-muted" style="font-size:.74rem"><i class="bi bi-geo-alt me-1"></i><?= e($row['tempat']) ?></div><?php endif; ?>
          </td>
          <td><span class="badge bg-primary-subtle text-primary"><?= e($row['jenis_kegiatan']) ?></span></td>
          <td style="font-size:.84rem"><?= e(mb_strimwidth($row['penyelenggara'],0,30,'…')) ?></td>
          <td style="white-space:nowrap;font-size:.82rem">
            <?= formatTanggal($row['tanggal_mulai'],'d M Y') ?>
            <?php if($row['tanggal_mulai'] !== $row['tanggal_selesai']): ?>
            <br><span class="text-muted">s/d <?= formatTanggal($row['tanggal_selesai'],'d M Y') ?></span>
            <?php endif; ?>
          </td>
          <td class="text-center fw-700"><?= $row['jumlah_jp'] ?? '—' ?></td>
          <td style="font-size:.8rem"><?= $row['nomor_sertifikat'] ? e($row['nomor_sertifikat']) : '—' ?></td>
          <td>
            <div class="d-flex gap-1">
              <a href="detail.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-outline-info" title="Detail"><i class="bi bi-eye"></i></a>
              <?php if($row['sertifikat_file']): ?>
              <a href="<?= BASE_URL ?>/uploads/sertifikat/<?= e($row['sertifikat_file']) ?>" target="_blank" class="btn btn-sm btn-outline-success" title="Sertifikat"><i class="bi bi-patch-check"></i></a>
              <?php endif; ?>
              <?php if(isAdmin()): ?>
              <a href="form.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-outline-warning" title="Edit"><i class="bi bi-pencil"></i></a>
              <a href="delete.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-outline-danger" data-confirm="Hapus data ini?"><i class="bi bi-trash"></i></a>
              <?php endif; ?>
            </div>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
  <?php if($pag['pages']>1): ?>
  <div class="card-footer d-flex justify-content-between align-items-center flex-wrap gap-2">
    <span class="text-muted small">Menampilkan <?= count($pag['data']) ?> dari <?= $pag['total'] ?> data</span>
    <?= renderPagination($pag,$baseUrl) ?>
  </div>
  <?php endif; ?>
</div>

<?php require_once ROOT_PATH . '/views/layouts/footer.php'; ?>
