<?php
require_once 'config/config.php';
require_once 'includes/functions.php';
startSecureSession();
$pageTitle = 'Technical Support & Inquiries';

$sent = false;
$errors = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name    = sanitize($_POST['name'] ?? '');
    $email   = sanitize($_POST['email'] ?? '');
    $phone   = sanitize($_POST['phone'] ?? '');
    $company = sanitize($_POST['company'] ?? '');
    $industry = sanitize($_POST['industry'] ?? '');
    $subject = sanitize($_POST['subject'] ?? '');
    $message = sanitize($_POST['message'] ?? '');

    if (!$name || !$email || !$message) {
        $errors[] = 'Full name, email, and detailed message are required.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = 'Please enter a valid business email.';
    } else {
        $body = "<h3>New Technical Inquiry - Rig N Dig</h3>
                 <hr>
                 <p><strong>Name:</strong> $name<br>
                 <strong>Email:</strong> $email<br>
                 <strong>Phone:</strong> $phone<br>
                 <strong>Company:</strong> $company<br>
                 <strong>Industry:</strong> $industry<br>
                 <strong>Inquiry Type:</strong> $subject</p>
                 <p><strong>Message Details:</strong><br>$message</p>";
        
        sendEmail(setting('email'), "RFQ/Inquiry: $subject - $company", $body, $name);
        
        // Save to Database (Mailbox)
        Database::execute(
            "INSERT INTO messages (name, email, phone, company, industry, subject, message) VALUES (?, ?, ?, ?, ?, ?, ?)",
            [$name, $email, $phone, $company, $industry, $subject, $message]
        );

        $sent = true;
    }
}

include 'includes/header.php';
?>

