<?php include(__DIR__ . '/../../session.php'); ?>

<!DOCTYPE html>
<html lang="en">

<?php include(__DIR__ . '/../../layouts/header.php'); ?>

<body>
  <div class="wrapper">
    <?php include(__DIR__ . '/../../layouts/sidebar.php'); ?>

    <div class="main-panel">
      <?php include(__DIR__ . '/../../layouts/navbar.php'); ?>

      <div class="container">
        <div class="page-inner">

          <div class="page-header">
            <h3 class="fw-bold mb-3">Admin</h3>
            <ul class="breadcrumbs mb-3">
              <li class="nav-home">
                <a href="#">
                  <i class="icon-home"></i>
                </a>
              </li>
              <li class="separator">
                <i class="icon-arrow-right"></i>
              </li>
              <li class="nav-item">
                <a href="#">Pengaturan</a>
              </li>
              <li class="separator">
                <i class="icon-arrow-right"></i>
              </li>
              <li class="nav-item">
                <a href="#">Admin</a>
              </li>
            </ul>

            <?php if (isset($_SESSION['admin_id']) && isset($admin['level'])): ?>
            <?php if ($admin['level'] === 'developer' || $admin['level'] === 'admin'): ?>
            <div class="ms-md-auto py-2 py-md-0">
              <a href="add-admin">
                <button class="btn btn-primary btn-round">Tambah Admin</button>
              </a>
            </div>
            <?php endif; ?>
            <?php endif; ?>
          </div>

            <?php if (isset($_SESSION['admin_id']) && isset($admin['level'])): ?>
            <?php if ($admin['level'] === 'developer' || $admin['level'] === 'admin'): ?>
          <div class="row">
            <div class="col-md-12">
              <div class="card">
                <div class="card-body">
                  <div class="table-responsive">
                    <table id="basic-datatables" class="display table table-striped table-hover">
                      <thead>
                        <tr>
                          <th>Nama Lengkap</th>
                          <th>Nama Pengguna</th>
                          <th>Level</th>
                          <th>Status</th>
                          <th>Aksi</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php foreach ($admins as $admin): ?>
                          <tr>
                            <td><?= htmlspecialchars($admin["fullname"]) ?></td>
                            <td><?= htmlspecialchars($admin["username"]) ?></td>
                            <td>
                              <div class="btn-group">
                                <?php if ($admin["level"] == "developer"): ?>
                                  <span class="badge badge-primary">Developer</span>
                                <?php elseif ($admin["level"] == "admin"): ?>
                                  <span class="badge badge-secondary">Admin</span>
                                <?php else: ?>
                                  <span class="badge badge-warning">Staff</span>
                                <?php endif; ?>
                              </div>
                            </td>
                            <td>
                              <div class="btn-group">
                                <span class="badge <?= $admin['status'] ? 'badge-success' : 'badge-danger' ?> btn-xs" data-id="<?= $admin['id']; ?>">
                                  <?= $admin['status'] ? 'Aktif' : 'Tidak Aktif'; ?>
                                </span>
                              </div>
                            </td>
                            <td>
                              <div class="btn-group">
                                <a href="edit-admin?id=<?= $admin["id"] ?>">
                                  <button class="btn btn-icon btn-round btn-warning">
                                    <i class="fas fa-edit"></i>
                                  </button>
                                </a>
                                <button class="btn btn-icon btn-round btn-danger  delete-btn" data-id="<?= $admin["id"] ?>">
                                  <i class="fas fa-trash"></i>
                                </button>
                              </div>
                            </td>
                          </tr>
                        <?php endforeach; ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
            <?php else: ?>
                <span>Askes Dilarang</span>
            <?php endif; ?>
            <?php endif; ?>

        </div>
      </div>

      <?php include(__DIR__ . '/../../layouts/footer.php'); ?>

    </div>
  </div>

  <?php include(__DIR__ . '/../../layouts/script.php'); ?>

  <script>
    $(document).ready(function() {

      $('#basic-datatables').DataTable();

      $('.delete-btn').on('click', function() {
        var adminId = $(this).data('id');
        var csrfToken = '<?= $csrf_token; ?>';

        if (confirm('Apakah Anda yakin ingin menghapus Banner ini?')) {
          $.ajax({
            url: 'update-admin',
            type: 'POST',
            data: {
              action: 'delete',
              id: adminId,
              csrf_token: csrfToken
            },
            success: function(response) {
              var res = JSON.parse(response);
              alert(res.message);

              if (res.status === 'success' || res.status === 'error') {
                location.reload();
              }
            },
            error: function() {
              alert('Terjadi kesalahan, coba lagi!');
            }
          });
        }
      });
    });
  </script>

</body>

</html>