<?php
$page_title = 'Contact Us - RoutexShipping';
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../app/Utils.php';
require_once __DIR__ . '/../app/Services/EmailService.php';
require_once __DIR__ . '/../includes/homeheader.php';

$message_sent = false;
$error_message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!Utils::verifyCSRFToken($_POST['csrf_token'] ?? '')) {
        $error_message = 'Security token invalid. Please try again.';
    } else {
        $name = Utils::sanitize($_POST['name'] ?? '');
        $email = Utils::sanitize($_POST['email'] ?? '');
        $phone = Utils::sanitize($_POST['phone'] ?? '');
        $subject = Utils::sanitize($_POST['subject'] ?? '');
        $message = Utils::sanitize($_POST['message'] ?? '');

        if (empty($name) || empty($email) || empty($message)) {
            $error_message = 'Please fill in all required fields.';
        } elseif (!Utils::validateEmail($email)) {
            $error_message = 'Please enter a valid email address.';
        } else {
            // Send email
            $email_service = new EmailService($pdo);
            $body = "
            <h2>New Contact Form Submission</h2>
            <p><strong>Name:</strong> $name</p>
            <p><strong>Email:</strong> $email</p>
            <p><strong>Phone:</strong> $phone</p>
            <p><strong>Subject:</strong> $subject</p>
            <p><strong>Message:</strong></p>
            <p>$message</p>
            ";

            if ($email_service->send(ADMIN_EMAIL, "New Contact: $subject", $body)) {
                $message_sent = true;
            } else {
                $error_message = 'Failed to send message. Please try again.';
            }
        }
    }
}

$csrf_token = Utils::generateCSRFToken();
?>

<!-- Page Header -->
<section class="page-header position-relative bg-cover jarallax" data-jarallax data-speed=".2">
    <img src="images/tracking-bg.jpg" alt="Contact Us" class="jarallax-img">
    <div class="parallax-overly"></div>
    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <div class="page-header-content text-center text-white py-5">
                    <h1 class="display-3 fw-bold">Contact Us</h1>
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb justify-content-center mb-0">
                            <li class="breadcrumb-item"><a href="<?= BASE_URL ?>/" class="text-white">Home</a></li>
                            <li class="breadcrumb-item active text-white" aria-current="page">Contact</li>
                        </ol>
                    </nav>
                </div>
            </div>
        </div>
    </div>
</section>