<div class="max-w-7xl mx-auto px-4 py-20">
  <div class="text-center mb-16 fade-in">
    <div class="section-tag">📬 Global Sourcing Network</div>
    <h1 class="section-title">Technical Support & <span class="text-orange-500">Quotations</span></h1>
    <div class="section-divider mx-auto"></div>
    <p class="text-gray-400 max-w-2xl mx-auto text-lg">Partner with Pakistan's leading industrial supplier. Whether you're in Oil & Gas, Mining, or Infrastructure — our technical team is ready to deliver.</p>
  </div>

  <div class="grid grid-cols-1 lg:grid-cols-5 gap-12">
    <!-- Form -->
    <div class="lg:col-span-3 fade-in">
      <?php if ($sent): ?>
      <div class="glass-panel p-10 text-center mb-8 border-green-600/30">
        <div class="text-5xl mb-4">✨</div>
        <h3 class="text-2xl font-bold text-white mb-2">Request Transmitted</h3>
        <p class="text-gray-400">Technical documentation and quotation will be prepared. Our specialists will contact you within 24-48 business hours.</p>
        <button onclick="window.location.reload()" class="mt-6 btn-outline">Send Another Inquiry</button>
      </div>
      <?php else: ?>
        <?php foreach ($errors as $e): ?>
        <div class="bg-red-900/50 border border-red-600 text-red-200 rounded-xl p-4 text-sm mb-6 flex items-center gap-3">
          <span>⚠️</span> <?= e($e) ?>
        </div>
        <?php endforeach; ?>

        <form method="POST" class="glass-card p-8 md:p-10 space-y-6">
          <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
              <label class="text-[10px] font-bold uppercase tracking-widest text-gray-500 block mb-2">Full Name <span class="text-orange-500">*</span></label>
              <input type="text" name="name" required value="<?= e($_POST['name'] ?? '') ?>" placeholder="John Doe">
            </div>
            <div>
              <label class="text-[10px] font-bold uppercase tracking-widest text-gray-500 block mb-2">Business Email <span class="text-orange-500">*</span></label>
              <input type="email" name="email" required value="<?= e($_POST['email'] ?? '') ?>" placeholder="john@company.com">
            </div>
          </div>

          <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
              <label class="text-[10px] font-bold uppercase tracking-widest text-gray-500 block mb-2">Company / Organization</label>
              <input type="text" name="company" value="<?= e($_POST['company'] ?? '') ?>" placeholder="e.g. OGDCL, PPL">
            </div>
            <div>
              <label class="text-[10px] font-bold uppercase tracking-widest text-gray-500 block mb-2">Industry Sector</label>
              <select name="industry">
                <option value="">Select Industry...</option>
                <option>Oil & Gas</option>
                <option>Mining & Minerals</option>
                <option>Power Generation</option>
                <option>Construction & Infra</option>
                <option>Manufacturing</option>
                <option>Other</option>
              </select>
            </div>
          </div>

          <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
              <label class="text-[10px] font-bold uppercase tracking-widest text-gray-500 block mb-2">Phone / WhatsApp</label>
              <input type="tel" name="phone" value="<?= e($_POST['phone'] ?? '') ?>" placeholder="+92 ...">
            </div>
            <div>
              <label class="text-[10px] font-bold uppercase tracking-widest text-gray-500 block mb-2">Inquiry Type</label>
              <select name="subject">
                <option>Product Quotation (RFQ)</option>
                <option>Workshop & Repair Service</option>
                <option>Technical Consultation</option>
                <option>Partnership & Distribution</option>
                <option>General Inquiry</option>
              </select>
            </div>
          </div>

          <div>
            <label class="text-[10px] font-bold uppercase tracking-widest text-gray-500 block mb-2">Message & Specifications <span class="text-orange-500">*</span></label>
            <textarea name="message" rows="6" required placeholder="Include sizes, pressure classes, material standards (API/ASME)..."><?= e($_POST['message'] ?? '') ?></textarea>
          </div>

          <div class="flex items-center justify-between gap-4 pt-2">
            <p class="text-[10px] text-gray-500 italic">By submitting, you agree to our privacy & data handling terms.</p>
            <button type="submit" class="btn-primary px-10 py-4 text-sm uppercase tracking-widest font-black">Transmit Request →</button>
          </div>
        </form>
      <?php endif; ?>
    </div>

    <!-- Info Column -->
    <div class="lg:col-span-2 space-y-8 fade-in">
      <div class="glass-panel p-8 rounded-2xl relative overflow-hidden group">
        <div class="absolute top-0 right-0 w-32 h-32 bg-orange-600/5 rounded-full -mr-16 -mt-16 blur-2xl group-hover:bg-orange-600/10 transition-colors"></div>
        <h3 class="text-xl font-black text-white mb-6">Connect Directly</h3>
        
        <div class="space-y-6">
          <?php foreach ([
            ['📍', 'Corporate HQ', setting('address'), 'https://maps.google.com/?q=' . urlencode(setting('address'))],
            ['📞', 'Direct Line', setting('phone'), 'tel:' . setting('phone')],
            ['💬', 'WhatsApp Business', setting('whatsapp'), 'https://wa.me/' . preg_replace('/[^0-9]/', '', setting('whatsapp'))],
            ['✉️', 'Technical Desk', setting('email'), 'mailto:' . setting('email')],
          ] as [$icon, $label, $val, $link]): if(!$val) continue; ?>
          <a href="<?= $link ?>" target="_blank" class="flex gap-5 group/item">
            <div class="w-12 h-12 rounded-xl bg-navy-800 border border-white/5 flex items-center justify-center shrink-0 group-hover/item:border-orange-500 transition-colors">
              <span class="text-xl"><?= $icon ?></span>
            </div>
            <div>
              <div class="text-[10px] font-bold uppercase tracking-widest text-gray-500 mb-1"><?= $label ?></div>
              <div class="text-sm font-medium text-gray-300 group-hover/item:text-orange-500 transition-colors"><?= e($val) ?></div>
            </div>
          </a>
          <?php endforeach; ?>
        </div>

        <div class="mt-10 pt-8 border-t border-white/5">
          <div class="text-[10px] font-bold uppercase tracking-widest text-gray-500 mb-4">Response Standards</div>
          <div class="flex items-center gap-3 text-xs text-green-500 font-bold mb-2">
            <span class="w-2 h-2 rounded-full bg-green-500 animate-pulse"></span>
            24/7 Technical Monitoring
          </div>
          <p class="text-xs text-gray-500">Typical quotation turnaround: 12-24 Hours for standard API stock items.</p>
        </div>
      </div>

      <!-- Static Map Preview -->
      <div class="glass-panel overflow-hidden rounded-2xl h-60 grayscale hover:grayscale-0 transition-all duration-700">
        <iframe
          src="https://maps.google.com/maps?q=Islamabad+Pakistan&output=embed"
          class="w-full h-full border-0" loading="lazy"></iframe>
      </div>
    </div>
  </div>
</div>

<?php include 'includes/footer.php'; ?>
