<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
require_once 'backend/db.php';

$success_message = '';
$error_message = '';
$user_email = '';

if (isset($_SESSION['user_id'])) {
    $stmt = mysqli_prepare($conn, "SELECT email FROM users WHERE user_id = ?");
    mysqli_stmt_bind_param($stmt, "i", $_SESSION['user_id']);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    if ($row = mysqli_fetch_assoc($result)) {
        $user_email = $row['email'];
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $phone = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_STRING);
    $contact_type = filter_input(INPUT_POST, 'contact_type', FILTER_SANITIZE_STRING);
    $message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);
    $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error_message = "Please enter a valid email address.";
    } elseif (empty($message)) {
        $error_message = "Please enter your message.";
    } elseif (!in_array($contact_type, ['urgent', 'response_needed', 'information'])) {
        $error_message = "Please select a valid contact type.";
    } else {
        $stmt = mysqli_prepare($conn, "INSERT INTO contact_requests (user_id, email, phone, contact_type, message) VALUES (?, ?, ?, ?, ?)");
        mysqli_stmt_bind_param($stmt, "issss", $user_id, $email, $phone, $contact_type, $message);
        
        if (mysqli_stmt_execute($stmt)) {
            $success_message = "Your message has been sent successfully! We'll get back to you soon.";
        } else {
            $error_message = "An error occurred while sending your message. Please try again.";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact Us - BTS Platform</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css">
    <style>
        .contact-hero {
            background: linear-gradient(135deg, #0d6efd 0%, #0dcaf0 100%);
            color: white;
            padding: 80px 0;
            margin-bottom: 50px;
        }
        
        .contact-form {
            background: white;
            border-radius: 15px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.1);
            padding: 30px;
            margin-bottom: 50px;
        }
        
        .contact-info-card {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 30px;
            height: 100%;
            transition: transform 0.3s ease;
        }
        
        .contact-info-card:hover {
            transform: translateY(-5px);
        }
        
        .contact-icon {
            font-size: 2rem;
            color: #0d6efd;
            margin-bottom: 20px;
        }
        
        .form-control:focus {
            border-color: #0d6efd;
            box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);
        }
        
        .btn-contact {
            padding: 12px 30px;
            font-weight: 500;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .alert {
            border-radius: 10px;
            padding: 15px 20px;
        }
    </style>
</head>
<body>
    <?php include_once 'navbar.php'; ?>

    <!-- Hero Section -->
    <div class="contact-hero">
        <div class="container text-center">
            <h1 class="display-4 fw-bold mb-4">Contact Us</h1>
            <p class="lead mb-0">We're here to help! Send us a message and we'll respond as soon as possible.</p>
        </div>
    </div>

    <div class="container">
        <div class="row">
            <!-- Contact Form -->
            <div class="col-lg-8 mb-4">
                <div class="contact-form">
                    <?php if ($success_message): ?>
                        <div class="alert alert-success" role="alert">
                            <i class="fas fa-check-circle me-2"></i><?php echo htmlspecialchars($success_message); ?>
                        </div>
                    <?php endif; ?>
                    
                    <?php if ($error_message): ?>
                        <div class="alert alert-danger" role="alert">
                            <i class="fas fa-exclamation-circle me-2"></i><?php echo htmlspecialchars($error_message); ?>
                        </div>
                    <?php endif; ?>

                    <form method="POST" action="contact.php">
                        <div class="mb-4">
                            <label for="email" class="form-label">Email address <span class="text-danger">*</span></label>
                            <input type="email" class="form-control" id="email" name="email" required 
                                   value="<?php echo htmlspecialchars($user_email); ?>"
                                   <?php echo isset($_SESSION['user_id']) ? 'readonly' : ''; ?>>
                        </div>

                        <div class="mb-4">
                            <label for="phone" class="form-label">Phone number</label>
                            <input type="tel" class="form-control" id="phone" name="phone" 
                                   placeholder="Optional">
                        </div>

                        <div class="mb-4">
                            <label for="contact_type" class="form-label">Contact type <span class="text-danger">*</span></label>
                            <select class="form-select" id="contact_type" name="contact_type" required>
                                <option value="">Select a contact type</option>
                                <option value="urgent">Urgent - Need immediate assistance</option>
                                <option value="response_needed">Response needed - Please reply</option>
                                <option value="information">Information only - No response needed</option>
                            </select>
                        </div>

                        <div class="mb-4">
                            <label for="message" class="form-label">Message <span class="text-danger">*</span></label>
                            <textarea class="form-control" id="message" name="message" rows="5" required></textarea>
                        </div>

                        <button type="submit" class="btn btn-primary btn-contact">
                            <i class="fas fa-paper-plane me-2"></i>Send Message
                        </button>
                    </form>
                </div>
            </div>

            <!-- Contact Information -->
            <div class="col-lg-4">
                <div class="contact-info-card mb-4">
                    <div class="text-center">
                        <i class="fas fa-clock contact-icon"></i>
                        <h4>Response Time</h4>
                        <p class="text-muted mb-0">We typically respond within 24-48 hours</p>
                    </div>
                </div>

                <div class="contact-info-card mb-4">
                    <div class="text-center">
                        <i class="fas fa-envelope contact-icon"></i>
                        <h4>Email</h4>
                        <p class="text-muted mb-0">support@btsplatform.com</p>
                    </div>
                </div>

                <div class="contact-info-card">
                    <div class="text-center">
                        <i class="fas fa-map-marker-alt contact-icon"></i>
                        <h4>Location</h4>
                        <p class="text-muted mb-0">123 Education Street<br>City, Country</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html> 