<?php
ob_start();
include '../../koneksi.php';
include '../auth.php';
include '../function/landingpage_helper.php';

$user_id = $_SESSION['user_id']; 

$data = getLandingPageData($user_id, $conn);
$disabled = $data['disabled'];
$slug_url = $data['slug_url'];
$showLandingPage = $data['showLandingPage'];

if (!$showLandingPage) { 
  $_SESSION['error'] = "Lengkapi semua data terlebih dahulu sebelum mengakses Pengaturan Landing Page!";
  
  $redirectURL = $_SERVER['HTTP_REFERER'] ?? '../';
  header("Location: " . $redirectURL);
  exit();
}

$sql = "SELECT * FROM landingpage WHERE id_user = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
  $row = $result->fetch_assoc();
  $form_action = "updatelp.php";  
} else {
  $form_action = "addlp.php";   
}

$gambar_utama_required = empty($row['gambar_utama']) ? 'required' : '';
$gambar_tentang_required = empty($row['gambar_tentang']) ? 'required' : '';
$gambar_keunggulan_required = empty($row['gambar_keunggulan']) ? 'required' : '';

$stmt->close();
$conn->close();
?>

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

<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">

  <title>Pengaturan Landing Page | User</title>
  <meta content="" name="description">
  <meta content="" name="keywords">
  <meta name="robots" content="noindex">

  <!-- Favicons -->
  <link href="../../assets/img/favicon.png" rel="icon">
  <link href="../../assets/img/apple-touch-icon.png" rel="apple-touch-icon">

  <!-- Google Fonts -->
  <link href="https://fonts.gstatic.com" rel="preconnect">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Nunito:300,300i,400,400i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">

  <!-- Vendor CSS Files -->
  <link href="../../assets/template/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <link href="../../assets/template/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
  <link href="../../assets/template/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  <link href="../../assets/template/vendor/quill/quill.snow.css" rel="stylesheet">
  <link href="../../assets/template/vendor/quill/quill.bubble.css" rel="stylesheet">
  <link href="../../assets/template/vendor/remixicon/remixicon.css" rel="stylesheet">
  <link href="../../assets/template/vendor/simple-datatables/style.css" rel="stylesheet">

  <!-- Template Main CSS File -->
  <link href="../../assets/template/css/style.css" rel="stylesheet">

  <!-- =======================================================
  * Template Name: NiceAdmin
  * Template URL: https://bootstrapmade.com/nice-admin-bootstrap-admin-html-template/
  * Updated: Apr 20 2024 with Bootstrap v5.3.3
  * Author: BootstrapMade.com
  * License: https://bootstrapmade.com/license/
  ======================================================== -->
</head>

