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

<style>
: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; --green-600: #059669;
  --amber-400: #fbbf24; --amber-500: #f59e0b; --amber-600: #d97706;
  --amber-50: #fffbeb; --amber-100: #fef3c7;
  --red-500: #ef4444;
  --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);
  --radius: 14px; --radius-sm: 9px; --radius-xs: 6px;
  --shadow: 0 4px 24px rgba(13,148,136,0.10), 0 1px 4px rgba(0,0,0,0.05);
  --shadow-sm: var(--card-shadow);
}
.dp-wrapper { max-width: 1400px; margin: 0 auto; padding: 0 28px 60px; }
.dp-page-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 24px; flex-wrap: wrap; gap: 12px; }
.dp-header-left h4 { font-size: 1.2rem; font-weight: 800; color: var(--teal-800); margin: 0 0 5px; display: flex; align-items: center; gap: 11px; }
.dp-header-left h4 .hi { width: 40px; height: 40px; border-radius: 11px; background: linear-gradient(135deg, var(--teal-800), var(--teal-500)); display: flex; align-items: center; justify-content: center; color: #fff; font-size: 1rem; box-shadow: 0 4px 12px rgba(13,148,136,0.32); flex-shrink: 0; }
.dp-breadcrumb { display: flex; align-items: center; gap: 6px; font-size: 0.77rem; color: var(--slate-400); padding-left: 51px; }
.dp-breadcrumb a { color: var(--teal-600); text-decoration: none; font-weight: 600; }
.dp-breadcrumb a:hover { text-decoration: underline; }
.dp-breadcrumb .sep { color: var(--slate-200); }
.btn-back { display: inline-flex; align-items: center; gap: 6px; padding: 8px 18px; border-radius: 50px; border: 1.5px solid var(--slate-200); background: #fff; color: var(--slate-700); font-size: 0.83rem; font-weight: 600; text-decoration: none; transition: all .2s; }
.btn-back:hover { border-color: var(--teal-400); color: var(--teal-700); background: var(--teal-50); }
.flash-alert { display: flex; align-items: flex-start; gap: 10px; border-radius: var(--radius-sm); padding: 13px 16px; margin-bottom: 20px; font-size: 0.85rem; }
.flash-alert.success { background: var(--teal-50); border-left: 4px solid var(--teal-600); color: var(--teal-800); }
.flash-alert .fa-close { margin-left: auto; cursor: pointer; font-size: 1rem; opacity: .55; background: none; border: none; color: inherit; padding: 0; line-height: 1; }
.dp-grid { display: grid; grid-template-columns: 320px 1fr; gap: 18px; align-items: start; }
.side-card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow-sm); border: 1.5px solid var(--slate-100); overflow: hidden; margin-bottom: 16px; }
.side-card:last-child { margin-bottom: 0; }
.side-card-header { padding: 14px 18px; font-size: 0.84rem; font-weight: 700; display: flex; align-items: center; gap: 7px; }
.side-card-header.teal { background: linear-gradient(135deg, var(--teal-800), var(--teal-600)); color: #fff; }
.side-card-header.amber { background: linear-gradient(135deg, var(--amber-600), var(--amber-400)); color: #fff; }
.side-card-body { padding: 18px; }
.info-row { margin-bottom: 14px; }
.info-row:last-child { margin-bottom: 0; }
.info-row .ir-label { font-size: 0.69rem; font-weight: 700; text-transform: uppercase; letter-spacing: .5px; color: var(--slate-400); margin-bottom: 3px; }
.info-row .ir-value { font-size: 0.88rem; color: var(--slate-900); font-weight: 600; }
.ke-badge { display: inline-flex; align-items: center; justify-content: center; min-width: 52px; height: 32px; border-radius: var(--radius-xs); background: linear-gradient(135deg, var(--teal-700), var(--teal-500)); color: #fff; font-size: 0.82rem; font-weight: 800; box-shadow: 0 2px 8px rgba(13,148,136,0.28); padding: 0 12px; }
.status-pill { display: inline-flex; align-items: center; gap: 5px; border-radius: 50px; padding: 4px 13px; font-size: 0.73rem; font-weight: 700; }
.status-pill.terlaksana { background: var(--teal-50);  color: var(--teal-700); border: 1px solid var(--teal-100); }
.status-pill.pending    { background: var(--amber-50); color: var(--amber-600); border: 1px solid var(--amber-100); }
.status-pill.batal      { background: #fef2f2; color: var(--red-500); border: 1px solid #fecaca; }
.host-name { font-weight: 700; color: var(--slate-900); }
.host-place { font-size: 0.76rem; color: var(--slate-400); margin-top: 2px; }
.winner-block { text-align: center; padding: 4px 0 6px; }
.winner-trophy { font-size: 2.8rem; margin-bottom: 8px; }
.winner-name { font-size: 1.1rem; font-weight: 800; color: var(--green-600); margin-bottom: 6px; }
.winner-nomor { display: inline-block; border-radius: 50px; padding: 4px 16px; font-size: 0.78rem; font-weight: 800; background: var(--amber-50); color: var(--amber-600); border: 1px solid var(--amber-100); margin-bottom: 14px; }
.winner-dana { background: var(--teal-50); border: 1.5px solid var(--teal-100); border-radius: var(--radius-sm); padding: 12px 16px; }
.winner-dana .wd-label { font-size: 0.71rem; font-weight: 600; text-transform: uppercase; letter-spacing: .5px; color: var(--slate-400); margin-bottom: 4px; }
.winner-dana .wd-value { font-size: 1.4rem; font-weight: 800; color: var(--teal-700); }
.no-winner { text-align: center; padding: 16px 0; color: var(--slate-400); }
.no-winner i { font-size: 2.2rem; display: block; margin-bottom: 8px; }
.no-winner p { font-size: 0.83rem; margin: 0; }
.main-card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; border: 1.5px solid var(--slate-100); }
.main-card-header { display: flex; align-items: center; justify-content: space-between; padding: 18px 24px; border-bottom: 1.5px solid var(--slate-100); background: var(--slate-50); flex-wrap: wrap; gap: 10px; }
.main-card-header h5 { font-size: 0.92rem; font-weight: 700; color: var(--teal-800); margin: 0; display: flex; align-items: center; gap: 8px; }
.main-card-header h5 i { color: var(--teal-500); }
.header-badges { display: flex; gap: 6px; }
.count-pill { display: inline-flex; align-items: center; gap: 4px; border-radius: 50px; padding: 4px 12px; font-size: 0.73rem; font-weight: 700; }
.count-pill.lunas { background: var(--teal-50); color: var(--teal-700); border: 1px solid var(--teal-100); }
.count-pill.belum { background: var(--amber-50); color: var(--amber-600); border: 1px solid var(--amber-100); }
.dp-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.dp-table { width: 100%; border-collapse: collapse; min-width: 600px; }
.dp-table thead tr { background: var(--slate-50); border-bottom: 2px solid var(--slate-200); }
.dp-table thead th { padding: 12px 16px; font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: .65px; color: var(--slate-400); white-space: nowrap; }
.dp-table thead th.center { text-align: center; }
.dp-table tbody tr { border-bottom: 1px solid var(--slate-100); transition: background .15s; }
.dp-table tbody tr:hover { background: var(--teal-50); }
.dp-table tbody tr:last-child { border-bottom: none; }
.dp-table tbody td { padding: 13px 16px; font-size: 0.86rem; color: var(--slate-700); vertical-align: middle; }
.dp-table tbody td.center { text-align: center; }
.dp-table tfoot tr { background: var(--slate-50); border-top: 2px solid var(--slate-200); }
.dp-table tfoot td { padding: 12px 16px; font-size: 0.83rem; font-weight: 700; color: var(--slate-500); }
.dp-table tfoot td.total-label { text-align: right; }
.dp-table tfoot td.total-value { color: var(--green-600); font-size: 0.95rem; }
.urut-badge { display: inline-flex; align-items: center; justify-content: center; min-width: 28px; height: 28px; border-radius: var(--radius-xs); background: var(--slate-100); color: var(--slate-700); font-size: 0.8rem; font-weight: 700; padding: 0 6px; }
.peserta-name { font-weight: 700; color: var(--slate-900); }
.nominal-cell { font-weight: 600; color: var(--slate-700); }
.bayar-pill { display: inline-flex; align-items: center; gap: 5px; border-radius: 50px; padding: 3px 11px; font-size: 0.72rem; font-weight: 700; }
.bayar-pill.lunas { background: var(--teal-50); color: var(--teal-700); border: 1px solid var(--teal-100); }
.bayar-pill.belum { background: var(--amber-50); color: var(--amber-600); border: 1px solid var(--amber-100); }
.metode-pill { display: inline-block; border-radius: 50px; padding: 3px 11px; font-size: 0.71rem; font-weight: 700; background: var(--slate-100); color: var(--slate-500); border: 1px solid var(--slate-200); }
.date-cell { font-size: 0.81rem; color: var(--slate-500); }
.act-btn-bayar { display: inline-flex; align-items: center; gap: 5px; padding: 5px 13px; border-radius: var(--radius-xs); border: 1.5px solid var(--teal-100); color: var(--teal-700); background: var(--teal-50); font-size: 0.79rem; font-weight: 700; cursor: pointer; transition: all .18s; }
.act-btn-bayar:hover { background: var(--teal-100); border-color: var(--teal-400); }
.progress-section { padding: 16px 24px 20px; border-top: 1.5px solid var(--slate-100); background: var(--slate-50); }
.progress-meta { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; font-size: 0.78rem; }
.progress-meta .pm-label { color: var(--slate-400); font-weight: 600; }
.progress-meta .pm-pct { font-weight: 800; color: var(--teal-700); }
.progress-track { height: 10px; background: var(--slate-200); border-radius: 50px; overflow: hidden; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--teal-600), var(--teal-400)); border-radius: 50px; transition: width .6s ease; }
.progress-sub { margin-top: 6px; font-size: 0.75rem; color: var(--slate-400); text-align: right; }

/* ── Modal Bayar ── */
.dp-modal-backdrop { display: none; position: fixed; inset: 0; background: rgba(15,23,42,0.45); z-index: 1040; align-items: center; justify-content: center; }
.dp-modal-backdrop.show { display: flex; }
.dp-modal { background: #fff; border-radius: var(--radius); width: 100%; max-width: 460px; margin: 16px; box-shadow: 0 20px 60px rgba(0,0,0,0.2); overflow: hidden; animation: modalIn .2s ease; }
@keyframes modalIn { from { transform: scale(.95); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.dp-modal-header { padding: 16px 20px; background: linear-gradient(135deg, var(--teal-800), var(--teal-600)); color: #fff; display: flex; align-items: center; justify-content: space-between; font-size: 0.92rem; font-weight: 700; }
.dp-modal-close { background: none; border: none; color: rgba(255,255,255,.75); font-size: 1.1rem; cursor: pointer; padding: 0; line-height: 1; }
.dp-modal-close:hover { color: #fff; }
.dp-modal-body { padding: 22px 20px; }
.dp-modal-peserta { text-align: center; padding-bottom: 18px; margin-bottom: 18px; border-bottom: 1.5px solid var(--slate-100); }
.dp-modal-peserta .mp-name { font-size: 1.1rem; font-weight: 800; color: var(--teal-800); margin-bottom: 4px; }
.dp-modal-peserta .mp-nominal { font-size: 1.3rem; font-weight: 800; color: var(--green-600); }
.dp-modal-label { font-size: 0.77rem; font-weight: 700; text-transform: uppercase; letter-spacing: .4px; color: var(--slate-500); margin-bottom: 7px; display: block; margin-top: 14px; }
.dp-modal-select { width: 100%; padding: 11px 14px; border: 1.5px solid var(--slate-200); border-radius: var(--radius-sm); font-size: 0.9rem; color: var(--slate-900); background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E") no-repeat right 14px center; -webkit-appearance: none; appearance: none; transition: border-color .2s, box-shadow .2s; }
.dp-modal-select:focus { border-color: var(--teal-600); box-shadow: 0 0 0 3px rgba(13,148,136,0.12); outline: none; }
.kas-hint { display: flex; align-items: center; gap: 5px; font-size: 0.72rem; color: var(--slate-400); margin-top: 5px; }
.dp-modal-footer { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; background: var(--slate-50); border-top: 1.5px solid var(--slate-100); }
.dp-modal-cancel { padding: 9px 18px; border-radius: var(--radius-xs); border: 1.5px solid var(--slate-200); background: #fff; color: var(--slate-500); font-size: 0.84rem; font-weight: 600; cursor: pointer; transition: all .2s; }
.dp-modal-cancel:hover { background: var(--slate-100); }
.dp-modal-confirm { padding: 9px 20px; border-radius: var(--radius-xs); border: none; background: linear-gradient(135deg, var(--teal-700), var(--teal-500)); color: #fff; font-size: 0.84rem; font-weight: 700; cursor: pointer; box-shadow: 0 3px 10px rgba(13,148,136,0.32); transition: all .2s; }
.dp-modal-confirm:hover { background: linear-gradient(135deg, var(--teal-800), var(--teal-600)); }

/* DT overrides */
.dp-dt-top { display: flex; justify-content: space-between; align-items: center; padding: 14px 24px 10px; flex-wrap: wrap; gap: 10px; }
.dp-dt-bot { display: flex; justify-content: space-between; align-items: center; padding: 10px 24px 8px; flex-wrap: wrap; gap: 10px; }
.dataTables_filter label, .dataTables_length label, .dataTables_info { font-size: 0.82rem; color: var(--slate-500); }
.paginate_button { border-radius: var(--radius-xs) !important; font-size: 0.82rem !important; }
.paginate_button.current { background: var(--teal-600) !important; border-color: var(--teal-600) !important; color: #fff !important; }
@media (max-width: 900px) { .dp-wrapper { padding: 0 12px 48px; } .dp-grid { grid-template-columns: 1fr; } .dp-page-header { flex-direction: column; } }
</style>

<div class="dp-wrapper">

  <div class="dp-page-header">
    <div class="dp-header-left">
      <h4>
        <span class="hi"><i class="bi bi-calendar-check"></i></span>
        Detail Pertemuan Arisan
      </h4>
      <div class="dp-breadcrumb">
        <a href="<?php echo base_url('dashboard'); ?>">Dashboard</a>
        <span class="sep">/</span>
        <a href="<?php echo base_url('arisan'); ?>">Arisan</a>
        <span class="sep">/</span>
        <a href="<?php echo base_url('arisan/pertemuan/' . $pertemuan->arisan_id); ?>">Pertemuan</a>
        <span class="sep">/</span>
        <span>Detail</span>
      </div>
    </div>
    <a href="<?php echo base_url('arisan/pertemuan/' . $pertemuan->arisan_id); ?>" class="btn-back">
      <i class="bi bi-arrow-left"></i> Kembali
    </a>
  </div>

  <?php if($this->session->flashdata('success')): ?>
  <div class="flash-alert success" id="flashSuccess">
    <i class="bi bi-check-circle-fill"></i>
    <span><?php echo $this->session->flashdata('success'); ?></span>
    <button class="fa-close" onclick="this.parentElement.remove()">✕</button>
  </div>
  <?php endif; ?>

  <div class="dp-grid">

    <!-- Sidebar -->
    <div class="dp-sidebar">

      <div class="side-card">
        <div class="side-card-header teal">
          <i class="bi bi-info-circle"></i> Info Pertemuan
        </div>
        <div class="side-card-body">
          <div class="info-row">
            <div class="ir-label">Nama Arisan</div>
            <div class="ir-value"><?php echo $pertemuan->nama_arisan; ?></div>
          </div>
          <div class="info-row">
            <div class="ir-label">Pertemuan Ke</div>
            <div class="ir-value"><span class="ke-badge">Ke-<?php echo $pertemuan->pertemuan_ke; ?></span></div>
          </div>
          <div class="info-row">
            <div class="ir-label">Tanggal Pertemuan</div>
            <div class="ir-value">
              <i class="bi bi-calendar3" style="color:var(--teal-500);margin-right:4px"></i>
              <?php echo date('d F Y', strtotime($pertemuan->tanggal_pertemuan)); ?>
            </div>
          </div>
          <div class="info-row">
            <div class="ir-label">Tuan Rumah</div>
            <div class="ir-value">
              <?php if(!empty($pertemuan->nama_tuan_rumah)): ?>
                <div class="host-name"><i class="bi bi-house-door" style="color:var(--teal-500);margin-right:4px"></i><?php echo $pertemuan->nama_tuan_rumah; ?></div>
                <?php if(!empty($pertemuan->tempat_pertemuan)): ?>
                  <div class="host-place"><?php echo $pertemuan->tempat_pertemuan; ?></div>
                <?php endif; ?>
              <?php else: ?>
                <span style="color:var(--slate-400)">—</span>
              <?php endif; ?>
            </div>
          </div>
          <div class="info-row">
            <div class="ir-label">Status</div>
            <div class="ir-value">
              <?php if($pertemuan->status_pertemuan == 'terlaksana'): ?>
                <span class="status-pill terlaksana"><i class="bi bi-check-circle-fill" style="font-size:.6rem"></i> Terlaksana</span>
              <?php elseif($pertemuan->status_pertemuan == 'pending'): ?>
                <span class="status-pill pending"><i class="bi bi-clock-fill" style="font-size:.6rem"></i> Pending</span>
              <?php else: ?>
                <span class="status-pill batal"><i class="bi bi-x-circle-fill" style="font-size:.6rem"></i> Batal</span>
              <?php endif; ?>
            </div>
          </div>
          <?php if(!empty($pertemuan->keterangan)): ?>
          <div class="info-row">
            <div class="ir-label">Keterangan</div>
            <div class="ir-value" style="font-weight:400;color:var(--slate-500);font-size:.84rem"><?php echo $pertemuan->keterangan; ?></div>
          </div>
          <?php endif; ?>
        </div>
      </div>

      <!-- Pemenang -->
      <div class="side-card">
        <div class="side-card-header amber">
          <i class="bi bi-trophy-fill"></i> Pemenang Undian
        </div>
        <div class="side-card-body">
          <?php if(!empty($pertemuan->nama_pemenang)): ?>
            <div class="winner-block">
              <div class="winner-trophy">🏆</div>
              <div class="winner-name"><?php echo $pertemuan->nama_pemenang; ?></div>
              <div class="winner-nomor">Nomor Urut: <?php echo $pertemuan->nomor_undian; ?></div>
              <div class="winner-dana">
                <div class="wd-label">Dana Diterima</div>
                <div class="wd-value">Rp <?php echo number_format($pertemuan->total_iuran, 0, ',', '.'); ?></div>
              </div>
            </div>
          <?php else: ?>
            <div class="no-winner">
              <i class="bi bi-question-circle"></i>
              <p>Belum ada pemenang<br><small>Undian belum dilaksanakan</small></p>
            </div>
          <?php endif; ?>
        </div>
      </div>

    </div><!-- /sidebar -->

    <!-- Main -->
    <div class="main-card">
      <?php
      $total_lunas = 0; $total_belum = 0;
      foreach($iuran as $i) { if($i->status_bayar == 'lunas') $total_lunas++; else $total_belum++; }
      $persentase = count($iuran) > 0 ? ($total_lunas / count($iuran)) * 100 : 0;
      ?>
      <div class="main-card-header">
        <h5><i class="bi bi-cash-stack"></i> Daftar Iuran Peserta</h5>
        <div class="header-badges">
          <span class="count-pill lunas"><i class="bi bi-check-circle-fill" style="font-size:.6rem"></i> <?php echo $total_lunas; ?> Lunas</span>
          <span class="count-pill belum"><i class="bi bi-clock-fill" style="font-size:.6rem"></i> <?php echo $total_belum; ?> Belum</span>
        </div>
      </div>

      <?php if(empty($iuran)): ?>
        <div style="text-align:center;padding:48px 24px;color:var(--slate-400)">
          <i class="bi bi-inbox" style="font-size:2rem;display:block;margin-bottom:10px"></i>
          <p style="margin:0;font-size:.84rem">Belum ada data iuran</p>
        </div>
      <?php else: ?>
        <div class="dp-table-wrap">
          <table class="dp-table" id="tableIuran">
            <thead>
              <tr>
                <th class="center" style="width:56px">No</th>
                <th>Nama Peserta</th>
                <th>Nominal</th>
                <th>Status</th>
                <th>Tgl Bayar</th>
                <th>Metode</th>
                <?php if(is_admin()): ?><th class="center" style="width:90px">Aksi</th><?php endif; ?>
              </tr>
            </thead>
            <tbody>
              <?php foreach($iuran as $i): ?>
              <tr>
                <td class="center"><span class="urut-badge"><?php echo $i->nomor_urut; ?></span></td>
                <td><span class="peserta-name"><?php echo $i->nama_lengkap; ?></span></td>
                <td><span class="nominal-cell">Rp <?php echo number_format($i->nominal_bayar, 0, ',', '.'); ?></span></td>
                <td>
                  <?php if($i->status_bayar == 'lunas'): ?>
                    <span class="bayar-pill lunas"><i class="bi bi-check-circle-fill" style="font-size:.55rem"></i> Lunas</span>
                  <?php else: ?>
                    <span class="bayar-pill belum"><i class="bi bi-clock-fill" style="font-size:.55rem"></i> Belum Bayar</span>
                  <?php endif; ?>
                </td>
                <td class="date-cell">
                  <?php echo $i->status_bayar == 'lunas' ? date('d/m/Y', strtotime($i->tanggal_bayar)) : '<span style="color:var(--slate-400)">—</span>'; ?>
                </td>
                <td>
                  <?php if($i->status_bayar == 'lunas'): ?>
                    <span class="metode-pill"><?php echo ucfirst($i->metode_pembayaran); ?></span>
                    <?php if(!empty($i->kas_rt_id)): ?>
                      <span style="font-size:.65rem;color:var(--teal-600);margin-left:4px" title="Tercatat di kas"><i class="bi bi-bank"></i></span>
                    <?php endif; ?>
                  <?php else: ?>
                    <span style="color:var(--slate-400)">—</span>
                  <?php endif; ?>
                </td>
                <?php if(is_admin()): ?>
                <td class="center">
                  <?php if($i->status_bayar == 'belum'): ?>
                    <button class="act-btn-bayar"
                            onclick="openModalBayar(<?php echo $i->id; ?>, '<?php echo addslashes($i->nama_lengkap); ?>', '<?php echo number_format($i->nominal_bayar, 0, ',', '.'); ?>')">
                      <i class="bi bi-check-circle"></i> Bayar
                    </button>
                  <?php else: ?>
                    <i class="bi bi-check-circle-fill" style="color:var(--teal-500);font-size:1.1rem"></i>
                  <?php endif; ?>
                </td>
                <?php endif; ?>
              </tr>
              <?php endforeach; ?>
            </tbody>
            <tfoot>
              <tr>
                <td colspan="2" class="total-label">TOTAL DANA :</td>
                <td colspan="<?php echo is_admin() ? '5' : '4'; ?>" class="total-value">
                  Rp <?php echo number_format($pertemuan->total_iuran, 0, ',', '.'); ?>
                </td>
              </tr>
            </tfoot>
          </table>
        </div>
      <?php endif; ?>

      <div class="progress-section">
        <div class="progress-meta">
          <span class="pm-label">Progress Pembayaran</span>
          <span class="pm-pct"><?php echo number_format($persentase, 1); ?>%</span>
        </div>
        <div class="progress-track">
          <div class="progress-fill" style="width:<?php echo $persentase; ?>%"></div>
        </div>
        <div class="progress-sub"><?php echo $total_lunas; ?> dari <?php echo count($iuran); ?> peserta sudah membayar</div>
      </div>

    </div>
  </div>
</div>

<!-- ── Modal Bayar Iuran (dengan pilihan kas) ── -->
<?php if(is_admin()): ?>
<div class="dp-modal-backdrop" id="modalBayarBackdrop">
  <div class="dp-modal">
    <div class="dp-modal-header">
      <span><i class="bi bi-cash-coin" style="margin-right:7px"></i>Konfirmasi Pembayaran Iuran</span>
      <button class="dp-modal-close" onclick="closeModalBayar()">✕</button>
    </div>
    <form id="formBayar" method="post" action="">
      <div class="dp-modal-body">
        <div class="dp-modal-peserta">
          <div class="mp-name" id="modalNama">—</div>
          <div class="mp-nominal" id="modalNominal">—</div>
        </div>

        <label class="dp-modal-label">Metode Pembayaran</label>
        <select name="metode" class="dp-modal-select" required>
          <option value="tunai" selected>💵 Tunai</option>
          <option value="transfer">🏦 Transfer</option>
        </select>

        <label class="dp-modal-label">Catat ke Kas <small style="text-transform:none;font-weight:400;color:var(--slate-400)">(opsional)</small></label>
        <select name="kas_id" class="dp-modal-select" id="modalKasSelect">
          <option value="">— Tidak dicatat ke kas —</option>
          <?php foreach($kas_list as $kas): ?>
          <option value="<?php echo $kas->id; ?>"
                  <?php echo ($kas_default_id == $kas->id) ? 'selected' : ''; ?>>
            <?php echo $kas->nama; ?> 
            (Saldo: Rp <?php echo number_format($kas->saldo_saat_ini, 0, ',', '.'); ?>)
          </option>
          <?php endforeach; ?>
        </select>
        <div class="kas-hint">
          <i class="bi bi-info-circle"></i>
          Jika dipilih, iuran akan otomatis masuk sebagai <strong>pemasukan</strong> di kas tersebut
        </div>
      </div>
      <div class="dp-modal-footer">
        <button type="button" class="dp-modal-cancel" onclick="closeModalBayar()">Batal</button>
        <button type="submit" class="dp-modal-confirm">
          <i class="bi bi-check-circle"></i> Konfirmasi Bayar
        </button>
      </div>
    </form>
  </div>
</div>
<?php endif; ?>

<script>
function openModalBayar(id, nama, nominal) {
  document.getElementById('modalNama').textContent    = nama;
  document.getElementById('modalNominal').textContent = 'Rp ' + nominal;
  document.getElementById('formBayar').action = '<?php echo base_url('arisan/bayar_iuran/'); ?>' + id;
  document.getElementById('modalBayarBackdrop').classList.add('show');
}
function closeModalBayar() {
  document.getElementById('modalBayarBackdrop').classList.remove('show');
}
document.getElementById('modalBayarBackdrop')?.addEventListener('click', function(e) {
  if (e.target === this) closeModalBayar();
});

$(document).ready(function() {
  $('#tableIuran').DataTable({
    language: { url: '//cdn.datatables.net/plug-ins/1.13.7/i18n/id.json' },
    order: [[0, 'asc']], pageLength: 50, responsive: true, searching: true,
    dom: '<"dp-dt-top"lf>rt<"dp-dt-bot"ip>',
    initComplete: function() {
      $('.dataTables_filter input').css({'border':'1.5px solid #e2e8f0','border-radius':'8px','padding':'7px 13px','font-size':'0.84rem','outline':'none'})
        .on('focus', function(){ $(this).css({'border-color':'#0d9488','box-shadow':'0 0 0 3px rgba(13,148,136,0.12)'}); })
        .on('blur',  function(){ $(this).css({'border-color':'#e2e8f0','box-shadow':'none'}); });
    }
  });
  setTimeout(function() {
    var el = document.getElementById('flashSuccess');
    if (el) { el.style.transition = 'opacity .4s'; el.style.opacity = '0'; setTimeout(function(){ el.remove(); }, 420); }
  }, 4500);
});
</script>