<?php include_once 'fungsi/fungsi.php'; ?>
<?php
if (isset($_POST['simpan'])) { insert_user(); }
if (isset($_POST['hapus']))  { delete_user(); }
?>
<?php include 'comp/header.php'; ?>

<div class="content-wrapper">
  <div class="content-header">
    <div class="container-fluid">
      <div class="page-header">
        <div>
          <h1>Pengguna</h1>
          <ul class="page-breadcrumb">
            <li><a href="index.php">Home</a></li>
            <li><span class="sep">›</span></li>
            <li>Pengguna</li>
          </ul>
        </div>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modal-tambah-user">
          <i class="fas fa-user-plus"></i> Tambah Pengguna
        </button>
      </div>
    </div>
  </div>

  <section class="content">
    <div class="container-fluid">
      <div class="table-container">
        <div class="table-toolbar" style="flex-direction:column;align-items:stretch;gap:12px;">
          <div style="display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:12px;width:100%;">
            <span class="table-toolbar-title">
              <i class="fas fa-users" style="margin-right:8px;color:var(--primary);"></i>
              Daftar Pengguna
            </span>
            <form action="" method="GET" style="display:flex;gap:8px;align-items:center;">
              <input type="hidden" name="m" value="admin">
              <input type="hidden" name="s" value="title">
              <input type="text" name="cari" class="form-control form-control-sm" placeholder="Cari nama..." value="<?= isset($_GET['cari']) ? $_GET['cari'] : '' ?>" style="width:200px;border-radius:8px;">
              <button type="submit" name="go" class="btn btn-primary btn-sm" style="border-radius:8px;"><i class="fas fa-search"></i></button>
              <?php if(isset($_GET['go']) && isset($_GET['cari'])): ?>
                <a href="?m=admin&s=title" class="btn btn-light btn-sm" style="border-radius:8px;border:1px solid var(--border);">Reset</a>
              <?php endif; ?>
            </form>
          </div>
        </div>
        <div class="table-responsive">
          <table class="table">
            <thead>
              <tr>
                <th>No</th>
                <th>Nama</th>
                <th>Level</th>
                <th>Foto</th>
                <th>Aksi</th>
              </tr>
            </thead>
            <tbody>
              <?php
                $i = 1;
                foreach (select_user() as $adm):
              ?>
              <tr>
                <td><?= $i++; ?></td>
                <td>
                  <div style="font-weight:600;color:var(--text-main);"><?= $adm['nama']; ?></div>
                </td>
                <td>
                  <?php
                    if ($adm['level'] == 'admin'):
                  ?>
                    <span class="badge badge-level-admin">
                      <i class="fas fa-shield-alt" style="font-size:10px;"></i> Admin
                    </span>
                  <?php elseif ($adm['level'] == 'kasir'): ?>
                    <span class="badge badge-level-kasir">
                      <i class="fas fa-cash-register" style="font-size:10px;"></i> Kasir
                    </span>
                  <?php elseif ($adm['level'] == 'logistik'): ?>
                    <span class="badge badge-level-logistik">
                      <i class="fas fa-truck" style="font-size:10px;"></i> Logistik
                    </span>
                  <?php else: ?>
                    <span class="badge"><?= $adm['level']; ?></span>
                  <?php endif; ?>
                </td>
                <td>
                  <?php if ($adm['foto'] != ''): ?>
                    <img src="img/<?= $adm['foto'] ?>" class="table-avatar" alt="<?= $adm['nama'] ?>">
                  <?php else: ?>
                    <img src="img/user_logo.png" class="table-avatar" alt="User">
                  <?php endif; ?>
                </td>
                <td>
                  <div class="action-buttons">
                    <a href="index.php?m=admin&s=detail&id=<?= $adm['id'] ?>" class="btn btn-sm btn-warning" title="Edit">
                      <i class="fas fa-edit"></i>
                    </a>
                    <?php if ($adm['level'] != 'admin'): ?>
                    <!-- Delete trigger -->
                    <button type="button" class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#hapus-user<?= $adm['id'] ?>" title="Hapus">
                      <i class="fas fa-trash-alt"></i>
                    </button>
                    <?php endif; ?>
                  </div>
                </td>
              </tr>
              <?php endforeach; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </section>
</div>

<!-- Modals must be outside the wrapper -->
<?php foreach (select_user() as $adm): ?>
<?php if ($adm['level'] != 'admin'): ?>
<!-- Modal Hapus User -->
<form action="" method="POST" id="form-hapus-user<?= $adm['id'] ?>">
  <input type="hidden" name="id" value="<?= $adm['id'] ?>">
  <input type="hidden" name="hapus" value="1">
  <div class="modal fade" id="hapus-user<?= $adm['id'] ?>" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title"><i class="fas fa-exclamation-triangle" style="color:#DC2626;margin-right:8px;"></i>Hapus Pengguna</h5>
          <button type="button" class="close" data-bs-dismiss="modal"><span>&times;</span></button>
        </div>
        <div class="modal-body">
          <p style="color:var(--text-secondary);margin-bottom:16px;">Apakah Anda yakin ingin menghapus pengguna ini?</p>
          <div class="modal-detail-row">
            <div class="modal-detail-label">Nama</div>
            <div class="modal-detail-value"><?= $adm['nama']; ?></div>
          </div>
          <div class="modal-detail-row">
            <div class="modal-detail-label">Level</div>
            <div class="modal-detail-value" style="text-transform:capitalize;"><?= $adm['level']; ?></div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
          <button type="submit" class="btn btn-danger" form="form-hapus-user<?= $adm['id'] ?>">
            <i class="fas fa-trash-alt"></i> Hapus
          </button>
        </div>
      </div>
    </div>
  </div>
</form>
<?php endif; ?>
<?php endforeach; ?>
<div class="modal fade" id="modal-tambah-user" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title"><i class="fas fa-user-plus" style="margin-right:8px;color:var(--primary);"></i>Tambah Pengguna Baru</h5>
        <button type="button" class="close" data-bs-dismiss="modal"><span>&times;</span></button>
      </div>
      <div class="modal-body">
        <form action="" method="POST" enctype="multipart/form-data" id="form-tambah-user">
          <div class="form-group">
            <label>Username</label>
            <input type="text" class="form-control" name="username" placeholder="Masukkan username" autofocus>
          </div>
          <div class="form-group">
            <label>Password</label>
            <input type="text" class="form-control" name="password" placeholder="Masukkan password">
          </div>
          <div class="form-group">
            <label>Nama Lengkap</label>
            <input type="text" class="form-control" name="nama" placeholder="Masukkan nama lengkap">
          </div>
          <div class="form-group">
            <label>Level</label>
            <select name="level" class="form-control">
              <option value="kasir">Kasir</option>
            </select>
          </div>
          <div class="form-group">
            <label>Foto <span style="font-weight:400;text-transform:none;color:var(--text-muted);">(Opsional)</span></label>
            <input type="file" name="foto" style="font-size:0.875rem;color:var(--text-secondary);">
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
        <button type="submit" form="form-tambah-user" name="simpan" class="btn btn-primary">
          <i class="fas fa-save"></i> Simpan
        </button>
      </div>
    </div>
  </div>
</div>

<?php include 'comp/footer.php'; ?>
