<?= $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, 0.08);
  --text: #1a1a1a;
  --muted: #6b7280;
  --blue: #2563eb;
  --blue-light: #eff6ff;
  --green: #059669;
  --green-light: #ecfdf5;
  --green-dark: #27500A;
  --red: #dc2626;
  --red-light: #fef2f2;
  --amber: #d97706;
  --amber-light: #fffbeb;
  --amber-dark: #633806;
  --purple: #534AB7;
  --purple-light: #EEEDFE;
  --purple-dark: #3C3489;
}

.pg-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 { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 20px; gap: 12px; flex-wrap: wrap; }
.page-title { font-size: 20px; font-weight: 600; display: flex; align-items: center; gap: 10px; }
.title-icon { width: 36px; height: 36px; background: var(--purple); 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 */
.pg-layout { display: grid; grid-template-columns: 300px 1fr; gap: 16px; align-items: start; }
@media (max-width: 820px) { .pg-layout { grid-template-columns: 1fr; } }

/* Panel */
.pg-panel { 
  background: var(--surface); 
  border: 0.5px solid var(--border); 
  border-radius: 16px; 
  overflow: hidden;

  /* TAMBAHAN SHADOW */
  box-shadow: 0 8px 25px rgba(0,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; }

/* Form */
.form-hint { color: #94a3b8; font-size: 12px; line-height: 1.5; margin: 0 0 14px; }
.form-lbl { font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; margin-bottom: 5px; display: block; }
.fgroup { margin-bottom: 13px; }
.pg-input, .pg-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; }
.pg-input:focus, .pg-select:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(37,99,235,.1); background: #fff; }
.pg-divider { height: 0.5px; background: var(--border); margin: 14px 0; }
.btn-submit { background: var(--purple); 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(--purple-dark); }

/* Table */
.tbl-wrap { overflow-x: auto; }
.pg-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.pg-table thead tr { background: #fafafa; }
.pg-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; }
.pg-table tbody tr { border-bottom: 0.5px solid var(--border); transition: background .1s; }
.pg-table tbody tr:last-child { border-bottom: none; }
.pg-table tbody tr:hover { background: #fafafa; }
.pg-table tbody td { padding: 11px 14px; vertical-align: middle; }

/* Badges & cells */
.badge-period { font-family: var(--fm); font-size: 11px; color: var(--purple-dark); background: var(--purple-light); padding: 3px 8px; border-radius: 5px; font-weight: 500; }
.badge-status { font-size: 11px; font-weight: 600; padding: 3px 9px; border-radius: 20px; display: inline-flex; align-items: center; gap: 4px; }
.b-lunas { background: var(--green-light); color: var(--green-dark); }
.b-pending { background: var(--amber-light); color: var(--amber-dark); }
.salary { font-weight: 600; font-size: 12px; color: var(--green); font-family: var(--fm); }
.jam { font-size: 12px; color: var(--muted); font-family: var(--fm); }
.name-cell { font-weight: 500; font-size: 13px; }

/* Action buttons */
.btn-pay { background: var(--green); border: none; color: #fff; border-radius: 7px; padding: 5px 10px; font-size: 11px; font-weight: 600; cursor: pointer; display: inline-flex; align-items: center; gap: 4px; font-family: var(--ff); transition: background .12s; }
.btn-pay:hover { background: var(--green-dark); }
.done-chip { font-size: 12px; color: var(--green); display: inline-flex; align-items: center; gap: 4px; }

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

<div class="pg-root">

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

  <!-- Header -->
  <div class="page-top">
    <div>
      <div class="page-title">
        <div class="title-icon">
          <i class="bi bi-envelope-paper-fill"></i>
        </div>
        Hitung &amp; Bayar Gaji
      </div>
      <div class="page-sub">Kalkulasi absensi dan penerbitan slip penggajian</div>
    </div>
  </div>

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

  <!-- Stat strip -->
  <?php
    $total_slip   = count($penggajian);
    $belum_bayar  = count(array_filter($penggajian, fn($p) => $p['status_pembayaran_penggajian'] === 'Belum Dibayar'));
    $lunas        = $total_slip - $belum_bayar;
    $total_bulan  = array_sum(array_column(
        array_filter($penggajian, fn($p) => substr($p['periode_bulan_penggajian'], 0, 7) === date('Y-m')),
        'total_gaji_penggajian'
    ));
  ?>
  <div class="stat-strip">
    <div class="stat-pill">
      <div class="stat-dot" style="background:var(--purple)"></div>
      <div>
        <div class="stat-val"><?= $total_slip ?></div>
        <div class="stat-lbl">Total Slip</div>
      </div>
    </div>
    <div class="stat-pill">
      <div class="stat-dot" style="background:var(--amber)"></div>
      <div>
        <div class="stat-val"><?= $belum_bayar ?></div>
        <div class="stat-lbl">Belum Dibayar</div>
      </div>
    </div>
    <div class="stat-pill">
      <div class="stat-dot" style="background:var(--green)"></div>
      <div>
        <div class="stat-val"><?= $lunas ?></div>
        <div class="stat-lbl">Lunas</div>
      </div>
    </div>
    <div class="stat-pill">
      <div class="stat-dot" style="background:var(--blue)"></div>
      <div>
        <div class="stat-val">Rp <?= number_format($total_bulan, 0, ',', '.') ?></div>
        <div class="stat-lbl">Total Bulan Ini</div>
      </div>
    </div>
  </div>

  <div class="pg-layout">

    <!-- Form Generate -->
    <div class="pg-panel">
      <div class="panel-head">
        <div class="panel-head-title">
          <div class="head-icon" style="background:var(--purple-light)">
            <i class="bi bi-calculator" style="color:var(--purple);font-size:12px"></i>
          </div>
          Generate Slip Gaji
        </div>
      </div>
      <div class="panel-body">
        <p class="form-hint">Otomatis menghitung total jam × Rate/Jam karyawan pada bulan yang dipilih.</p>
        <form action="<?= base_url('/hrd/generate-gaji') ?>" method="post">
          <div class="fgroup">
            <label class="form-lbl">Pilih Karyawan</label>
            <select name="id_karyawan" class="pg-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">Periode Bulan</label>
            <input type="month" name="periode_bulan_penggajian" class="pg-input"
                   value="<?= date('Y-m') ?>" required>
          </div>
          <div class="pg-divider"></div>
          <button type="submit" class="btn-submit">
            <i class="bi bi-lightning-charge-fill"></i>
            Kalkulasi Gaji
          </button>
        </form>
      </div>
    </div>

    <!-- Table Riwayat -->
    <div class="pg-panel">
      <div class="panel-head">
        <div class="panel-head-title">
          <div class="head-icon" style="background:#f1f5f9">
            <i class="bi bi-envelope-paper-fill" style="color:var(--muted);font-size:12px"></i>
          </div>
          Riwayat Pembayaran Gaji
        </div>
        <span class="count-chip"><?= count($penggajian) ?> slip</span>
      </div>
      <div class="tbl-wrap">
        <table class="pg-table">
          <thead>
            <tr>
              <th>Periode</th>
              <th>Karyawan</th>
              <th>Total Jam</th>
              <th>Total Gaji</th>
              <th>Status</th>
              <th>Aksi</th>
            </tr>
          </thead>
          <tbody>
            <?php if(empty($penggajian)): ?>
              <tr>
                <td colspan="6" 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 riwayat penggajian
                </td>
              </tr>
            <?php else: ?>
              <?php foreach($penggajian as $p): ?>
              <tr>
                <td><span class="badge-period"><?= esc($p['periode_bulan_penggajian']) ?></span></td>
                <td><div class="name-cell"><?= esc($p['nama_karyawan']) ?></div></td>
                <td><span class="jam"><?= $p['total_jam_penggajian'] ?> Jam</span></td>
                <td><span class="salary">Rp <?= number_format($p['total_gaji_penggajian'], 0, ',', '.') ?></span></td>
                <td>
                  <?php if($p['status_pembayaran_penggajian'] === 'Belum Dibayar'): ?>
                    <span class="badge-status b-pending">● Menunggu</span>
                  <?php else: ?>
                    <span class="badge-status b-lunas">
                      ● Lunas (<?= date('d/m/y', strtotime($p['tanggal_bayar_penggajian'])) ?>)
                    </span>
                  <?php endif; ?>
                </td>
                <td>
                  <?php if($p['status_pembayaran_penggajian'] === 'Belum Dibayar'): ?>
                    <form action="<?= base_url('/hrd/bayar-gaji/' . $p['id_penggajian']) ?>"
                          method="post" style="margin:0"
                          onsubmit="return confirm('Bayar gaji ini menggunakan Uang Kas Operasional?')">
                      <button type="submit" class="btn-pay">
                        <i class="bi bi-cash"></i> Bayar
                      </button>
                    </form>
                  <?php else: ?>
                    <span class="done-chip">
                      <i class="bi bi-check-circle-fill"></i>
                    </span>
                  <?php endif; ?>
                </td>
              </tr>
              <?php endforeach; ?>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>

  </div>
</div>

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