<?php
require_once 'config.php';
require_once 'includes/mailer.php';

$page_title  = 'Contact Us - YASH GRANDE';
$page_desc   = 'Get in touch with YASH GRANDE for reservations, inquiries, and luxury hospitality services.';
$loader_text = 'Connecting You to Luxury';

$success = $error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['contact_submit'])) {
    $name     = trim($_POST['name'] ?? '');
    $email    = trim($_POST['email'] ?? '');
    $phone    = trim($_POST['phone'] ?? '');
    $subject  = trim($_POST['subject'] ?? '');
    $checkin  = $_POST['checkin'] ?? null;
    $checkout = $_POST['checkout'] ?? null;
    $guests   = $_POST['guests'] ?? '';
    $message  = trim($_POST['message'] ?? '');

    if ($name && $email && $subject && $message) {
        try {
            $pdo  = getConnection();
            $stmt = $pdo->prepare("INSERT INTO contact_messages (name, email, phone, subject, checkin_date, checkout_date, guests, message, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW())");
            if ($stmt->execute([$name, $email, $phone ?: null, $subject, $checkin ?: null, $checkout ?: null, $guests ?: null, $message])) {
                // Send email to admin
                $emailBody = "
                    <div style='font-family: Arial, sans-serif; padding: 20px; background: #f4f4f4;'>
                        <div style='background: #fff; padding: 20px; border-radius: 10px; border: 1px solid #C5A46D;'>
                            <h2 style='color: #0F0F0F;'>New Contact Inquiry - $subject</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>
                            <hr style='border: 0; border-top: 1px solid #C5A46D; margin: 20px 0;'>
                            <p><strong>Message:</strong></p>
                            <p style='white-space: pre-wrap;'>$message</p>
                            <hr style='border: 0; border-top: 1px solid #C5A46D; margin: 20px 0;'>
                            <p style='color: #666; font-size: 0.8rem;'>Submitted on: " . date('Y-m-d H:i:s') . "</p>
                        </div>
                    </div>
                ";
                sendEmail(ADMIN_EMAIL, "New Inquiry: $subject", $emailBody);

                // Send confirmation email to user
                $userEmailBody = "
                    <div style='font-family: Arial, sans-serif; padding: 20px; background: #f4f4f4;'>
                        <div style='background: #fff; padding: 20px; border-radius: 10px; border: 1px solid #C5A46D;'>
                            <h2 style='color: #0F0F0F;'>Thank you for contacting YASH GRANDE</h2>
                            <p>Dear $name,</p>
                            <p>We have received your message regarding <strong>$subject</strong>. Our team will review your inquiry and get back to you within 24 hours.</p>
                            <hr style='border: 0; border-top: 1px solid #C5A46D; margin: 20px 0;'>
                            <p><strong>Your Message:</strong></p>
                            <p style='white-space: pre-wrap;'>$message</p>
                            <hr style='border: 0; border-top: 1px solid #C5A46D; margin: 20px 0;'>
                            <p style='color: #666; font-size: 0.8rem;'>This is an automated confirmation. Please do not reply to this email.</p>
                        </div>
                    </div>
                ";
                sendEmail($email, "Thank you for contacting YASH GRANDE", $userEmailBody);
                $success = 'Thank you for your message! We will get back to you within 24 hours.';
            }
        } catch (PDOException $e) {
            $error = 'Sorry, there was an error sending your message. Please try again later or contact us directly.';
            // For debugging: $error .= ' ' . $e->getMessage();
        }
    } else {
        $error = 'Please fill in all required fields (Name, Email, Subject, Message).';
    }
}

