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

$getAllBanners = "SELECT * FROM banner ORDER BY id ASC";
$stmt = $db->prepare($getAllBanners);
$stmt->execute();
$banners = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!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">
            <h4 class="page-title">Gambar Slide</h4>
            <ul class="breadcrumbs">
              <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="#">Konten</a>
              </li>
              <li class="separator">
                <i class="icon-arrow-right"></i>
              </li>
              <li class="nav-item">
                <a href="#">Gambar Slide</a>
              </li>
            </ul>
          </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-header">
                  <div class="card-title">Tambah Gambar Slide</div>
                </div>
                <div class="card-body">
                  <!-- Form starts here -->
                  <form id="addForm" enctype="multipart/form-data">
                    <input type="hidden" name="csrf_token" id="csrf_token" value="<?= $csrf_token; ?>">

                    <div class="row">
                      <!-- Title -->
                      <div class="col-md-6">
                        <div class="form-group">
                          <label for="title">Judul</label>
                          <input type="text" class="form-control" id="title" name="title" placeholder="Masukan Judul Gambar" required />
                        </div>
                      </div>

                      <!-- Image Upload -->
                      <div class="col-md-6">
                        <div class="form-group">
                          <label for="image">Unggah Gambar</label>
                          <input type="file" class="form-control" id="image" name="image" accept="image/*" required onchange="previewImage()" />
                          <div id="imagePreview" class="mt-2"></div>
                        </div>
                      </div>
                    </div>

                    <!-- Action Buttons -->
                    <div class="form-group">
                      <button type="reset" class="btn btn-secondary" onclick="resetPreview()">
                        <i class="fas fa-undo"></i> Atur Ulang
                      </button>
                      <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save"></i> Simpan
                      </button>
                    </div>
                  </form>
                  <!-- Form ends here -->
                </div>
              </div>
            </div>
          </div>
        <?php else: ?>
            <span>Askes Dilarang</span>
        <?php endif; ?>
        <?php endif; ?>

          <!-- Start Content -->
          <div class="row banner">
            <div class="col-md-12">
              <div class="card">
                <div class="card-header">
                  <div class="card-title">Daftar Gambar Slide</div>
                </div>
                <div class="card-body">
                  <div class="table-responsive">
                    <table id="basic-datatables" class="display table table-striped table-hover">
                      <thead>
                        <tr>
                          <th>No</th>
                          <th>Judul</th>
                          <th>Gambar</th>
                          <th>Status</th>
                          <th>Aksi</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php $no = 1;
                        foreach ($banners as $banner): ?>
                          <tr>
                            <td><?= $no++; ?></td>
                            <td><?= htmlspecialchars($banner['title']); ?></td>
                            <td>
                              <a href="/<?= htmlspecialchars($banner['image']); ?>" class="col-6 col-md-3 mb-4">
                                <img src="/<?= htmlspecialchars($banner['image']); ?>" class="img-fluid" width="100">
                              </a>
                            </td>
                            <td>
                              <?php if ($banner['status'] == 1): ?>
                                <span class="badge badge-success">Aktif</span>
                              <?php else: ?>
                                <span class="badge badge-danger">Tidak Aktif</span>
                              <?php endif; ?>
                            </td>
                            <td>
                              <div class="btn-group">
                                <a href="edit-banner?id=<?= $banner["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="<?= $banner["id"] ?>">
                                  <i class="fas fa-trash"></i>
                                </button>
                              </div>
                            </td>
                          </tr>
                        <?php endforeach; ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!-- End Content -->

        </div>
      </div>

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

    </div>
  </div>

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

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

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

      $('#addForm').on('submit', function(e) {
        e.preventDefault();

        var formData = new FormData(this);
        formData.append('action', 'add');

        $.ajax({
          url: 'update-banner',
          type: 'POST',
          data: formData,
          contentType: false,
          processData: false,
          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!');
          }
        });
      });

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

        if (confirm('Apakah Kamu yakin ingin menghapus Gambar ini?')) {
          $.ajax({
            url: 'update-banner',
            type: 'POST',
            data: {
              action: 'delete',
              id: bannerId,
              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>