<?php
include_once '../db/koneksi.php';

if (!isset($_SESSION['kode_admin'])) {
  echo '<script>alert("Sesi admin tidak valid!");window.location.href="keluar.php";</script>';
  exit;
}

// Ambil data web
$q = mysqli_query($koneksi, "SELECT * FROM tb_web LIMIT 1");
$data = mysqli_fetch_assoc($q);
?>

<div class="container-xxl flex-grow-1 container-p-y">
  <div class="row mb-4">
    <div class="col-md-6">
      <h4>Pengaturan Website</h4>
    </div>
  </div>

  <!-- Form: Judul, Deskripsi, Kata Kunci -->
  <div class="card mb-4">
    <div class="card-header">Informasi Dasar</div>
    <div class="card-body">
      <form id="formWebInfo">
        <div class="row g-3">
          <div class="col-md-4">
            <label>Judul Web</label>
            <input type="text" name="judul_web" class="form-control"
              value="<?= htmlspecialchars($data['judul_web']) ?>">
          </div>
          <div class="col-md-4">
            <label>Deskripsi</label>
            <input type="text" name="deskripsi_web" class="form-control"
              value="<?= htmlspecialchars($data['deskripsi_web']) ?>">
          </div>
          <div class="col-md-4">
            <label>Kata Kunci</label>
            <input type="text" name="kata_kunci_web" class="form-control"
              value="<?= htmlspecialchars($data['kata_kunci_web']) ?>">
          </div>
        </div>
        <div class="text-end pt-3">
          <button type="submit" class="btn btn-primary">Simpan</button>
        </div>
      </form>
    </div>
  </div>

  <!-- Warna Tema -->
  <div class="card mb-4">
    <div class="card-header">Warna Tema</div>
    <div class="card-body">
      <form id="formWarna">
        <div class="row g-3 w-50">
          <div class="col-md-4">
            <label>Primary</label>
            <input type="color" name="primary_color" class="form-control form-control-color"
              value="<?= htmlspecialchars($data['primary_color']) ?>">
          </div>
          <div class="col-md-4">
            <label>Secondary</label>
            <input type="color" name="secondary_color" class="form-control form-control-color"
              value="<?= htmlspecialchars($data['secondary_color']) ?>">
          </div>
          <div class="col-md-4">
            <label>Tertiary</label>
            <input type="color" name="tertiary_color" class="form-control form-control-color"
              value="<?= htmlspecialchars($data['tertiary_color']) ?>">
          </div>
        </div>

        <div class="text-end pt-3">
          <button type="submit" class="btn btn-primary">Simpan</button>
        </div>
      </form>
    </div>
  </div>

  <!-- Logo & Favicon -->
  <div class="card mb-4">
    <div class="card-header">Logo & Favicon</div>
    <div class="card-body">
      <form id="formLogo" enctype="multipart/form-data">
        <div class="row g-3">
          <div class="col-md-4 text-center">
            <img src="../asset/img/<?= $data['logo_web'] ?>" alt="Logo" class="img-fluid mb-2" style="max-height:100px">
            <input type="file" name="logo_web" class="form-control">
          </div>
          <div class="col-md-4 text-center">
            <img src="../asset/img/<?= $data['favicon_web'] ?>" alt="Favicon" class="img-fluid mb-2"
              style="max-height:100px">
            <input type="file" name="favicon_web" class="form-control">
          </div>
          <div class="col-md-4 text-center">
            <img src="../asset/img/<?= $data['logo_aplikasi_web'] ?>" alt="Logo Aplikasi" class="img-fluid mb-2"
              style="max-height:100px">
            <input type="file" name="logo_aplikasi_web" class="form-control">
          </div>
        </div>
        <div class="text-end pt-3">
          <button type="submit" class="btn btn-primary">Simpan</button>
        </div>
      </form>
    </div>
  </div>

  <div class="card mb-4">
    <div class="card-header">Popup</div>
    <div class="card-body">
      <form id="formPopup" enctype="multipart/form-data">
        <div class="row g-3">
          <div class="col-md-4 text-center">
            <img src="../asset/popup/<?= $data['popup_pengumuman_gambar'] ?>" alt="Popup" class="img-fluid mb-2"
              style="max-height:100px">
            <input type="file" name="popup_pengumuman_gambar" class="form-control">
          </div>
          <div class="col-md-4">
            <input type="text" name="judul_popup" class="form-control" placeholder="Judul Text Popup"
              value="<?= htmlspecialchars($data['judul_popup']) ?>">
          </div>
          <div class="col-md-4">
            <input type="text" name="teks_popup" class="form-control" placeholder="Text Popup"
              value="<?= htmlspecialchars($data['popup_pengumuman_link_alt']) ?>">
          </div>
        </div>
        <div class="text-end pt-3">
          <button type="submit" class="btn btn-primary">Simpan</button>
        </div>
      </form>
    </div>
  </div>

  <!-- Sosial Media -->
  <div class="card mb-4">
    <div class="card-header">Sosial Media & Livechat</div>
    <div class="card-body">
      <form id="formSosmed">
        <div class="row g-3">
          <div class="col-md-4">
            <label>WhatsApp</label>
            <input type="text" name="whatsapp" class="form-control" value="<?= htmlspecialchars($data['whatsapp']) ?>">
          </div>
          <div class="col-md-4">
            <label>Facebook</label>
            <input type="text" name="facebook" class="form-control" value="<?= htmlspecialchars($data['facebook']) ?>">
          </div>
          <div class="col-md-4">
            <label>Telegram</label>
            <input type="text" name="telegram" class="form-control" value="<?= htmlspecialchars($data['telegram']) ?>">
          </div>
          <div class="col-12">
            <label>Link Livechat</label>
            <input type="text" name="link_livechat_web" class="form-control"
              value="<?= htmlspecialchars($data['link_livechat_web']) ?>">
          </div>
          <div class="col-12">
            <label>Script Livechat</label>
            <textarea name="script_livechat_web"
              class="form-control"><?= htmlspecialchars($data['script_livechat_web']) ?></textarea>
          </div>
        </div>
        <div class="text-end pt-3">
          <button type="submit" class="btn btn-primary">Simpan</button>
        </div>
      </form>
    </div>
  </div>

  <!-- Teks Berjalan -->
  <div class="card mb-4">
    <div class="card-header">Teks Berjalan</div>
    <div class="card-body">
      <form id="formTeks">
        <div class="row g-3">
          <div class="col-md-4">
            <input type="text" name="teks_berjalan_1" class="form-control" placeholder="Teks 1"
              value="<?= htmlspecialchars($data['teks_berjalan_1']) ?>">
          </div>
          <div class="col-md-4">
            <input type="text" name="teks_berjalan_2" class="form-control" placeholder="Teks 2"
              value="<?= htmlspecialchars($data['teks_berjalan_2']) ?>">
          </div>
          <div class="col-md-4">
            <input type="text" name="teks_berjalan_3" class="form-control" placeholder="Teks 3"
              value="<?= htmlspecialchars($data['teks_berjalan_3']) ?>">
          </div>
        </div>
        <div class="text-end pt-3">
          <button type="submit" class="btn btn-primary">Simpan</button>
        </div>
      </form>
    </div>
  </div>

  <!-- RTP -->
  <div class="card mb-4">
    <div class="card-header">RTP (Persentase)</div>
    <div class="card-body">
      <form id="formRtp">
        <div class="row g-3">
          <div class="col-md-6">
            <input type="number" name="rtp_awal" class="form-control" placeholder="RTP Awal"
              value="<?= htmlspecialchars($data['rtp_awal']) ?>" min="0" max="100">
          </div>
          <div class="col-md-6">
            <input type="number" name="rtp_akhir" class="form-control" placeholder="RTP Akhir"
              value="<?= htmlspecialchars($data['rtp_akhir']) ?>" min="0" max="100">
          </div>
        </div>
        <div class="text-end pt-3">
          <button type="submit" class="btn btn-primary">Simpan</button>
        </div>
      </form>
    </div>
  </div>
</div>

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
  function ajaxForm(formId, action) {
    $(formId).on("submit", function (e) {
      e.preventDefault();
      let formData = new FormData(this);
      formData.append("action", action);
      $.ajax({
        url: "../function/proses_pengaturan.php",
        type: "POST",
        data: formData,
        processData: false,
        contentType: false,
        success: function (res) {
          alert(res.message);
          if (res.success) location.reload();
        },
        error: function () {
          alert("Terjadi kesalahan koneksi!");
        }
      });
    });
  }

  ajaxForm("#formWebInfo", "update_info");
  ajaxForm("#formWarna", "update_warna");
  ajaxForm("#formLogo", "update_logo");
  ajaxForm("#formSosmed", "update_sosmed");
  ajaxForm("#formTeks", "update_teks");
  ajaxForm("#formRtp", "update_rtp");
  ajaxForm("#formPopup", "update_popup");

</script>