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

<style>
/* ─── DESIGN TOKENS ──────────────────────────────────────── */
: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);
}

/* ─── WRAPPER ────────────────────────────────────────────── */
.pt-wrapper {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 28px 60px;
}

/* ─── PAGE HEADER ────────────────────────────────────────── */
.pt-page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 12px;
}
.pt-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;
  letter-spacing: -.3px;
}
.pt-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;
}
.pt-breadcrumb {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.77rem;
  color: var(--slate-400);
  padding-left: 51px;
}
.pt-breadcrumb a { color: var(--teal-600); text-decoration: none; font-weight: 600; }
.pt-breadcrumb a:hover { text-decoration: underline; }
.pt-breadcrumb .sep { color: var(--slate-200); }

.btn-tambah-pt {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 10px 22px;
  border-radius: 50px;
  border: none;
  background: linear-gradient(135deg, var(--teal-800), var(--teal-600));
  color: #fff;
  font-size: 0.85rem;
  font-weight: 700;
  text-decoration: none;
  box-shadow: 0 3px 14px rgba(13,148,136,0.36);
  transition: all .2s;
  white-space: nowrap;
}
.btn-tambah-pt:hover {
  background: linear-gradient(135deg, var(--teal-900), var(--teal-700));
  box-shadow: 0 6px 18px rgba(13,148,136,0.44);
  transform: translateY(-1px);
  color: #fff;
}

/* ─── FLASH ALERTS ────────────────────────────────────────── */
.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;
}
.flash-alert .fa-close:hover { opacity: 1; }

/* ─── INFO ARISAN CARD ───────────────────────────────────── */
.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); }

.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); }
.btn-outline-sm.amber {
  border-color: var(--amber-100);
  background: var(--amber-50);
  color: var(--amber-600);
}
.btn-outline-sm.amber:hover { border-color: var(--amber-400); background: var(--amber-100); }

/* ─── WARNING BELUM ADA PESERTA ──────────────────────────── */
.warn-card {
  background: var(--amber-50);
  border: 1.5px solid var(--amber-100);
  border-left: 4px solid var(--amber-400);
  border-radius: var(--radius-sm);
  padding: 16px 18px;
  margin-bottom: 18px;
  display: flex;
  align-items: flex-start;
  gap: 12px;
}
.warn-card i { color: var(--amber-500); font-size: 1.1rem; flex-shrink: 0; margin-top: 1px; }
.warn-card h6 { font-weight: 700; color: var(--amber-600); margin: 0 0 5px; font-size: 0.9rem; }
.warn-card p  { font-size: 0.83rem; color: var(--slate-500); margin: 0 0 10px; }
.btn-warn-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 16px;
  border-radius: 50px;
  border: 1.5px solid var(--amber-400);
  background: #fff;
  color: var(--amber-600);
  font-size: 0.81rem;
  font-weight: 700;
  text-decoration: none;
  transition: all .2s;
}
.btn-warn-link:hover { background: var(--amber-50); }

/* ─── MAIN CARD ───────────────────────────────────────────── */
.pt-card {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  border: 1.5px solid var(--slate-100);
}
.pt-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;
}
.pt-card-header h5 {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--teal-800);
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}
.pt-card-header h5 i { color: var(--teal-500); }

/* ─── EMPTY STATE ────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 60px 24px;
}
.empty-state-icon {
  width: 72px; height: 72px;
  border-radius: 50%;
  background: var(--teal-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; }

/* ─── TABLE ───────────────────────────────────────────────── */
.pt-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.pt-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 860px;
}
.pt-table thead tr {
  background: var(--slate-50);
  border-bottom: 2px solid var(--slate-200);
}
.pt-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;
}
.pt-table thead th.center { text-align: center; }
.pt-table tbody tr {
  border-bottom: 1px solid var(--slate-100);
  transition: background .15s;
}
.pt-table tbody tr:hover { background: var(--teal-50); }
.pt-table tbody tr:last-child { border-bottom: none; }
.pt-table tbody td {
  padding: 14px 18px;
  font-size: 0.87rem;
  color: var(--slate-700);
  vertical-align: middle;
}
.pt-table tbody td.center { text-align: center; }

/* ─── TABLE CELL PARTS ────────────────────────────────────── */
.ke-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 52px;
  height: 34px;
  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 10px;
  white-space: nowrap;
}

.date-cell { font-size: 0.83rem; color: var(--slate-700); white-space: nowrap; }
.date-cell i { color: var(--slate-400); }

