<?php // kelola_karyawan.php — Views (CodeIgniter 4) ?>
<?= $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;
  --bg: #f6f5f2;
  --surface: #ffffff;
  --border: rgba(0, 0, 0, 0.08);
  --text: #1a1a1a;
  --muted: #6b7280;
  --blue: #2563eb;
  --blue-light: #eff6ff;
  --green: #059669;
  --green-light: #ecfdf5;
  --red: #dc2626;
  --red-light: #fef2f2;
  --amber: #d97706;
  --amber-light: #fffbeb;
}

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

/* 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(--blue); 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: 130px; }
.stat-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.stat-val { font-size: 18px; font-weight: 600; line-height: 1; }
.stat-lbl { font-size: 11px; color: var(--muted); margin-top: 2px; font-weight: 500; }

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

/* Panel (card) */
.kar-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: 14px 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; }

/* 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(--blue-light); color: var(--blue); }
.mode-edit { background: var(--amber-light); color: var(--amber); }

/* Form */
.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: 14px; }
.kar-input { 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; }
.kar-input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(37,99,235,.1); background: #fff; }
.kar-divider { height: 0.5px; background: var(--border); margin: 14px 0; }

/* Buttons */
.btn-primary { background: var(--blue); 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; }
.btn-primary:hover { background: #1d4ed8; }
.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: border-color .15s, color .15s; }
.btn-cancel:hover { border-color: #94a3b8; color: var(--text); }

/* 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-toggle { color: var(--green); border-color: rgba(5,150,105,.25); }
.abt-toggle:hover { background: var(--green-light); border-color: var(--green); }
.abt-off { color: var(--red); border-color: rgba(220,38,38,.25); }
.abt-off:hover { background: var(--red-light); border-color: var(--red); }
.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; align-items: center; }

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

/* Badges & cells */
.badge-id { font-family: var(--fm); font-size: 11px; color: var(--blue); background: var(--blue-light); padding: 3px 8px; border-radius: 5px; font-weight: 500; }
.badge-pos { font-size: 11px; color: var(--muted); background: #f1f5f9; 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-aktif { background: var(--green-light); color: var(--green); }
.b-nonaktif { background: #fee2e2; color: var(--red); }
.salary { font-weight: 600; font-size: 12px; color: var(--green); font-family: var(--fm); }
.name-cell { font-weight: 500; display: flex; align-items: center; gap: 8px; }
.avatar { width: 28px; height: 28px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 10px; font-weight: 700; flex-shrink: 0; font-family: var(--fm); }
.empty-cell { text-align: center; padding: 32px; color: var(--muted); font-size: 13px; }

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

/* Count chip */
.count-chip { font-size: 11px; color: var(--muted); background: #f1f5f9; padding: 3px 10px; border-radius: 20px; font-weight: 500; }
</style>

<div class="kar-root">

  <div class="page-top">
    <div>
      <div class="page-title">
        <div class="title-icon">
          <i class="bi bi-people-fill"></i>
        </div>
        Kelola Karyawan
      </div>
      <div class="page-sub">Manajemen data pegawai dan posisi pekerjaan</div>
    </div>
  </div>

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

  <!-- Stat Strip -->
  <?php
    $total   = count($karyawan);
    $aktif   = count(array_filter($karyawan, fn($k) => $k['status_karyawan'] === 'Aktif'));
    $nonaktif = $total - $aktif;
    $avgRate = $total ? array_sum(array_column($karyawan, 'rate_gaji_per_jam_karyawan')) / $total : 0;
  ?>
  <div class="stat-strip">
    <div class="stat-pill">
      <div class="stat-dot" style="background:var(--blue)"></div>
      <div>
        <div class="stat-val"><?= $total ?></div>
        <div class="stat-lbl">Total Karyawan</div>
      </div>
    </div>
    <div class="stat-pill">
      <div class="stat-dot" style="background:var(--green)"></div>
      <div>
        <div class="stat-val"><?= $aktif ?></div>
        <div class="stat-lbl">Aktif</div>
      </div>
    </div>
    <div class="stat-pill">
      <div class="stat-dot" style="background:var(--red)"></div>
      <div>
        <div class="stat-val"><?= $nonaktif ?></div>
        <div class="stat-lbl">Non-Aktif</div>
      </div>
    </div>
    <div class="stat-pill">
      <div class="stat-dot" style="background:var(--amber)"></div>
      <div>
        <div class="stat-val">Rp <?= number_format($avgRate, 0, ',', '.') ?></div>
        <div class="stat-lbl">Rata-rata Rate/Jam</div>
      </div>
    </div>
  </div>

  <div class="kar-layout">

    <!-- FORM PANEL -->
    <div class="kar-panel">
      <div class="panel-head">
        <span class="panel-head-title">
          <div class="head-icon" style="background:var(--blue-light)">
            <i class="bi bi-person-plus-fill" style="color:var(--blue);font-size:12px"></i>
          </div>
          <span id="form-title">Registrasi Karyawan</span>
        </span>
      </div>
      <div class="panel-body">
        <form action="<?= base_url('/hrd/store-karyawan') ?>" method="post" id="form-karyawan">
          <input type="hidden" name="id_karyawan" id="id_karyawan">

          <div class="fgroup">
            <label class="form-lbl">Nama Lengkap</label>
            <input type="text" name="nama_karyawan" id="nama_karyawan"
                   class="kar-input" placeholder="cth: Budi Santoso" required>
          </div>
          <div class="fgroup">
            <label class="form-lbl">Posisi / Job Desk</label>
            <input type="text" name="posisi_karyawan" id="posisi_karyawan"
                   class="kar-input" placeholder="cth: Staff Keuangan" required>
          </div>
          <div class="fgroup">
            <label class="form-lbl">Rate Gaji per Jam (IDR)</label>
            <input type="number" name="rate_gaji_per_jam_karyawan" id="rate_gaji_per_jam_karyawan"
                   class="kar-input" value="10000" required>
          </div>

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

          <button type="submit" id="btn-submit" class="btn-primary">
            <i class="bi bi-plus-lg"></i>
            Tambah Karyawan
          </button>
          <button type="button" id="btn-cancel" class="btn-cancel" onclick="resetFormKar()">
            <i class="bi bi-x-lg"></i>
            Batal Edit
          </button>
        </form>
      </div>
    </div>

    <!-- TABLE PANEL -->
    <div class="kar-panel">
      <div class="panel-head">
        <span class="panel-head-title">
          <div class="head-icon" style="background:#f1f5f9">
            <i class="bi bi-people-fill" style="color:var(--muted);font-size:12px"></i>
          </div>
          Daftar Karyawan
        </span>
        <span class="count-chip"><?= count($karyawan) ?> karyawan</span>
      </div>
      <div class="tbl-wrap">
        <table class="kar-table">
          <thead>
            <tr>
              <th>ID</th>
              <th>Nama</th>
              <th>Posisi</th>
              <th>Rate/Jam</th>
              <th>Status</th>
              <th>Aksi</th>
            </tr>
          </thead>
          <tbody>
            <?php if(empty($karyawan)): ?>
              <tr>
                <td colspan="6" class="empty-cell">
                  <i class="bi bi-people" style="font-size:28px;display:block;margin-bottom:8px;opacity:.4"></i>
                  Belum ada karyawan terdaftar
                </td>
              </tr>
            <?php else: ?>
              <?php foreach($karyawan as $k): ?>
                <?php
                  // Generate initials & avatar color
                  $words    = explode(' ', $k['nama_karyawan']);
                  $initials = strtoupper(substr($words[0], 0, 1) . (isset($words[1]) ? substr($words[1], 0, 1) : ''));
                  $colors   = [
                    ['bg'=>'var(--blue-light)',    'text'=>'var(--blue)'],
                    ['bg'=>'var(--green-light)',   'text'=>'var(--green)'],
                    ['bg'=>'var(--amber-light)',   'text'=>'var(--amber)'],
                    ['bg'=>'#fce7f3',              'text'=>'#db2777'],
                    ['bg'=>'#f1f5f9',              'text'=>'#64748b'],
                  ];
                  $c = $colors[$k['id_karyawan'] % count($colors)];
                ?>
                <tr>
                  <td><span class="badge-id">#<?= str_pad($k['id_karyawan'], 3, '0', STR_PAD_LEFT) ?></span></td>
                  <td>
                    <div class="name-cell">
                      <div class="avatar" style="background:<?= $c['bg'] ?>;color:<?= $c['text'] ?>">
                        <?= $initials ?>
                      </div>
                      <?= esc($k['nama_karyawan']) ?>
                    </div>
                  </td>
                  <td><span class="badge-pos"><?= esc($k['posisi_karyawan']) ?></span></td>
                  <td><span class="salary">Rp <?= number_format($k['rate_gaji_per_jam_karyawan'], 0, ',', '.') ?></span></td>
                  <td>
                    <?php if($k['status_karyawan'] === 'Aktif'): ?>
                      <span class="badge-status b-aktif">● Aktif</span>
                    <?php else: ?>
                      <span class="badge-status b-nonaktif">● Non-Aktif</span>
                    <?php endif; ?>
                  </td>
                  <td>
                    <div class="action-row">

                      <!-- Edit -->
                      <button type="button" class="abt abt-edit"
                          onclick="editKaryawan(<?= htmlspecialchars(json_encode($k)) ?>)">
                        <i class="bi bi-pencil"></i> Edit
                      </button>

                      <!-- Toggle Status -->
                      <button type="button"
                          onclick="toggleStatus(<?= $k['id_karyawan'] ?>)"
                          class="abt <?= $k['status_karyawan'] === 'Aktif' ? 'abt-toggle' : 'abt-off' ?>"
                          title="<?= $k['status_karyawan'] === 'Aktif' ? 'Nonaktifkan' : 'Aktifkan' ?>">
                      
                          <?php if($k['status_karyawan'] === 'Aktif'): ?>
                          <i class="bi bi-check-circle"></i>
                          <?php else: ?>
                          <i class="bi bi-x-circle"></i>
                          <?php endif; ?>
                        </button>

                      <!-- Delete -->
                      <form action="<?= base_url('/hrd/delete-karyawan/' . $k['id_karyawan']) ?>"
                            method="post" style="display:inline;margin:0"
                            onsubmit="return confirm('Hapus karyawan ini? Data absen dan gaji terkait akan ikut terhapus.')">
                        <button type="submit" class="abt abt-del" title="Hapus">
                          <i class="bi bi-trash"></i>
                        </button>
                      </form>

                    </div>
                  </td>
                </tr>
              <?php endforeach; ?>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>

  </div>
</div>

<script>
function editKaryawan(data) {
  document.getElementById('id_karyawan').value              = data.id_karyawan;
  document.getElementById('nama_karyawan').value            = data.nama_karyawan;
  document.getElementById('posisi_karyawan').value          = data.posisi_karyawan;
  document.getElementById('rate_gaji_per_jam_karyawan').value = data.rate_gaji_per_jam_karyawan;

  document.getElementById('form-title').textContent = 'Edit Karyawan';
  document.getElementById('btn-submit').innerHTML   = '<i class="bi bi-check-lg"></i> Simpan Perubahan';
  document.getElementById('btn-cancel').style.display = 'flex';

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

  document.getElementById('nama_karyawan').focus();
  document.getElementById('form-karyawan').action = '<?= base_url('/hrd/update-karyawan') ?>';
}

function resetFormKar() {
  document.getElementById('form-karyawan').reset();
  document.getElementById('id_karyawan').value    = '';
  document.getElementById('form-title').textContent = 'Registrasi Karyawan';
  document.getElementById('btn-submit').innerHTML = '<i class="bi bi-plus-lg"></i> Tambah Karyawan';
  document.getElementById('btn-cancel').style.display = 'none';

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

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

function toggleStatus(id) {
    fetch('<?= base_url('/hrd/toggle-status-karyawan') ?>/' + id, {
        method: 'POST',
        headers: {
            'X-Requested-With': 'XMLHttpRequest'
        }
    })
    .then(response => response.text())
    .then(() => {
        location.reload();
    })
    .catch(error => {
        console.error(error);
        alert('Gagal mengubah status');
    });
}
</script>

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