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

$user_id = $_SESSION['user_id']; 

$stmt = $conn->prepare("SELECT nama_usaha, alamat, alamat_gmaps, username_fb, username_ig FROM datausaha WHERE id_user = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$data_usaha = $result->fetch_assoc();

// Jika data sudah ada, gunakan update.php
$form_action = $data_usaha ? "update.php" : "add.php";

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

$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 Data Usaha | 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 collapsed" 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" 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 Data Usaha</h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="">Beranda</a></li>
          <li class="breadcrumb-item">Data Usaha</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']); 
      ?>
    <?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']); 
      ?>
    <?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="#du-overview">Data Usaha</button>
                </li>

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

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

              <div class="tab-pane fade show active profile-overview" id="du-overview">
                    <h5 class="card-title">Informasi Data Usaha</h5>
                    
                    <div class="row">
                        <div class="col-lg-2 col-md-4 label">Nama Usaha</div>
                        <div class="col-lg-10 col-md-8"><?php echo htmlspecialchars($data_usaha['nama_usaha'] ?? '-'); ?></div>
                    </div>
                    
                    <div class="row">
                        <div class="col-lg-2 col-md-4 label">Alamat Usaha</div>
                        <div class="col-lg-10 col-md-8"><?php echo htmlspecialchars($data_usaha['alamat'] ?? '-'); ?></div>
                    </div>
                    
                    <div class="row">
                        <div class="col-lg-2 col-md-4 label">Google Maps</div>
                        <div class="col-lg-10 col-md-8">
                            <div class="mt-2">
                              <?php if (!empty($data_usaha['alamat_gmaps'])): ?>
                                  <iframe src="<?php echo htmlspecialchars($data_usaha['alamat_gmaps']); ?>" width="100%" height="300" style="border:0;" allowfullscreen="" loading="lazy"></iframe>
                              <?php else: ?>
                                  -
                              <?php endif; ?>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-lg-2 col-md-4 label">Facebook</div>
                        <div class="col-lg-10 col-md-8">
                            <a href="https://facebook.com/<?php echo htmlspecialchars($data_usaha['username_fb'] ?? ''); ?>" target="_blank">
                                <?php echo htmlspecialchars($data_usaha['username_fb'] ?? '-'); ?>
                            </a>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-lg-2 col-md-4 label">Instagram</div>
                        <div class="col-lg-10 col-md-8">
                            <a href="https://instagram.com/<?php echo htmlspecialchars($data_usaha['username_ig'] ?? ''); ?>" target="_blank">
                                <?php echo htmlspecialchars($data_usaha['username_ig'] ?? '-'); ?>
                            </a>
                        </div>
                    </div>
                </div>

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

                <form method="POST" action="<?= $form_action ?>">
                    <div class="row mb-3">
                      <label for="nama_usaha" class="col-md-4 col-lg-2 col-form-label">Nama Usaha<span class="text-danger">*</span></label>
                      <div class="col-md-8 col-lg-10">
                        <textarea type="text" class="form-control" id="nama_usaha" name="nama_usaha" placeholder="Masukkan nama usaha anda" required><?= htmlspecialchars($data_usaha['nama_usaha'] ?? '') ?></textarea>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label for="alamat_usaha" class="col-md-4 col-lg-2 col-form-label">Alamat Usaha<span class="text-danger">*</span></label>
                      <div class="col-md-8 col-lg-10">
                        <textarea class="form-control" id="alamat_usaha" name="alamat_usaha" placeholder="JL. Raya ..." required><?= htmlspecialchars($data_usaha['alamat'] ?? '') ?></textarea>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label for="google_maps" class="col-md-4 col-lg-2 col-form-label">Iframe Google Maps<span class="text-danger">*</span></label>
                      <div class="col-md-8 col-lg-10">
                        <textarea type="url" class="form-control" id="google_maps" name="google_maps" oninput="extractURL()" placeholder="https://www.google.com/maps/..." required><?= htmlspecialchars($data_usaha['alamat_gmaps'] ?? '') ?></textarea>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label for="facebook" class="col-md-4 col-lg-2 col-form-label">Username Facebook</label>
                      <div class="col-md-8 col-lg-10">
                        <div class="input-group">
                          <span class="input-group-text">https://fb.com/</span>
                          <input type="text" name="facebook" class="form-control" id="facebook" value="<?= htmlspecialchars($data_usaha['username_fb'] ?? '') ?>" placeholder="usernameakun">
                        </div>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label for="instagram" class="col-md-4 col-lg-2 col-form-label">Username Instagram</label>
                      <div class="col-md-8 col-lg-10">
                        <div class="input-group">
                          <span class="input-group-text">https://ig.com/</span>
                          <input type="text" name="instagram" id="instagram" class="form-control" value="<?= htmlspecialchars($data_usaha['username_ig'] ?? '') ?>" placeholder="usernameakun">
                        </div>
                      </div>
                    </div>

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

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

  <script>
    function extractURL() {
      let inputField = document.getElementById("google_maps");
      let iframeMatch = inputField.value.match(/src="(https:[^"]+)"/);

      if (iframeMatch) {
        inputField.value = iframeMatch[1]; // Hanya ambil URL dari src
      }
    }
  </script>

</body>

</html>