<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<style>
@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap');
:root {
  --teal-900:#0d3d38;--teal-800:#0b5a52;--teal-700:#0b7a6e;
  --teal-600:#0d9488;--teal-500:#14b8a6;--teal-400:#2dd4bf;
  --teal-100:#ccfbf1;--teal-50:#f0fdfa;
  --green-500:#10b981;--amber-500:#f59e0b;--amber-100:#fef3c7;
  --red-500:#ef4444;--blue-500:#3b82f6;
  --slate-50:#f8fafc;--slate-100:#f1f5f9;--slate-200:#e2e8f0;
  --slate-400:#94a3b8;--slate-500:#64748b;--slate-700:#334155;--slate-900:#0f172a;
  --card-shadow:0 1px 3px rgba(0,0,0,.06),0 4px 20px rgba(0,0,0,.06);
}
.nt-wrap { font-family:'Plus Jakarta Sans',system-ui,sans-serif; color:var(--slate-900); }

/* Page header */
.ar-page-header{display:flex;justify-content:space-between;align-items:flex-start;flex-wrap:wrap;gap:14px;margin-bottom:24px;}
.ar-header-left h4{font-size:1.25rem;font-weight:800;color:var(--teal-800);margin:0 0 5px;display:flex;align-items:center;gap:11px;}
.ar-header-left h4 .hi{width:40px;height:40px;border-radius:11px;background:linear-gradient(135deg,var(--teal-600),var(--teal-500));display:flex;align-items:center;justify-content:center;color:#fff;font-size:1.05rem;box-shadow:0 4px 12px rgba(13,148,136,.32);flex-shrink:0;}
.ar-breadcrumb{display:flex;align-items:center;gap:6px;font-size:.77rem;color:var(--slate-400);padding-left:51px;}
.ar-breadcrumb a{color:var(--teal-600);text-decoration:none;font-weight:600;}
.ar-breadcrumb .sep{color:var(--slate-200);}
.btn-tambah{display:inline-flex;align-items:center;gap:7px;padding:10px 24px;border-radius:50px;border:none;background:linear-gradient(135deg,var(--teal-600),var(--teal-500));color:#fff;font-family:'Plus Jakarta Sans',sans-serif;font-size:.85rem;font-weight:700;text-decoration:none;cursor:pointer;white-space:nowrap;box-shadow:0 4px 12px rgba(13,148,136,.28);transition:all .2s;}
.btn-tambah:hover{transform:translateY(-2px);box-shadow:0 8px 20px rgba(13,148,136,.38);color:#fff;}

/* Stat cards */
.nt-stat-grid{display:grid;grid-template-columns:repeat(4,1fr);gap:16px;margin-bottom:20px;animation:fadeUp .35s ease both;}
.nt-stat-card{border-radius:14px;padding:18px 16px;position:relative;overflow:hidden;color:#fff;transition:transform .2s;}
.nt-stat-card:hover{transform:translateY(-3px);}
.nt-stat-card::before{content:'';position:absolute;top:-24px;right:-24px;width:110px;height:110px;border-radius:50%;background:rgba(255,255,255,.1);}
.nt-stat-card .sc-label{font-size:.7rem;font-weight:700;opacity:.85;text-transform:uppercase;letter-spacing:.06em;margin-bottom:6px;position:relative;z-index:1;}
.nt-stat-card .sc-value{font-size:1.45rem;font-weight:800;position:relative;z-index:1;}
.nt-stat-card .sc-icon{position:absolute;right:14px;top:50%;transform:translateY(-50%);width:46px;height:46px;background:rgba(255,255,255,.18);border-radius:12px;display:flex;align-items:center;justify-content:center;font-size:1.25rem;z-index:1;}
.sc-teal{background:linear-gradient(135deg,var(--teal-800),var(--teal-600));box-shadow:0 8px 24px rgba(13,148,136,.28);}
.sc-green{background:linear-gradient(135deg,#059669,var(--green-500));box-shadow:0 8px 24px rgba(16,185,129,.28);}
.sc-amber{background:linear-gradient(135deg,#d97706,var(--amber-500));box-shadow:0 8px 24px rgba(245,158,11,.28);}
.sc-blue{background:linear-gradient(135deg,#1d4ed8,var(--blue-500));box-shadow:0 8px 24px rgba(59,130,246,.28);}

/* Filter */
.nt-filter-card{background:#fff;border-radius:14px;border:1px solid var(--slate-200);box-shadow:var(--card-shadow);margin-bottom:20px;overflow:hidden;animation:fadeUp .4s .1s ease both;}
.nt-filter-header{display:flex;justify-content:space-between;align-items:center;padding:13px 20px;background:linear-gradient(90deg,var(--teal-50),#fff);border-bottom:1px solid rgba(13,148,136,.1);}
.nt-filter-header h5{margin:0;font-size:.9rem;font-weight:800;color:var(--slate-900);display:flex;align-items:center;gap:8px;}
.nt-filter-header h5 i{color:var(--teal-600);}
.filter-pill{background:var(--amber-500);color:#fff;font-size:.72rem;font-weight:800;padding:2px 9px;border-radius:20px;}
.nt-filter-body{padding:16px 20px;}

/* List */
.nt-list{animation:fadeUp .4s .2s ease both;}
.nt-item{background:#fff;border-radius:14px;border:1.5px solid var(--slate-200);box-shadow:var(--card-shadow);margin-bottom:14px;overflow:hidden;transition:all .2s;}
.nt-item:hover{border-color:var(--teal-400);box-shadow:0 8px 28px rgba(13,148,136,.1);transform:translateY(-2px);}
.nt-item-inner{display:flex;align-items:stretch;}
.nt-accent{width:5px;flex-shrink:0;}
.accent-draft{background:var(--slate-400);}
.accent-menunggu{background:var(--amber-500);}
.accent-final{background:var(--green-500);}
.nt-body{flex:1;padding:16px 18px;}
.nt-top{display:flex;justify-content:space-between;align-items:flex-start;margin-bottom:10px;gap:12px;}
.nt-title{font-size:.95rem;font-weight:800;color:var(--slate-900);margin-bottom:4px;}
.nt-meta{display:flex;flex-wrap:wrap;align-items:center;gap:10px;font-size:.77rem;color:var(--slate-500);}
.nt-meta i{color:var(--teal-600);}
.nt-meta .sep{color:var(--slate-300);}
.nt-badges{display:flex;align-items:center;gap:6px;flex-shrink:0;flex-wrap:wrap;}
.badge{padding:3px 10px;border-radius:20px;font-size:.7rem;font-weight:700;display:inline-flex;align-items:center;gap:4px;}
.badge-draft{background:var(--slate-100);color:var(--slate-600);}
.badge-menunggu{background:var(--amber-100);color:#92400e;}
.badge-final{background:#dcfce7;color:#166534;}
.badge-tim{background:var(--teal-100);color:var(--teal-800);}
.nt-poin-chips{display:flex;flex-wrap:wrap;gap:5px;margin-top:8px;}
.poin-chip{background:var(--teal-50);color:var(--teal-700);border:1px solid var(--teal-100);padding:3px 9px;border-radius:20px;font-size:.7rem;font-weight:600;}
.nt-bottom{display:flex;justify-content:space-between;align-items:center;padding-top:10px;border-top:1px solid var(--slate-100);margin-top:10px;}
.nt-actions{display:flex;gap:6px;}
.btn-sm-icon{width:30px;height:30px;border-radius:8px;border:1.5px solid var(--slate-200);background:#fff;color:var(--slate-500);display:flex;align-items:center;justify-content:center;font-size:.8rem;cursor:pointer;transition:all .15s;text-decoration:none;}
.btn-sm-icon:hover{border-color:var(--teal-400);color:var(--teal-600);background:var(--teal-50);}
.btn-sm-icon.red:hover{border-color:#fca5a5;color:var(--red-500);background:#fff5f5;}
.btn-sm-icon.pdf{border-color:#fca5a5;color:var(--red-500);}
.btn-sm-icon.pdf:hover{background:#fff5f5;}
.nt-hadir{display:flex;align-items:center;gap:6px;font-size:.77rem;color:var(--slate-500);}

/* Empty */
.nt-empty{background:#fff;border-radius:14px;border:1px solid var(--slate-200);box-shadow:var(--card-shadow);padding:56px 20px;text-align:center;}
.nt-empty-icon{width:80px;height:80px;border-radius:20px;background:var(--slate-100);color:var(--slate-400);display:flex;align-items:center;justify-content:center;font-size:2rem;margin:0 auto 16px;}

@keyframes fadeUp{from{opacity:0;transform:translateY(12px);}to{opacity:1;transform:none;}}
@media(max-width:991px){.nt-stat-grid{grid-template-columns:repeat(2,1fr);}}
@media(max-width:767px){.ar-page-header{flex-direction:column;}.btn-tambah{width:100%;justify-content:center;}.nt-stat-grid{gap:10px;}.nt-top{flex-direction:column;}.nt-badges{margin-top:6px;}}
</style>

<div class="nt-wrap">

  <!-- Header -->
  <div class="ar-page-header">
    <div class="ar-header-left">
      <h4>
        <span class="hi"><i class="bi bi-journal-text"></i></span>
        Notulen Rapat
      </h4>
      <div class="ar-breadcrumb">
        <a href="<?= base_url('dashboard') ?>">Dashboard</a>
        <span class="sep">/</span>
        <span>Notulen Rapat</span>
      </div>
    </div>
    <?php if (!is_warga()): ?>
    <a href="<?= base_url('notulen/tambah') ?>" class="btn-tambah">
      <i class="bi bi-plus-circle-fill"></i> Buat Notulen
    </a>
    <?php endif; ?>
  </div>

  <!-- Stats -->
  <div class="nt-stat-grid">
    <div class="nt-stat-card sc-teal">
      <div class="sc-label"><i class="bi bi-journal-text me-1"></i>Total Notulen</div>
      <div class="sc-value"><?= number_format($statistik['total']) ?></div>
      <div class="sc-icon"><i class="bi bi-journal-text"></i></div>
    </div>
    <div class="nt-stat-card sc-green">
      <div class="sc-label"><i class="bi bi-check-circle me-1"></i>Final</div>
      <div class="sc-value"><?= number_format($statistik['final']) ?></div>
      <div class="sc-icon"><i class="bi bi-check-circle"></i></div>
    </div>
    <div class="nt-stat-card sc-amber">
      <div class="sc-label"><i class="bi bi-hourglass-split me-1"></i>Menunggu</div>
      <div class="sc-value"><?= number_format($statistik['menunggu']) ?></div>
      <div class="sc-icon"><i class="bi bi-hourglass-split"></i></div>
    </div>
    <div class="nt-stat-card sc-blue">
      <div class="sc-label"><i class="bi bi-pencil-square me-1"></i>Draft</div>
      <div class="sc-value"><?= number_format($statistik['draft']) ?></div>
      <div class="sc-icon"><i class="bi bi-pencil-square"></i></div>
    </div>
  </div>

  <!-- Filter -->
  <div class="nt-filter-card">
    <div class="nt-filter-header">
      <h5><i class="bi bi-funnel-fill"></i> Cari & Filter Notulen</h5>
      <?php if ($this->input->get('status') || $this->input->get('tim') || $this->input->get('dari') || $this->input->get('sampai') || $this->input->get('q')): ?>
      <span class="filter-pill">Filter Aktif</span>
      <?php endif; ?>
    </div>
    <div class="nt-filter-body">
      <form method="get" class="row g-2 align-items-end">
        <div class="col-md-3 col-12">
          <label class="form-label fw-semibold small">Cari</label>
          <input type="text" name="q" class="form-control"
                 value="<?= htmlspecialchars($this->input->get('q') ?? '') ?>"
                 placeholder="Judul, moderator, tempat…">
        </div>
        <div class="col-md-2 col-6">
          <label class="form-label fw-semibold small">Status</label>
          <select name="status" class="form-select" onchange="this.form.submit()">
            <option value="">Semua Status</option>
            <option value="draft"    <?= $this->input->get('status') == 'draft'    ? 'selected' : '' ?>>Draft</option>
            <option value="menunggu" <?= $this->input->get('status') == 'menunggu' ? 'selected' : '' ?>>Menunggu</option>
            <option value="final"    <?= $this->input->get('status') == 'final'    ? 'selected' : '' ?>>Final</option>
          </select>
        </div>
        <div class="col-md-2 col-6">
          <label class="form-label fw-semibold small">Tim / Proyek</label>
          <select name="tim" class="form-select" onchange="this.form.submit()">
            <option value="">Semua Tim</option>
            <?php foreach (['Keamanan','Kebersihan','Sosial','Infrastruktur','Umum'] as $t): ?>
            <option value="<?= $t ?>" <?= $this->input->get('tim') == $t ? 'selected' : '' ?>><?= $t ?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-2 col-6">
          <label class="form-label fw-semibold small">Dari Tanggal</label>
          <input type="date" name="dari" class="form-control" value="<?= $this->input->get('dari') ?? '' ?>">
        </div>
        <div class="col-md-2 col-6">
          <label class="form-label fw-semibold small">Sampai</label>
          <input type="date" name="sampai" class="form-control" value="<?= $this->input->get('sampai') ?? '' ?>">
        </div>
        <div class="col-md-1 col-12 d-flex gap-1">
          <button type="submit" class="btn btn-sm btn-teal w-50" style="background:var(--teal-600);color:#fff;border:none;border-radius:8px;padding:9px 0;">
            <i class="bi bi-search"></i>
          </button>
          <a href="<?= base_url('notulen') ?>" class="btn btn-sm btn-outline-secondary w-50" style="border-radius:8px;padding:9px 0;text-align:center;">
            <i class="bi bi-x"></i>
          </a>
        </div>
      </form>
    </div>
  </div>

  <!-- Daftar -->
  <?php
  $status_labels = ['draft' => 'Draft', 'menunggu' => 'Menunggu Persetujuan', 'final' => 'Final'];
  if (!empty($notulen)):
  ?>
  <div class="nt-list">
    <?php foreach ($notulen as $n):
      $peserta  = $this->Notulen_model->get_peserta($n->id);
      $poin_all = $this->Notulen_model->get_poin($n->id);
      $hadir    = array_filter($peserta, fn($p) => $p->kehadiran === 'hadir');
    ?>
    <div class="nt-item">
      <div class="nt-item-inner">
        <div class="nt-accent accent-<?= $n->status ?>"></div>
        <div class="nt-body">
          <div class="nt-top">
            <div style="flex:1;min-width:0;">
              <div class="nt-title"><?= htmlspecialchars($n->judul_rapat) ?></div>
              <div class="nt-meta">
                <i class="bi bi-calendar3"></i>
                <?= date('d F Y', strtotime($n->tanggal)) ?>
                <span class="sep">•</span>
                <i class="bi bi-clock"></i>
                <?= $n->waktu_mulai ? date('H:i', strtotime($n->waktu_mulai)) : '-' ?>
                <?= $n->waktu_selesai ? '–' . date('H:i', strtotime($n->waktu_selesai)) : '' ?>
                <span class="sep">•</span>
                <i class="bi bi-geo-alt"></i>
                <?= htmlspecialchars($n->tempat) ?>
              </div>
            </div>
            <div class="nt-badges">
              <span class="badge badge-tim"><?= htmlspecialchars($n->tim) ?></span>
              <?php if ($n->status === 'draft'): ?>
                <span class="badge badge-draft"><i class="bi bi-pencil-square"></i> Draft</span>
              <?php elseif ($n->status === 'menunggu'): ?>
                <span class="badge badge-menunggu"><i class="bi bi-hourglass-split"></i> Menunggu</span>
              <?php else: ?>
                <span class="badge badge-final"><i class="bi bi-check-circle-fill"></i> Final</span>
              <?php endif; ?>
            </div>
          </div>

          <!-- Poin chip preview -->
          <?php if (!empty($poin_all)): ?>
          <div class="nt-poin-chips">
            <?php foreach (array_slice($poin_all, 0, 2) as $p): ?>
            <span class="poin-chip"><?= mb_strimwidth(htmlspecialchars($p->teks), 0, 35, '…') ?></span>
            <?php endforeach; ?>
            <?php if (count($poin_all) > 2): ?>
            <span class="poin-chip" style="background:var(--slate-100);color:var(--slate-500);">+<?= count($poin_all) - 2 ?> lagi</span>
            <?php endif; ?>
          </div>
          <?php endif; ?>

          <div class="nt-bottom">
            <div class="nt-hadir">
              <i class="bi bi-people" style="color:var(--teal-600)"></i>
              <?= count($hadir) ?>/<?= count($peserta) ?> hadir
              <span class="sep" style="margin:0 4px;">•</span>
              <i class="bi bi-person" style="color:var(--teal-600)"></i>
              <?= htmlspecialchars($n->moderator) ?>
            </div>
            <div class="nt-actions">
              <a href="<?= base_url('notulen/detail/' . $n->id) ?>" class="btn-sm-icon" title="Lihat Detail">
                <i class="bi bi-eye"></i>
              </a>
              <?php if (!is_warga()): ?>
              <a href="<?= base_url('notulen/edit/' . $n->id) ?>" class="btn-sm-icon" title="Edit">
                <i class="bi bi-pencil"></i>
              </a>
              <?php endif; ?>
              <a href="<?= base_url('notulen/cetak/' . $n->id) ?>" class="btn-sm-icon pdf" title="Export PDF" target="_blank">
                <i class="bi bi-file-earmark-pdf"></i>
              </a>
              <?php if (!is_warga()): ?>
             <button class="btn-sm-icon red" title="Hapus"
   onclick="konfirmasiHapusNotulen('<?= base_url('notulen/hapus/' . $n->id) ?>','<?= addslashes($n->judul_rapat) ?>')">
  <i class="bi bi-trash"></i>
</button>
              <?php endif; ?>
            </div>
          </div>

        </div>
      </div>
    </div>
    <?php endforeach; ?>
  </div>

  <div class="text-center mt-3" style="font-size:.78rem;color:var(--slate-500);">
    <i class="bi bi-info-circle me-1"></i>
    Menampilkan <?= count($notulen) ?> dari <?= $statistik['total'] ?> notulen
  </div>

  <?php else: ?>
  <div class="nt-empty">
    <div class="nt-empty-icon"><i class="bi bi-journal-x"></i></div>
    <h5 class="fw-700 mb-2" style="font-weight:700;">Belum Ada Notulen</h5>
    <p class="text-muted small mb-3">
      <?php if ($this->input->get('q') || $this->input->get('status') || $this->input->get('tim')): ?>
        Tidak ada notulen yang sesuai filter. Coba reset filter.
      <?php else: ?>
        Belum ada notulen rapat. Mulai dengan membuat notulen baru.
      <?php endif; ?>
    </p>
    <?php if (!is_warga()): ?>
    <a href="<?= base_url('notulen/tambah') ?>" class="btn-tambah">
      <i class="bi bi-plus-circle-fill"></i> Buat Notulen Baru
    </a>
    <?php endif; ?>
  </div>
  <?php endif; ?>

</div>
<!-- Modal Konfirmasi Hapus Notulen -->
<div class="modal fade" id="modalHapusNotulen" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" style="max-width:400px;">
    <div class="modal-content" style="border-radius:16px;border:none;overflow:hidden;font-family:'Plus Jakarta Sans',sans-serif;">
      <div class="modal-header" style="background:#fef2f2;border-bottom:1.5px solid #fecaca;padding:18px 20px;">
        <div class="d-flex align-items-center gap-2">
          <div style="width:36px;height:36px;background:#fee2e2;border-radius:10px;display:flex;align-items:center;justify-content:center;">
            <i class="bi bi-trash-fill" style="color:#dc2626;font-size:1rem;"></i>
          </div>
          <h6 class="modal-title mb-0 fw-bold" style="color:#991b1b;font-size:.95rem;">Hapus Notulen Rapat</h6>
        </div>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body" style="padding:20px;">
        <p style="font-size:.85rem;color:#334155;margin-bottom:10px;">Anda akan menghapus notulen:</p>
        <div style="background:#fef2f2;border:1.5px solid #fecaca;border-radius:10px;padding:12px 16px;display:flex;align-items:center;gap:10px;">
          <div style="width:34px;height:34px;border-radius:9px;background:#fee2e2;color:#dc2626;display:flex;align-items:center;justify-content:center;font-size:1.1rem;flex-shrink:0;">
            <i class="bi bi-journal-text"></i>
          </div>
          <div style="font-weight:700;font-size:.88rem;color:#991b1b;" id="hapusNotulenJudul">-</div>
        </div>
        <div style="background:#fffbeb;border:1.5px solid #fde68a;border-radius:8px;padding:9px 14px;margin-top:12px;font-size:.78rem;color:#92400e;">
          <i class="bi bi-exclamation-triangle-fill me-1"></i>
          Data akan <strong>terhapus permanen</strong> beserta semua poin dan peserta.
        </div>
      </div>
      <div class="modal-footer" style="border:none;padding:0 20px 18px;gap:8px;">
        <button type="button" class="btn" data-bs-dismiss="modal"
          style="border-radius:8px;border:1.5px solid #e5e7eb;font-size:.82rem;font-weight:600;color:#64748b;padding:7px 18px;flex:1;">
          <i class="bi bi-x-lg me-1"></i>Batal
        </button>
        <a href="#" id="btnKonfirmasiHapusNotulen"
          style="border-radius:8px;border:none;font-size:.82rem;font-weight:700;color:#fff;padding:7px 18px;flex:1;background:linear-gradient(135deg,#dc2626,#ef4444);box-shadow:0 4px 12px rgba(220,38,38,.28);cursor:pointer;display:inline-flex;align-items:center;justify-content:center;gap:6px;text-decoration:none;">
          <i class="bi bi-trash-fill"></i>Ya, Hapus
        </a>
      </div>
    </div>
  </div>
</div>

<script>
var _bsModalHapusNotulen = null;

function konfirmasiHapusNotulen(url, judul) {
  var elJudul = document.getElementById('hapusNotulenJudul');
  var elBtn   = document.getElementById('btnKonfirmasiHapusNotulen');
  if(elJudul) elJudul.textContent = judul || '-';
  if(elBtn)   elBtn.href          = url;

  var modalEl = document.getElementById('modalHapusNotulen');
  if(!modalEl) return;
  if(!_bsModalHapusNotulen) _bsModalHapusNotulen = new bootstrap.Modal(modalEl);
  _bsModalHapusNotulen.show();
}
</script>