.host-name { font-weight: 700; color: var(--slate-900); font-size: 0.88rem; }
.host-place { font-size: 0.76rem; color: var(--slate-400); margin-top: 2px; }

.winner-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 700;
  color: var(--green-600);
  font-size: 0.87rem;
}
.winner-cell i { color: var(--amber-400); }

.undian-pill {
  display: inline-block;
  border-radius: 50px;
  padding: 3px 12px;
  font-size: 0.75rem;
  font-weight: 800;
  background: var(--amber-50);
  color: var(--amber-600);
  border: 1px solid var(--amber-100);
}

.dana-cell { font-weight: 700; color: var(--green-600); font-size: 0.9rem; }

.status-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;
}
.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; }

/* ─── ACTION BUTTONS ───────────────────────────────────────── */
.act-btn-group {
  display: flex;
  gap: 6px;
  justify-content: center;
  align-items: center;
}

.act-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 6px 12px;
  border-radius: var(--radius-xs);
  font-size: 0.75rem;
  font-weight: 600;
  text-decoration: none;
  transition: all .18s;
  border: 1.5px solid;
  white-space: nowrap;
}

.act-btn.detail {
  border-color: var(--teal-100);
  color: var(--teal-700);
  background: var(--teal-50);
}
.act-btn.detail:hover { 
  background: var(--teal-100); 
  border-color: var(--teal-400); 
  color: var(--teal-800); 
}

.act-btn.edit {
  border-color: var(--amber-100);
  color: var(--amber-600);
  background: var(--amber-50);
}
.act-btn.edit:hover { 
  background: var(--amber-100); 
  border-color: var(--amber-400); 
  color: var(--amber-700); 
}

/* ─── DATATABLES OVERRIDES ───────────────────────────────── */
.pt-dt-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 24px 10px;
  flex-wrap: wrap;
  gap: 10px;
}
.pt-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;
}

/* ─── MOBILE ─────────────────────────────────────────────── */
@media (max-width: 767px) {
  .pt-wrapper     { padding: 0 12px 48px; }
  .pt-page-header { flex-direction: column; align-items: flex-start; }
  .arisan-info-card { flex-direction: column; }
  .aic-actions    { width: 100%; flex-wrap: wrap; }
}
</style>