<!-- Contact Section -->
<section class="contact-section sec-padding bg-shade">
    <div class="container">
        <div class="row g-4 mb-5">
            <!-- Contact Info Cards -->
            <div class="col-lg-4 col-md-6">
                <div class="service-card bg-white round text-center p-4 h-100">
                    <div class="icon-lg bg-primary text-white rounded-circle mx-auto mb-3 d-flex align-items-center justify-content-center">
                        <i class="fas fa-phone-volume"></i>
                    </div>
                    <h3 class="h4">Phone Number</h3>
                    <p class="mb-1"><?php echo htmlspecialchars($settings['company_phone'] ?? '+1-800-ROUTEX-1'); ?></p>
                    <p class="text-muted small">Available 24/7 for support</p>
                </div>
            </div>

            <div class="col-lg-4 col-md-6">
                <div class="service-card bg-white round text-center p-4 h-100">
                    <div class="icon-lg bg-primary text-white rounded-circle mx-auto mb-3 d-flex align-items-center justify-content-center">
                        <i class="fas fa-envelope"></i>
                    </div>
                    <h3 class="h4">Email Address</h3>
                    <p class="mb-1"><?php echo htmlspecialchars($settings['company_email'] ?? 'info@routexshipping.com'); ?></p>
                    <p class="text-muted small">Response within 2 hours</p>
                </div>
            </div>

            <div class="col-lg-4 col-md-6 mx-auto">
                <div class="service-card bg-white round text-center p-4 h-100">
                    <div class="icon-lg bg-primary text-white rounded-circle mx-auto mb-3 d-flex align-items-center justify-content-center">
                        <i class="fas fa-map-marker-alt"></i>
                    </div>
                    <h3 class="h4">Office Location</h3>
                    <p class="mb-1"><?php echo nl2br(htmlspecialchars($settings['company_address'] ?? "123 Logistics Avenue\nGlobal City, GC 12345")); ?></p>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-lg-8 mx-auto">
                <div class="bg-white p-5 rounded shadow-sm">
                    <div class="sec-intro text-center mb-4">
                        <span class="sub-title">Get In Touch</span>
                        <h2 class="sec-title">Send Us a Message</h2>
                    </div>

                    <?php if ($message_sent): ?>
                    <div class="alert alert-success d-flex align-items-center" role="alert">
                        <i class="fas fa-check-circle me-2"></i>
                        <div>Thank you! Your message has been sent successfully.</div>
                    </div>
                    <?php elseif ($error_message): ?>
                    <div class="alert alert-danger d-flex align-items-center" role="alert">
                        <i class="fas fa-exclamation-circle me-2"></i>
                        <div><?php echo htmlspecialchars($error_message); ?></div>
                    </div>
                    <?php endif; ?>

                    <form method="POST" class="row g-3">
                        <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf_token); ?>">
                        <div class="col-md-6">
                            <input type="text" name="name" class="form-control p-3" placeholder="Full Name *" required>
                        </div>
                        <div class="col-md-6">
                            <input type="email" name="email" class="form-control p-3" placeholder="Email Address *" required>
                        </div>
                        <div class="col-md-6">
                            <input type="tel" name="phone" class="form-control p-3" placeholder="Phone Number">
                        </div>
                        <div class="col-md-6">
                            <input type="text" name="subject" class="form-control p-3" placeholder="Subject *" required>
                        </div>
                        <div class="col-12">
                            <textarea name="message" rows="5" class="form-control p-3" placeholder="Your Message *" required></textarea>
                        </div>
                        <div class="col-12 text-center">
                            <button type="submit" class="btn btn-primary px-5 py-3">Send Message <i class="fas fa-paper-plane ms-2"></i><span></span></button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</section>

  <footer class="footer bg-cover jarallax" data-jarallax data-speed=".2">
      <img src="images/footer-bg.jpg" alt="" class="jarallax-img">
      <div class="parallax-overly"></div>
      <div class="container">
        
         <div class="row footer-widgets">
            <div class="col-lg-4 col-sm-6 mb-5 mb-lg-0">
               <div class="footer-widget about-footer">
                  <div class="f-logo">
                     <a href="index.php"><img src="images/routex-logo.png" alt="Logo"></a>
                  </div>
                  <p>Our dedication lies in embracing challeng es and pioneering innovation within the more atractive
                     advertising sector.</p>
                  <div class="social-share">
                     <a href="#"><i class="fab fa-facebook-f"></i></a>
                     <a href="#"><i class="fab fa-x-twitter"></i></a>
                     <a href="#"><i class="fab fa-instagram"></i></a>
                     <a href="#"><i class="fab fa-linkedin-in"></i></a>
                  </div>

               </div>
            </div>
            <div class="col-lg-2 col-sm-4 offset-sm-1 offset-lg-0">
               <div class="footer-widget">
                  <h3 class="widget-title">Quick Links</h3>
                  <ul>
                     <li><a href="#">About Us</a></li>
                     <li><a href="#">Our Services</a></li>
                     <li><a href="#">Project</a></li>
                     <li><a href="#">FAQ's</a></li>
                     <li><a href="#">Our Blog</a></li>
                     <li><a href="">Contact Us</a></li>
                  </ul>
               </div>
            </div>

            <div class="col-lg-3 ps-lg-5 col-sm-6">
               <div class="footer-widget">
                  <h3 class="widget-title">Our Services</h3>
                  <ul>
                     <li><a href="#">Ship Fright Shipping</a></li>
                     <li><a href="#">Less Than Truckload</a></li>
                     <li><a href="#">Container Freight</a></li>
                     <li><a href="#">Adult Health</a></li>
                     <li><a href="#">Rail Freight Shipping</a></li>
                     <li><a href="#">Air Fright Trucking</a></li>
                  </ul>
               </div>
            </div>

            <div class="col-lg-3 col-sm-4 offset-sm-1 offset-lg-0">
               <div class="footer-widget contact-widget">
                  <h3 class="widget-title">Opening Hours</h3>
                  <div class="opening-inner">
                     <div class="d-flex align-items-center justify-content-between mb-3">
                        <span>Week Days</span>
                        <span>09.00 - 7.00</span>
                     </div>
                     <div class="d-flex align-items-center justify-content-between mb-3">
                        <span>Saturday</span>
                        <span>08.00 - 2.00</span>
                     </div>
                     <div class="d-flex align-items-center justify-content-between mb-3">
                        <span>Sunday</span>
                        <span>Day Off</span>
                     </div>
                  </div>
                  <a href="contact.html" class="btn btn-info btn-xs mt-4">Contact Us <span></span></a>
               </div>
            </div>
         </div>
      </div>
      <div class="footer-bottom">
         <div class="container">
            <div class="d-flex flex-wrap justify-content-between align-items-center">
               <p class="text-info copy-right mb-0 order-2 order-md-1">
                  Copyright &copy; 2026 <a href="#">Routex</a> All Rights Reserved.</p>
               <nav class="footer-nav order-1 order-md-2">
                  <ul>
                     <li><a href="#">Privacy Policy </a></li>
                     <li><a href="#">Terms & Condition</a></li>
                  </ul>
               </nav>
            </div>
         </div>
      </div>
   </footer>
  
