<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Upload Form</title>
  <!-- <link rel="stylesheet" href="css/upload.css"> -->
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <style>
    body {
      font-family: Arial, sans-serif;
    }

    .form-container {
      max-width: 800px;
      margin: 0 auto;
      padding: 20px;
      border: 1px solid #ccc;
      border-radius: 8px;
      background-color: #f9f9f9;
    }

    .form-group {
      margin-bottom: 15px;
    }

    label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
    }

    input[type="file"], input[type="text"], input[type="password"], input[type="email"], select {
      width: 100%;
      padding: 8px;
      margin-bottom: 10px;
      border: 1px solid #ccc;
      border-radius: 4px;
    }

    .radio-group {
      display: flex;
      flex-wrap: wrap;
    }

    .radio-option {
      margin-right: 20px;
      display: flex;
      align-items: center;
    }

    .radio-option img {
      width: 50px;
      height: 50px;
      margin-right: 10px;
      border: 1px solid #ccc;
      border-radius: 4px;
    }

    .radio-option input[type="radio"] {
      margin-right: 5px;
    }

    .submit-btn {
      padding: 10px 20px;
      background-color: #28a745;
      color: #fff;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }

    .submit-btn:hover {
      background-color: #218838;
    }

    .error-message {
      color: red;
      font-size: 12px;
    }
  </style>
</head>
<body>

<div class="form-container">
  <h2>Upload Form</h2>
  <form id="uploadForm" action="/documentUpload" method="post" enctype="multipart/form-data">
    <input type="hidden" id="company_id" name="company_id" value="">
    <input type="hidden" id="booth_id" name="booth_id" value="">

    <div class="form-group">
      <label for="document">Upload Documents (PDF, DOC, DOCX) - Up to 10 files:</label>
      <input type="file" id="document" name="document[]" accept=".pdf,.doc,.docx" multiple>
    </div>

    <div class="form-group">
      <label for="image">Upload Images (JPEG, PNG, etc.) - Up to 10 files:</label>
      <input type="file" id="image" name="image[]" accept="image/*" multiple>
    </div>

    <div class="form-group">
      <label for="video">Upload Videos (MP4, AVI, etc.) - Up to 10 files:</label>
      <input type="file" id="video" name="video[]" accept="video/*" multiple>
    </div>

    <div class="form-group">
      <label>Select Booth:</label>
      <div class="radio-group" id="booth-options">
        <!-- Radio buttons will be inserted here dynamically -->
      </div>
    </div>

    <button type="submit" class="submit-btn">Submit</button>
    <div class="error-message" id="error-message"></div>
  </form>
</div>

<script>
$(document).ready(function() {
  const thumbImages = [
    'image1.jpg',
    'image2.jpg',
    'image3.jpg'  // Add paths to your thumbnail images here
  ];

  const boothOptions = $('#booth-options');

  thumbImages.forEach(function(image, index) {
    const radioOption = `
       <div class="radio-option">
        <input type="radio" id="booth${index + 1}" name="booth" value="${index + 1}">
        <label for="booth${index + 1}">
          <img src="thumbs/${image}" alt="Booth ${index + 1}">
          Booth ${index + 1}
        </label>
      </div>
    `;
    boothOptions.append(radioOption);
  });

  const urlParams = new URLSearchParams(window.location.search);
  const companyId = urlParams.get('company_id');
  if (companyId) {
    $('#company_id').val(companyId);
  }

  $('#uploadForm').submit(function(e) {
    const documentFiles = $('#document')[0].files;
    const imageFiles = $('#image')[0].files;
    const videoFiles = $('#video')[0].files;
    const booth = $('input[name="booth"]:checked').val();
    // const selectedBooth = $('input[name="booth_id"]:checked').attr('id'); // Get the selected booth radio button ID
    // const boothId = selectedBooth ? selectedBooth.replace('booth', '') : ''; 

    let errorMessage = '';
    if (!booth) {
      errorMessage += 'Please select a booth.<br>';
    }
    if (documentFiles.length === 0) {
      errorMessage += 'Please upload at least one document.<br>';
    } else if (documentFiles.length > 10) {
      errorMessage += 'You can upload up to 10 documents only.<br>';
    }

    if (imageFiles.length === 0) {
      errorMessage += 'Please upload at least one image.<br>';
    } else if (imageFiles.length > 10) {
      errorMessage += 'You can upload up to 10 images only.<br>';
    }

    if (videoFiles.length === 0) {
      errorMessage += 'Please upload at least one video.<br>';
    } else if (videoFiles.length > 10) {
      errorMessage += 'You can upload up to 10 videos only.<br>';
    }

    if (errorMessage) {
      e.preventDefault();
      $('#error-message').html(errorMessage);
    } else {
      $('#booth_id').val(booth); // Set booth_id before form submission
    }
  });
});
</script>

</body>
</html>