<body>

  <!-- ======= Header ======= -->
  <header id="header" class="header fixed-top d-flex align-items-center">

    <div class="d-flex align-items-center justify-content-between">
      <a href="" class="logo d-flex align-items-center">
        <img src="../../assets/img/logo.png" alt="">
        <span class="d-none d-lg-block">Portal UMKM</span>
      </a>
      <i class="bi bi-list toggle-sidebar-btn"></i>
    </div><!-- End Logo -->

    <nav class="header-nav ms-auto">
      <ul class="d-flex align-items-center">

        <li class="nav-item dropdown pe-3">

          <a class="nav-link nav-profile d-flex align-items-center pe-0" href="#" data-bs-toggle="dropdown">
          <span class="fw-semibold dropdown-toggle ps-2">Hai, <?php echo $_SESSION['nama_lengkap']; ?>!</span>
          </a><!-- End Profile Iamge Icon -->

          <ul class="dropdown-menu dropdown-menu-end dropdown-menu-arrow profile">
            <li class="dropdown-header">
            <h6><?php echo $_SESSION['nama_lengkap']; ?></h6>
            <span><?php echo ucfirst($_SESSION['role']); ?></span>
            </li>
            <li>
              <hr class="dropdown-divider">
            </li>

            <li>
              <a class="dropdown-item d-flex align-items-center" href="../update-profile/">
                <i class="bi bi-gear"></i>
                <span>Pengaturan</span>
              </a>
            </li>
            <li>
              <hr class="dropdown-divider">
            </li>

            <li>
              <a class="dropdown-item d-flex align-items-center" href="#" data-bs-toggle="modal" data-bs-target="#confirmLogoutModal">
                <i class="bi bi-box-arrow-right"></i>
                <span>Keluar</span>
              </a>
            </li>

          </ul><!-- End Profile Dropdown Items -->
        </li><!-- End Profile Nav -->

      </ul>
    </nav><!-- End Icons Navigation -->

  </header><!-- End Header -->

  <!-- ======= Sidebar ======= -->
  <aside id="sidebar" class="sidebar bg-gradient-primary text-white vh-100 p-3 shadow-lg">

  <ul class="nav flex-column sidebar-nav">

    <li class="nav-item mb-2">
      <a class="nav-link d-flex align-items-center" href="../../users">
        <i class="bi bi-columns-gap"></i>
        <span>Dashboard</span>
      </a>
    </li>

    <li class="nav-item mb-2">
      <a class="nav-link d-flex align-items-center" href="../data-usaha">
        <i class="bi bi-shop"></i>
        <span>Data Usaha</span>
      </a>
    </li>

    <li class="nav-item mb-2">
      <a class="nav-link d-flex align-items-center" href="../produk">
        <i class="bi bi-box-seam"></i>
        <span>Produk</span>
      </a>
    </li>

    <li class="nav-item mb-2">
      <a class="nav-link d-flex align-items-center" href="../keunggulan">
        <i class="bi bi-patch-check"></i>          
        <span>Keunggulan</span>
      </a>
    </li>

    <li class="nav-item mb-2">
      <a class="nav-link d-flex align-items-center" href="../testimoni">
        <i class="bi bi-chat-dots"></i>
        <span>Testimoni</span>
      </a>
    </li>

    <li class="nav-item mb-2">
      <a class="nav-link d-flex align-items-center" href="../seo">
        <i class="bi bi-search"></i>
        <span>SEO</span>
      </a>
    </li>

    <?php if ($showLandingPage): ?>
        <li class="nav-item mb-2"> 
            <a class="nav-link d-flex align-items-center collapsed" href="../landingpage">
                <i class="bi bi-journal-richtext"></i>
                <span>Landing Page</span>
            </a>
        </li>
    <?php endif; ?>

    <li class="nav-item text-center mt-3">
        <a class="btn btn-primary px-4 fs-6 py-2 <?= $disabled ? 'disabled' : ''; ?>" 
           href="../../<?php echo $slug_url; ?>" target="_blank">
            <i class="bi bi-eye"></i> Lihat Landing Page
        </a>
    </li>

  </ul>

  </aside>


  <main id="main" class="main">

    <div class="pagetitle">
      <h1>Pengaturan Landing Page</h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="">Beranda</a></li>
          <li class="breadcrumb-item">Landing Page</li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

    <?php if (isset($_SESSION['success'])): ?>
      <div class="alert alert-success alert-dismissible fade show" role="alert">
        <?php echo $_SESSION['success']; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
      </div>
      <?php unset($_SESSION['success']); // Hapus pesan setelah ditampilkan 
      ?>
    <?php endif; ?>

    <?php if (isset($_SESSION['error'])): ?>
      <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <?php echo $_SESSION['error']; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
      </div>
      <?php unset($_SESSION['error']); // Hapus pesan setelah ditampilkan 
      ?>
    <?php endif; ?>

    <section class="section profile">
      <div class="row">
        <div class="col-xl-12">

          <div class="card">
            <div class="card-body pt-3">
              <!-- Bordered Tabs -->
              <ul class="nav nav-tabs nav-tabs-bordered">

                <li class="nav-item">
                  <button class="nav-link active" data-bs-toggle="tab" data-bs-target="#lp-overview">Data Landingpage</button>
                </li>

                <li class="nav-item">
                  <button class="nav-link" data-bs-toggle="tab" data-bs-target="#lp-edit">Edit Landingpage</button>
                </li>

              </ul>
              <div class="tab-content pt-2">

              <div class="tab-pane fade show active profile-overview" id="lp-overview">
                    <h5 class="card-title">Informasi Landingpage</h5>
                    
                    <div class="row">
                        <div class="col-lg-2 col-md-4 label">No. Template</div>
                        <div class="col-lg-10 col-md-8"><?php echo htmlspecialchars($row['no_template'] ?? '-'); ?></div>
                    </div>
                    
                    <div class="row">
                        <div class="col-lg-2 col-md-4 label">Headline Utama</div>
                        <div class="col-lg-10 col-md-8"><?php echo htmlspecialchars($row['judul_utama'] ?? '-'); ?></div>
                    </div>

                    <div class="row">
                        <div class="col-lg-2 col-md-4 label">Keterangan Bawah Headline Utama</div>
                        <div class="col-lg-10 col-md-8"><?php echo htmlspecialchars($row['deskripsi_utama'] ?? '-'); ?></div>
                    </div>
                    
                    <div class="row">
                        <div class="col-lg-2 col-md-4 label">Tentang Usaha</div>
                        <div class="col-lg-10 col-md-8"><?php echo htmlspecialchars($row['tentang_usaha'] ?? '-'); ?></div>
                    </div>
                   
                    <div class="row">
                        <div class="col-lg-2 col-md-4 label">Keterangan CTA Bawah</div>
                        <div class="col-lg-10 col-md-8"><?php echo htmlspecialchars($row['keterangan_cta_bawah'] ?? '-'); ?></div>
                    </div>
                       
                    <div class="row">
                      <div class="col-lg-2 col-md-4 label">Gambar Utama</div>
                      <div class="col-lg-10 col-md-8">
                          <?php if (!empty($row['gambar_utama'])): ?>
                              <img src="../../uploads/assets/<?php echo htmlspecialchars($row['gambar_utama']); ?>" alt="<?php echo htmlspecialchars($row['gambar_utama']); ?>" class="img-fluid" style="width: 384px; height: 199px; object-fit: cover;">
                          <?php else: ?>
                              <p>-</p>
                          <?php endif; ?>
                      </div>
                    </div>

                    <div class="row">
                      <div class="col-lg-2 col-md-4 label">Gambar Tentang Usaha</div>
                      <div class="col-lg-10 col-md-8">
                          <?php if (!empty($row['gambar_utama'])): ?>
                              <img src="../../uploads/assets/<?php echo htmlspecialchars($row['gambar_tentang']); ?>" alt="<?php echo htmlspecialchars($row['gambar_tentang']); ?>" class="img-fluid" style="width: 384px; height: 199px; object-fit: cover;">
                          <?php else: ?>
                              <p>-</p>
                          <?php endif; ?>
                      </div>
                    </div>

                    <div class="row">
                      <div class="col-lg-2 col-md-4 label">Gambar Keunggulan</div>
                      <div class="col-lg-10 col-md-8">
                          <?php if (!empty($row['gambar_utama'])): ?>
                              <img src="../../uploads/assets/<?php echo htmlspecialchars($row['gambar_keunggulan']); ?>" alt="<?php echo htmlspecialchars($row['gambar_keunggulan']); ?>" class="img-fluid" style="width: 384px; height: 199px; object-fit: cover;">
                          <?php else: ?>
                              <p>-</p>
                          <?php endif; ?>
                      </div>
                    </div>

                </div>

                <div class="tab-pane fade profile-edit pt-3" id="lp-edit">

                <form class="row g-3" action="<?= $form_action ?>" method="POST" enctype="multipart/form-data">
                <div class="col-md-12">
                    <label for="no_template" class="form-label">Pilih Template<span class="text-danger">*</span></label>
                    <div class="input-group">
                    <select id="no_template" name="no_template" class="form-select form-select-sm" required>
                        <option selected disabled>--Pilih Template--</option>
                        <?php 
                        $selected_template = $_SESSION['old_input']['no_template'] ?? ($row['no_template'] ?? '');
                        $templates = [
                            1 => "Template 1 (Produk)",
                            2 => "Template 2 (Produk)",
                            3 => "Template 3 (Produk)",
                            4 => "Template 4 (Layanan)",
                            5 => "Template 5 (Layanan)",
                            6 => "Template 6 (Layanan)"
                        ];

                        foreach ($templates as $key => $label): ?>
                            <option value="<?= $key ?>" <?= ($selected_template == $key) ? 'selected' : '' ?>>
                                <?= $label ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    </div>
                    <div class="d-flex flex-wrap gap-1 mt-2 justify-content-md-end">
                        <a href="https://portalumkm.com/toko-roti/" target="_blank" class="btn btn-primary btn-sm px-2 py-1">Demo 1</a>
                        <a href="https://portalumkm.com/toko-hijab" target="_blank" class="btn btn-primary btn-sm px-2 py-1">Demo 2</a>
                        <a href="https://portalumkm.com/toko-batik" target="_blank" class="btn btn-primary btn-sm px-2 py-1">Demo 3</a>
                        <a href="https://portalumkm.com/putra-jahit/" target="_blank" class="btn btn-primary btn-sm px-2 py-1">Demo 4</a>
                        <a href="https://portalumkm.com/rizki-service/" target="_blank" class="btn btn-primary btn-sm px-2 py-1">Demo 5</a>
                        <a href="https://portalumkm.com/jasa-kebersihan/" target="_blank" class="btn btn-primary btn-sm px-2 py-1">Demo 6</a>
                    </div>
                </div>

                  <div class="col-md-12">
                      <label for="judul_utama" class="form-label">Headline Utama<span class="text-danger">*</span></label>
                      <textarea type="text" class="form-control" id="judul_utama" name="judul_utama" required><?= $_SESSION['old_input']['judul_utama'] ?? ($row['judul_utama'] ?? '') ?></textarea>
                  </div>

                  <div class="col-md-12">
                      <label for="deskripsi_utama" class="form-label">Keterangan Bawah Headline Utama<span class="text-danger">*</span></label>
                      <textarea type="text" class="form-control" id="deskripsi_utama" name="deskripsi_utama" 
                           required><?= $_SESSION['old_input']['deskripsi_utama'] ?? ($row['deskripsi_utama'] ?? '') ?></textarea>
                  </div>

                  <div class="col-12">
                      <label for="tentang_usaha" class="form-label">Jelaskan Tentang Usaha Anda<span class="text-danger">*</span></label>
                      <textarea class="form-control" id="tentang_usaha" name="tentang_usaha"><?= $_SESSION['old_input']['tentang_usaha'] ?? ($row['tentang_usaha'] ?? '') ?></textarea>
                  </div>

                  <div class="col-md-12">
                      <label for="keterangan_cta_bawah" class="form-label">Keterangan CTA Bawah<span class="text-danger">*</span></label>
                      <textarea type="text" class="form-control" id="keterangan_cta_bawah" name="keterangan_cta_bawah"><?= $_SESSION['old_input']['keterangan_cta_bawah'] ?? ($row['keterangan_cta_bawah'] ?? '') ?></textarea>
                  </div>

                  <div class="col-12">
                      <label for="gambar_utama" class="form-label">Unggah Gambar Utama<span class="text-danger">*</span></label>
                      <input class="form-control" type="file" id="gambar_utama" name="gambar_utama" accept="image/*" <?= $gambar_utama_required ?>>
                  </div>

                  <div class="col-12">
                      <label for="gambar_tentang" class="form-label">Unggah Gambar Tentang Usaha<span class="text-danger">*</span></label>
                      <input class="form-control" type="file" id="gambar_tentang" name="gambar_tentang" accept="image/*" <?= $gambar_tentang_required ?>>
                  </div>

                  <div class="col-12">
                      <label for="gambar_keunggulan" class="form-label">Unggah Gambar Keunggulan<span class="text-danger">*</span></label>
                      <input class="form-control" type="file" id="gambar_keunggulan" name="gambar_keunggulan" accept="image/*" <?= $gambar_keunggulan_required ?>>
                  </div>

                  <div class="text-center">
                      <button type="submit" class="btn btn-primary me-2"><i class="bi bi-check-circle"></i> Simpan</button>
                  </div>
              </form>

              </div><!-- End Bordered Tabs -->

            </div>
          </div>

        </div>
      </div>
    </section>

        <!-- Modal Konfirmasi Logout -->
        <div class="modal fade" id="confirmLogoutModal" tabindex="-1" aria-labelledby="logoutModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="logoutModalLabel">Konfirmasi Logout</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body">
            Apakah Anda yakin ingin keluar dari halaman ini?
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
            <a href="../logout.php" id="logoutConfirmBtn" class="btn btn-danger">Keluar</a>
          </div>
        </div>
      </div>
    </div>

  </main><!-- End #main -->

  <!-- ======= Footer ======= -->
  <footer id="footer" class="footer">
    <div class="copyright">
      &copy; Copyright <strong><span>Portal UMKM</span></strong>. All Rights Reserved
    </div>
    <div class="credits">
      <!-- All the links in the footer should remain intact. -->
      <!-- You can delete the links only if you purchased the pro version. -->
      <!-- Licensing information: https://bootstrapmade.com/license/ -->
      <!-- Purchase the pro version with working PHP/AJAX contact form: https://bootstrapmade.com/nice-admin-bootstrap-admin-html-template/ -->
      <!-- Designed by <a href="https://bootstrapmade.com/">BootstrapMade</a> -->
    </div>
  </footer><!-- End Footer -->

  <a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>

  <!-- Vendor JS Files -->
  <script src="../../assets/template/vendor/apexcharts/apexcharts.min.js"></script>
  <script src="../../assets/template/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="../../assets/template/vendor/chart.js/chart.umd.js"></script>
  <script src="../../assets/template/vendor/echarts/echarts.min.js"></script>
  <script src="../../assets/template/vendor/quill/quill.js"></script>
  <script src="../../assets/template/vendor/simple-datatables/simple-datatables.js"></script>
  <script src="../../assets/template/vendor/tinymce/tinymce.min.js"></script>
  <script src="../../assets/template/vendor/php-email-form/validate.js"></script>

  <!-- Template Main JS File -->
  <script src="../../assets/template/js/main.js"></script>

</body>

</html>