<?php
require_once __DIR__ . '/includes/config.php';
$pageTitle = 'Contact Us';
$db = getDB();
$success = '';
$error = '';

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

    // Validation
    if (empty($name) || empty($email) || empty($message)) {
        $error = 'Please fill in all required fields.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Please enter a valid email address.';
    } elseif (strlen($message) < 10) {
        $error = 'Message must be at least 10 characters long.';
    } else {
        try {
            // Store in database
            $stmt = $db->prepare("INSERT INTO contact_messages (name, email, phone, subject, message, created_at) VALUES (?, ?, ?, ?, ?, NOW())");
            $stmt->execute([$name, $email, $phone, $subject, $message]);

            // Send email to admin
            $adminEmail = 'admin@electro-shop.com';
            $siteName = getSetting('site_name') ?: 'ElectroShop';
            $emailSubject = "New Contact Message from $name";
            $emailBody = "You have received a new contact message:\n\n";
            $emailBody .= "Name: " . sanitize($name) . "\n";
            $emailBody .= "Email: " . sanitize($email) . "\n";
            if (!empty($phone)) {
                $emailBody .= "Phone: " . sanitize($phone) . "\n";
            }
            $emailBody .= "Subject: " . sanitize($subject) . "\n";
            $emailBody .= "Message:\n" . sanitize($message) . "\n\n";
            $emailBody .= "Reply to: " . sanitize($email) . "\n";

            $headers = "From: noreply@electro-shop.com\r\n";
            $headers .= "Reply-To: " . sanitize($email) . "\r\n";
            $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";

            @mail($adminEmail, $emailSubject, $emailBody, $headers);

            // Send confirmation to user
            $userSubject = "We received your message - $siteName";
            $userBody = "Hello " . sanitize($name) . ",\n\n";
            $userBody .= "Thank you for contacting us. We have received your message and will get back to you as soon as possible.\n\n";
            $userBody .= "Best regards,\n" . $siteName . " Team";
            $userHeaders = "From: noreply@electro-shop.com\r\nContent-Type: text/plain; charset=UTF-8\r\n";

            @mail($email, $userSubject, $userBody, $userHeaders);

            $success = 'Thank you for your message! We will get back to you soon.';
        } catch (Exception $e) {
            $error = 'An error occurred while processing your request. Please try again.';
        }
    }
}

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

<!-- Contact Page Hero -->
<div class="container-fluid py-5" style="background-color: #f8f9fa;">
    <div class="container">
        <h1 class="display-4 fw-bold mb-3">Contact Us</h1>
        <p class="fs-5 text-muted">We'd love to hear from you. Get in touch with us for any inquiries or feedback.</p>
    </div>
</div>

