<?php
require_once '../src/config/db.php';
require_once '../src/includes/functions.php';

require_once '../src/includes/lang.php'; // This will define $t and $isRTL

$contact_info = get_contact_info($pdo);

$lang = $t['lang']; // Define the $lang variable for the form action

$message_sent = false;
$error_message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $message = trim($_POST['message'] ?? '');

    if (empty($name) || empty($email) || empty($message)) {
        $error_message = $isRTL ? 'يرجى ملء جميع الحقول.' : 'Please fill in all fields.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error_message = $isRTL ? 'البريد الإلكتروني غير صالح.' : 'Invalid email address.';
    } else {
        try {
            $stmt = $pdo->prepare("INSERT INTO contact_messages (name, email, message) VALUES (?, ?, ?)");
            $stmt->execute([$name, $email, $message]);
            $message_sent = true;
        } catch (Exception $e) {
            error_log($e->getMessage());
            $error_message = $isRTL ? 'حدث خطأ أثناء إرسال الرسالة. يرجى المحاولة مرة أخرى.' : 'An error occurred while sending the message. Please try again.';
        }
    }
}

include '../src/includes/header.php';
?>

<!-- Page Header -->
<section class="page-header py-5 py-lg-6">
    <div class="container text-center">
        <h1 class="display-4 fw-bolder text-white"><?php echo $t['nav_contact']; ?></h1>
        <p class="lead text-white-50 mx-auto" style="max-width: 800px;"><?php echo $t['contact_subtitle']; ?></p>
    </div>
</section>

<!-- Map Section -->
<section class="contact-map-section">
    <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d426583.7298836992!2d44.17975938124999!3d33.315241200000005!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x15577f67a0a74115%3A0x621bf7e2a3e794d!2sBaghdad%2C%20Iraq!5e0!3m2!1sen!2s!4v1678886400000!5m2!1sen!2s" width="100%" height="450" style="border:0;" allowfullscreen="" loading="lazy" referrerpolicy="no-referrer-when-downgrade" title="Google Maps location of Baghdad, Iraq"></iframe>
</section>

<!-- Contact Section -->
<section class="py-5 py-lg-6">
    <div class="container">
        <hr class="section-divider">
        <div class="row g-5">
            <div class="col-lg-5">
                <div class="contact-info-card h-100">
                    <h3><?php echo $t['contact_info_title']; ?></h3>
                    
                    <div class="info-item">
                        <div class="icon"><i class="bi bi-telephone-fill"></i></div>
                        <div>
                            <strong><?php echo $t['contact_phone_iraq']; ?></strong>
                            <a href="tel:<?php echo htmlspecialchars($contact_info['phone_iraq'] ?? ''); ?>" dir="ltr"><?php echo htmlspecialchars($contact_info['phone_iraq'] ?? ''); ?></a>
                        </div>
                    </div>

                    <div class="info-item">
                        <div class="icon"><i class="bi bi-telephone-fill"></i></div>
                        <div>
                            <strong><?php echo $t['contact_phone_jordan']; ?></strong>
                            <a href="tel:<?php echo htmlspecialchars($contact_info['phone_jordan'] ?? ''); ?>" dir="ltr"><?php echo htmlspecialchars($contact_info['phone_jordan'] ?? ''); ?></a>
                        </div>
                    </div>

                    <div class="info-item">
                        <div class="icon"><i class="bi bi-envelope-fill"></i></div>
                        <div>
                            <strong><?php echo $t['contact_email_gen']; ?></strong>
                            <a href="mailto:<?php echo htmlspecialchars($contact_info['email_info'] ?? ''); ?>"><?php echo htmlspecialchars($contact_info['email_info'] ?? ''); ?></a>
                        </div>
                    </div>

                    <hr class="my-4">

                    <div class="text-center">
                        <a href="<?php echo htmlspecialchars($contact_info['whatsapp'] ?? '#'); ?>" target="_blank" rel="noopener" class="btn btn-success rounded-pill px-4 py-2">
                            <i class="bi bi-whatsapp me-2"></i>
                            <?php echo $t['contact_whatsapp_btn']; ?>
                        </a>
                    </div>

                </div>
            </div>
            <div class="col-lg-7">
                <div class="contact-form-card">
                    <h3><?php echo $t['contact_form_title']; ?></h3>

                    <?php if ($message_sent): ?>
                        <div class="alert alert-success">✅ <?php echo $isRTL ? 'شكراً لك. تم استلام رسالتك بنجاح.' : 'Thank you. Your message has been received successfully.'; ?></div>
                    <?php endif; ?>
                    <?php if ($error_message): ?>
                        <div class="alert alert-danger">⚠️ <?php echo htmlspecialchars($error_message); ?></div>
                    <?php endif; ?>

                    <form action="contact.php?lang=<?php echo $lang; ?>" method="POST" class="contact-form">
                        <div class="mb-3">
                            <label for="name" class="form-label-custom"><?php echo $t['contact_name']; ?></label>
                            <input type="text" class="form-control form-control-custom" id="name" name="name" required>
                        </div>
                        <div class="mb-3">
                            <label for="email" class="form-label-custom"><?php echo $t['contact_email']; ?></label>
                            <input type="email" class="form-control form-control-custom" id="email" name="email" required>
                        </div>
                        <div class="mb-4">
                            <label for="message" class="form-label-custom"><?php echo $t['contact_message']; ?></label>
                            <textarea class="form-control form-control-custom" id="message" name="message" rows="6" placeholder="<?php echo $t['contact_msg_ph']; ?>" required></textarea>
                        </div>
                        <button type="submit" class="btn btn-submit"><?php echo $t['contact_send_btn']; ?></button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</section>

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