<?php
define('SECURE_ACCESS', true);

$pageTitle = "Contact Us";
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

$error = '';
$success = '';

// Handle Form Submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!verifyCSRFToken($_POST['csrf_token'] ?? '')) {
        $error = "Security check failed (CSRF). Please try again.";
    } else {
        $name = trim($_POST['name'] ?? '');
        $email = trim($_POST['email'] ?? '');
        $subject = trim($_POST['subject'] ?? '');
        $message = trim($_POST['message'] ?? '');

        if (empty($name) || empty($email) || empty($subject) || empty($message)) {
            $error = "All fields are required.";
        } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error = "Please enter a valid email address.";
        } else {
            try {
                $stmt = $pdo->prepare("INSERT INTO contact_messages (name, email, subject, message) VALUES (?, ?, ?, ?)");
                $stmt->execute([$name, $email, $subject, $message]);
                
                $_SESSION['flash_success'] = "Thank you! Your message has been received. We will get back to you shortly.";
                header("Location: contact");
                exit();
            } catch (PDOException $e) {
                $error = "Failed to submit message: " . $e->getMessage();
            }
        }
    }
}

require_once __DIR__ . '/../includes/header.php';
?>

<div class="container-fluid px-md-5 py-5">
    <div class="row g-5">
        <!-- Contact Form Column -->
        <div class="col-12 col-lg-7">
            <div class="card border-0 shadow-sm p-4 p-md-5 rounded-3 bg-white h-100">
                <h2 class="fw-bold text-dark mb-2"><i class="bi bi-chat-left-text-fill text-primary me-2"></i>Send Us a Message</h2>
                <p class="text-secondary mb-4">Have questions about our events or corporate program? Feel free to reach out using the form below.</p>
                
                <?php if (!empty($error)): ?>
                    <div class="alert alert-danger border-0 shadow-sm mb-4" role="alert">
                        <i class="bi bi-x-circle-fill me-2"></i><?php echo htmlspecialchars($error); ?>
                    </div>
                <?php endif; ?>

                <form method="POST" action="contact">
                    <input type="hidden" name="csrf_token" value="<?php echo getCSRFToken(); ?>">
                    
                    <div class="row g-3">
                        <div class="col-12 col-md-6">
                            <label for="name" class="form-label small fw-bold text-dark">Your Name *</label>
                            <input type="text" class="form-control py-2.5" id="name" name="name" required placeholder="Ex: John Doe">
                        </div>
                        <div class="col-12 col-md-6">
                            <label for="email" class="form-label small fw-bold text-dark">Email Address *</label>
                            <input type="email" class="form-control py-2.5" id="email" name="email" required placeholder="Ex: john@example.com">
                        </div>
                        <div class="col-12">
                            <label for="subject" class="form-label small fw-bold text-dark">Subject *</label>
                            <input type="text" class="form-control py-2.5" id="subject" name="subject" required placeholder="Ex: Event Sponsorship Query">
                        </div>
                        <div class="col-12">
                            <label for="message" class="form-label small fw-bold text-dark">Message *</label>
                            <textarea class="form-control" id="message" name="message" rows="6" required placeholder="Type your detailed query here..."></textarea>
                        </div>
                    </div>

                    <div class="mt-4">
                        <button type="submit" class="btn btn-primary px-4 py-2.5 fw-bold shadow-sm">
                            <i class="bi bi-send-fill me-1"></i> Submit Inquiry
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- Corporate Contact Details Column -->
        <div class="col-12 col-lg-5">
            <div class="card border-0 shadow-sm p-4 p-md-5 rounded-3 bg-white h-100 d-flex flex-column justify-content-between">
                <div>
                    <h2 class="fw-bold text-dark mb-4"><i class="bi bi-building-fill text-primary me-2"></i>Contact Details</h2>
                    
                    <!-- Contact Cards -->
                    <div class="d-flex align-items-start mb-4">
                        <div class="p-3 bg-primary bg-opacity-10 text-primary rounded-circle me-3">
                            <i class="bi bi-geo-alt-fill fs-4"></i>
                        </div>
                        <div>
                            <h5 class="fw-bold text-dark mb-1">Corporate Office</h5>
                            <p class="text-secondary small mb-0"><?php echo nl2br(htmlspecialchars($site_cfg['company_address'] ?? '123 Corporate Tower, Outer Ring Road, Bengaluru, Karnataka, 560103')); ?></p>
                        </div>
                    </div>

                    <div class="d-flex align-items-start mb-4">
                        <div class="p-3 bg-success bg-opacity-10 text-success rounded-circle me-3">
                            <i class="bi bi-envelope-fill fs-4"></i>
                        </div>
                        <div>
                            <h5 class="fw-bold text-dark mb-1">Email Support</h5>
                            <p class="text-secondary small mb-0">
                                <a href="mailto:<?php echo htmlspecialchars($site_cfg['contact_email'] ?? 'support@reneneteducare.com'); ?>" class="text-decoration-none text-success fw-semibold">
                                    <?php echo htmlspecialchars($site_cfg['contact_email'] ?? 'support@reneneteducare.com'); ?>
                                </a>
                            </p>
                        </div>
                    </div>

                    <div class="d-flex align-items-start">
                        <div class="p-3 bg-info bg-opacity-10 text-info rounded-circle me-3">
                            <i class="bi bi-telephone-fill fs-4"></i>
                        </div>
                        <div>
                            <h5 class="fw-bold text-dark mb-1">Phone Number</h5>
                            <p class="text-secondary small mb-0"><?php echo htmlspecialchars($site_cfg['contact_phone'] ?? '+91 80 1234 5678'); ?><br><span class="text-muted fs-7">Mon - Fri, 9:00 AM - 6:00 PM IST</span></p>
                        </div>
                    </div>
                </div>

                <!-- Google Map Placeholder -->
                <div class="mt-4 border rounded overflow-hidden shadow-sm position-relative" style="height: 180px; background-image: url('https://images.unsplash.com/photo-1524661135-423995f22d0b?w=600&auto=format&fit=crop'); background-size: cover; background-position: center;">
                    <div class="position-absolute top-0 start-0 w-100 h-100 bg-dark bg-opacity-30 d-flex align-items-center justify-content-center">
                        <span class="badge bg-white text-dark p-3 fw-bold border"><i class="bi bi-map-fill me-1 text-primary"></i> Bengaluru, IN</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
require_once __DIR__ . '/../includes/footer.php';
?>
