<?php
// PHP SCRIPT START

// In a real application, ensure this file is included *after* session_start()
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// ------------------ DATABASE CONNECTION ------------------
// comments
// db.php  $pdo 
include "db.php"; 


if (!isset($pdo) || !($pdo instanceof PDO)) {
    // If db.php failed to establish connection or set $pdo
    error_log("PDO object (\$pdo) not set or not an instance of PDO after including db.php.");
    $error_msg = "Database connection object is missing. Please ensure db.php establishes the PDO connection correctly.";
    $pdo_ready = false;
} else {
    $pdo_ready = true;
}

// --------------------------------------------------------------------------------
// create table query
/*
CREATE TABLE contact_messages (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    phone VARCHAR(50),
    email VARCHAR(255) NOT NULL,
    subject VARCHAR(255),
    message TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
*/
// --------------------------------------------------------------------------------

// ------------------ FORM SUBMISSION HANDLING ------------------
$success_msg = "";
if (isset($error_msg) && !$pdo_ready) {
    // Already set error message above if PDO is not ready
} else {
    $error_msg = ""; // Reset for submission processing
}


if ($_SERVER["REQUEST_METHOD"] === "POST" && $pdo_ready) {
    // Sanitize and trim inputs
    $name    = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
    $phone   = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_SPECIAL_CHARS);
    $email   = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $subject = filter_input(INPUT_POST, 'subject', FILTER_SANITIZE_SPECIAL_CHARS);
    $message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_SPECIAL_CHARS);

    if ($name && $phone && $email && $message) {
        
        try {
            // 1. Prepare the INSERT statement (Securely using prepared statements)
            // $pdo is used from the included db.php
            $sql = "INSERT INTO contact_messages (name, phone, email, subject, message, created_at) 
                    VALUES (:name, :phone, :email, :subject, :message, NOW())";
            
            $stmt = $pdo->prepare($sql);

            // 2. Bind parameters and execute
            $stmt->execute([
                ':name'    => $name,
                ':phone'   => $phone,
                ':email'   => $email,
                ':subject' => $subject,
                ':message' => $message,
            ]);

            // Success message
            $success_msg = "Message received and saved successfully! We will get back to you soon.";
            
            // Clear POST data after success to prevent form resubmission on refresh
            $_POST = array(); 

        } catch (\PDOException $e) {
            // Database insertion error handling
            $error_msg = "An error occurred while saving your message to the database. Check the 'contact_messages' table structure.";
            // Log the detailed error for debugging purposes (IMPORTANT)
            error_log("Contact Form Database Error (Insertion failed): " . $e->getMessage());
        }


    } else {
        $error_msg = "Please fill in all required fields.";
    }
}

