<div class="main-content">
  <div class="container-fluid" style="margin-top: 50px;">

    <!-- JUDUL DASHBOARD -->
    <div class="row mb-4">
      <div class="col-12">
        <h1><i class="bi bi-speedometer2 me-2"></i>Dashboard SIDIK</h1>
        <p>Selamat datang, <strong><?= htmlspecialchars($this->session->userdata('nama_pegawai')) ?></strong>!</p>
      </div>
    </div>

  <div class="row mb-4">
  <div class="col-12">
    <div class="card">
      <div class="card-header">
        <h5>Profil Saya</h5>
      </div>
      <div class="card-body">
        <p><strong>Nama:</strong> <?= htmlspecialchars($profil->nama ?? '-') ?></p>
        <p><strong>NIP:</strong> <?= htmlspecialchars($profil->nip ?? '-') ?></p>
        <p><strong>Jabatan:</strong> <?= htmlspecialchars($profil->jabatan ?? '-') ?></p>
        <p><strong>Unit Kerja:</strong> <?= htmlspecialchars($profil->unor_nama ?? '-') ?></p>
      </div>
    </div>
  </div>
</div>

<!-- Klaim sebagai Pimpinan -->
<div class="row mb-4">
  <div class="col-12">
    <?php if (!$is_pimpinan): ?>
      <div class="alert alert-info">
        <p>Anda belum terdaftar sebagai pimpinan. Jika Anda memiliki bawahan, silakan klaim sebagai pimpinan dan pilih level Anda:</p>
        <?= form_open('dashboard', ['class' => 'row g-2']) ?>
          <input type="hidden" name="action" value="claim_with_level">
          <div class="col-md-4">
            <select name="level" class="form-control" required>
              <option value="">-- Pilih Level --</option>
              <option value="top_leader">Top Leader</option>
              <option value="middle_leader">Middle Leader</option>
              <option value="low_leader">Low Leader</option>
            </select>
          </div>
          <div class="col-md-2">
            <button type="submit" class="btn btn-success">Klaim</button>
          </div>
        <?= form_close() ?>
      </div>
    <?php else: ?>
      <div class="alert alert-success">
        <p>
          <strong>Anda terdaftar sebagai:</strong> 
          <?= 
            $level === 'top_leader' ? 'Top Leader' :
            ($level === 'middle_leader' ? 'Middle Leader' : 'Low Leader')
          ?>
        </p>
        <?= form_open('dashboard', ['class' => 'd-inline']) ?>
          <input type="hidden" name="action" value="unclaim">
          <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Batalkan klaim sebagai pimpinan?')">Batalkan Klaim</button>
        <?= form_close() ?>
      </div>
    <?php endif; ?>
  </div>
</div>

<!-- Tambah Bawahan (hanya jika pimpinan) -->
<?php if ($is_pimpinan): ?>
  <div class="row mb-4">
    <div class="col-12">
      <div class="card">
        <div class="card-header">
          <h5>Tambah Bawahan</h5>
        </div>
        <div class="card-body">
          <?= form_open('dashboard', ['class' => 'row g-2']) ?>
            <input type="hidden" name="action" value="tambah_bawahan">
            <div class="col-md-4">
              <input type="text" name="nip_bawahan" class="form-control" placeholder="Masukkan NIP Bawahan" required>
            </div>
            <div class="col-md-2">
              <button type="submit" class="btn btn-primary">Tambah</button>
            </div>
          <?= form_close() ?>
          <?php if ($this->session->flashdata('error')): ?>
            <div class="alert alert-danger mt-2"><?= $this->session->flashdata('error') ?></div>
          <?php endif; ?>
        </div>
      </div>
    </div>
  </div>

  <!-- Daftar Bawahan -->
  <div class="row">
    <div class="col-12">
      <div class="card">
        <div class="card-header">
          <h5>Daftar Bawahan</h5>
        </div>
        <div class="card-body">
          <?php if (empty($daftar_bawahan)): ?>
            <div class="alert alert-warning">Belum ada bawahan.</div>
          <?php else: ?>
            <div class="table-responsive">
              <table class="table table-bordered">
                <thead>
                  <tr>
                    <th>No</th>
                    <th>Nama Bawahan</th>
                    <th>NIP</th>
                    <th>Jabatan</th>
                    <th>Status Dialog</th>
                    <th>Aksi</th>
                  </tr>
                </thead>
                <tbody>
                  <?php foreach ($daftar_bawahan as $i => $b): ?>
                    <tr>
                      <td><?= $i + 1 ?></td>
                      <td><?= htmlspecialchars($b->nama) ?></td>
                      <td><?= htmlspecialchars($b->nip) ?></td>
                      <td><?= htmlspecialchars($b->jabatan) ?></td>
                      <td>
                        <?php
                        $status = $b->status_dialog;
                        if ($status === 'belum_mulai') echo '<span class="badge bg-secondary">Belum Mulai</span>';
                        elseif ($status === 'draft') echo '<span class="badge bg-warning">Draft</span>';
                        elseif ($status === 'on_proses') echo '<span class="badge bg-info">On Proses</span>';
                        elseif ($status === 'selesai') echo '<span class="badge bg-success">Selesai</span>';
                        ?>
                      </td>
                      <td>
                        <?= form_open('dashboard', ['class' => 'd-inline']) ?>
                          <input type="hidden" name="action" value="hapus_bawahan">
                          <input type="hidden" name="bawahan_id" value="<?= $b->id_peg ?>">
                          <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Hapus bawahan ini?')">Hapus</button>
                        <?= form_close() ?>
                      </td>
                    </tr>
                  <?php endforeach; ?>
                </tbody>
              </table>
            </div>
          <?php endif; ?>
        </div>
      </div>
    </div>
  </div>
<?php endif; ?>