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

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $subject = trim($_POST['subject'] ?? '');
    $message = trim($_POST['message'] ?? '');
    
    if (empty($name) || empty($email) || empty($subject) || empty($message)) {
        $error = 'Please fill in all fields';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Please enter a valid email address';
    } else {
        $to = getSystemSetting($pdo, 'system_email');
        $emailSubject = "Contact Form: $subject";
        $emailMessage = "
        <html>
        <body>
            <h3>Contact Form Submission</h3>
            <p><strong>Name:</strong> $name</p>
            <p><strong>Email:</strong> $email</p>
            <p><strong>Subject:</strong> $subject</p>
            <p><strong>Message:</strong></p>
            <p>" . nl2br(htmlspecialchars($message)) . "</p>
        </body>
        </html>
        ";
        
        if (sendEmail($to, $emailSubject, $emailMessage)) {
            $success = 'Thank you for contacting us. We will get back to you soon!';
        } else {
            $error = 'Failed to send message. Please try again later.';
        }
    }
}
?>

<?php include 'header.php'; ?>

<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header bg-primary text-white">
                <h4 class="mb-0"><i class="fas fa-envelope me-2"></i>Send us a Message</h4>
            </div>
            <div class="card-body">
                <?php if ($error): ?>
                    <div class="alert alert-danger"><?php echo $error; ?></div>
                <?php endif; ?>
                
                <?php if ($success): ?>
                    <div class="alert alert-success"><?php echo $success; ?></div>
                <?php endif; ?>
                
                <form method="POST">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Your Name *</label>
                            <input type="text" class="form-control" name="name" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Email Address *</label>
                            <input type="email" class="form-control" name="email" required>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Subject *</label>
                        <input type="text" class="form-control" name="subject" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Message *</label>
                        <textarea class="form-control" name="message" rows="5" required></textarea>
                    </div>
                    <button type="submit" class="btn btn-primary">Send Message</button>
                </form>
            </div>
        </div>
    </div>
    
    <div class="col-md-4">
        <div class="card">
            <div class="card-header bg-info text-white">
                <h4 class="mb-0"><i class="fas fa-address-card me-2"></i>Contact Info</h4>
            </div>
            <div class="card-body">
                <p><i class="fas fa-map-marker-alt text-primary me-2"></i> <?php echo getSystemSetting($pdo, 'system_address'); ?></p>
                <p><i class="fas fa-phone text-primary me-2"></i> <?php echo getSystemSetting($pdo, 'system_phone'); ?></p>
                <p><i class="fas fa-envelope text-primary me-2"></i> <?php echo getSystemSetting($pdo, 'system_email'); ?></p>
                <hr>
                <h5>Office Hours</h5>
                <p>Monday - Friday: 9:00 AM - 6:00 PM<br>
                Saturday: 10:00 AM - 4:00 PM<br>
                Sunday: Closed</p>
            </div>
        </div>
        
        <div class="card mt-4">
            <div class="card-header bg-success text-white">
                <h5 class="mb-0"><i class="fas fa-users me-2"></i>Emergency Contact</h5>
            </div>
            <div class="card-body text-center">
                <p class="mb-2">For urgent matters, please contact:</p>
                <h4 class="text-success"><i class="fas fa-phone-alt"></i> +91 9876543210</h4>
                <small>Available 24/7 for emergencies</small>
            </div>
        </div>
    </div>
</div>

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