/**
* Theme Name: Cargo Trucking Child
* Description: This is a child theme of Cargo Trucking.
* Author: <a href="https://cmsmasters.net/">cmsmasters</a>
* Template: cargo-trucking
* Version: 1.0.0
* Tested up to: 6.6
* Requires PHP: 7.4
* License:
* License URI:
* Text Domain: cargo-trucking-child
* Copyright: cmsmasters 2025 / All Rights Reserved
*/

function tuvas_enquiry_popup() {
  if ( is_front_page() || is_home() ) {
    ?>
    <style>
    #tuvas-overlay {
      position: fixed;
      inset: 0;
      background: rgba(0,0,0,0.5);
      display: flex;
      align-items: center;
      justify-content: center;
      z-index: 99999;
      padding: 1rem;
    }
    #tuvas-modal {
      background: #fff;
      border-radius: 12px;
      padding: 2rem;
      width: 100%;
      max-width: 500px;
      position: relative;
    }
    #tuvas-modal h2 {
      font-size: 20px;
      font-weight: 600;
      margin-bottom: 1.25rem;
      color: #111;
    }
    #tuvas-close {
      position: absolute;
      top: 1rem;
      right: 1rem;
      background: none;
      border: none;
      font-size: 22px;
      cursor: pointer;
      color: #666;
      line-height: 1;
    }
    .tuvas-row {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 12px;
      margin-bottom: 12px;
    }
    .tuvas-group {
      display: flex;
      flex-direction: column;
      gap: 4px;
      margin-bottom: 12px;
    }
    .tuvas-group label {
      font-size: 11px;
      font-weight: 600;
      text-transform: uppercase;
      letter-spacing: 0.05em;
      color: #666;
    }
    .tuvas-group input,
    .tuvas-group select,
    .tuvas-group textarea {
      width: 100%;
      padding: 9px 12px;
      border: 1px solid #ddd;
      border-radius: 8px;
      font-size: 14px;
      color: #111;
      outline: none;
      font-family: inherit;
      background: #fff;
    }
    .tuvas-group textarea {
      resize: vertical;
    }
    #tuvas-submit {
      width: 100%;
      background: #25D366;
      color: #fff;
      border: none;
      border-radius: 8px;
      padding: 0.7rem 1rem;
      font-size: 15px;
      font-weight: 600;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 8px;
      margin-top: 0.5rem;
    }
    #tuvas-submit:hover { background: #1ebe5d; }
    #tuvas-submit svg {
      width: 18px;
      height: 18px;
      fill: #fff;
      flex-shrink: 0;
    }
    @media (max-width: 480px) {
      .tuvas-row { grid-template-columns: 1fr; }
    }
    </style>

    <div id="tuvas-overlay">
      <div id="tuvas-modal">
        <button id="tuvas-close" aria-label="Close">&times;</button>
        <h2>Send an Enquiry</h2>

        <div class="tuvas-row">
          <div class="tuvas-group">
            <label for="tuvas-name">Your Name</label>
            <input type="text" id="tuvas-name" placeholder="Enter name">
          </div>
          <div class="tuvas-group">
            <label for="tuvas-email">Your Email</label>
            <input type="email" id="tuvas-email" placeholder="Enter email">
          </div>
        </div>

        <div class="tuvas-row">
          <div class="tuvas-group">
            <label for="tuvas-company">Company</label>
            <input type="text" id="tuvas-company" placeholder="Enter company name">
          </div>
          <div class="tuvas-group">
            <label for="tuvas-service">Services Needed</label>
            <select id="tuvas-service">
              <option>3PL Services</option>
              <option>Warehouse Services</option>
              <option>Transport Services</option>
              <option>E-commerce Services</option>
              <option>Freight Forwarding</option>
            </select>
          </div>
        </div>

        <div class="tuvas-group">
          <label for="tuvas-desc">Brief Description</label>
          <textarea id="tuvas-desc" rows="4" placeholder="Tell us about your needs or challenges"></textarea>
        </div>

        <button id="tuvas-submit">
          <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z"/></svg>
          Send via WhatsApp
        </button>
      </div>
    </div>

    <script>
    (function () {
      var STORAGE_KEY = 'tuvas_popup_seen';
      var WA_NUMBER   = '919999999999';

      var overlay = document.getElementById('tuvas-overlay');

      if (localStorage.getItem(STORAGE_KEY)) {
        overlay.style.display = 'none';
      }

      document.getElementById('tuvas-close').addEventListener('click', closePopup);
      overlay.addEventListener('click', function (e) {
        if (e.target === overlay) closePopup();
      });

      function closePopup() {
        overlay.style.display = 'none';
        localStorage.setItem(STORAGE_KEY, '1');
      }

      document.getElementById('tuvas-submit').addEventListener('click', function () {
        var name    = document.getElementById('tuvas-name').value.trim();
        var email   = document.getElementById('tuvas-email').value.trim();
        var company = document.getElementById('tuvas-company').value.trim();
        var service = document.getElementById('tuvas-service').value;
        var desc    = document.getElementById('tuvas-desc').value.trim();

        if (!name)  { alert('Please enter your name.');  return; }
        if (!email) { alert('Please enter your email.'); return; }

        var msg = 'Hello! I have an enquiry:%0A%0A'
          + '*Name:* '           + encodeURIComponent(name)             + '%0A'
          + '*Email:* '          + encodeURIComponent(email)            + '%0A'
          + '*Company:* '        + encodeURIComponent(company || 'N/A') + '%0A'
          + '*Service Needed:* ' + encodeURIComponent(service)          + '%0A'
          + '*Description:* '    + encodeURIComponent(desc    || 'N/A');

        window.open('https://wa.me/' + WA_NUMBER + '?text=' + msg, '_blank');
        closePopup();
      });
    })();
    </script>
    <?php
  }
}
add_action( 'wp_footer', 'tuvas_enquiry_popup' );