<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>
<style>
@import url('https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,400;0,9..40,500;0,9..40,600;1,9..40,400&family=DM+Mono:wght@400;500&display=swap');

body { background: #f6f5f2; }

:root {
  --ff: 'DM Sans', sans-serif;
  --fm: 'DM Mono', monospace;
  --surface: #ffffff;
  --border: rgba(0,0,0,.08);
  --text: #1a1a1a;
  --muted: #6b7280;
  --blue: #2563eb;
  --blue-light: #eff6ff;
  --blue-dark: #0C447C;
  --green: #059669;
  --green-light: #ecfdf5;
  --green-dark: #27500A;
  --red: #dc2626;
  --red-light: #fef2f2;
  --amber: #d97706;
  --amber-light: #fffbeb;
  --amber-dark: #633806;
  --teal: #0F6E56;
  --teal-light: #E1F5EE;
  --teal-dark: #085041;
}

.abs-root { font-family: var(--ff); padding: 1.5rem 0; color: var(--text); }

/* Nav */
.nav-hrd { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 20px; }
.nav-hrd a { padding: 8px 14px; background: var(--surface); color: var(--muted); text-decoration: none; border-radius: 9px; border: 0.5px solid var(--border); font-size: 12px; font-weight: 500; display: inline-flex; align-items: center; gap: 6px; transition: all .12s; }
.nav-hrd a:hover { color: var(--text); border-color: #94a3b8; }
.nav-hrd a.active { background: var(--blue); color: #fff; border-color: var(--blue); }
.nav-hrd a i { font-size: 12px; }

/* Page header */
.page-top { margin-bottom: 20px; }
.page-title { font-size: 20px; font-weight: 600; display: flex; align-items: center; gap: 10px; }
.title-icon { width: 36px; height: 36px; background: var(--teal); border-radius: 10px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.title-icon i { color: #fff; font-size: 15px; }
.page-sub { font-size: 13px; color: var(--muted); margin-top: 3px; padding-left: 46px; }

/* Stat strip */
.stat-strip { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 20px; }
.stat-pill { background: var(--surface); border: 0.5px solid var(--border); border-radius: 10px; padding: 10px 16px; display: flex; align-items: center; gap: 10px; flex: 1; min-width: 120px; }
.stat-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.stat-val { font-size: 17px; font-weight: 600; line-height: 1; }
.stat-lbl { font-size: 11px; color: var(--muted); margin-top: 2px; font-weight: 500; }

/* Layout */
.abs-layout { display: grid; grid-template-columns: 320px 1fr; gap: 16px; align-items: start; }
@media (max-width: 820px) { .abs-layout { grid-template-columns: 1fr; } }

/* Panel */
.abs-panel { background: var(--surface); border: 0.5px solid var(--border); border-radius: 16px; overflow: hidden; box-shadow: 0 8px 25px rgba(0,0,0,.06); }
.panel-head { padding: 13px 18px; border-bottom: 0.5px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.panel-head-title { font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.head-icon { width: 26px; height: 26px; border-radius: 7px; display: flex; align-items: center; justify-content: center; }
.panel-body { padding: 18px; }
.count-chip { font-size: 11px; color: var(--muted); background: #f1f5f9; padding: 3px 10px; border-radius: 20px; font-weight: 500; }

/* Mode badge */
.mode-badge { font-size: 11px; font-weight: 600; padding: 4px 10px; border-radius: 20px; display: inline-flex; align-items: center; gap: 5px; }
.mode-add  { background: var(--teal-light); color: var(--teal); }
.mode-edit { background: var(--amber-light); color: var(--amber-dark); }

/* Form */
.form-lbl  { font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; margin-bottom: 5px; display: block; }
.seg-label { font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; display: flex; align-items: center; gap: 5px; margin-bottom: 6px; }
.seg-dot   { width: 7px; height: 7px; border-radius: 50%; }
.fgroup    { margin-bottom: 13px; }
.abs-input, .abs-select { width: 100%; background: #fafafa; border: 0.5px solid var(--border); color: var(--text); border-radius: 9px; padding: 9px 12px; font-size: 13px; outline: none; transition: border-color .15s, box-shadow .15s; font-family: var(--ff); box-sizing: border-box; }
.abs-input:focus, .abs-select:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(37,99,235,.1); background: #fff; }
.time-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.abs-divider   { height: 0.5px; background: var(--border); margin: 14px 0; }
.divider-label { display: flex; align-items: center; gap: 8px; margin: 14px 0; color: var(--muted); font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; }
.divider-label::before, .divider-label::after { content: ''; flex: 1; height: 0.5px; background: var(--border); }

/* ── Multi-sesi ─────────────────────────────── */
.sesi-list { display: flex; flex-direction: column; gap: 10px; }

.sesi-card {
  background: #fafafa;
  border: 0.5px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  position: relative;
}
.sesi-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.sesi-num {
  font-size: 11px;
  font-weight: 600;
  color: var(--teal);
  background: var(--teal-light);
  padding: 2px 8px;
  border-radius: 20px;
}
.btn-rm-sesi {
  background: transparent;
  border: 0.5px solid rgba(220,38,38,.25);
  border-radius: 6px;
  color: var(--red);
  font-size: 11px;
  padding: 3px 8px;
  cursor: pointer;
  font-family: var(--ff);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: all .12s;
}
.btn-rm-sesi:hover { background: var(--red-light); border-color: var(--red); }

.btn-add-sesi {
  background: transparent;
  border: 0.5px dashed var(--teal);
  border-radius: 9px;
  color: var(--teal);
  font-size: 12px;
  font-weight: 600;
  padding: 9px 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  width: 100%;
  margin-top: 10px;
  font-family: var(--ff);
  transition: all .12s;
}
.btn-add-sesi:hover { background: var(--teal-light); }
/* ─────────────────────────────────────────── */

/* Buttons */
.btn-submit { background: var(--teal); border: none; color: #fff; border-radius: 9px; padding: 10px 14px; font-size: 13px; font-weight: 600; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 7px; width: 100%; font-family: var(--ff); transition: background .15s; margin-top: 4px; }
.btn-submit:hover { background: var(--teal-dark); }
.btn-cancel { background: transparent; border: 0.5px solid var(--border); color: var(--muted); border-radius: 9px; padding: 9px 14px; font-size: 13px; font-weight: 500; cursor: pointer; display: none; align-items: center; justify-content: center; gap: 6px; width: 100%; margin-top: 8px; font-family: var(--ff); transition: all .12s; }
.btn-cancel:hover { border-color: var(--red); color: var(--red); background: var(--red-light); }

/* Table */
.tbl-wrap { overflow-x: auto; }
.abs-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.abs-table thead tr { background: #fafafa; }
.abs-table thead th { padding: 10px 14px; text-align: left; color: var(--muted); font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .07em; border-bottom: 0.5px solid var(--border); white-space: nowrap; }
.abs-table tbody tr { border-bottom: 0.5px solid var(--border); transition: background .1s; }
.abs-table tbody tr:last-child { border-bottom: none; }
.abs-table tbody tr:hover { background: #fafafa; }
.abs-table tbody td { padding: 11px 14px; vertical-align: middle; }

/* Cells & badges */
.cell-date  { font-family: var(--fm); font-size: 11px; color: var(--teal-dark); background: var(--teal-light); padding: 3px 8px; border-radius: 5px; font-weight: 500; white-space: nowrap; }
.name-cell  { font-weight: 500; font-size: 13px; }
.time-badge { display: inline-flex; align-items: center; gap: 5px; font-family: var(--fm); font-size: 12px; font-weight: 500; }
.t-in  { color: var(--green); }
.t-sep { color: var(--muted); }
.t-out { color: var(--red); }
.hours-pill { display: inline-flex; align-items: center; gap: 4px; background: var(--blue-light); color: var(--blue-dark); font-size: 11px; font-weight: 600; padding: 3px 9px; border-radius: 20px; font-family: var(--fm); }

/* Sesi badges in table */
.sesi-badges { display: flex; flex-direction: column; gap: 4px; }
.sesi-badge-row { display: flex; align-items: center; gap: 6px; }
.sesi-tag { font-size: 10px; font-weight: 600; color: var(--teal); background: var(--teal-light); padding: 2px 6px; border-radius: 4px; font-family: var(--fm); }

/* Action buttons */
.abt { background: transparent; border: 0.5px solid var(--border); border-radius: 7px; padding: 5px 8px; cursor: pointer; display: inline-flex; align-items: center; font-size: 11px; font-weight: 500; font-family: var(--ff); transition: all .12s; color: var(--muted); gap: 4px; }
.abt-edit { color: var(--blue); border-color: rgba(37,99,235,.25); }
.abt-edit:hover { background: var(--blue-light); border-color: var(--blue); }
.abt-del  { color: var(--red); border-color: rgba(220,38,38,.25); }
.abt-del:hover { background: var(--red-light); border-color: var(--red); }
.action-row { display: flex; gap: 5px; }

/* Alerts */
.alert-abs   { padding: 10px 14px; border-radius: 10px; margin-bottom: 16px; display: flex; align-items: center; gap: 9px; font-size: 13px; font-weight: 500; }
.alert-abs-s { background: var(--green-light); color: var(--green-dark); border: 0.5px solid rgba(5,150,105,.2); }
.alert-abs-e { background: var(--red-light);   color: var(--red);        border: 0.5px solid rgba(220,38,38,.2); }
</style>

<div class="abs-root">

  <!-- Nav -->
  <div class="nav-hrd">
    <a href="/hrd/karyawan"><i class="bi bi-people-fill"></i> Data Karyawan</a>
    <a href="/hrd/absensi" class="active"><i class="bi bi-calendar-check"></i> Absensi</a>
    <a href="/hrd/penggajian"><i class="bi bi-credit-card"></i> Penggajian</a>
  </div>

  <!-- Header -->
  <div class="page-top">
    <div class="page-title">
      <div class="title-icon"><i class="bi bi-calendar-week-fill"></i></div>
      Log Absensi
    </div>
    <div class="page-sub">Catat &amp; kelola kehadiran harian karyawan · mendukung multi sesi/hari</div>
  </div>

  <?php if(session()->getFlashdata('success')): ?>
    <div class="alert-abs alert-abs-s">
      <i class="bi bi-check-circle-fill"></i>
      <?= session()->getFlashdata('success') ?>
    </div>
  <?php endif; ?>
  <?php if(session()->getFlashdata('error')): ?>
    <div class="alert-abs alert-abs-e">
      <i class="bi bi-exclamation-triangle-fill"></i>
      <?= session()->getFlashdata('error') ?>
    </div>
  <?php endif; ?>

  <!-- Stat strip -->
  <?php
    /*
     * $absensi sekarang adalah array of records dari tabel absensi_sesi:
     *   id_absensi, id_karyawan, nama_karyawan, tanggal_absensi,
     *   sesi (JSON array of {jam_masuk, jam_keluar, jam_kerja})
     *   total_jam_kerja_absensi (sum semua sesi)
     *
     * Kalau struktur database masih per-baris (satu baris = satu sesi),
     * group terlebih dahulu di controller sebelum dikirim ke view.
     */
    $total_catatan = count($absensi);
    $hari_ini      = count(array_filter($absensi, fn($a) => $a['tanggal_absensi'] === date('Y-m-d')));
    $avg_jam       = $total_catatan
        ? array_sum(array_column($absensi, 'total_jam_kerja_absensi')) / $total_catatan
        : 0;
    $periode_aktif = date('M Y');
  ?>
  <div class="stat-strip">
    <div class="stat-pill">
      <div class="stat-dot" style="background:var(--teal)"></div>
      <div><div class="stat-val"><?= $total_catatan ?></div><div class="stat-lbl">Total Catatan</div></div>
    </div>
    <div class="stat-pill">
      <div class="stat-dot" style="background:var(--blue)"></div>
      <div><div class="stat-val"><?= $hari_ini ?></div><div class="stat-lbl">Hari Ini</div></div>
    </div>
    <div class="stat-pill">
      <div class="stat-dot" style="background:var(--green)"></div>
      <div><div class="stat-val"><?= number_format($avg_jam, 1) ?>j</div><div class="stat-lbl">Rata-rata/Hari</div></div>
    </div>
    <div class="stat-pill">
      <div class="stat-dot" style="background:var(--amber)"></div>
      <div><div class="stat-val"><?= $periode_aktif ?></div><div class="stat-lbl">Periode Aktif</div></div>
    </div>
  </div>

  <div class="abs-layout">

    <!-- ═══════════════ FORM PANEL ═══════════════ -->
    <div class="abs-panel">
      <div class="panel-head">
        <div class="panel-head-title">
          <div class="head-icon" style="background:var(--teal-light)">
            <i class="bi bi-calendar-plus" style="color:var(--teal);font-size:12px"></i>
          </div>
          <span id="form-title">Input Absen Harian</span>
        </div>
        <span class="mode-badge mode-add" id="mode-badge">+ Catat</span>
      </div>
      <div class="panel-body">
        <form action="<?= base_url('/hrd/store-absensi') ?>" method="post" id="form-absensi">
          <input type="hidden" name="id_absensi" id="id_absensi">

          <div class="fgroup">
            <label class="form-lbl">Karyawan</label>
            <select name="id_karyawan" id="id_karyawan" class="abs-select" required>
              <?php foreach($karyawan as $k): ?>
                <option value="<?= $k['id_karyawan'] ?>">
                  <?= esc($k['nama_karyawan']) ?> — <?= esc($k['posisi_karyawan']) ?>
                </option>
              <?php endforeach; ?>
            </select>
          </div>

          <div class="fgroup">
            <label class="form-lbl">Tanggal</label>
            <input type="date" name="tanggal_absensi" id="tanggal_absensi"
                   class="abs-input" required value="<?= date('Y-m-d') ?>">
          </div>

          <div class="divider-label">Sesi Jam Kerja</div>

          <!-- Daftar sesi (dinamis) -->
          <div class="sesi-list" id="sesi-list">
            <!-- Sesi 1 (selalu ada, tidak bisa dihapus) -->
            <div class="sesi-card" data-sesi="1">
              <div class="sesi-card-header">
                <span class="sesi-num">Sesi 1</span>
              </div>
              <div class="time-row">
                <div class="fgroup" style="margin-bottom:0">
                  <div class="seg-label"><div class="seg-dot" style="background:var(--green)"></div>Masuk</div>
                  <input type="time" name="sesi[1][masuk]" class="abs-input" required>
                </div>
                <div class="fgroup" style="margin-bottom:0">
                  <div class="seg-label"><div class="seg-dot" style="background:var(--red)"></div>Keluar</div>
                  <input type="time" name="sesi[1][keluar]" class="abs-input" required>
                </div>
              </div>
            </div>
          </div>

          <button type="button" class="btn-add-sesi" onclick="tambahSesi()">
            <i class="bi bi-plus-circle"></i> Tambah Sesi
          </button>

          <div class="abs-divider"></div>

          <button type="submit" id="btn-submit" class="btn-submit">
            <i class="bi bi-check-lg"></i>
            <span id="btn-label">Catat Jam Kerja</span>
          </button>
          <button type="button" id="btn-cancel" class="btn-cancel" onclick="resetFormAbs()">
            <i class="bi bi-x-lg"></i> Batal Edit
          </button>
        </form>
      </div>
    </div>

    <!-- ═══════════════ TABLE PANEL ═══════════════ -->
    <div class="abs-panel">
      <div class="panel-head">
        <div class="panel-head-title">
          <div class="head-icon" style="background:#f1f5f9">
            <i class="bi bi-calendar3" style="color:var(--muted);font-size:12px"></i>
          </div>
          Riwayat Jam Kerja
        </div>
        <span class="count-chip"><?= count($absensi) ?> catatan</span>
      </div>
      <div class="tbl-wrap">
        <?php if(empty($absensi)): ?>
          <div style="text-align:center;padding:32px;color:var(--muted);font-size:13px">
            <i class="bi bi-inbox" style="font-size:28px;display:block;margin-bottom:8px;opacity:.4"></i>
            Belum ada catatan absensi
          </div>
        <?php else: ?>
        <table class="abs-table">
          <thead>
            <tr>
              <th>Tanggal</th>
              <th>Karyawan</th>
              <th>Sesi Jam Kerja</th>
              <th>Total</th>
              <th>Aksi</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach($absensi as $a):
              // $a['sesi'] sudah array PHP — di-decode di controller
            ?>
            <tr>
              <td><span class="cell-date"><?= date('d M Y', strtotime($a['tanggal_absensi'])) ?></span></td>
              <td><div class="name-cell"><?= esc($a['nama_karyawan']) ?></div></td>
              <td>
                <div class="sesi-badges">
                  <?php foreach($a['sesi'] as $idx => $s): ?>
                  <div class="sesi-badge-row">
                    <span class="sesi-tag">Sesi <?= $idx + 1 ?></span>
                    <span class="time-badge">
                      <span class="t-in"><?= substr($s['masuk'],  0, 5) ?></span>
                      <span class="t-sep">→</span>
                      <span class="t-out"><?= substr($s['keluar'], 0, 5) ?></span>
                    </span>
                    <?php if (!empty($s['jam'])): ?>
                      <span style="font-family:var(--fm);font-size:10px;color:var(--muted)">(<?= $s['jam'] ?>j)</span>
                    <?php endif; ?>
                  </div>
                  <?php endforeach; ?>
                </div>
              </td>
              <td>
                <span class="hours-pill">
                  <i class="bi bi-clock" style="font-size:10px"></i>
                  <?= $a['total_jam_kerja_absensi'] ?>j
                </span>
              </td>
              <td>
                <div class="action-row">
                  <button type="button" class="abt abt-edit"
                      onclick="editAbsensi(<?= htmlspecialchars(json_encode($a)) ?>)">
                    <i class="bi bi-pencil"></i> Edit
                  </button>
                  <form action="<?= base_url('/hrd/delete-absensi/' . $a['id_absensi']) ?>"
                        method="post" style="display:inline;margin:0"
                        onsubmit="return confirm('Hapus catatan absen ini?')">
                    <button type="submit" class="abt abt-del">
                      <i class="bi bi-trash"></i>
                    </button>
                  </form>
                </div>
              </td>
            </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
        <?php endif; ?>
      </div>
    </div>

  </div>
</div>

<?= $this->endSection() ?>

<?= $this->section('js') ?>
<script>
let sesiCount = 1;

function tambahSesi() {
  sesiCount++;
  const list = document.getElementById('sesi-list');
  const div  = document.createElement('div');
  div.className   = 'sesi-card';
  div.dataset.sesi = sesiCount;
  div.innerHTML = `
    <div class="sesi-card-header">
      <span class="sesi-num">Sesi ${sesiCount}</span>
      <button type="button" class="btn-rm-sesi" onclick="hapusSesi(this)">
        <i class="bi bi-x"></i> Hapus
      </button>
    </div>
    <div class="time-row">
      <div class="fgroup" style="margin-bottom:0">
        <div class="seg-label"><div class="seg-dot" style="background:var(--green)"></div>Masuk</div>
        <input type="time" name="sesi[${sesiCount}][masuk]" class="abs-input" required>
      </div>
      <div class="fgroup" style="margin-bottom:0">
        <div class="seg-label"><div class="seg-dot" style="background:var(--red)"></div>Keluar</div>
        <input type="time" name="sesi[${sesiCount}][keluar]" class="abs-input" required>
      </div>
    </div>`;
  list.appendChild(div);
  renumberSesi();
}

function hapusSesi(btn) {
  const card = btn.closest('.sesi-card');
  card.remove();
  renumberSesi();
}

function renumberSesi() {
  document.querySelectorAll('#sesi-list .sesi-card').forEach((card, i) => {
    const n = i + 1;
    card.dataset.sesi = n;
    card.querySelector('.sesi-num').textContent = `Sesi ${n}`;
    card.querySelectorAll('input[type=time]').forEach(inp => {
      const isKeluar = inp.name.includes('[keluar]');
      inp.name = `sesi[${n}][${isKeluar ? 'keluar' : 'masuk'}]`;
    });
  });
  sesiCount = document.querySelectorAll('#sesi-list .sesi-card').length;
}

function editAbsensi(data) {
  document.getElementById('form-title').textContent = 'Edit Absensi';
  document.getElementById('id_absensi').value       = data.id_absensi;
  document.getElementById('id_karyawan').value      = data.id_karyawan;
  document.getElementById('tanggal_absensi').value  = data.tanggal_absensi;

  // Bersihkan sesi lama, isi ulang dari data
  const list = document.getElementById('sesi-list');
  list.innerHTML = '';
  sesiCount = 0;

  const sesiArr = data.sesi && data.sesi.length
    ? data.sesi
    : [{ masuk: data.jam_masuk_absensi, keluar: data.jam_keluar_absensi }];

  sesiArr.forEach((s, i) => {
    sesiCount++;
    const isFirst = i === 0;
    const div = document.createElement('div');
    div.className    = 'sesi-card';
    div.dataset.sesi = sesiCount;
    div.innerHTML = `
      <div class="sesi-card-header">
        <span class="sesi-num">Sesi ${sesiCount}</span>
        ${!isFirst ? `<button type="button" class="btn-rm-sesi" onclick="hapusSesi(this)"><i class="bi bi-x"></i> Hapus</button>` : ''}
      </div>
      <div class="time-row">
        <div class="fgroup" style="margin-bottom:0">
          <div class="seg-label"><div class="seg-dot" style="background:var(--green)"></div>Masuk</div>
          <input type="time" name="sesi[${sesiCount}][masuk]" class="abs-input" required value="${(s.masuk||'').substring(0,5)}">
        </div>
        <div class="fgroup" style="margin-bottom:0">
          <div class="seg-label"><div class="seg-dot" style="background:var(--red)"></div>Keluar</div>
          <input type="time" name="sesi[${sesiCount}][keluar]" class="abs-input" required value="${(s.keluar||'').substring(0,5)}">
        </div>
      </div>`;
    list.appendChild(div);
  });

  document.getElementById('btn-label').textContent        = 'Simpan Perubahan';
  document.getElementById('btn-cancel').style.display     = 'flex';

  const mb = document.getElementById('mode-badge');
  mb.textContent = '✎ Edit';
  mb.className   = 'mode-badge mode-edit';

  document.getElementById('form-absensi').action = '<?= base_url('/hrd/update-absensi') ?>';
  window.scrollTo({ top: 0, behavior: 'smooth' });
}

function resetFormAbs() {
  document.getElementById('form-title').textContent  = 'Input Absen Harian';
  document.getElementById('btn-label').textContent   = 'Catat Jam Kerja';
  document.getElementById('form-absensi').reset();
  document.getElementById('id_absensi').value        = '';
  document.getElementById('btn-cancel').style.display = 'none';
  document.getElementById('tanggal_absensi').value   = new Date().toISOString().split('T')[0];

  // Reset ke 1 sesi kosong
  const list = document.getElementById('sesi-list');
  list.innerHTML = `
    <div class="sesi-card" data-sesi="1">
      <div class="sesi-card-header"><span class="sesi-num">Sesi 1</span></div>
      <div class="time-row">
        <div class="fgroup" style="margin-bottom:0">
          <div class="seg-label"><div class="seg-dot" style="background:var(--green)"></div>Masuk</div>
          <input type="time" name="sesi[1][masuk]" class="abs-input" required>
        </div>
        <div class="fgroup" style="margin-bottom:0">
          <div class="seg-label"><div class="seg-dot" style="background:var(--red)"></div>Keluar</div>
          <input type="time" name="sesi[1][keluar]" class="abs-input" required>
        </div>
      </div>
    </div>`;
  sesiCount = 1;

  const mb = document.getElementById('mode-badge');
  mb.textContent = '+ Catat';
  mb.className   = 'mode-badge mode-add';

  document.getElementById('form-absensi').action = '<?= base_url('/hrd/store-absensi') ?>';

  // Submit form tanpa reload halaman
document.getElementById('form-absensi').addEventListener('submit', async function(e) {
  e.preventDefault();

  const form = this;
  const formData = new FormData(form);

  try {
    const response = await fetch(form.action, {
      method: 'POST',
      body: formData
    });

    if (response.ok) {

      // Reload tabel saja
      location.reload();

      // kalau mau TANPA reload sama sekali nanti bisa dibuat update row otomatis
    } else {
      alert('Gagal menyimpan data absensi');
    }

  } catch (error) {
    console.error(error);
    alert('Terjadi kesalahan');
  }
});
}
</script>
<?= $this->endSection() ?>