<!-- Contact Content -->
<div class="container py-5">
    <div class="row g-5">
        <!-- Contact Form -->
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-light border-0 py-4">
                    <h5 class="mb-0"><i class="fas fa-envelope text-primary me-2"></i>Send us a Message</h5>
                </div>
                <div class="card-body p-5">
                    <?php if ($success): ?>
                    <div class="alert alert-success alert-dismissible fade show mb-4" role="alert">
                        <i class="fas fa-check-circle me-2"></i><?= sanitize($success) ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                    <?php endif; ?>
                    
                    <?php if ($error): ?>
                    <div class="alert alert-danger alert-dismissible fade show mb-4" role="alert">
                        <i class="fas fa-exclamation-circle me-2"></i><?= sanitize($error) ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                    <?php endif; ?>

                    <form method="POST" action="">
                        <div class="mb-4">
                            <label class="form-label fw-semibold">Full Name *</label>
                            <input type="text" name="name" class="form-control form-control-lg" placeholder="Your name" required maxlength="100">
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-semibold">Email Address *</label>
                            <input type="email" name="email" class="form-control form-control-lg" placeholder="your@email.com" required maxlength="100">
                            <small class="text-muted">We'll never share your email.</small>
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-semibold">Phone Number</label>
                            <input type="tel" name="phone" class="form-control form-control-lg" placeholder="Optional: +91 XXXXX XXXXX" maxlength="20">
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-semibold">Subject *</label>
                            <input type="text" name="subject" class="form-control form-control-lg" placeholder="How can we help?" required maxlength="200">
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-semibold">Message *</label>
                            <textarea name="message" class="form-control" rows="6" placeholder="Your message here..." required minlength="10" maxlength="5000"></textarea>
                            <small class="text-muted">Minimum 10 characters required.</small>
                        </div>

                        <button type="submit" class="btn btn-primary btn-lg px-5">
                            <i class="fas fa-paper-plane me-2"></i>Send Message
                        </button>
                    </form>
                </div>
            </div>
        </div>

        <!-- Contact Information -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body p-4">
                    <h5 class="card-title mb-3"><i class="fas fa-phone text-primary me-2"></i>Call Us</h5>
                    <?php
                    $phones = getAllPhones();
                    if (!empty($phones)) {
                        foreach ($phones as $phone) {
                            if ($phone['is_active']) {
                                echo '<p class="mb-2"><a href="tel:' . sanitize($phone['phone_number']) . '" class="text-decoration-none">' . sanitize($phone['phone_number']) . '</a></p>';
                            }
                        }
                    } else {
                        echo '<p class="text-muted">Contact information not available</p>';
                    }
                    ?>
                </div>
            </div>

            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body p-4">
                    <h5 class="card-title mb-3"><i class="fas fa-envelope text-primary me-2"></i>Email Us</h5>
                    <p class="mb-0"><a href="mailto:info@electro-shop.com" class="text-decoration-none">info@electro-shop.com</a></p>
                </div>
            </div>

            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body p-4">
                    <h5 class="card-title mb-3"><i class="fas fa-clock text-primary me-2"></i>Business Hours</h5>
                    <p class="mb-1"><strong>Monday - Friday:</strong> 9:00 AM - 6:00 PM</p>
                    <p class="mb-1"><strong>Saturday:</strong> 10:00 AM - 4:00 PM</p>
                    <p class="mb-0"><strong>Sunday:</strong> Closed</p>
                </div>
            </div>

            <div class="card border-0 shadow-sm">
                <div class="card-body p-4">
                    <h5 class="card-title mb-3"><i class="fas fa-map-marker-alt text-primary me-2"></i>Location</h5>
                    <p class="mb-0">
                        ElectroShop Headquarters<br>
                        123 Tech Street<br>
                        Innovation City, State 12345<br>
                        India
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- FAQ Section -->
<div class="container-fluid py-5" style="background-color: #f8f9fa;">
    <div class="container">
        <h2 class="display-6 fw-bold mb-5">Frequently Asked Questions</h2>
        <div class="row g-4">
            <div class="col-lg-6">
                <div class="accordion accordion-flush" id="faqAccordion1">
                    <div class="accordion-item border-0 mb-3 shadow-sm">
                        <h2 class="accordion-header">
                            <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#faq1">
                                What are your shipping options?
                            </button>
                        </h2>
                        <div id="faq1" class="accordion-collapse collapse show" data-bs-parent="#faqAccordion1">
                            <div class="accordion-body">
                                We offer free shipping on orders above ₹999. Standard delivery takes 3-5 business days.
                            </div>
                        </div>
                    </div>
                    <div class="accordion-item border-0 mb-3 shadow-sm">
                        <h2 class="accordion-header">
                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#faq2">
                                Do you offer returns?
                            </button>
                        </h2>
                        <div id="faq2" class="accordion-collapse collapse" data-bs-parent="#faqAccordion1">
                            <div class="accordion-body">
                                Yes! We have a 30-day return policy. Items must be in original condition with packaging.
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-lg-6">
                <div class="accordion accordion-flush" id="faqAccordion2">
                    <div class="accordion-item border-0 mb-3 shadow-sm">
                        <h2 class="accordion-header">
                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#faq3">
                                What payment methods do you accept?
                            </button>
                        </h2>
                        <div id="faq3" class="accordion-collapse collapse" data-bs-parent="#faqAccordion2">
                            <div class="accordion-body">
                                We accept all major credit/debit cards, UPI, net banking, and digital wallets.
                            </div>
                        </div>
                    </div>
                    <div class="accordion-item border-0 mb-3 shadow-sm">
                        <h2 class="accordion-header">
                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#faq4">
                                How can I track my order?
                            </button>
                        </h2>
                        <div id="faq4" class="accordion-collapse collapse" data-bs-parent="#faqAccordion2">
                            <div class="accordion-body">
                                You'll receive a tracking link via email once your order ships. You can check status anytime.
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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