<div class="pt-wrapper">

  <!-- ── Page Header ───────────────────────────────────────── -->
  <div class="pt-page-header">
    <div class="pt-header-left">
      <h4>
        <span class="hi"><i class="bi bi-calendar-event"></i></span>
        Pertemuan & Undian Arisan
      </h4>
      <div class="pt-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>Pertemuan</span>
      </div>
    </div>
    <?php if(is_admin() && $total_peserta > 0): ?>
    <a href="<?php echo base_url('arisan/tambah_pertemuan/' . $arisan->id); ?>" class="btn-tambah-pt">
      <i class="bi bi-plus-circle"></i> Tambah Pertemuan
    </a>
    <?php endif; ?>
  </div>

  <!-- ── Flash Alerts ──────────────────────────────────────── -->
  <?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; ?>

  <!-- ── Info Arisan Card ───────────────────────────────────── -->
  <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 Peserta</div>
          <div class="mi-value"><?php echo $total_peserta; ?> Orang</div>
        </div>
        <div class="aic-meta-item">
          <div class="mi-label">Iuran/Pertemuan</div>
          <div class="mi-value teal">Rp <?php echo number_format($arisan->nominal_iuran, 0, ',', '.'); ?></div>
        </div>
        <div class="aic-meta-item">
          <div class="mi-label">Dana/Pertemuan</div>
          <div class="mi-value green">Rp <?php echo number_format($arisan->nominal_iuran * $total_peserta, 0, ',', '.'); ?></div>
        </div>
        <div class="aic-meta-item">
          <div class="mi-label">Total Pertemuan</div>
          <div class="mi-value"><?php echo count($pertemuan); ?> Kali</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/peserta/' . $arisan->id); ?>" class="btn-outline-sm teal">
        <i class="bi bi-people"></i> Peserta
      </a>
      <a href="<?php echo base_url('arisan/riwayat/' . $arisan->id); ?>" class="btn-outline-sm amber">
        <i class="bi bi-trophy"></i> Riwayat
      </a>
    </div>
  </div>

  <!-- ── Warning Belum Ada Peserta ─────────────────────────── -->
  <?php if($total_peserta == 0): ?>
  <div class="warn-card">
    <i class="bi bi-exclamation-triangle-fill"></i>
    <div>
      <h6>Belum Ada Peserta!</h6>
      <p>Anda harus menambahkan peserta terlebih dahulu sebelum membuat pertemuan arisan.</p>
      <a href="<?php echo base_url('arisan/peserta/' . $arisan->id); ?>" class="btn-warn-link">
        <i class="bi bi-person-plus"></i> Tambah Peserta Sekarang
      </a>
    </div>
  </div>
  <?php endif; ?>

  <!-- ── Daftar Pertemuan Card ──────────────────────────────── -->
  <div class="pt-card">
    <div class="pt-card-header">
      <h5><i class="bi bi-calendar-event"></i> Daftar Pertemuan</h5>
    </div>

    <?php if(empty($pertemuan)): ?>
      <div class="empty-state">
        <div class="empty-state-icon">📅</div>
        <h6>Belum Ada Pertemuan</h6>
        <p>Tambahkan pertemuan pertama untuk memulai arisan.</p>
      </div>
    <?php else: ?>

      <div class="pt-table-wrap">
        <table class="pt-table" id="tablePertemuan">
          <thead>
            <tr>
              <th class="center" style="width:90px">Pertemuan</th>
              <th>Tanggal</th>
              <th>Tuan Rumah / Tempat</th>
              <th>Pemenang</th>
              <th class="center">No. Undian</th>
              <th>Total Dana</th>
              <th>Status</th>
              <th class="center" style="width:140px">Aksi</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach($pertemuan as $p): ?>
            <tr>
              <td class="center">
                <span class="ke-badge">Ke-<?php echo $p->pertemuan_ke; ?></span>
              </td>
              <td class="date-cell">
                <i class="bi bi-calendar3"></i>
                <?php echo date('d/m/Y', strtotime($p->tanggal_pertemuan)); ?>
              </td>
              <td>
                <?php if(!empty($p->nama_tuan_rumah)): ?>
                  <div class="host-name"><i class="bi bi-house-door" style="color:var(--teal-500)"></i> <?php echo $p->nama_tuan_rumah; ?></div>
                  <?php if(!empty($p->tempat_pertemuan)): ?>
                    <div class="host-place"><?php echo $p->tempat_pertemuan; ?></div>
                  <?php endif; ?>
                <?php else: ?>
                  <span style="color:var(--slate-400);font-size:.82rem">—</span>
                <?php endif; ?>
              </td>
              <td>
                <?php if(!empty($p->nama_pemenang)): ?>
                  <div class="winner-cell">
                    <i class="bi bi-trophy-fill"></i>
                    <?php echo $p->nama_pemenang; ?>
                  </div>
                <?php else: ?>
                  <span style="color:var(--slate-400);font-size:.82rem">Belum ada</span>
                <?php endif; ?>
              </td>
              <td class="center">
                <?php if(!empty($p->nomor_undian)): ?>
                  <span class="undian-pill"><?php echo $p->nomor_undian; ?></span>
                <?php else: ?>
                  <span style="color:var(--slate-400)">—</span>
                <?php endif; ?>
              </td>
              <td>
                <span class="dana-cell">Rp <?php echo number_format($p->total_iuran, 0, ',', '.'); ?></span>
              </td>
              <td>
                <?php if($p->status_pertemuan == 'terlaksana'): ?>
                  <span class="status-pill terlaksana"><i class="bi bi-check-circle-fill" style="font-size:.6rem"></i> Terlaksana</span>
                <?php elseif($p->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; ?>
              </td>
              <td class="center">
                <div class="act-btn-group">
                  <a href="<?php echo base_url('arisan/detail_pertemuan/' . $p->id); ?>" class="act-btn detail" title="Detail">
                    <i class="bi bi-eye"></i>
                  </a>
                  <?php if(is_admin()): ?>
                  <a href="<?php echo base_url('arisan/edit_pertemuan/' . $p->id); ?>" class="act-btn edit" title="Edit Pertemuan">
                    <i class="bi bi-pencil-square"></i>
                  </a>
                  <?php endif; ?>
                </div>
              </td>
            </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>

    <?php endif; ?>
  </div><!-- /pt-card -->

</div><!-- /pt-wrapper -->

<script>
$(document).ready(function() {
  $('#tablePertemuan').DataTable({
    language: { url: '//cdn.datatables.net/plug-ins/1.13.7/i18n/id.json' },
    order: [[0, 'desc']],
    pageLength: 25,
    responsive: true,
    dom: '<"pt-dt-top"lf>rt<"pt-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'});
      });
    }
  });

  /* Auto-dismiss flash alerts */
  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>