<?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-400: #f87171; --red-500: #ef4444; --red-600: #dc2626;
  --blue-500: #3b82f6; --blue-600: #2563eb;
  --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);
}
.rw-wrapper { max-width: 1400px; margin: 0 auto; padding: 0 28px 60px; }
.rw-page-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 24px; flex-wrap: wrap; gap: 12px; }
.rw-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; }
.rw-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; }
.rw-breadcrumb { display: flex; align-items: center; gap: 6px; font-size: 0.77rem; color: var(--slate-400); padding-left: 51px; }
.rw-breadcrumb a { color: var(--teal-600); text-decoration: none; font-weight: 600; }
.rw-breadcrumb .sep { color: var(--slate-200); }

.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.error   { background: #fef2f2; border-left: 4px solid var(--red-500); color: #7f1d1d; }
.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; }

.arisan-info-card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow-sm); border: 1.5px solid var(--slate-100); padding: 22px 26px; margin-bottom: 18px; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 18px; }
.aic-left h5 { font-size: 1.05rem; font-weight: 800; color: var(--teal-800); margin: 0 0 12px; display: flex; align-items: center; gap: 8px; }
.aic-left h5 i { color: var(--teal-500); }
.aic-meta { display: flex; flex-wrap: wrap; gap: 24px; }
.aic-meta-item .mi-label { font-size: 0.71rem; font-weight: 600; text-transform: uppercase; letter-spacing: .5px; color: var(--slate-400); margin-bottom: 3px; }
.aic-meta-item .mi-value { font-size: 0.92rem; font-weight: 700; color: var(--slate-900); }
.aic-meta-item .mi-value.teal  { color: var(--teal-700); }
.aic-meta-item .mi-value.green { color: var(--green-600); }
.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.aktif    { background: var(--teal-50);   color: var(--teal-700);  border: 1px solid var(--teal-100); }
.status-pill.selesai  { background: #eff6ff; color: var(--blue-600); border: 1px solid #bfdbfe; }
.status-pill.nonaktif { background: var(--slate-100); color: var(--slate-500); border: 1px solid var(--slate-200); }
.aic-actions { display: flex; gap: 8px; flex-shrink: 0; }
.btn-outline-sm { display: inline-flex; align-items: center; gap: 6px; padding: 8px 15px; border-radius: 50px; font-size: 0.81rem; font-weight: 600; text-decoration: none; transition: all .2s; white-space: nowrap; border: 1.5px solid; }
.btn-outline-sm.back { border-color: var(--slate-200); background: #fff; color: var(--slate-700); }
.btn-outline-sm.back:hover { border-color: var(--teal-400); color: var(--teal-700); background: var(--teal-50); }
.btn-outline-sm.teal { border-color: var(--teal-100); background: var(--teal-50); color: var(--teal-700); }
.btn-outline-sm.teal:hover { border-color: var(--teal-500); background: var(--teal-100); color: var(--teal-800); }

.rw-card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; border: 1.5px solid var(--slate-100); margin-bottom: 18px; }
.rw-card-header { display: flex; align-items: center; padding: 18px 24px; border-bottom: 1.5px solid var(--slate-100); background: var(--slate-50); gap: 8px; }
.rw-card-header h5 { font-size: 0.92rem; font-weight: 700; color: var(--teal-800); margin: 0; display: flex; align-items: center; gap: 8px; }
.rw-card-header h5 i { color: var(--amber-400); }
.empty-state { text-align: center; padding: 60px 24px; }
.empty-state-icon { width: 72px; height: 72px; border-radius: 50%; background: var(--amber-50); display: flex; align-items: center; justify-content: center; margin: 0 auto 16px; font-size: 2rem; }
.empty-state h6 { font-weight: 700; color: var(--slate-700); margin-bottom: 5px; }
.empty-state p  { font-size: 0.84rem; color: var(--slate-400); margin: 0; }

.rw-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.rw-table { width: 100%; border-collapse: collapse; min-width: 900px; }
.rw-table thead tr { background: var(--slate-50); border-bottom: 2px solid var(--slate-200); }
.rw-table thead th { padding: 13px 18px; font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: .65px; color: var(--slate-400); white-space: nowrap; }
.rw-table thead th.center { text-align: center; }
.rw-table tbody tr { border-bottom: 1px solid var(--slate-100); transition: background .15s; }
.rw-table tbody tr:hover { background: #fffbeb; }
.rw-table tbody tr:last-child { border-bottom: none; }
.rw-table tbody td { padding: 14px 18px; font-size: 0.87rem; color: var(--slate-700); vertical-align: middle; }
.rw-table tbody td.center { text-align: center; }
.rw-table tfoot tr { background: var(--slate-50); border-top: 2px solid var(--slate-200); }
.rw-table tfoot td { padding: 13px 18px; font-size: 0.83rem; font-weight: 700; color: var(--slate-700); }
.rw-table tfoot td.total-label { text-align: right; color: var(--slate-500); }
.rw-table tfoot td.total-value { color: var(--green-600); font-size: 0.95rem; }

.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.8rem; font-weight: 800; box-shadow: 0 2px 8px rgba(13,148,136,0.28); padding: 0 10px; }
.undian-pill { display: inline-block; border-radius: 50px; padding: 4px 14px; font-size: 0.78rem; font-weight: 800; background: var(--amber-50); color: var(--amber-600); border: 1px solid var(--amber-100); }
.winner-cell { display: flex; align-items: center; gap: 7px; font-weight: 700; color: var(--green-600); font-size: 0.9rem; }
.winner-cell i { color: var(--amber-400); font-size: 1rem; }
.date-cell { font-size: 0.83rem; color: var(--slate-500); white-space: nowrap; }
.date-cell i { color: var(--slate-400); }
.nominal-cell { font-weight: 700; color: var(--green-600); font-size: 0.92rem; }
.terima-pill { display: inline-flex; align-items: center; gap: 5px; border-radius: 50px; padding: 4px 13px; font-size: 0.73rem; font-weight: 700; white-space: nowrap; }
.terima-pill.ya    { background: var(--teal-50);  color: var(--teal-700);  border: 1px solid var(--teal-100); }
.terima-pill.belum { background: var(--amber-50); color: var(--amber-600); border: 1px solid var(--amber-100); }
.kas-badge { display: inline-flex; align-items: center; gap: 3px; font-size: 0.65rem; color: var(--teal-600); margin-left: 5px; }

/* Tombol terima dana */
.btn-terima-dana { display: inline-flex; align-items: center; gap: 5px; padding: 5px 13px; border-radius: var(--radius-xs); border: 1.5px solid #bbf7d0; color: var(--green-600); background: #f0fdf4; font-size: 0.79rem; font-weight: 700; cursor: pointer; transition: all .18s; white-space: nowrap; }
.btn-terima-dana:hover { background: #dcfce7; border-color: var(--green-500); }

/* Statistik */
.stat-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; padding: 20px 24px 24px; border-top: 1.5px solid var(--slate-100); background: var(--slate-50); }
.stat-mini { border-radius: var(--radius-sm); padding: 16px 18px; display: flex; align-items: center; gap: 14px; border: 1.5px solid; }
.stat-mini.green { background: #f0fdf4; border-color: #bbf7d0; }
.stat-mini.amber { background: var(--amber-50); border-color: var(--amber-100); }
.stat-mini .sm-icon { width: 40px; height: 40px; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 1.1rem; flex-shrink: 0; }
.stat-mini.green .sm-icon { background: #dcfce7; color: var(--green-600); }
.stat-mini.amber .sm-icon { background: var(--amber-100); color: var(--amber-600); }
.stat-mini .sm-label { font-size: 0.71rem; font-weight: 600; text-transform: uppercase; letter-spacing: .5px; color: var(--slate-400); margin-bottom: 3px; }
.stat-mini .sm-value { font-size: 1.5rem; font-weight: 800; line-height: 1; }
.stat-mini.green .sm-value { color: var(--green-600); }
.stat-mini.amber .sm-value { color: var(--amber-600); }
.stat-mini .sm-sub { font-size: 0.73rem; color: var(--slate-400); margin-top: 2px; }

/* Modal terima dana */
.td-modal-backdrop { display: none; position: fixed; inset: 0; background: rgba(15,23,42,0.45); z-index: 1040; align-items: center; justify-content: center; }
.td-modal-backdrop.show { display: flex; }
.td-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; } }
.td-modal-header { padding: 16px 20px; background: linear-gradient(135deg, #059669, #10b981); color: #fff; display: flex; align-items: center; justify-content: space-between; font-size: 0.92rem; font-weight: 700; }
.td-modal-close { background: none; border: none; color: rgba(255,255,255,.75); font-size: 1.1rem; cursor: pointer; padding: 0; line-height: 1; }
.td-modal-close:hover { color: #fff; }
.td-modal-body { padding: 22px 20px; }
.td-winner-info { text-align: center; padding-bottom: 18px; margin-bottom: 18px; border-bottom: 1.5px solid var(--slate-100); }
.td-winner-info .tw-trophy { font-size: 2.5rem; margin-bottom: 8px; }
.td-winner-info .tw-name   { font-size: 1.1rem; font-weight: 800; color: var(--green-600); margin-bottom: 4px; }
.td-winner-info .tw-nominal { font-size: 1.4rem; font-weight: 800; color: var(--teal-700); }
.td-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; }
.td-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; }
.td-modal-select:focus { border-color: var(--green-600); box-shadow: 0 0 0 3px rgba(5,150,105,0.12); outline: none; }
.kas-hint { display: flex; align-items: center; gap: 5px; font-size: 0.72rem; color: var(--slate-400); margin-top: 5px; }
.warn-box { background: var(--amber-50); border: 1px solid var(--amber-100); border-radius: var(--radius-sm); padding: 10px 14px; display: flex; align-items: flex-start; gap: 8px; font-size: 0.78rem; color: var(--amber-600); margin-top: 12px; }
.td-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); }
.td-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; }
.td-modal-cancel:hover { background: var(--slate-100); }
.td-modal-confirm { padding: 9px 20px; border-radius: var(--radius-xs); border: none; background: linear-gradient(135deg, #059669, #10b981); color: #fff; font-size: 0.84rem; font-weight: 700; cursor: pointer; box-shadow: 0 3px 10px rgba(5,150,105,0.32); transition: all .2s; }
.td-modal-confirm:hover { background: linear-gradient(135deg, #047857, #059669); }

/* DT */
.rw-dt-top { display: flex; justify-content: space-between; align-items: center; padding: 14px 24px 10px; flex-wrap: wrap; gap: 10px; }
.rw-dt-bot { display: flex; justify-content: space-between; align-items: center; padding: 10px 24px 16px; 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: 767px) { .rw-wrapper { padding: 0 12px 48px; } .rw-page-header { flex-direction: column; } .arisan-info-card { flex-direction: column; } .aic-actions { width: 100%; } .stat-row { grid-template-columns: 1fr; } }
</style>

<div class="rw-wrapper">

  <div class="rw-page-header">
    <div class="rw-header-left">
      <h4>
        <span class="hi"><i class="bi bi-trophy"></i></span>
        Riwayat Pemenang Arisan
      </h4>
      <div class="rw-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>
        <span>Riwayat Pemenang</span>
      </div>
    </div>
  </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; ?>
  <?php if($this->session->flashdata('error')): ?>
  <div class="flash-alert error" id="flashError">
    <i class="bi bi-exclamation-triangle-fill"></i>
    <span><?php echo $this->session->flashdata('error'); ?></span>
    <button class="fa-close" onclick="this.parentElement.remove()">✕</button>
  </div>
  <?php endif; ?>

  <?php
  $total_terbagi = 0;
  foreach($riwayat as $r) { $total_terbagi += $r->nominal_dapat; }
  $total_peserta_arisan = $this->Arisan_model->count_peserta_by_arisan($arisan->id);
  $sudah_terima = 0;
  foreach($riwayat as $r) { if($r->sudah_diterima == 'ya') $sudah_terima++; }
  ?>

  <div class="arisan-info-card">
    <div class="aic-left">
      <h5><i class="bi bi-gift-fill"></i><?php echo $arisan->nama_arisan; ?></h5>
      <div class="aic-meta">
        <div class="aic-meta-item">
          <div class="mi-label">Total Pemenang</div>
          <div class="mi-value"><?php echo count($riwayat); ?> Orang</div>
        </div>
        <div class="aic-meta-item">
          <div class="mi-label">Dana per Undian</div>
          <div class="mi-value green">Rp <?php echo number_format($arisan->nominal_iuran * $total_peserta_arisan, 0, ',', '.'); ?></div>
        </div>
        <div class="aic-meta-item">
          <div class="mi-label">Total Dana Terbagi</div>
          <div class="mi-value teal">Rp <?php echo number_format($total_terbagi, 0, ',', '.'); ?></div>
        </div>
        <div class="aic-meta-item">
          <div class="mi-label">Status</div>
          <div class="mi-value">
            <?php if($arisan->status == 'aktif'): ?>
              <span class="status-pill aktif"><i class="bi bi-circle-fill" style="font-size:.42rem"></i> Aktif</span>
            <?php elseif($arisan->status == 'selesai'): ?>
              <span class="status-pill selesai"><i class="bi bi-flag-fill" style="font-size:.58rem"></i> Selesai</span>
            <?php else: ?>
              <span class="status-pill nonaktif"><i class="bi bi-circle" style="font-size:.42rem"></i> Nonaktif</span>
            <?php endif; ?>
          </div>
        </div>
      </div>
    </div>
    <div class="aic-actions">
      <a href="<?php echo base_url('arisan'); ?>" class="btn-outline-sm back">
        <i class="bi bi-arrow-left"></i> Kembali
      </a>
      <a href="<?php echo base_url('arisan/pertemuan/' . $arisan->id); ?>" class="btn-outline-sm teal">
        <i class="bi bi-calendar-event"></i> Pertemuan
      </a>
    </div>
  </div>

  <div class="rw-card">
    <div class="rw-card-header">
      <h5><i class="bi bi-trophy-fill"></i> Daftar Pemenang</h5>
    </div>

    <?php if(empty($riwayat)): ?>
      <div class="empty-state">
        <div class="empty-state-icon">🏆</div>
        <h6>Belum Ada Pemenang</h6>
        <p>Pemenang akan muncul setelah undian dilaksanakan.</p>
      </div>
    <?php else: ?>

      <div class="rw-table-wrap">
        <table class="rw-table" id="tableRiwayat">
          <thead>
            <tr>
              <th class="center" style="width:90px">Pertemuan</th>
              <th class="center" style="width:90px">No. Undian</th>
              <th>Nama Pemenang</th>
              <th>Tanggal Dapat</th>
              <th>Nominal Dana</th>
              <th>Status Terima</th>
              <th>Tgl Diterima</th>
              <?php if(is_admin()): ?><th class="center" style="width:130px">Aksi</th><?php endif; ?>
            </tr>
          </thead>
          <tbody>
            <?php foreach($riwayat as $r): ?>
            <tr>
              <td class="center"><span class="ke-badge">Ke-<?php echo $r->pertemuan_ke; ?></span></td>
              <td class="center"><span class="undian-pill"><?php echo $r->nomor_undian; ?></span></td>
              <td>
                <div class="winner-cell">
                  <i class="bi bi-trophy-fill"></i>
                  <?php echo $r->nama_lengkap; ?>
                </div>
              </td>
              <td class="date-cell"><i class="bi bi-calendar3"></i> <?php echo date('d/m/Y', strtotime($r->tanggal_dapat)); ?></td>
              <td>
                <span class="nominal-cell">Rp <?php echo number_format($r->nominal_dapat, 0, ',', '.'); ?></span>
              </td>
              <td>
                <?php if($r->sudah_diterima == 'ya'): ?>
                  <span class="terima-pill ya">
                    <i class="bi bi-check-circle-fill" style="font-size:.65rem"></i> Sudah Diterima
                  </span>
                  <?php if(!empty($r->kas_rt_id)): ?>
                    <span class="kas-badge"><i class="bi bi-bank"></i> Kas</span>
                  <?php endif; ?>
                <?php else: ?>
                  <span class="terima-pill belum">
                    <i class="bi bi-clock-fill" style="font-size:.62rem"></i> Belum Diterima
                  </span>
                <?php endif; ?>
              </td>
              <td class="date-cell">
                <?php if($r->sudah_diterima == 'ya' && !empty($r->tanggal_diterima)): ?>
                  <i class="bi bi-calendar3"></i> <?php echo date('d/m/Y', strtotime($r->tanggal_diterima)); ?>
                <?php else: ?>
                  <span style="color:var(--slate-400)">—</span>
                <?php endif; ?>
              </td>
              <?php if(is_admin()): ?>
              <td class="center">
                <?php if($r->sudah_diterima != 'ya'): ?>
                  <button class="btn-terima-dana"
                          onclick="openModalTerima(<?php echo $r->id; ?>, '<?php echo addslashes($r->nama_lengkap); ?>', '<?php echo number_format($r->nominal_dapat, 0, ',', '.'); ?>')">
                    <i class="bi bi-cash-coin"></i> Bayar Dana
                  </button>
                <?php else: ?>
                  <i class="bi bi-check-circle-fill" style="color:var(--green-600);font-size:1.1rem"></i>
                <?php endif; ?>
              </td>
              <?php endif; ?>
            </tr>
            <?php endforeach; ?>
          </tbody>
          <tfoot>
            <tr>
              <td colspan="4" class="total-label">TOTAL DANA TERBAGI :</td>
              <td colspan="<?php echo is_admin() ? '4' : '3'; ?>" class="total-value">
                Rp <?php echo number_format($total_terbagi, 0, ',', '.'); ?>
              </td>
            </tr>
          </tfoot>
        </table>
      </div>

      <div class="stat-row">
        <div class="stat-mini green">
          <div class="sm-icon"><i class="bi bi-check-circle-fill"></i></div>
          <div>
            <div class="sm-label">Sudah Menerima Dana</div>
            <div class="sm-value"><?php echo $sudah_terima; ?></div>
            <div class="sm-sub">dari <?php echo count($riwayat); ?> pemenang</div>
          </div>
        </div>
        <div class="stat-mini amber">
          <div class="sm-icon"><i class="bi bi-clock-fill"></i></div>
          <div>
            <div class="sm-label">Belum Menerima Dana</div>
            <div class="sm-value"><?php echo count($riwayat) - $sudah_terima; ?></div>
            <div class="sm-sub">pemenang</div>
          </div>
        </div>
      </div>

    <?php endif; ?>
  </div>

</div>

<!-- ── Modal Terima Dana ── -->
<?php if(is_admin()): ?>
<div class="td-modal-backdrop" id="modalTerimaBackdrop">
  <div class="td-modal">
    <div class="td-modal-header">
      <span><i class="bi bi-cash-coin" style="margin-right:7px"></i>Konfirmasi Pembayaran Dana Pemenang</span>
      <button class="td-modal-close" onclick="closeModalTerima()">✕</button>
    </div>
    <form id="formTerima" method="post" action="">
      <div class="td-modal-body">
        <div class="td-winner-info">
          <div class="tw-trophy">🏆</div>
          <div class="tw-name" id="terima_nama">—</div>
          <div class="tw-nominal" id="terima_nominal">—</div>
        </div>

        <label class="td-modal-label">Keluarkan dari Kas <small style="text-transform:none;font-weight:400;color:var(--slate-400)">(opsional)</small></label>
        <select name="kas_id" class="td-modal-select" id="terimaKasSelect">
          <option value="">— Tidak dikurangi dari 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, dana pemenang akan otomatis tercatat sebagai <strong>pengeluaran</strong> di kas tersebut
        </div>

        <div class="warn-box">
          <i class="bi bi-exclamation-triangle-fill" style="flex-shrink:0"></i>
          <span>Pastikan dana sudah benar-benar diserahkan kepada pemenang sebelum mengkonfirmasi.</span>
        </div>
      </div>
      <div class="td-modal-footer">
        <button type="button" class="td-modal-cancel" onclick="closeModalTerima()">Batal</button>
        <button type="submit" class="td-modal-confirm">
          <i class="bi bi-check-circle"></i> Konfirmasi Diterima
        </button>
      </div>
    </form>
  </div>
</div>
<?php endif; ?>

<script>
function openModalTerima(id, nama, nominal) {
  document.getElementById('terima_nama').textContent    = nama;
  document.getElementById('terima_nominal').textContent = 'Rp ' + nominal;
  document.getElementById('formTerima').action = '<?php echo base_url('arisan/terima_dana/'); ?>' + id;
  document.getElementById('modalTerimaBackdrop').classList.add('show');
}
function closeModalTerima() {
  document.getElementById('modalTerimaBackdrop').classList.remove('show');
}
document.getElementById('modalTerimaBackdrop')?.addEventListener('click', function(e) {
  if (e.target === this) closeModalTerima();
});

$(document).ready(function() {
  $('#tableRiwayat').DataTable({
    language: { url: '//cdn.datatables.net/plug-ins/1.13.7/i18n/id.json' },
    order: [[0, 'desc']], pageLength: 50, responsive: true,
    dom: '<"rw-dt-top"lf>rt<"rw-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() {
    ['flashSuccess','flashError'].forEach(function(id) {
      var el = document.getElementById(id);
      if (el) { el.style.transition='opacity .4s'; el.style.opacity='0'; setTimeout(function(){ el.remove(); }, 420); }
    });
  }, 4500);
});
</script>