<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Mutasi Tabungan — <?php echo isset($warga->nama_lengkap) ? $warga->nama_lengkap : 'Warga'; ?></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap');

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --teal-900: #0b5a52;
  --teal-700: #0b7a6e;
  --teal-600: #0d9488;
  --teal-500: #14b8a6;
  --teal-100: #ccfbf1;
  --teal-50:  #f0fdfa;
  --navy:     #1e3a5f;
  --slate-900: #0f172a;
  --slate-700: #334155;
  --slate-600: #475569;
  --slate-500: #64748b;
  --slate-400: #94a3b8;
  --slate-200: #e2e8f0;
  --slate-100: #f1f5f9;
  --slate-50:  #f8fafc;
  --green-700: #15803d;
  --green-50:  #f0fdf4;
  --red-700:   #b91c1c;
  --red-50:    #fef2f2;
}

body {
  font-family: 'Inter', Arial, sans-serif;
  font-size: 12px;
  color: var(--slate-900);
  background: #eef2f7;
  line-height: 1.5;
}

.page-wrap {
  max-width: 820px;
  margin: 0 auto;
  padding: 20px 16px 60px;
}

/* ─── ACTION BAR ─── */
.action-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  background: #fff;
  border-radius: 12px;
  padding: 11px 16px;
  margin-bottom: 16px;
  box-shadow: 0 1px 6px rgba(0,0,0,.08);
}
.ab-left {
  display: flex; align-items: center; gap: 9px;
  font-size: 13px; font-weight: 800; color: var(--teal-900);
}
.ab-icon {
  width: 32px; height: 32px;
  background: linear-gradient(135deg, var(--teal-600), var(--teal-500));
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 14px;
}
.ab-right { display: flex; gap: 7px; flex-wrap: wrap; }
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 16px; border-radius: 50px; border: none;
  font-size: 11.5px; font-weight: 700; cursor: pointer;
  text-decoration: none; font-family: 'Inter', sans-serif;
  transition: all .15s; white-space: nowrap;
}
.btn-back  { background: var(--slate-100); color: var(--slate-700); border: 1.5px solid var(--slate-200); }
.btn-back:hover  { background: var(--slate-200); }
.btn-print { background: linear-gradient(135deg, var(--teal-700), var(--teal-500)); color: #fff; }
.btn-print:hover { transform: translateY(-1px); color: #fff; }

/* ─── DOC HEADER ─── */
.doc-header {
  background: #fff;
  border-radius: 12px;
  padding: 20px 22px 16px;
  margin-bottom: 14px;
  box-shadow: 0 1px 4px rgba(0,0,0,.07);
  border-top: 4px solid var(--teal-600);
}
.dh-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 14px;
  margin-bottom: 14px;
  padding-bottom: 14px;
  border-bottom: 1.5px solid var(--slate-100);
}
.dh-title {
  font-size: 17px; font-weight: 900;
  color: var(--teal-900); letter-spacing: -.3px;
  margin-bottom: 3px;
}
.dh-sub { font-size: 10.5px; color: var(--slate-500); line-height: 1.7; }
.dh-sub strong { color: var(--slate-700); }
.dh-right { text-align: right; font-size: 10.5px; color: var(--slate-500); line-height: 1.8; }
.dh-right strong { color: var(--slate-700); }

/* ─── INFO GRID ─── */
.info-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px 20px;
}
.info-item label {
  font-size: 9.5px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .05em; color: var(--slate-400); display: block; margin-bottom: 1px;
}
.info-item span { font-size: 11.5px; font-weight: 600; color: var(--slate-900); }

/* ─── SUMMARY STRIP ─── */
.summary-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}
.s-card {
  background: #fff;
  border-radius: 10px;
  padding: 13px 16px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
  border: 1px solid var(--slate-200);
}
.s-card-label { font-size: 9.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--slate-400); margin-bottom: 4px; }
.s-card-value { font-size: 13px; font-weight: 900; color: var(--slate-900); }
.s-card-value.debit  { color: var(--green-700); }
.s-card-value.kredit { color: var(--red-700); }
.s-card-value.saldo  { color: var(--teal-700); }

