<?php
session_set_cookie_params([
    'lifetime' => 0,
    'path' => '/',
    'domain' => '',
    'secure' => !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off',
    'httponly' => true,
    'samesite' => 'Lax'
]);
session_start();
if (!isset($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
$errors = [];
$success = null;
$values = [
    'firstName' => '',
    'lastName' => '',
    'email' => '',
    'phone' => '',
    'service' => '',
    'budget' => '',
    'timeline' => '',
    'message' => '',
    'newsletter' => ''
];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!isset($_POST['csrf_token']) || !hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])) {
        $errors[] = 'Invalid submission token.';
    } else {
        $values['firstName'] = trim((string)($_POST['firstName'] ?? ''));
        $values['lastName'] = trim((string)($_POST['lastName'] ?? ''));
        $values['email'] = filter_var(trim((string)($_POST['email'] ?? '')), FILTER_SANITIZE_EMAIL);
        $values['phone'] = trim((string)($_POST['phone'] ?? ''));
        $values['service'] = trim((string)($_POST['service'] ?? ''));
        $values['budget'] = trim((string)($_POST['budget'] ?? ''));
        $values['timeline'] = trim((string)($_POST['timeline'] ?? ''));
        $values['message'] = trim((string)($_POST['message'] ?? ''));
        $newsletterChecked = isset($_POST['newsletter']) ? 'Yes' : 'No';
        $terms = isset($_POST['terms']);
        if ($values['firstName'] === '') $errors[] = 'First name is required.';
        if ($values['lastName'] === '') $errors[] = 'Last name is required.';
        if ($values['email'] === '' || !filter_var($values['email'], FILTER_VALIDATE_EMAIL)) $errors[] = 'Valid email is required.';
        if ($values['message'] === '') $errors[] = 'Project details are required.';
        if (!$terms) $errors[] = 'You must agree to Terms & Privacy Policy.';
        if (!$errors) {
            try {
                $dbDir = __DIR__ . '/../data';
                if (!is_dir($dbDir)) { @mkdir($dbDir, 0775, true); }
                $dbPath = $dbDir . '/site.db';
                $pdo = new PDO('sqlite:' . $dbPath);
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $pdo->exec('CREATE TABLE IF NOT EXISTS contacts (id INTEGER PRIMARY KEY AUTOINCREMENT, first_name TEXT, last_name TEXT, email TEXT, phone TEXT, service TEXT, budget TEXT, timeline TEXT, message TEXT, newsletter TEXT, ip TEXT, ua TEXT, created_at TEXT)');
                $stmt = $pdo->prepare('INSERT INTO contacts (first_name,last_name,email,phone,service,budget,timeline,message,newsletter,ip,ua,created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,datetime("now"))');
                $stmt->execute([
                    $values['firstName'],
                    $values['lastName'],
                    $values['email'],
                    $values['phone'],
                    $values['service'],
                    $values['budget'],
                    $values['timeline'],
                    $values['message'],
                    $newsletterChecked,
                    $_SERVER['REMOTE_ADDR'] ?? '',
                    $_SERVER['HTTP_USER_AGENT'] ?? ''
                ]);
            } catch (Throwable $t) {
                $logFile = ($dbDir ?? (__DIR__ . '/../data')) . '/errors.log';
                @file_put_contents($logFile, '[' . date('c') . '] DB error: ' . $t->getMessage() . "\n", FILE_APPEND);
            }
            $success = 'Thank you, ' . htmlspecialchars($values['firstName']) . '! Your project inquiry has been sent.';
            $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
            $values = [
                'firstName' => '',
                'lastName' => '',
                'email' => '',
                'phone' => '',
                'service' => '',
                'budget' => '',
                'timeline' => '',
                'message' => '',
                'newsletter' => ''
            ];
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact Us - Home Enhancement Services</title>
    <meta name="description" content="Get in touch with Home Enhancement Services. Contact us for free consultations, quotes, and professional home improvement services.">
    <meta name="keywords" content="contact us, home enhancement, interior design consultation, free quote, home improvement services">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;500;600;700&family=Poppins:wght@300;400;500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="../assets/css/style.css">
    <link rel="preload" as="image" href="https://images.pexels.com/photos/7652180/pexels-photo-7652180.jpeg?auto=compress&cs=tinysrgb&w=1000&h=400">
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-lg navbar-light fixed-top">
            <div class="container">
                <a class="navbar-brand" href="../index.html">
                    <span class="brand-text">HOME RENOVATE GUIDE</span>
                </a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarNav">
                    <ul class="navbar-nav ms-auto">
                        <li class="nav-item">
                            <a class="nav-link" aria-current="page" href="about.html">About Us</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="contact.php">Contact</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="subscribe.php">Subscribe</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <section class="contact-hero-section" style="background: linear-gradient(rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0.6)), url('https://images.pexels.com/photos/3184465/pexels-photo-3184465.jpeg?auto=compress&cs=tinysrgb&w=1920&h=800'); background-size: cover; background-position: center;">
        <div class="container h-100">
            <div class="row h-100 align-items-center">
                <div class="col-lg-8 mx-auto text-center text-white">
                    <h1 class="display-4 fw-bold mb-4 text-white">Contact HOME RENOVATE GUIDE</h1>
                    <p class="lead">Tell us about your project and we will get back within 24 hours.</p>
                </div>
            </div>
        </div>
    </section>
    <section class="py-5">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 mx-auto">
                    <?php if (!empty($errors)) { echo '<div class="alert alert-danger" role="alert"><ul class="mb-0">'; foreach ($errors as $e) { echo '<li>' . htmlspecialchars($e) . '</li>'; } echo '</ul></div>'; } ?>
                    <?php if ($success) { echo '<div class="alert alert-success" role="alert">' . $success . '</div>'; } ?>
                    <div class="card border-0 shadow-lg">
                        <div class="card-body p-4">
                            <img src="https://images.pexels.com/photos/7652180/pexels-photo-7652180.jpeg?auto=compress&cs=tinysrgb&w=1000&h=400" class="img-fluid rounded mb-4" alt="Contact our home enhancement team" loading="lazy" width="1000" height="400">
                            <form id="contactForm" action="contact.php" method="POST" novalidate>
                                <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($_SESSION['csrf_token']); ?>">
                                <div class="row g-3">
                                    <div class="col-md-6">
                                        <label for="firstName" class="form-label">First Name *</label>
                                        <input type="text" class="form-control" id="firstName" name="firstName" value="<?php echo htmlspecialchars($values['firstName']); ?>" required>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="lastName" class="form-label">Last Name *</label>
                                        <input type="text" class="form-control" id="lastName" name="lastName" value="<?php echo htmlspecialchars($values['lastName']); ?>" required>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="email" class="form-label">Email Address *</label>
                                        <input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($values['email']); ?>" required>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="phone" class="form-label">Phone Number</label>
                                        <input type="tel" class="form-control" id="phone" name="phone" value="<?php echo htmlspecialchars($values['phone']); ?>">
                                    </div>
                                    <div class="col-12">
                                        <label for="service" class="form-label">Service Interested In</label>
                                        <select class="form-select" id="service" name="service">
                                            <option value="" <?php echo $values['service']===''?'selected':''; ?>>Select a service...</option>
                                            <option value="interior-design" <?php echo $values['service']==='interior-design'?'selected':''; ?>>Interior Design</option>
                                            <option value="furniture-selection" <?php echo $values['service']==='furniture-selection'?'selected':''; ?>>Furniture Selection</option>
                                            <option value="wall-art" <?php echo $values['service']==='wall-art'?'selected':''; ?>>Wall Art & Decor</option>
                                            <option value="lighting-design" <?php echo $values['service']==='lighting-design'?'selected':''; ?>>Lighting Design</option>
                                        </select>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="budget" class="form-label">Budget Range</label>
                                        <input type="text" class="form-control" id="budget" name="budget" value="<?php echo htmlspecialchars($values['budget']); ?>">
                                    </div>
                                    <div class="col-md-6">
                                        <label for="timeline" class="form-label">Timeline</label>
                                        <input type="text" class="form-control" id="timeline" name="timeline" value="<?php echo htmlspecialchars($values['timeline']); ?>">
                                    </div>
                                    <div class="col-12">
                                        <label for="message" class="form-label">Project Details *</label>
                                        <textarea class="form-control" id="message" name="message" rows="5" placeholder="Tell us about your project..." required><?php echo htmlspecialchars($values['message']); ?></textarea>
                                    </div>
                                    <div class="col-12">
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" id="newsletter" name="newsletter" value="yes" <?php echo $values['newsletter']==='Yes'?'checked':''; ?>>
                                            <label class="form-check-label" for="newsletter">
                                                Subscribe to our newsletter for design tips and updates
                                            </label>
                                        </div>
                                    </div>
                                    <div class="col-12">
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" id="terms" name="terms" required>
                                            <label class="form-check-label" for="terms">
                                                I agree to the <a href="terms.html" target="_blank">Terms & Conditions</a> and <a href="privacy-policy.html" target="_blank">Privacy Policy</a> *
                                            </label>
                                        </div>
                                    </div>
                                    <div class="col-12 text-center">
                                        <button type="submit" class="btn btn-primary btn-lg px-5">
                                            <i class="fas fa-paper-plane me-2"></i>Send Message
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <footer class="footer py-5">
        <div class="container">
            <div class="row">
                <div class="col-md-4 mb-4">
                    <h5>Home Renovate Guide</h5>
                    <p>Transforming spaces into beautiful, functional environments that reflect your unique style and personality.</p>
                </div>
                <div class="col-md-4 mb-4">
                    <h5>Quick Links</h5>
                    <ul class="footer-links">
                        <li><a href="../index.html">Home</a></li>
                        <li><a href="#">Services</a></li>
                        <li><a href="about.html">About Us</a></li>
                        <li><a href="contact.php">Contact</a></li>
                        <li><a href="subscribe.php">Subscribe</a></li>
                        <li><a href="privacy-policy.html">Privacy Policy</a></li>
                        <li><a href="terms.html">Terms & Conditions</a></li>
                    </ul>
                </div>
                <div class="col-md-4 mb-4">
                    <h5>Contact Us</h5>
                    <address>
                        <p><i class="fas fa-map-marker-alt me-2"></i> 81 Grapevine Rd <br> Oak View, California(CA), 93022</p>
                        <p><i class="fas fa-phone me-2"></i> (805) 649-4891</p>
                        <p><i class="fas fa-envelope me-2"></i> info@homerenovateguide.com</p>
                    </address>
                </div>
            </div>
            <hr>
            <div class="row">
                <div class="col-md-12 text-center">
                    <p class="copyright">© 2025 Home Renovate Guide. All Rights Reserved.</p>
                </div>
            </div>
        </div>
    </footer>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="../assets/js/main.js"></script>
</body>
</html>
