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

$getDataWebsite = "SELECT * FROM configuration";
$stmt = $db->prepare($getDataWebsite);
$stmt->execute();
$config = $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">
            <h3 class="fw-bold mb-3">Edit Website</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="#">Website</a>
              </li>
              <li class="separator">
                <i class="icon-arrow-right"></i>
              </li>
              <li class="nav-item">
                <a href="#">Edit Website</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 card-with-nav">
                <div class="card-header">
                  <div class="row row-nav-line">
                    <ul class="nav nav-tabs nav-line nav-color-secondary w-100 ps-4" role="tablist">
                      <li class="nav-item">
                        <a class="nav-link active show" data-bs-toggle="tab" href="#website" role="tab" aria-selected="true">
                          Website
                        </a>
                      </li>
                      <li class="nav-item">
                        <a class="nav-link" data-bs-toggle="tab" href="#seo" role="tab" aria-selected="false">
                          SEO
                        </a>
                      </li>
                    </ul>
                  </div>
                </div>
                <div class="card-body">
                  <div class="tab-content">
                    <!-- Website Form -->
                    <div class="tab-pane fade show active" id="website" role="tabpanel">
                      <form id="websiteForm" method="POST" enctype="multipart/form-data">
                        <input type="hidden" name="csrf_token" id="csrf_token" value="<?= $csrf_token; ?>">
                        <div class="row mt-3">
                          <!-- Website Title -->
                          <div class="col-md-6">
                            <div class="form-group">
                              <label for="title">Judul Website</label>
                              <input type="text" class="form-control" id="title" name="title" placeholder="Masukkan Judul Website" value="<?= $config['title']; ?>" required />
                            </div>
                          </div>

                          <!-- Description -->
                          <div class="col-md-6">
                            <div class="form-group">
                              <label for="description">Deskripsi Website</label>
                              <textarea id="description" name="description" class="form-control" rows="3" placeholder="Masukkan Deskripsi Website" required><?= $config['description']; ?></textarea>
                            </div>
                          </div>

                          <!-- Logo -->
                          <div class="col-md-6">
                            <div class="form-group">
                              <label for="logo">Logo</label>
                              <input type="file" class="form-control" id="logo" name="logo" accept="image/*" />
                            </div>
                          </div>

                          <!-- Favicon -->
                          <div class="col-md-6">
                            <div class="form-group">
                              <label for="favicon">Favicon</label>
                              <input type="file" class="form-control" id="favicon" name="favicon" accept="image/*" />
                            </div>
                          </div>

                          <!-- Social Media Links -->
                          <div class="col-md-4">
                            <div class="form-group">
                              <label for="telegram">Telegram</label>
                              <input type="text" class="form-control" id="telegram" name="telegram" placeholder="Masukkan Username Telegram" value="<?= $config['telegram']; ?>" />
                            </div>
                          </div>
                          <div class="col-md-4">
                            <div class="form-group">
                              <label for="facebook">Facebook</label>
                              <input type="text" class="form-control" id="facebook" name="facebook" placeholder="Masukkan Username Facebook" value="<?= $config['facebook']; ?>" />
                            </div>
                          </div>
                          <div class="col-md-4">
                            <div class="form-group">
                              <label for="whatsapp">WhatsApp</label>
                              <input type="tel" class="form-control" id="whatsapp" name="whatsapp" placeholder="Masukkan Nomor WhatsApp" value="<?= $config['whatsapp']; ?>" />
                            </div>
                          </div>

                          <!-- Live Chat URL -->
                          <div class="col-md-4">
                            <div class="form-group">
                              <label for="livechat">Live Chat ID</label>
                              <input type="text" class="form-control" id="livechat" name="livechat" placeholder="Masukan Tautan Live Chat" value="<?= $config['livechat']; ?>" />
                            </div>
                          </div>

                          <!-- Live Chat JS -->
                          <div class="col-md-8">
                            <div class="form-group">
                              <label for="livechat_js">Kode JS Live Chat</label>
                              <textarea id="livechat_js" name="livechat_js" class="form-control" row="2" placeholder="Gunakan Live Chat ID" disabled><?= $config['livechat_js']; ?></textarea>
                            </div>
                          </div>

                          <!-- Theme and Maintenance Mode -->
                          <div class="col-md-6">
                            <div class="form-group">
                              <label for="theme">Warna Tema</label>
                              <select class="form-control" id="theme" name="theme">
                                <option value="dark-orange" <?= $config['theme'] === 'dark-orange' ? 'selected' : ''; ?>>Oranye Gelap</option>
                                <option value="dark-blue" <?= $config['theme'] === 'dark-blue' ? 'selected' : ''; ?>>Biru Gelap</option>
                                <option value="dark-brown" <?= $config['theme'] === 'dark-brown' ? 'selected' : ''; ?>>Coklat Gelap</option>
                              </select>
                            </div>
                          </div>

                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="maintenance">Pemeliharaan</label>
                                    <select class="form-control" id="maintenance" name="maintenance">
                                        <option value="0" <?= isset($config['maintenance']) && $config['maintenance'] == 0 ? 'selected' : ''; ?>>Aktif</option>
                                        <option value="1" <?= isset($config['maintenance']) && $config['maintenance'] == 1 ? 'selected' : ''; ?>>Tidak Aktif</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <!-- Action Buttons -->
                        <div class="form-group">
                          <button type="reset" class="btn btn-secondary">Atur Ulang</button>
                          <button type="submit" class="btn btn-primary">Simpan</button>
                        </div>
                      </form>
                    </div>

                    <!-- SEO Form -->
                    <div class="tab-pane fade" id="seo" role="tabpanel">
                      <form id="seoForm" method="POST">
                        <input type="hidden" name="csrf_token" id="csrf_token" value="<?= $csrf_token; ?>">
                        <div class="row mt-3">
                          <!-- Seo Description -->
                          <div class="col-md-4">
                            <div class="form-group">
                              <label for="seo">Deskripsi SEO</label>
                              <textarea id="seo" name="seo" class="form-control" placeholder="Masukan Seo Description"><?= $config['seo']; ?></textarea>
                            </div>
                          </div>

                          <!-- Keyword -->
                          <div class="col-md-4">
                            <div class="form-group">
                              <label for="keyword">Kata Kunci SEO</label>
                              <textarea id="keyword" name="keyword" class="form-control" placeholder="Masukan Seo Keyword"><?= $config['keyword']; ?></textarea>
                            </div>
                          </div>

                          <!-- Canonical -->
                          <div class="col-md-4">
                            <div class="form-group">
                              <label for="canonical">Canonical</label>
                              <textarea id="canonical" name="canonical" class="form-control" placeholder="Masukan Tautan Canonical"><?= $config['canonical']; ?></textarea>
                            </div>
                          </div>

                          <!-- AMP -->
                          <div class="col-md-4">
                            <div class="form-group">
                              <label for="amp">AMP</label>
                              <textarea id="amp" name="amp" class="form-control" placeholder="Masukan Tautan AMP"><?= $config['amp']; ?></textarea>
                            </div>
                          </div>
                        </div>
                        <!-- Action Buttons -->
                        <div class="form-group">
                          <button type="reset" class="btn btn-secondary">Atur Ulang</button>
                          <button type="submit" class="btn btn-primary">Simpan</button>
                        </div>
                      </form>
                    </div>
                  </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() {

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

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

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

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

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

        $.ajax({
          url: 'update-website',
          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>

</body>

</html>