/* ─── TABLE CARD ─── */
.table-card {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 1px 8px rgba(0,0,0,.08);
}
.table-head {
  padding: 11px 16px;
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: 8px;
  background: linear-gradient(90deg, var(--teal-50), #fff);
  border-bottom: 1.5px solid rgba(13,148,136,.1);
}
.th-title { font-size: 12.5px; font-weight: 800; color: var(--slate-900); display: flex; align-items: center; gap: 6px; }
.th-title i { color: var(--teal-600); }
.th-pill {
  background: var(--teal-50); color: var(--teal-700);
  border: 1px solid var(--teal-100);
  padding: 3px 11px; border-radius: 999px;
  font-size: 10.5px; font-weight: 700;
}

/* ─── TABLE ─── */
.data-table { width: 100%; border-collapse: collapse; }
.data-table thead tr { background: var(--navy); }
.data-table thead th {
  color: #fff; font-size: 8px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .07em;
  padding: 9px; text-align: left;
  border-right: 1px solid rgba(255,255,255,.07);
}
.data-table thead th:last-child { border-right: none; }
.data-table thead th.right { text-align: right; }
.data-table tbody tr { border-bottom: 1px solid #f0f4f8; }
.data-table tbody tr:last-child { border-bottom: none; }
.data-table tbody tr:hover { background: var(--teal-50) !important; }
.data-table tbody td { padding: 7px 9px; vertical-align: middle; font-size: 11px; }

.tc  { text-align: center; }
.tr  { text-align: right; }
.mono { font-family: 'Courier New', monospace; font-size: 9.5px; color: var(--slate-500); }
.num  { color: var(--slate-400); font-weight: 700; font-size: 9.5px; }

.amt-debit  { font-weight: 700; color: var(--green-700); font-size: 11px; }
.amt-kredit { font-weight: 700; color: var(--red-700);   font-size: 11px; }
.amt-saldo  { font-weight: 800; color: var(--teal-700);  font-size: 11px; }
.amt-zero   { color: var(--slate-400); font-size: 10.5px; }

.jenis-badge {
  display: inline-flex; align-items: center; gap: 3px;
  padding: 2px 8px; border-radius: 999px; font-size: 9.5px; font-weight: 700;
}
.badge-setor { background: #dcfce7; color: #15803d; }
.badge-tarik { background: #fee2e2; color: #b91c1c; }

.status-badge {
  display: inline-block; padding: 2px 7px;
  border-radius: 999px; font-size: 9px; font-weight: 700;
}
.st-disetujui { background: #dcfce7; color: #15803d; }
.st-pending   { background: #fef3c7; color: #92400e; }
.st-ditolak   { background: #fee2e2; color: #b91c1c; }

/* ─── SALDO AKHIR ROW ─── */
.saldo-akhir-row td {
  background: linear-gradient(90deg, var(--teal-50), #fff) !important;
  border-top: 2px solid var(--teal-500) !important;
  font-weight: 800 !important;
  padding: 10px 9px !important;
}

/* ─── FOOTER ─── */
.doc-footer {
  margin-top: 14px;
  display: flex; justify-content: space-between; align-items: flex-start;
  flex-wrap: wrap; gap: 10px;
  background: #fff; border-radius: 10px;
  padding: 12px 16px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
  font-size: 10px; color: var(--slate-500); line-height: 1.7;
}
.doc-footer strong { color: var(--slate-700); }

/* ─── TTD ─── */
.ttd-section {
  margin-top: 10px;
  background: #fff; border-radius: 10px;
  padding: 16px 20px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
  display: grid; grid-template-columns: 1fr 1fr; gap: 20px;
}
.ttd-box { text-align: center; }
.ttd-box .ttd-label { font-size: 10px; font-weight: 600; color: var(--slate-600); }
.ttd-box .ttd-space { height: 52px; margin: 8px 0; }
.ttd-box .ttd-name  { font-size: 10.5px; font-weight: 700; color: var(--slate-900); border-top: 1px solid var(--slate-300); padding-top: 5px; }

/* ══════ PRINT ══════ */
@media print {
  @page { size: A4 portrait; margin: 10mm 8mm; }
  body { background: #fff; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .action-bar { display: none !important; }
  .page-wrap  { padding: 0; max-width: 100%; }
  .doc-header { border-radius: 0; padding: 10px 12px 8px; margin-bottom: 8px; box-shadow: none; border-top: 3px solid var(--teal-600); }
  .dh-title { font-size: 14px; }
  .dh-sub, .dh-right, .info-item span { font-size: 8.5px; }
  .info-item label { font-size: 8px; }
  .summary-strip { gap: 7px; margin-bottom: 8px; }
  .s-card { padding: 9px 11px; border-radius: 6px; }
  .s-card-label { font-size: 8px; }
  .s-card-value { font-size: 11px; }
  .table-card { border-radius: 0; box-shadow: none; border: 1px solid var(--slate-200); }
  .data-table thead th { padding: 6px; font-size: 7px; }
  .data-table tbody td { padding: 4px 6px; font-size: 8.5px; }
  .amt-debit, .amt-kredit, .amt-saldo { font-size: 8.5px; }
  .jenis-badge, .status-badge { font-size: 7.5px; padding: 1px 5px; }
  .doc-footer { margin-top: 8px; padding: 8px 12px; border-radius: 0; box-shadow: none; border-top: 1px solid var(--slate-200); font-size: 7.5px; }
  .ttd-section { border-radius: 0; box-shadow: none; padding: 12px; margin-top: 8px; }
  .ttd-box .ttd-label, .ttd-box .ttd-name { font-size: 8.5px; }
  tbody tr { page-break-inside: avoid; }
}
</style>
</head>
<body>

<?php
// ── Kalkulasi mutasi ──────────────────────────────────────────
$saldo_awal     = isset($tabungan->saldo_awal) ? (float)$tabungan->saldo_awal : 0;
$saldo_berjalan = $saldo_awal;
$total_debit    = 0;
$total_kredit   = 0;
$rows           = [];

if (!empty($transaksi)) {
    foreach ($transaksi as $t) {
        $nominal = isset($t->nominal) ? (float)$t->nominal : 0;
        $jenis   = isset($t->jenis_transaksi) ? strtolower($t->jenis_transaksi) : '';
        if ($jenis === 'setor') {
            $debit = $nominal; $kredit = 0;
            $saldo_berjalan += $nominal; $total_debit += $nominal;
        } elseif ($jenis === 'tarik') {
            $debit = 0; $kredit = $nominal;
            $saldo_berjalan -= $nominal; $total_kredit += $nominal;
        } else {
            $debit = $kredit = 0;
        }
        $rows[] = ['t' => $t, 'debit' => $debit, 'kredit' => $kredit, 'saldo_r' => $saldo_berjalan];
    }
}

$saldo_akhir = $saldo_berjalan;
$total_trx   = count($rows);

$nama_warga = isset($warga->nama_lengkap) ? $warga->nama_lengkap : '-';
$nik_warga  = isset($warga->nik)          ? $warga->nik          : '-';
$nohp_warga = isset($warga->no_hp)        ? $warga->no_hp        : '-';
$jenis_tab  = isset($tabungan->jenis_tabungan) ? ucfirst($tabungan->jenis_tabungan) : '-';
$status_tab = isset($tabungan->status)    ? ucfirst($tabungan->status) : '-';

$tgl_awal  = !empty($transaksi) ? date('d/m/Y', strtotime(reset($transaksi)->tanggal_transaksi ?? date('Y-m-d'))) : '-';
$tgl_akhir = !empty($transaksi) ? date('d/m/Y', strtotime(end($transaksi)->tanggal_transaksi   ?? date('Y-m-d'))) : '-';
?>

<div class="page-wrap">

  <!-- ACTION BAR -->
  <div class="action-bar">
    <div class="ab-left">
      <div class="ab-icon"><i class="bi bi-wallet2"></i></div>
      Cetak Mutasi Tabungan
    </div>
    <div class="ab-right">
      <a href="<?php echo base_url('tabungan/detail/' . (isset($warga->id) ? $warga->id : '')); ?>" 
         class="btn btn-back">
        <i class="bi bi-arrow-left"></i> Kembali
      </a>
      <button onclick="window.print()" class="btn btn-print">
        <i class="bi bi-printer-fill"></i> Cetak / PDF
      </button>
    </div>
  </div>

  <!-- HEADER -->
  <div class="doc-header">
    <div class="dh-top">
      <div>
        <div class="dh-title">BUKU MUTASI TABUNGAN</div>
        <div class="dh-sub">
          Jenis: <strong><?php echo $jenis_tab; ?></strong>
          &nbsp;·&nbsp; Status: <strong><?php echo $status_tab; ?></strong><br>
          Periode: <strong><?php echo $tgl_awal; ?></strong>
          s/d <strong><?php echo $tgl_akhir; ?></strong>
          &nbsp;·&nbsp; Total Transaksi: <strong><?php echo number_format($total_trx); ?></strong>
        </div>
      </div>
      <div class="dh-right">
        Dicetak: <strong><?php echo date('d/m/Y H:i'); ?> WIB</strong><br>
        Tanggal: <strong><?php echo date('d F Y'); ?></strong>
      </div>
    </div>
    <div class="info-grid">
      <div class="info-item">
        <label>Nama Warga</label>
        <span><?php echo htmlspecialchars($nama_warga); ?></span>
      </div>
      <div class="info-item">
        <label>NIK</label>
        <span style="font-family:'Courier New',monospace;font-size:11px;"><?php echo htmlspecialchars($nik_warga); ?></span>
      </div>
      <div class="info-item">
        <label>No HP</label>
        <span><?php echo htmlspecialchars($nohp_warga); ?></span>
      </div>
      <div class="info-item">
        <label>Jenis Tabungan</label>
        <span><?php echo $jenis_tab; ?></span>
      </div>
      <div class="info-item">
        <label>Saldo Awal</label>
        <span>Rp <?php echo number_format($saldo_awal, 0, ',', '.'); ?></span>
      </div>
      <div class="info-item">
        <label>Saldo Akhir</label>
        <span style="color:var(--teal-700);font-weight:800;">Rp <?php echo number_format($saldo_akhir, 0, ',', '.'); ?></span>
      </div>
    </div>
  </div>

  <!-- SUMMARY STRIP -->
  <div class="summary-strip">
    <div class="s-card">
      <div class="s-card-label">Total Transaksi</div>
      <div class="s-card-value"><?php echo $total_trx; ?> trx</div>
    </div>
    <div class="s-card">
      <div class="s-card-label">Total Setoran (Debit)</div>
      <div class="s-card-value debit">Rp <?php echo number_format($total_debit, 0, ',', '.'); ?></div>
    </div>
    <div class="s-card">
      <div class="s-card-label">Total Penarikan (Kredit)</div>
      <div class="s-card-value kredit">Rp <?php echo number_format($total_kredit, 0, ',', '.'); ?></div>
    </div>
    <div class="s-card">
      <div class="s-card-label">Saldo Akhir</div>
      <div class="s-card-value saldo">Rp <?php echo number_format($saldo_akhir, 0, ',', '.'); ?></div>
    </div>
  </div>

  <!-- TABLE MUTASI -->
  <div class="table-card">
    <div class="table-head">
      <div class="th-title">
        <i class="bi bi-arrow-left-right"></i>
        Rincian Mutasi Tabungan
      </div>
      <span class="th-pill"><?php echo number_format($total_trx); ?> transaksi</span>
    </div>

    <?php if (!empty($rows)): ?>
    <table class="data-table">
      <thead>
        <tr>
          <th style="width:28px;">No</th>
          <th style="width:88px;">Tgl Transaksi</th>
          <th style="width:70px;">Kode</th>
          <th>Keterangan</th>
          <th style="width:60px;" class="tc">Jenis</th>
          <th style="width:108px;" class="right">Debit (+)</th>
          <th style="width:108px;" class="right">Kredit (−)</th>
          <th style="width:112px;" class="right">Saldo</th>
          <th style="width:68px;" class="tc">Status</th>
          <th style="width:75px;">Kas</th>
        </tr>
      </thead>
      <tbody>
        <?php $no = 1; foreach ($rows as $r):
          $t      = $r['t'];
          $jenis  = isset($t->jenis_transaksi) ? strtolower($t->jenis_transaksi) : '';
          $status = isset($t->status) ? strtolower($t->status) : 'pending';
          $tgl    = !empty($t->tanggal_transaksi) ? date('d/m/Y', strtotime($t->tanggal_transaksi)) : '-';
          $kode   = isset($t->id) ? str_pad($t->id, 6, '0', STR_PAD_LEFT) : '-';
          $ket    = isset($t->keterangan) ? $t->keterangan : '-';
          $kas    = isset($t->nama_kas) ? $t->nama_kas : (isset($t->master_kas_id) ? 'Kas #'.$t->master_kas_id : '-');
          $bg     = $no % 2 === 0 ? '#f8fafc' : '#fff';
          $st_label = ['disetujui' => 'Disetujui', 'pending' => 'Pending', 'ditolak' => 'Ditolak'];
        ?>
        <tr style="background:<?php echo $bg; ?>;">
          <td class="tc num"><?php echo $no++; ?></td>
          <td style="font-size:10.5px; color:var(--slate-600);"><?php echo $tgl; ?></td>
          <td class="mono">TRX-<?php echo $kode; ?></td>
          <td style="font-size:10.5px; color:var(--slate-700);"><?php echo htmlspecialchars($ket); ?></td>
          <td class="tc">
            <span class="jenis-badge badge-<?php echo $jenis; ?>">
              <?php echo $jenis === 'setor' ? '↑ Setor' : '↓ Tarik'; ?>
            </span>
          </td>
          <td class="tr">
            <?php if ($r['debit'] > 0): ?>
              <span class="amt-debit">Rp <?php echo number_format($r['debit'], 0, ',', '.'); ?></span>
            <?php else: ?>
              <span class="amt-zero">—</span>
            <?php endif; ?>
          </td>
          <td class="tr">
            <?php if ($r['kredit'] > 0): ?>
              <span class="amt-kredit">Rp <?php echo number_format($r['kredit'], 0, ',', '.'); ?></span>
            <?php else: ?>
              <span class="amt-zero">—</span>
            <?php endif; ?>
          </td>
          <td class="tr">
            <span class="amt-saldo">Rp <?php echo number_format($r['saldo_r'], 0, ',', '.'); ?></span>
          </td>
          <td class="tc">
            <span class="status-badge st-<?php echo $status; ?>">
              <?php echo $st_label[$status] ?? ucfirst($status); ?>
            </span>
          </td>
          <td style="font-size:9.5px; color:var(--slate-500);"><?php echo htmlspecialchars($kas); ?></td>
        </tr>
        <?php endforeach; ?>

        <!-- SALDO AKHIR ROW -->
        <tr class="saldo-akhir-row">
          <td colspan="5" style="font-size:11px; color:var(--teal-900);">
            <i class="bi bi-bookmark-fill" style="color:var(--teal-500);"></i>
            &nbsp;SALDO AKHIR
          </td>
          <td class="tr" style="color:var(--green-700);">Rp <?php echo number_format($total_debit, 0, ',', '.'); ?></td>
          <td class="tr" style="color:var(--red-700);">Rp <?php echo number_format($total_kredit, 0, ',', '.'); ?></td>
          <td class="tr" style="color:var(--teal-700); font-size:12px;">Rp <?php echo number_format($saldo_akhir, 0, ',', '.'); ?></td>
          <td colspan="2"></td>
        </tr>
      </tbody>
    </table>

    <?php else: ?>
    <div style="padding:50px 20px; text-align:center; color:var(--slate-400);">
      <div style="font-size:40px; margin-bottom:10px;">💰</div>
      <div style="font-weight:800; font-size:13px; color:var(--slate-600); margin-bottom:3px;">Belum ada mutasi tabungan</div>
      <div style="font-size:11px;">Belum ada transaksi yang tercatat untuk rekening ini</div>
    </div>
    <?php endif; ?>
  </div>

  <!-- FOOTER INFO -->
  <div class="doc-footer">
    <div>
      Dokumen ini merupakan rekap mutasi tabungan resmi.<br>
      Harap simpan sebagai bukti transaksi. Data otomatis dari sistem.
    </div>
    <div style="text-align:right;">
      <?php echo date('d F Y, H:i'); ?> WIB<br>
      <strong>Saldo Akhir: Rp <?php echo number_format($saldo_akhir, 0, ',', '.'); ?></strong>
    </div>
  </div>

  <!-- TTD -->
  <div class="ttd-section">
    <div class="ttd-box">
      <div class="ttd-label">Nasabah / Warga</div>
      <div class="ttd-space"></div>
      <div class="ttd-name">(<?php echo htmlspecialchars($nama_warga); ?>)</div>
    </div>
    <div class="ttd-box">
      <div class="ttd-label">Petugas / Admin</div>
      <div class="ttd-space"></div>
      <div class="ttd-name">(__________________________)</div>
    </div>
  </div>

</div>
</body>
</html>