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

$info_popup = "SELECT * FROM popup ORDER BY id ASC";
$stmt = $db->prepare($info_popup);
$stmt->execute();
$popup = $stmt->fetch(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">Popup</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="#">Popup</a>
              </li>
            </ul>
          </div>

          <?php if ($popup): ?>

            <div class="row">
              <div class="col-md-12">
                <div class="card">
                  <div class="card-header">
                    <div class="card-title">Informasi Popup</div>
                  </div>
                  <div class="card-body">
                    <!-- Form starts here -->
                    <form id="editForm" enctype="multipart/form-data">
                      <input type="hidden" name="csrf_token" id="csrf_token" value="<?= $csrf_token; ?>">
                      <input type="hidden" name="id" id="id" value="<?= $popup['id']; ?>">

                      <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 Promosi" value="<?= $popup['title']; ?>" required />
                          </div>
                        </div>

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

                        <!-- Description -->
                        <div class="col-md-12">
                          <div class="form-group">
                            <label for="description">Deskripsi</label>
                            <textarea id="description" class="form-control summernote" rows="5" name="description" placeholder="Masukan Deskripsi"><?= $popup['description']; ?></textarea>
                          </div>
                        </div>
                      </div>

                      <!-- Status Switch -->
                      <div class="col-md-4">
                        <div class="form-group">
                          <label for="status">Status</label>
                          <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" id="status" name="status" value="1" <?= $popup['status'] == 1 ? 'checked' : ''; ?> />
                            <label class="form-check-label" for="status">Aktif</label>
                          </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 endif; ?>

      </div>
    </div>

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

  </div>
  </div>

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

  <script>
    $(document).ready(function() {
      $('#editForm').on('submit', function(e) {
        e.preventDefault();

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

        $.ajax({
          url: 'update-popup',
          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!');
          }
        });
      });
    });
  </script>

  <script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
  <script>
    flatpickr("#expired_at", {
      enableTime: true,
      dateFormat: "d-F-Y H:i",
      locale: {
        weekdays: {
          shorthand: ["Min", "Sen", "Sel", "Rab", "Kam", "Jum", "Sab"],
          longhand: ["Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"],
        },
        months: {
          shorthand: ["Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Agu", "Sep", "Okt", "Nov", "Des"],
          longhand: [
            "Januari", "Februari", "Maret", "April", "Mei", "Juni",
            "Juli", "Agustus", "September", "Oktober", "November", "Desember"
          ],
        },
      },
      time_24hr: true,
      minuteIncrement: 1,
    });
  </script>

</body>

</html>
