<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Daftar Pemilih Pemilu — <?php echo date('d F Y'); ?></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;
}

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

/* ─── WRAPPER ─── */
.page-wrap {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px 16px 60px;
}

/* ─── ACTION BAR (layar saja) ─── */
.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-excel { background: linear-gradient(135deg,#059669,#10b981); color:#fff; }
.btn-excel:hover { transform:translateY(-1px); color:#fff; }
.btn-print { background: linear-gradient(135deg,var(--teal-700),var(--teal-500)); color:#fff; }
.btn-print:hover { transform:translateY(-1px); color:#fff; }

/* ─── HEADER CETAK ─── */
.doc-header {
  background: #fff;
  border-radius: 12px;
  padding: 18px 20px 14px;
  margin-bottom: 12px;
  box-shadow: 0 1px 4px rgba(0,0,0,.07);
  border-top: 4px solid var(--teal-600);
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 12px;
}
.dh-title {
  font-size: 16px; font-weight: 900;
  color: var(--teal-900); letter-spacing: -.2px;
  margin-bottom: 4px;
}
.dh-sub {
  font-size: 10.5px; color: var(--slate-500); line-height: 1.65;
}
.dh-sub strong { color: var(--slate-700); }
.dh-right {
  text-align: right; font-size: 10.5px;
  color: var(--slate-500); line-height: 1.75;
  white-space: nowrap;
}
.dh-right strong { color: var(--slate-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 9px; text-align: left;
  white-space: nowrap;
  border-right: 1px solid rgba(255,255,255,.07);
}
.data-table thead th:last-child { border-right: none; }
.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; }

/* cell helpers */
.tc   { text-align: center; }
.mono { font-family: 'Courier New', monospace; font-size: 9.5px; color: var(--slate-500); letter-spacing: .02em; }
.num  { color: var(--slate-400); font-weight: 700; font-size: 9.5px; }

.nama-text  { font-weight: 700; font-size: 11px; color: var(--slate-900); }
.ttl-text   { font-size: 9.5px; color: var(--slate-400); margin-top: 1px; }

/* badges */
.jk-badge {
  display: inline-flex; align-items: center; gap: 2px;
  padding: 2px 7px; border-radius: 999px;
  font-size: 9.5px; font-weight: 800;
}
.jk-l { background: #dbeafe; color: #1d4ed8; }
.jk-p { background: #fce7f3; color: #be185d; }

.umur-val { font-weight: 900; color: var(--teal-700); font-size: 12px; }
.umur-sat { font-size: 9.5px; color: var(--slate-400); }

.kel-badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px; border-radius: 999px;
  font-size: 9.5px; font-weight: 700;
}
.kel-dot { width: 5px; height: 5px; border-radius: 50%; flex-shrink: 0; }

.dom-badge {
  display: inline-block; padding: 2px 7px;
  border-radius: 999px; font-size: 9.5px; font-weight: 700;
}
.dom-tetap     { background: #dcfce7; color: #166534; }
.dom-kost      { background: #fef3c7; color: #92400e; }
.dom-kontrak   { background: #cffafe; color: #155e75; }
.dom-pendatang { background: #fee2e2; color: #991b1b; }
.dom-sementara { background: #f3f4f6; color: #6b7280; }

/* ─── 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); }

/* ══════════════════════════
   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: 13px; }
  .dh-sub, .dh-right { font-size: 8.5px; }

  .table-card { border-radius: 0; box-shadow: none; border: 1px solid var(--slate-200); }
  .table-head { padding: 6px 10px; }
  .th-title   { font-size: 10px; }
  .th-pill    { font-size: 9px; padding: 2px 8px; }

  .data-table thead th { padding: 6px 6px; font-size: 7px; }
  .data-table tbody td { padding: 4px 6px; font-size: 8.5px; }
  .nama-text  { font-size: 8.5px; }
  .ttl-text   { font-size: 7.5px; }
  .jk-badge, .kel-badge, .dom-badge { font-size: 7.5px; padding: 1px 5px; }
  .umur-val   { font-size: 9px; }
  .mono       { font-size: 7.5px; }
  .num        { font-size: 7.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;
  }

  tbody tr { page-break-inside: avoid; }
}
</style>
</head>
<body>
<?php
// ── helper & variabel ──────────────────────────────────────────
$today = new DateTime();

$dom_label   = ['tetap'=>'Tetap','kost'=>'Kost','kontrak'=>'Kontrak','pendatang'=>'Pendatang','sementara'=>'Sementara'];
$kawin_label = ['belum_kawin'=>'Belum Kawin','kawin'=>'Kawin','cerai_hidup'=>'Cerai Hidup','cerai_mati'=>'Cerai Mati'];

$kel_cfg = [
    'Pemula' => ['bg'=>'#dcfce7','txt'=>'#166534','dot'=>'#22c55e'],
    'Muda'   => ['bg'=>'#dbeafe','txt'=>'#1e40af','dot'=>'#3b82f6'],
    'Dewasa' => ['bg'=>'#ede9fe','txt'=>'#5b21b6','dot'=>'#8b5cf6'],
    'Lansia' => ['bg'=>'#fef3c7','txt'=>'#92400e','dot'=>'#f59e0b'],
    'Tua'    => ['bg'=>'#fee2e2','txt'=>'#991b1b','dot'=>'#ef4444'],
];

function kel_p($u) {
    if ($u >= 17 && $u <= 21) return 'Pemula';
    if ($u >= 22 && $u <= 35) return 'Muda';
    if ($u >= 36 && $u <= 55) return 'Dewasa';
    if ($u >= 56 && $u <= 70) return 'Lansia';
    return 'Tua';
}

// ── hitung statistik ──────────────────────────────────────────
$total = count($pemilih);
$pria = $wanita = 0;
$rows = [];

foreach ($pemilih as $p) {
    $umur = 0;
    if (!empty($p->tanggal_lahir)) $umur = $today->diff(new DateTime($p->tanggal_lahir))->y;
    $kel = kel_p($umur);
    if (($p->jenis_kelamin ?? '') === 'L') $pria++; else $wanita++;
    $rows[] = ['p' => $p, 'umur' => $umur, 'kel' => $kel];
}
?>

<div class="page-wrap">

  <!-- ACTION BAR -->
  <div class="action-bar">
    <div class="ab-left">
      <div class="ab-icon"><i class="bi bi-person-vcard-fill"></i></div>
      Daftar Pemilih Pemilu
    </div>
    <div class="ab-right">
      <a href="<?php echo base_url('warga/pemilih'); ?>" class="btn btn-back">
        <i class="bi bi-arrow-left"></i> Kembali
      </a>
      <a href="<?php echo base_url('export/index/pemilih/excel'); ?>" class="btn btn-excel">
        <i class="bi bi-file-earmark-excel-fill"></i> Excel
      </a>
      <button onclick="window.print()" class="btn btn-print">
        <i class="bi bi-printer-fill"></i> Cetak / PDF
      </button>
    </div>
  </div>

  <!-- HEADER CETAK -->
  <div class="doc-header">
    <div>
      <div class="dh-title">DAFTAR PEMILIH PEMILU</div>
      <div class="dh-sub">
        WNI usia <strong>≥ 17 tahun</strong> atau sudah/pernah menikah &amp; bukan warga meninggal<br>
        Data per: <strong><?php echo date('d F Y'); ?></strong>
        &nbsp;·&nbsp; Total: <strong><?php echo number_format($total); ?> pemilih</strong>
        &nbsp;·&nbsp; L: <strong><?php echo $pria; ?></strong>
        &nbsp;·&nbsp; P: <strong><?php echo $wanita; ?></strong>
      </div>
    </div>
    <div class="dh-right">
      Dicetak: <strong><?php echo date('d/m/Y H:i'); ?> WIB</strong>
    </div>
  </div>

  <!-- TABLE -->
  <div class="table-card">
    <div class="table-head">
      <div class="th-title">
        <i class="bi bi-person-vcard"></i>
        Daftar Lengkap Pemilih
      </div>
      <span class="th-pill"><?php echo number_format($total); ?> orang</span>
    </div>

    <?php if (!empty($pemilih)): ?>
    <table class="data-table">
      <thead>
        <tr>
          <th style="width:28px;">No</th>
          <th style="width:108px;">NIK</th>
          <th>Nama Lengkap</th>
          <th style="width:36px;">L/P</th>
          <th style="width:72px;">Tgl Lahir</th>
          <th style="width:48px;">Umur</th>
          <th style="width:72px;">Kelompok</th>
          <th style="width:88px;">Status Kawin</th>
          <th style="width:66px;">Domisili</th>
          <th style="width:108px;">No KK</th>
        </tr>
      </thead>
      <tbody>
      <?php $no = 1; foreach ($rows as $r):
        $p   = $r['p'];
        $umur = $r['umur'];
        $kel  = $r['kel'];
        $jk   = $p->jenis_kelamin ?? '';
        $dom  = $p->status_domisili ?? 'tetap';
        $bg   = $no % 2 === 0 ? '#f8fafc' : '#fff';
        $kc   = $kel_cfg[$kel] ?? $kel_cfg['Pemula'];
      ?>
      <tr style="background:<?php echo $bg; ?>;">

        <td class="tc num"><?php echo $no++; ?></td>

        <td class="mono"><?php echo htmlspecialchars($p->nik ?? '-'); ?></td>

        <td>
          <div class="nama-text"><?php echo htmlspecialchars($p->nama_lengkap ?? '-'); ?></div>
          <?php if (!empty($p->tempat_lahir)): ?>
          <div class="ttl-text"><?php echo htmlspecialchars($p->tempat_lahir); ?></div>
          <?php endif; ?>
        </td>

        <td class="tc">
          <span class="jk-badge <?php echo $jk === 'L' ? 'jk-l' : 'jk-p'; ?>">
            <?php echo $jk === 'L' ? '♂ L' : '♀ P'; ?>
          </span>
        </td>

        <td class="tc" style="font-size:10px;color:var(--slate-600);">
          <?php echo !empty($p->tanggal_lahir) ? date('d/m/Y', strtotime($p->tanggal_lahir)) : '-'; ?>
        </td>

        <td class="tc">
          <span class="umur-val"><?php echo $umur; ?></span>
          <span class="umur-sat"> thn</span>
        </td>

        <td class="tc">
          <span class="kel-badge" style="background:<?php echo $kc['bg']; ?>;color:<?php echo $kc['txt']; ?>;">
            <span class="kel-dot" style="background:<?php echo $kc['dot']; ?>;"></span>
            <?php echo $kel; ?>
          </span>
        </td>

        <td style="font-size:10px;color:var(--slate-600);">
          <?php echo $kawin_label[$p->status_perkawinan ?? ''] ?? ($p->status_perkawinan ?? '-'); ?>
        </td>

        <td class="tc">
          <span class="dom-badge dom-<?php echo $dom; ?>">
            <?php echo $dom_label[$dom] ?? ucfirst($dom); ?>
          </span>
        </td>

        <td>
          <div class="mono"><?php echo htmlspecialchars($p->no_kk ?? '-'); ?></div>
          <?php if (!empty($p->nama_kepala_kk)): ?>
          <div class="ttl-text"><?php echo htmlspecialchars($p->nama_kepala_kk); ?></div>
          <?php endif; ?>
        </td>

      </tr>
      <?php endforeach; ?>
      </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 data pemilih</div>
      <div style="font-size:11px;">Pastikan tanggal lahir warga sudah diisi</div>
    </div>
    <?php endif; ?>
  </div>

  <!-- FOOTER -->
  <div class="doc-footer">
    <div>
      Ketentuan: WNI usia ≥ 17 tahun atau sudah/pernah menikah, tidak termasuk warga meninggal.<br>
      Berdasarkan UU No. 7/2017 &amp; Peraturan KPU. Data otomatis dari sistem.
    </div>
    <div style="text-align:right;">
      <?php echo date('d F Y, H:i'); ?> WIB<br>
      <strong><?php echo number_format($total); ?> pemilih</strong>
    </div>
  </div>

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