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

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Dashboard - Reviews</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" />
  <link rel="stylesheet" href="/css/dashstyle.css" />


  <style>
    body>script+div[style^="text"] {
      display: none
    }

    html,
    body {
      overflow-x: hidden !important
    }
  </style>
</head>

<body>
  <!-- Sidebar -->
  <aside class="sidebar" id="sidebar">
    <div class="logo">
      <a href="/dashboard" style="font-size: 30px; font-weight: bold; text-decoration: none; color: orangered;">Dashboard</a>
    </div>

    <nav class="nav-menu" aria-label="Main navigation">
      <a href="/dashboard" class="nav-item"><i class="fas fa-table"></i><span>Orders</span></a>
      <a href="/edit-reservations" class="nav-item"><i class="fa-solid fa-clipboard"></i><span>Table Bookings</span></a>
      <a href="/add-items" class="nav-item"><i class="fa-solid fa-bowl-food"></i><span>Add Items</span></a>
      <a href="/edit-reviews" class="nav-item active"><i class="fa-solid fa-comment"></i><span>Reviews</span></a>
      <a href="/qr-form" class="nav-item"><i class="fa-solid fa-qrcode"></i><span>Barcode</span></a>
    </nav>

    <div class="premium-box">
      <h3>Facing problem need Assistance!</h3>
      <button class="premium-btn"><a href="tel:+919026257744" style="text-decoration: none; color: orangered;">Call Now</a></button>
    </div>
  </aside>

  <!-- Main Content -->
  <main class="main-content">
    <!-- Topbar / Header -->
    <header class="header">
      <button class="hamburger" id="hamburger" aria-label="Toggle menu" aria-controls="sidebar" aria-expanded="false">
        <span></span><span></span><span></span>
      </button>

      <div class="welcome-section">
        <p class="greeting">👋 <%= greeting %>, <%= customerName %>!</p>
        <h1 class="welcome-title">Update Your Reviews</h1>
      </div>

      <div class="header-right">
        <div class="search-bar">
          <form method="get" action="/edit-reviews" style="display:flex;">
                <i class="fas fa-search" style="color:#666; margin-right:8px;"></i>
                <input type="text" name="search" placeholder="Name, Reviews" value="<%= rsearch || '' %>"  aria-label="Search" id="reviewSearch" />
            </form>
        </div>

        <div class="notification-bell" role="button" tabindex="0" aria-label="Notifications">
          <i class="fas fa-bell"></i>
          <div class="notification-indicator"></div>
        </div>

        <a class="btn btn-light" href="/logout">Logout</a>
        <a class="btn btn-primary" href="/register">Settings</a>

        <div class="user-profile" title="Alex Morgan">
          <img src="https://i.pravatar.cc/100?img=8" alt="Alex Morgan" class="profile-pic" />
        </div>
      </div>
    </header>

    <!-- Dashboard Container -->
    <div class="dashboard-container">
      <!-- Main Dashboard Content -->
      <section class="dashboard-main">

        <!--List Item-->
        <section class="card">
          <div class="card-header">
            <h2 class="section-title">Review List</h2>
            <div class="table-actions">
              <i class="fas fa-plus-circle"></i>
            </div>
          </div>

          <div class="table-responsive" id="reviewsTable">
            <% if (reviews.length> 0) { %>
              <table class="table">
                <thead>
                  <tr>
                    <th style="text-align: center;">Action</th>
                    <th style="text-align: center;">Name</th>
                    <th style="text-align: center;">Review</th>
                    <th style="text-align: center;">Image</th>
                  </tr>
                </thead>
                <tbody>
                  <% reviews.forEach((row, index)=> { %>
                    <tr>
                      <td style="text-align: center;">
                        <div style="display: flex; justify-content: center; gap: 5px;">
                          <!-- This button just opens the modal -->
                          <button class="btn btn-sm btn-primary" onclick="openEditModal('<%= row._id %>')"><i
                              class="fas fa-edit"></i></button>
                          <a href="/reviews/delete/<%= row._id %>" class="btn btn-sm btn-light"
                            onclick="return confirm('Are you sure you want to delete this item?')"><i
                              class="fas fa-trash"></i></a>
                        </div>
                      </td>
                      <td style="text-align: center;">
                        <%= row.cname %>
                      </td>
                      <td style="text-align: center;">
                        <%= row.review %>
                      </td>
                      <td style="text-align: center;">
                        <img src="/uploads/<%= row.image %>" alt="Customer Image" class="img-thumbnail"
                          style="width: 50px; height: 50px;" />
                      </td>
                    </tr>
                    <% }) %>
                </tbody>
              </table>
              <% } else { %>
                <div style="text-align: center; margin-bottom: 50px;">
                  <h1>No Reviews Found</h1>
                </div>
              <% } %>
          </div>
        </section>
      </section>
    </div>



    <% reviews.forEach((row, index)=> { %>
      <!-- Edit Modal for this row -->
      <div id="editModal-<%= row._id %>" class="custom-modal">
        <div class="modal-content">
          <span class="close-modal" onclick="closeModal('<%= row._id %>')">&times;</span>
          <h3>Edit Review</h3>
          <form action="/reviews/update/<%= row._id %>" method="POST" enctype="multipart/form-data">
            <div class="form-group">
              <input type="text" name="cname" value="<%= row.cname %>" required>
            </div>

            <div class="form-group">
              <input type="text" name="review" value="<%= row.review %>" required>
            </div>

            <div class="form-group">
              <img src="/uploads/<%= row.image %>" style="width: 100px;" class="img-thumbnail"><br>
              <div class="file-upload">
                <input type="file" name="image" id="fileInput" accept="image/*">
                <label for="fileInput">📁 Choose Image</label>
                <span id="fileName">No file chosen</span>
              </div>
              <input type="hidden" name="old_image" value="<%= row.image %>">
            </div>

            <div class="form-group">
              <button type="submit" class="btn-submit">Save Changes</button>
            </div>
          </form>
        </div>
      </div>
      <% }); %>


        <script>
          function openEditModal(id) {
            document.getElementById('editModal-' + id).style.display = 'block';
          }

          function closeModal(id) {
            document.getElementById('editModal-' + id).style.display = 'none';
          }

          // Optional: Close modal when clicking outside
          window.onclick = function (event) {
            const modals = document.querySelectorAll('.modal');
            modals.forEach(modal => {
              if (event.target === modal) {
                modal.style.display = "none";
              }
            });
          };
        </script>




  </main>

  <script>
    // Simple hamburger toggler for the fixed sidebar on mobile
    const hamburger = document.getElementById('hamburger');
    const sidebar = document.getElementById('sidebar');
    if (hamburger && sidebar) {
      hamburger.addEventListener('click', () => {
        const open = sidebar.classList.toggle('open');
        hamburger.setAttribute('aria-expanded', String(open));
      });
    }
  </script>

  <script>
    const fileInput = document.getElementById("fileInput");
    const fileName = document.getElementById("fileName");

    fileInput.addEventListener("change", function () {
      fileName.textContent = this.files.length > 0
        ? this.files[0].name
        : "No file chosen";
    });
  </script>

  <!--Ajax Search Script-->
  <script>
    const searchBox = document.getElementById("reviewSearch");
    let typingTimer;

    searchBox.addEventListener("input", function () {
      clearTimeout(typingTimer);
      typingTimer = setTimeout(() => {
        const value = searchBox.value.trim();

        fetch("/edit-reviews?search=" + encodeURIComponent(value), {
          headers: { "X-Requested-With": "XMLHttpRequest" }
        })
        .then(res => res.text())
        .then(html => {
          // Parse the returned HTML and extract the table part only
          const parser = new DOMParser();
          const doc = parser.parseFromString(html, "text/html");
          const newTable = doc.querySelector("#reviewsTable"); 

          // Replace only the table, not the whole page
          document.querySelector("#reviewsTable").innerHTML = newTable.innerHTML;
        });
      }, 400); // triggers after user stops typing for 0.4s
    });
  </script>

</body>

</html>