<?php
$pageTitle = "Contact Us - Tom's Java Jive";
$metaDescription = "Get in touch with Tom's Java Jive. We're here to help with orders, questions, and anything coffee-related.";
$canonicalUrl = 'https://tomsjavajive.com/contact.php';
$breadcrumbs = [
    ['name' => 'Home', 'url' => 'https://tomsjavajive.com'],
    ['name' => 'Contact', 'url' => 'https://tomsjavajive.com/contact.php']
];
require_once __DIR__ . '/includes/functions.php';

$sent = false;
$errors = [];

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

    if (empty($name))    $errors['name']    = 'Name is required.';
    if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL))
                         $errors['email']   = 'A valid email address is required.';
    if (empty($subject)) $errors['subject'] = 'Please choose a subject.';
    if (empty($message)) $errors['message'] = 'Message cannot be empty.';

    if (empty($errors)) {
        $html = "<p><strong>Name:</strong> {$name}<br><strong>Email:</strong> {$email}<br><strong>Subject:</strong> {$subject}</p><p>" . nl2br(htmlspecialchars($message)) . "</p>";
        sendEmail('sales@tomsjavajive.com', "Contact Form: {$subject}", $html);
        $sent = true;
    }
}

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

<section class="section" style="padding-top: 2rem; padding-bottom: 4rem;">
    <div class="container" style="max-width: 900px;">

        <div style="text-align: center; margin-bottom: 3rem;">
            <h1 style="font-size: 2.5rem; margin-bottom: 0.75rem;">Contact Us</h1>
            <p class="text-muted" style="font-size: 1.1rem;">Questions about your order or just want to talk coffee? We'd love to hear from you.</p>
        </div>

        <div style="display: grid; grid-template-columns: 1fr 1.6fr; gap: 2.5rem; align-items: start;">

            <!-- Contact Info -->
            <div>
                <div class="card" style="margin-bottom: 1.5rem;">
                    <div class="card-body">
                        <h3 style="margin-bottom: 1.25rem; font-size: 1.1rem;">Get in Touch</h3>

                        <div style="display: flex; align-items: flex-start; gap: 0.85rem; margin-bottom: 1.25rem;">
                            <div style="width: 38px; height: 38px; background: rgba(255,94,26,0.1); border-radius: 50%; display: flex; align-items: center; justify-content: center; flex-shrink: 0; color: var(--color-primary);">
                                <i class="fas fa-envelope"></i>
                            </div>
                            <div>
                                <div style="font-weight: 600; margin-bottom: 0.2rem;">Email</div>
                                <a href="mailto:sales@tomsjavajive.com">sales@tomsjavajive.com</a>
                                <div class="text-muted" style="font-size: 0.85rem; margin-top: 0.15rem;">We reply within 1 business day</div>
                            </div>
                        </div>

                        <div style="display: flex; align-items: flex-start; gap: 0.85rem; margin-bottom: 1.25rem;">
                            <div style="width: 38px; height: 38px; background: rgba(255,94,26,0.1); border-radius: 50%; display: flex; align-items: center; justify-content: center; flex-shrink: 0; color: var(--color-primary);">
                                <i class="fas fa-phone"></i>
                            </div>
                            <div>
                                <div style="font-weight: 600; margin-bottom: 0.2rem;">Phone</div>
                                <a href="tel:+18172662022">(817) 266-2022</a>
                                <div class="text-muted" style="font-size: 0.85rem; margin-top: 0.15rem;">Mon–Fri, 9am–5pm CT</div>
                            </div>
                        </div>

                        <div style="display: flex; align-items: flex-start; gap: 0.85rem;">
                            <div style="width: 38px; height: 38px; background: rgba(255,94,26,0.1); border-radius: 50%; display: flex; align-items: center; justify-content: center; flex-shrink: 0; color: var(--color-primary);">
                                <i class="fas fa-map-marker-alt"></i>
                            </div>
                            <div>
                                <div style="font-weight: 600; margin-bottom: 0.2rem;">Location</div>
                                <div>Weatherford, TX 76088</div>
                                <div class="text-muted" style="font-size: 0.85rem; margin-top: 0.15rem;">United States</div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card">
                    <div class="card-body">
                        <h3 style="margin-bottom: 0.75rem; font-size: 1rem;">Quick Links</h3>
                        <ul style="list-style: none; padding: 0; margin: 0;">
                            <li style="margin-bottom: 0.6rem;"><a href="/track-order.php"><i class="fas fa-search" style="width: 16px; margin-right: 0.4rem;"></i> Track Your Order</a></li>
                            <li style="margin-bottom: 0.6rem;"><a href="/returns.php"><i class="fas fa-undo" style="width: 16px; margin-right: 0.4rem;"></i> Returns &amp; Refunds</a></li>
                            <li style="margin-bottom: 0.6rem;"><a href="/shipping.php"><i class="fas fa-truck" style="width: 16px; margin-right: 0.4rem;"></i> Shipping Info</a></li>
                            <li><a href="/faq.php"><i class="fas fa-question-circle" style="width: 16px; margin-right: 0.4rem;"></i> FAQ</a></li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- Contact Form -->
            <div class="card">
                <div class="card-body">
                    <?php if ($sent): ?>
                        <div style="text-align: center; padding: 2rem 0;">
                            <div style="font-size: 3rem; margin-bottom: 1rem;">☕</div>
                            <h3 style="margin-bottom: 0.5rem;">Message Sent!</h3>
                            <p class="text-muted">Thanks for reaching out. We'll get back to you within one business day.</p>
                            <a href="/shop.php" class="btn btn-primary mt-2">Browse Our Coffee</a>
                        </div>
                    <?php else: ?>
                        <h3 style="margin-bottom: 1.5rem; font-size: 1.1rem;">Send a Message</h3>
                        <form method="POST" action="">
                            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 1rem;">
                                <div class="form-group">
                                    <label class="form-label">Your Name *</label>
                                    <input type="text" name="name" class="form-input <?= isset($errors['name']) ? 'is-invalid' : '' ?>"
                                           value="<?= htmlspecialchars($_POST['name'] ?? '') ?>" required>
                                    <?php if (isset($errors['name'])): ?>
                                        <span class="form-error"><?= $errors['name'] ?></span>
                                    <?php endif; ?>
                                </div>
                                <div class="form-group">
                                    <label class="form-label">Email Address *</label>
                                    <input type="email" name="email" class="form-input <?= isset($errors['email']) ? 'is-invalid' : '' ?>"
                                           value="<?= htmlspecialchars($_POST['email'] ?? '') ?>" required>
                                    <?php if (isset($errors['email'])): ?>
                                        <span class="form-error"><?= $errors['email'] ?></span>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="form-label">Subject *</label>
                                <select name="subject" class="form-input <?= isset($errors['subject']) ? 'is-invalid' : '' ?>" required>
                                    <option value="">— Select a subject —</option>
                                    <option value="Order Question" <?= ($_POST['subject'] ?? '') === 'Order Question' ? 'selected' : '' ?>>Order Question</option>
                                    <option value="Shipping &amp; Delivery" <?= ($_POST['subject'] ?? '') === 'Shipping &amp; Delivery' ? 'selected' : '' ?>>Shipping &amp; Delivery</option>
                                    <option value="Returns &amp; Refunds" <?= ($_POST['subject'] ?? '') === 'Returns &amp; Refunds' ? 'selected' : '' ?>>Returns &amp; Refunds</option>
                                    <option value="Product Question" <?= ($_POST['subject'] ?? '') === 'Product Question' ? 'selected' : '' ?>>Product Question</option>
                                    <option value="Wholesale Inquiry" <?= ($_POST['subject'] ?? '') === 'Wholesale Inquiry' ? 'selected' : '' ?>>Wholesale Inquiry</option>
                                    <option value="Other" <?= ($_POST['subject'] ?? '') === 'Other' ? 'selected' : '' ?>>Other</option>
                                </select>
                                <?php if (isset($errors['subject'])): ?>
                                    <span class="form-error"><?= $errors['subject'] ?></span>
                                <?php endif; ?>
                            </div>

                            <div class="form-group">
                                <label class="form-label">Message *</label>
                                <textarea name="message" class="form-textarea <?= isset($errors['message']) ? 'is-invalid' : '' ?>"
                                          rows="5" placeholder="How can we help?" required><?= htmlspecialchars($_POST['message'] ?? '') ?></textarea>
                                <?php if (isset($errors['message'])): ?>
                                    <span class="form-error"><?= $errors['message'] ?></span>
                                <?php endif; ?>
                            </div>

                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-paper-plane"></i> Send Message
                            </button>
                        </form>
                    <?php endif; ?>
                </div>
            </div>

        </div>
    </div>
</section>

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