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

$pageTitle  = 'Program Kerja';
$activePage = 'program_kerja';

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

$where  = ['1=1'];
$params = [];
if ($search) { $where[] = '(nama_program LIKE ? OR deskripsi LIKE ?)'; $params[] = "%$search%"; $params[] = "%$search%"; }
if ($tahun)  { $where[] = 'tahun_ajaran LIKE ?'; $params[] = "%$tahun%"; }
if ($status) { $where[] = 'status = ?'; $params[] = $status; }

$whereStr = implode(' AND ', $where);
$sql = "SELECT p.*, (SELECT COUNT(*) FROM kegiatan_pendampingan k WHERE k.program_kerja_id=p.id) AS jml_kegiatan
        FROM program_kerja p WHERE {$whereStr} ORDER BY waktu_mulai DESC";
$pag = paginate($sql, $params, 15, $page);
$baseUrl = BASE_URL.'/modules/program_kerja/index.php?q='.urlencode($search).'&tahun='.urlencode($tahun).'&status='.urlencode($status);

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

<div class="page-header">
  <div class="page-header-title">
    <h1><i class="bi bi-clipboard2-check me-2 text-primary"></i>Program Kerja</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">Program Kerja</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 Program</a>
  <?php endif; ?>
</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-5"><input type="text" name="q" class="form-control form-control-sm" placeholder="Cari nama program…" value="<?= e($search) ?>"></div>
      <div class="col-sm-2"><input type="text" name="tahun" class="form-control form-control-sm" placeholder="Tahun Ajaran" value="<?= e($tahun) ?>"></div>
      <div class="col-sm-2">
        <select name="status" class="form-select form-select-sm">
          <option value="">Semua Status</option>
          <?php foreach(['Belum Mulai','Berjalan','Selesai'] as $st): ?>
          <option value="<?= $st ?>" <?= $status===$st?'selected':'' ?>><?= $st ?></option>
          <?php endforeach; ?>
        </select>
      </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/program_kerja/index.php" class="btn btn-outline-secondary btn-sm">Reset</a>
      </div>
    </form>
  </div>
</div>

<!-- Cards Grid -->
<div class="row g-3 mb-3">
  <?php if(empty($pag['data'])): ?>
  <div class="col-12"><div class="card"><div class="card-body text-center py-5 text-muted">Tidak ada program kerja</div></div></div>
  <?php else: ?>
  <?php foreach($pag['data'] as $prog):
    $stColor = ['Belum Mulai'=>'secondary','Berjalan'=>'warning','Selesai'=>'success'][$prog['status']] ?? 'secondary';
    $progress = $prog['target_kegiatan'] > 0
      ? min(100, round(($prog['jml_kegiatan'] / $prog['target_kegiatan']) * 100))
      : 0;
  ?>
  <div class="col-md-6 col-xl-4">
    <div class="card h-100">
      <div class="card-body">
        <div class="d-flex justify-content-between align-items-start mb-2">
          <span class="badge bg-<?= $stColor ?>-subtle text-<?= $stColor ?>"><?= e($prog['status']) ?></span>
          <span class="text-muted" style="font-size:.75rem"><?= e($prog['tahun_ajaran']) ?></span>
        </div>
        <h6 class="fw-700 mb-2"><?= e($prog['nama_program']) ?></h6>
        <?php if($prog['deskripsi']): ?>
        <p class="text-muted mb-2" style="font-size:.8rem"><?= e(mb_strimwidth($prog['deskripsi'],0,80,'…')) ?></p>
        <?php endif; ?>
        <div class="row g-1 text-center mb-3">
          <div class="col-4">
            <div class="fw-700 text-primary"><?= $prog['jml_kegiatan'] ?>/<?= $prog['target_kegiatan'] ?></div>
            <div class="text-muted" style="font-size:.7rem">Kegiatan</div>
          </div>
          <div class="col-4">
            <div class="fw-700"><?= formatTanggal($prog['waktu_mulai'],'d M') ?></div>
            <div class="text-muted" style="font-size:.7rem">Mulai</div>
          </div>
          <div class="col-4">
            <div class="fw-700"><?= formatTanggal($prog['waktu_selesai'],'d M') ?></div>
            <div class="text-muted" style="font-size:.7rem">Selesai</div>
          </div>
        </div>
        <!-- Progress bar -->
        <div class="mb-2">
          <div class="d-flex justify-content-between mb-1" style="font-size:.73rem">
            <span class="text-muted">Progres Kegiatan</span>
            <span class="fw-700 text-primary"><?= $progress ?>%</span>
          </div>
          <div class="progress" style="height:6px;border-radius:3px">
            <div class="progress-bar bg-primary" style="width:<?= $progress ?>%;border-radius:3px"></div>
          </div>
        </div>
        <?php if(isAdmin()): ?>
        <div class="d-flex gap-2 mt-3">
          <a href="form.php?id=<?= $prog['id'] ?>" class="btn btn-sm btn-outline-warning flex-1"><i class="bi bi-pencil me-1"></i>Edit</a>
          <a href="delete.php?id=<?= $prog['id'] ?>" class="btn btn-sm btn-outline-danger" data-confirm="Hapus program ini?"><i class="bi bi-trash"></i></a>
        </div>
        <?php endif; ?>
      </div>
    </div>
  </div>
  <?php endforeach; ?>
  <?php endif; ?>
</div>

<?php if($pag['pages']>1): ?>
<div class="d-flex justify-content-center"><?= renderPagination($pag,$baseUrl) ?></div>
<?php endif; ?>

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