<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>QR Code Scanner</title>
  <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
  <style>
    /* General Reset */
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: 'Roboto', sans-serif;
      display: flex;
      justify-content: center;
      align-items: flex-start;
      min-height: 100vh;
      background-color: #f0f0f0; /* Light grey background */
      color: #fff;
      padding: 20px;
    }

    .container {
      text-align: center;
      width: 100%;
      max-width: 500px;
      background: #fff;
      color: #333;
      padding: 20px;
      border-radius: 12px;
      box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
      animation: fadeIn 1s ease-out;
    }

    .logo {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-bottom: 15px;
    }

    .logo img {
      width: 120px;
      height: 90px;
    }

    h1 {
      font-size: 28px;
      font-weight: 700;
      margin-bottom: 10px;
    }

    .video-wrapper {
      position: relative;
      width: 100%;
      max-width: 400px;
      margin: 20px auto;
      border-radius: 12px;
      overflow: hidden;
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    video {
      width: 100%;
      display: block;
      border-radius: 12px;
    }

    .overlay {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.7);
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
      font-size: 36px;
      font-weight: bold;
      border-radius: 12px;
      visibility: hidden;
      opacity: 0;
      transition: visibility 0s, opacity 0.5s ease;
    }

    .overlay.show {
      visibility: visible;
      opacity: 1;
    }

    #status {
      font-size: 18px;
      font-weight: 500;
      margin: 10px 0;
      color: #28a745;
    }

    #location {
      font-size: 16px;
      font-weight: 400;
      margin-top: 10px;
      color: #555;
    }

    button {
      padding: 10px 20px;
      font-size: 16px;
      font-weight: 500;
      border: none;
      border-radius: 8px;
      background: #ff9800;
      color: #fff;
      cursor: pointer;
      transition: background 0.3s ease;
    }

    button:hover {
      background: #6a0dad; /* Darker orange on hover */
    }

    /* Fade-in Animation */
    @keyframes fadeIn {
      from {
        opacity: 0;
        transform: translateY(-20px);
      }
      to {
        opacity: 1;
        transform: translateY(0);
      }
    }

    /* Responsive Design */
    @media (max-width: 600px) {
      .container {
        padding: 15px;
      }

      h1 {
        font-size: 24px;
      }

      #status, #location {
        font-size: 14px;
      }

      button {
        font-size: 14px;
        padding: 8px 16px;
      }
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="logo">
      <img src="https://metoree.s3.ap-northeast-1.amazonaws.com/img/companies/logo/197201.png" alt="Logo">
    </div>
    <h1>QR Code Scanner</h1>
    <div class="video-wrapper">
      <video id="video" autoplay></video>
      <div id="overlay" class="overlay">Thank You!</div>
    </div>
    <p id="status">Waiting for QR code...</p>
    <p id="location">Fetching location...</p>
    <button onclick="restartScanner()">Restart Scanner</button>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/@zxing/library@0.18.6/umd/index.min.js"></script>
  <script>
    const statusElement = document.getElementById('status');
    const locationElement = document.getElementById('location');
    const overlayElement = document.getElementById('overlay');
    const videoElement = document.getElementById('video');
    let userLocation = { lat: null, lng: null };
    let codeReader = new ZXing.BrowserQRCodeReader();

    // Get user location using Geolocation API
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(
        (position) => {
          const { latitude, longitude } = position.coords;
          userLocation.lat = latitude;
          userLocation.lng = longitude;
          locationElement.textContent = `Location: Captured`;
        },
        (error) => {
          locationElement.textContent = "Unable to fetch location";
          console.error("Geolocation error:", error);
        }
      );
    } else {
      locationElement.textContent = "Geolocation is not supported by this browser.";
    }

    // QR Code Reader setup
    function startScanner() {
      codeReader.decodeFromVideoDevice(null, videoElement, (result, err) => {
        if (result) {
          // Display success message
          statusElement.textContent = `Information Captured Successfully.`;
          console.log('QR Code detected:', result.text);

          // Show "Thank You!" in the overlay
          overlayElement.classList.add('show');

          // Send QR Code data and location to Apps Script
          google.script.run.processQRCode(result.text, userLocation.lat, userLocation.lng);

          // Stop scanning after successful detection
          codeReader.reset();
        } else if (err) {
          console.error('QR Code scan error:', err);
        }
      });
    }

    function restartScanner() {
      // Reset overlay and status
      overlayElement.classList.remove('show');
      statusElement.textContent = "Waiting for QR code...";
      startScanner();
    }

    // Start scanning on page load
    startScanner();
  </script>
</body>
</html>
