<?php
include '../auth.php';
include '../../koneksi.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'];
?>

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

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

  <title>Tambahkan Produk / Layanan | 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 collapsed" 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" 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>Tambah Produk / Layanan</h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="">Beranda</a></li>
          <li class="breadcrumb-item">Produk</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">
      <div class="row">
        <div class="col-lg-12">
          <div class="card">
            <div class="card-body mt-3">

              <div class="row">

                <!-- Preview Image -->
                <div class="col-md-4 d-flex justify-content-center align-items-center">
                <img id="preview" 
                src="https://via.placeholder.com/150?text=No+Image" 
                class="img-fluid border rounded" 
                style="width: 350px; height: 350px; object-fit: cover;">
                </div>

                <!-- Form -->
                <div class="col-md-8">
                  <form action="addproduk.php" method="POST" enctype="multipart/form-data">
                    <div class="mb-3">
                      <label for="namaProduk" class="form-label">Nama Produk<span class="text-danger">*</span></label>
                      <input type="text" class="form-control" id="namaProduk" name="nama_produk" placeholder="Masukkan nama produk" value="<?= isset($_SESSION['old_input']['nama_produk']) ? htmlspecialchars($_SESSION['old_input']['nama_produk']) : '' ?>" required>
                    </div>

                    <div class="mb-3">
                      <label for="harga" class="form-label">Harga<span class="text-danger">*</span></label>
                      <input type="number" class="form-control" id="harga" name="harga" placeholder="Masukkan harga" value="<?= isset($_SESSION['old_input']['harga_produk']) ? htmlspecialchars($_SESSION['old_input']['harga_produk']) : '' ?>" required>
                    </div>

                    <div class="mb-3">
                      <label for="gambar" class="form-label">Unggah Gambar<span class="text-danger">*</span></label>
                      <input class="form-control" type="file" id="gambar" name="gambar" accept="image/*" onchange="previewImage(event)">
                      <img id="preview" src="" alt="Preview Gambar" class="mt-2" style="max-width: 100px; display: none;">
                    </div>

                    <div class="d-flex justify-content-end">
                      <button type="submit" class="btn btn-primary me-2"><i class="bi bi-check-circle"></i> Simpan</button>
                      <a href="../produk" class="btn btn-secondary me-2">
                        <i class="bi bi-x-circle"></i> Batal
                      </a>
                    </div>
                  </form>
                </div>
              </div>

            </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 Keluar</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>

  <script>
    function previewImage(event) {
      const input = event.target;
      const preview = document.getElementById("preview");

      if (input.files && input.files[0]) {
        const reader = new FileReader();

        reader.onload = function(e) {
          preview.src = e.target.result;
        };

        reader.readAsDataURL(input.files[0]);
      } else {
        preview.src = "https://via.placeholder.com/150?text=No+Image";
      }
    }
  </script>

</body>

</html>