// ------------------ HTML START & STYLES ------------------
// This block contains the necessary opening tags and all CSS styles (including mobile responsiveness)
$primaryColor = "#002344";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact Us - Premium Look</title>
    <!-- POPPINS FONT -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        :root{
            --primary-color: <?php echo $primaryColor; ?>;
            --background-color: #f5f7fb;
            --card-shadow: 0 12px 30px rgba(0, 35, 68, 0.08);
        }
        body {
            font-family: 'Poppins', sans-serif;
            margin: 0;
            padding: 0;
            overflow-x: hidden;
            background-color: var(--background-color);
        }
        
        /* ------------------------------------------- */
        /* CONTACT PAGE STYLES (COMMON / DESKTOP) */
        /* ------------------------------------------- */
        .contact-page-wrapper {
            min-height: 90vh;
            background: var(--background-color);
            padding: 60px 20px;
            display: flex;
            justify-content: center;
            align-items: flex-start;
        }
        .contact-container {
            width: 100%;
            max-width: 1200px;
            display: grid;
            grid-template-columns: 3fr 2fr;
            gap: 40px;
        }
        /* DESKTOP STYLES: Rounded corners and box-shadow */
        .contact-card, .info-card {
            background: #fff;
            border-radius: 18px;
            padding: 40px;
            box-shadow: var(--card-shadow);
            transition: all 0.3s ease;
        }
        .contact-title {
            font-size: 38px;
            font-weight: 800;
            color: var(--primary-color);
            margin-bottom: 5px;
        }
        .contact-subtitle {
            color: #6b7280;
            font-size: 17px;
            margin-bottom: 35px;
        }
        .contact-grid-2 {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
        }
        .form-group { margin-bottom: 20px; }
        .form-label {
            font-size: 14px;
            font-weight: 600;
            margin-bottom: 8px;
            display: block;
            color: #374151;
        }
        .form-control-custom, .form-textarea-custom {
            width: 100%;
            padding: 16px;
            border-radius: 12px;
            border: 1px solid #e5e7eb;
            background: #f9f9fb;
            font-size: 16px;
            color: #1f2937;
            box-sizing: border-box; 
            transition: border-color 0.3s, box-shadow 0.3s, background 0.3s;
        }
        .form-control-custom:focus, .form-textarea-custom:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(0, 35, 68, 0.1);
            outline: none;
            background: #fff;
        }
        .form-textarea-custom {
            min-height: 180px;
            resize: vertical;
        }
        .btn-submit {
            background: var(--primary-color);
            color: #fff;
            padding: 16px 35px;
            border-radius: 35px;
            border: none;
            cursor: pointer;
            font-weight: 700;
            font-size: 17px;
            letter-spacing: 0.5px;
            transition: background 0.3s, transform 0.1s;
            display: inline-block;
            width: auto;
        }
        .info-title { font-size: 28px; font-weight: 800; color: var(--primary-color); margin-bottom: 30px; }
        .info-item { margin-bottom: 25px; padding-left: 35px; position: relative; }
        .info-item-icon { position: absolute; left: 0; top: 2px; color: var(--primary-color); font-size: 22px; }
        .info-item-title { font-size: 14px; font-weight: 700; color: #9ca3af; margin-bottom: 3px; }
        .info-item-value { font-size: 17px; color: #374151; line-height: 1.5; }
        .map-wrapper { border-radius: 16px; overflow: hidden; border: 1px solid #e5e7eb; height: 320px; margin-top: 30px; }
        .map-wrapper iframe { width: 100%; height: 100%; border: none; }

        /* Tablet stacking */
        @media (max-width: 1100px) {
            .contact-container {
                grid-template-columns: 1fr;
                gap: 30px;
            }
            .contact-page-wrapper {
                padding: 40px 30px;
            }
        }

        /* ------------------------------------------- */
        /* MOBILE APP VIEW OVERHAUL (max-width: 650px) */
        /* ------------------------------------------- */
        @media (max-width: 650px) {
            /* 1. Full-Screen App Layout - No padding/margins */
            html, body {
                background-color: #fff !important; 
            }
            .contact-page-wrapper {
                padding: 0 !important; /* Remove padding to hit screen edges */
                background-color: #fff !important;
                align-items: stretch;
            }
            .contact-container {
                gap: 0 !important;
                padding-top: 0 !important;
            }

            /* 2. Card Styling for Native App Look - Remove shadows/borders */
            .contact-card, .info-card {
                padding: 30px 20px !important; /* Tighter mobile padding */
                box-shadow: none !important; /* KEY: Remove box shadow */
                border-radius: 0 !important;
                background: #fff !important;
                width: 100%;
                box-sizing: border-box;
            }

            /* 3. Section Separator */
            .contact-card {
                /* Creates a visual break between the form and info sections */
                border-bottom: 10px solid var(--background-color) !important; 
                margin-bottom: 0 !important;
            }

            /* 4. Tighter Titles and Subtitles */
            .contact-title {
                font-size: 26px !important;
                font-weight: 700 !important;
            }
            .contact-subtitle {
                font-size: 14px !important;
                margin-bottom: 20px !important;
            }

            /* 5. Stacked Form Fields (Grid-2 to 1 column) */
            .contact-grid-2 {
                grid-template-columns: 1fr !important;
                gap: 15px !important;
            }

            /* 6. Full-Width Button */
            .btn-submit {
                width: 100% !important;
                display: block !important;
                border-radius: 10px !important;
                font-size: 16px !important;
                padding: 18px !important;
            }

            /* 7. App-style Inputs (More rounded, slightly smaller padding) */
            .form-control-custom, .form-textarea-custom {
                padding: 14px !important;
                border-radius: 8px !important;
                background: #f9f9fb !important;
                font-size: 15px !important;
            }

            .map-wrapper {
                height: 250px !important;
                border-radius: 8px !important;
                margin-top: 20px !important;
            }
        }
    </style>
</head>
<body>

<?php 
// ------------------ ACTUAL HEADER INCLUSION ------------------
// This file is expected to contain the main navigation/header content.
// Note: It must NOT include the opening <html>, <head>, or <body> tags, 
// as those are included above.
include "header.php"; 
// -----------------------------------------------------------
?>

<div class="contact-page-wrapper">
    <div class="contact-container">

        <div class="contact-card">

            <?php if ($success_msg): ?>
                <div style="background:#d1fae5;color:#065f46;padding:15px;border-radius:10px;margin-bottom:20px;font-weight:600;">
                    <?= htmlspecialchars($success_msg) ?>
                </div>
            <?php endif; ?>

            <?php if ($error_msg): ?>
                <div style="background:#fee2e2;color:#991b1b;padding:15px;border-radius:10px;margin-bottom:20px;font-weight:600;">
                    <?= htmlspecialchars($error_msg) ?>
                </div>
            <?php endif; ?>

            <h1 class="contact-title">👋 Get in Touch</h1>
            <p class="contact-subtitle">We'd love to hear from you. Send us a message and we'll reply as soon as possible.</p>

            <form method="POST">

                <div class="contact-grid-2">
                    <div class="form-group">
                        <label class="form-label" for="name">Full Name</label>
                        <input type="text" name="name" id="name" class="form-control-custom" value="<?= isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '' ?>" placeholder="e.g., Kavin" required>
                    </div>

                    <div class="form-group">
                        <label class="form-label" for="phone">Phone</label>
                        <input type="tel" name="phone" id="phone" class="form-control-custom" value="<?= isset($_POST['phone']) ? htmlspecialchars($_POST['phone']) : '' ?>" placeholder="e.g., +91 98765 43210" required>
                    </div>
                </div>

                <div class="contact-grid-2">
                    <div class="form-group">
                        <label class="form-label" for="email">Email</label>
                        <input type="email" name="email" id="email" class="form-control-custom" value="<?= isset($_POST['email']) ? htmlspecialchars($_POST['email']) : '' ?>" placeholder="e.g., your@email.com" required>
                    </div>

                    <div class="form-group">
                        <label class="form-label" for="subject">Subject (Optional)</label>
                        <input type="text" name="subject" id="subject" class="form-control-custom" value="<?= isset($_POST['subject']) ? htmlspecialchars($_POST['subject']) : '' ?>" placeholder="e.g., Project Inquiry">
                    </div>
                </div>

                <div class="form-group">
                    <label class="form-label" for="message">Message</label>
                    <textarea name="message" id="message" class="form-textarea-custom" placeholder="Tell us more about what you need..." required><?= isset($_POST['message']) ? htmlspecialchars($_POST['message']) : '' ?></textarea>
                </div>

                <button class="btn-submit" type="submit">Send Message →</button>
            </form>
        </div>

        <div class="info-card">
            <h2 class="info-title">📍 Reach us directly</h2>

            <div class="info-list">
                <div class="info-item">
                    <span class="info-item-icon">📞</span> <div class="info-item-title">Phone / WhatsApp</div>
                    <div class="info-item-value">+91-6304408534</div>
                </div>

                <div class="info-item">
                    <span class="info-item-icon">📧</span>
                    <div class="info-item-title">Email</div>
                    <div class="info-item-value">support@lalitavarahifoods.com</div>
                </div>

                <div class="info-item">
                    <span class="info-item-icon">🏠</span>
                    <div class="info-item-title">Office Address</div>
                    <div class="info-item-value">
                        Your Business Name, 123 Main St<br>
                        Chennai, Tamil Nadu - 600001
                    </div>
                </div>

                <div class="map-wrapper">
                    <!-- Placeholder map iframe -->
                    <iframe
                        title="Business Location Map"
                        src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3887.7409431467736!2d80.25288437485209!3d13.05639388726787!2m3!1f0!2f0!0f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3a52675185d86ca5%3A0x537bffe46a094289!2sQuickbiz%20Solutions!5e0!3m2!1sen!2sin!4v1732708990000!5m2!1sen!2sin"
                        allowfullscreen=""
                        loading="lazy"
                        referrerpolicy="no-referrer-when-downgrade">
                    </iframe>
                </div>
            </div>
        </div>

    </div>
</div>

<?php 
// ------------------ ACTUAL FOOTER INCLUSION ------------------
// This file is expected to contain the footer content and the closing </body> and </html> tags.
include "footer.php"; 
// -------------------------------------------------------------
?>