include 'includes/header.php';
?>

    <!-- Page Header -->
    <section class="page-header" style="background: linear-gradient(var(--overlay), var(--overlay)), url('https://images.unsplash.com/photo-1571896349842-33c89424de2d?ixlib=rb-4.0.3&auto=format&fit=crop&w=2070&q=80') center/cover;">
        <div class="hero-overlay"></div>
        <div class="hero-content">
            <h1>Contact Us</h1>
            <p>We're here to assist you with reservations, inquiries, and creating unforgettable experiences</p>
        </div>
    </section>

    <!-- Contact Info Cards -->
    <section class="section">
        <div class="section-header">
            <h2>Get in Touch</h2>
            <p>Reach out to us for personalized assistance and luxury hospitality services</p>
        </div>
        <div class="features-grid">
            <div class="feature-card"><i class="fas fa-map-marker-alt"></i><h4>Visit Us</h4><p>419/1B,AKMG Nagar,<br>3rd street,<br>Dindigul</p></div>
            <div class="feature-card"><i class="fas fa-phone"></i><h4>Call Us</h4><p>Reservations: +91 98765 43210<br>Restaurant: To be updated...<br>Concierge: To be updated...</p></div>
            <div class="feature-card"><i class="fas fa-envelope"></i><h4>Email Us</h4><p>General: gm@yashgrande.com<br>Reservations: reservation@yashgrande.com<br>Restaurant: reservation@yashgrande.com</p></div>
            <div class="feature-card"><i class="fas fa-clock"></i><h4>Operating Hours</h4><p>Reception: 24/7<br>Restaurant: 7 AM - 11 PM<br>Concierge: 24/7</p></div>
        </div>
    </section>

    <!-- Contact Form -->
    <section class="section">
        <div class="section-header">
            <h2>Send Us a Message</h2>
            <p>Fill out the form below and we'll get back to you within 24 hours</p>
        </div>
        <div class="contact-form">
            <?php if ($success): ?>
                <div class="alert alert-success" style="padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem; display: flex; align-items: center; gap: 1rem; background: rgba(40,167,69,0.1); color: #28a745; border: 1px solid rgba(40,167,69,0.2);">
                    <i class="fas fa-check-circle"></i>
                    <?php echo $success; ?>
                </div>
            <?php endif; ?>
            <?php if ($error): ?>
                <div class="alert alert-error" style="padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem; display: flex; align-items: center; gap: 1rem; background: rgba(220,53,69,0.1); color: #dc3545; border: 1px solid rgba(220,53,69,0.2);">
                    <i class="fas fa-exclamation-circle"></i>
                    <?php echo $error; ?>
                </div>
            <?php endif; ?>
            <form method="POST" action="#contact-form" id="contact-form">
                <div class="form-group"><label>Full Name *</label><input type="text" name="name" required value="<?php echo htmlspecialchars($_POST['name'] ?? ''); ?>"></div>
                <div class="form-group"><label>Email Address *</label><input type="email" name="email" required value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>"></div>
                <div class="form-group"><label>Phone Number</label><input type="tel" name="phone" value="<?php echo htmlspecialchars($_POST['phone'] ?? ''); ?>"></div>
                <div class="form-group">
                    <label>Subject *</label>
                    <select name="subject" required style="width:100%;padding:1rem;background:rgba(255,255,255,0.1);border:1px solid rgba(197,164,109,0.3);border-radius:10px;color:var(--soft-white);font-family:var(--body-font);">
                        <option value="">Select Subject</option>
                        <?php foreach (['Room Reservation','Restaurant Booking','Event Inquiry','General Inquiry','Feedback','Complaint','Other'] as $s): ?>
                            <option value="<?php echo $s; ?>" <?php echo (($_POST['subject'] ?? '') == $s) ? 'selected' : ''; ?>><?php echo $s; ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group"><label>Check-in Date (if applicable)</label><input type="date" name="checkin" value="<?php echo htmlspecialchars($_POST['checkin'] ?? ''); ?>"></div>
                <div class="form-group"><label>Check-out Date (if applicable)</label><input type="date" name="checkout" value="<?php echo htmlspecialchars($_POST['checkout'] ?? ''); ?>"></div>
                <div class="form-group">
                    <label>Number of Guests</label>
                    <select name="guests" style="width:100%;padding:1rem;background:rgba(255,255,255,0.1);border:1px solid rgba(197,164,109,0.3);border-radius:10px;color:var(--soft-white);font-family:var(--body-font);">
                        <option value="">Select Guests</option>
                        <?php foreach (['1','2','3','4','5','6+'] as $g): ?>
                            <option value="<?php echo $g; ?>" <?php echo (($_POST['guests'] ?? '') == $g) ? 'selected' : ''; ?>><?php echo $g; ?> Guest<?php echo $g != '1' ? 's' : ''; ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group"><label>Message *</label><textarea name="message" rows="6" placeholder="Please provide details about your inquiry, special requests, or any questions you may have..." required><?php echo htmlspecialchars($_POST['message'] ?? ''); ?></textarea></div>
                <button type="submit" name="contact_submit" class="btn-primary" style="width:100%;">Send Message</button>
            </form>
        </div>
    </section>

    <!-- Quick Contact -->
    <section class="section">
        <div class="section-header">
            <h2>Quick Contact</h2>
            <p>For immediate assistance, use these direct contact options</p>
        </div>
        <div class="amenities-grid">
            <div class="amenity-card"><i class="fas fa-bed"></i><h4>Room Reservations</h4><p style="color:var(--premium-gray);margin:1rem 0;">+91 98765 43210</p><a href="tel:+919876543210" class="btn-secondary" style="padding:0.5rem 1.5rem;font-size:0.9rem;">Call Now</a></div>
            <div class="amenity-card"><i class="fas fa-utensils"></i><h4>Restaurant Bookings</h4><p style="color:var(--premium-gray);margin:1rem 0;">+91 98765 43211</p><a href="tel:+919876543211" class="btn-secondary" style="padding:0.5rem 1.5rem;font-size:0.9rem;">Call Now</a></div>
            <div class="amenity-card"><i class="fas fa-concierge-bell"></i><h4>Concierge Services</h4><p style="color:var(--premium-gray);margin:1rem 0;">+91 98765 43212</p><a href="tel:+919876543212" class="btn-secondary" style="padding:0.5rem 1.5rem;font-size:0.9rem;">Call Now</a></div>
            <div class="amenity-card"><i class="fas fa-envelope"></i><h4>General Inquiries</h4><p style="color:var(--premium-gray);margin:1rem 0;">info@yashgrande.com</p><a href="mailto:info@yashgrande.com" class="btn-secondary" style="padding:0.5rem 1.5rem;font-size:0.9rem;">Email Us</a></div>
        </div>
    </section>

    <!-- FAQ -->
    <section class="section">
        <div class="section-header">
            <h2>Frequently Asked Questions</h2>
            <p>Quick answers to common inquiries about our luxury hospitality services</p>
        </div>
        <div class="testimonials-grid">
            <div class="testimonial-card"><h4 style="color:var(--primary-gold);margin-bottom:1rem;">What are your check-in and check-out times?</h4><p class="testimonial-text">Check-in is at 3:00 PM and check-out is at 12:00 PM. Early check-in and late check-out are available upon request and subject to availability.</p></div>
            <div class="testimonial-card"><h4 style="color:var(--primary-gold);margin-bottom:1rem;">Do you offer airport transportation?</h4><p class="testimonial-text">Yes, we provide luxury airport transfer services. Please contact our concierge team to arrange pickup and drop-off services.</p></div>
            <div class="testimonial-card"><h4 style="color:var(--primary-gold);margin-bottom:1rem;">What dining options are available?</h4><p class="testimonial-text">Our fine dining restaurant serves breakfast, lunch, and dinner. We also offer 24/7 room service and can accommodate special dietary requirements.</p></div>
            <div class="testimonial-card"><h4 style="color:var(--primary-gold);margin-bottom:1rem;">Is parking available?</h4><p class="testimonial-text">Yes, we offer complimentary valet parking for all guests. Our secure parking facility is available 24/7 with professional attendants.</p></div>
            <div class="testimonial-card"><h4 style="color:var(--primary-gold);margin-bottom:1rem;">Do you host events and meetings?</h4><p class="testimonial-text">Absolutely! We have elegant event spaces and meeting rooms available for corporate events, weddings, and special celebrations.</p></div>
            <div class="testimonial-card"><h4 style="color:var(--primary-gold);margin-bottom:1rem;">What is your cancellation policy?</h4><p class="testimonial-text">We offer flexible cancellation policies. Standard reservations can be cancelled up to 24 hours before arrival without penalty.</p></div>
        </div>
    </section>

    <!-- CTA -->
    <section class="cta">
        <h2>Ready to Experience Luxury?</h2>
        <p>Contact us today to make your reservation or learn more about our premium hospitality services</p>
        <a href="#contact-form" class="btn-primary">Make a Reservation</a>
    </section>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Auto-hide alerts
            const alerts = document.querySelectorAll('.alert');
            alerts.forEach(alert => {
                setTimeout(() => {
                    alert.style.transition = 'opacity 0.5s ease, transform 0.5s ease';
                    alert.style.opacity = '0';
                    alert.style.transform = 'translateY(-20px)';
                    setTimeout(() => alert.remove(), 500);
                }, 5000);
            });
        });
    </script>

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