<?php
/**
 * Contact Us Page
 * LandSales Pro - Contact form
 */

define('APP_RUNNING', true);
require_once 'config.php';
require_once 'db_connect.php';
require_once 'includes/auth.php';

initSecureSession();

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

// Check if user is logged in
$is_logged_in = isset($_SESSION['user_id']);
$user_name = $_SESSION['full_name'] ?? $_SESSION['user_name'] ?? '';
$user_email = $_SESSION['email'] ?? '';
$user_phone = $_SESSION['phone'] ?? '';

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $subject = trim($_POST['subject'] ?? '');
    $message = trim($_POST['message'] ?? '');

    // Validation
    if(empty($name) || empty($email) || empty($subject) || empty($message)) {
        $error = 'Please fill in all required fields.';
    } elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Please enter a valid email address.';
    } else {
        // Prepare email
        $to = SUPPORT_EMAIL;
        $email_subject = "LandSalesPro Contact: " . $subject;

        $email_body = "You have received a new message from the LandSalesPro contact form.\n\n";
        $email_body .= "Name: $name\n";
        $email_body .= "Email: $email\n";
        $email_body .= "Phone: $phone\n";
        $email_body .= "Subject: $subject\n\n";
        $email_body .= "Message:\n$message\n";

        if($is_logged_in) {
            $email_body .= "\n---\nUser ID: " . $_SESSION['user_id'];
            $email_body .= "\nUser Email: " . ($_SESSION['email'] ?? 'N/A');
        }

        $headers = "From: $email\r\n";
        $headers .= "Reply-To: $email\r\n";
        $headers .= "X-Mailer: PHP/" . phpversion();

        // Send email
        if(mail($to, $email_subject, $email_body, $headers)) {
            $success = 'Your message has been sent successfully! We will get back to you soon.';

            // Log the contact
            try {
                db_execute("INSERT INTO activity_logs (user_id, action, description, ip_address, created_at)
                            VALUES (?, 'contact_form', ?, ?, NOW())",
                    'iss',
                    [$_SESSION['user_id'] ?? null, "Contact from: $name ($email) - $subject", $_SERVER['REMOTE_ADDR']]
                );
            } catch (Exception $e) {
                // Silently fail - logging is not critical
            }

            // Clear form
            $name = $email = $phone = $subject = $message = '';
        } else {
            $error = 'Failed to send message. Please try again or email us directly at ' . SUPPORT_EMAIL;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact Us - <?php echo APP_NAME; ?></title>
    <meta name="description" content="Contact LandSalesPro support team. Get help with land sales management, technical support, or general inquiries.">

    <!-- PWA Meta Tags -->
    <link rel="manifest" href="<?php echo SITE_URL; ?>/manifest.json">
    <meta name="theme-color" content="#667eea">

    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="<?php echo SITE_URL; ?>/assets/font-awesome/css/all.min.css">
    <link href="<?php echo SITE_URL; ?>/assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <style>
        :root {
            --primary: #667eea;
            --primary-dark: #764ba2;
            --secondary: #1e293b;
        }
        * { box-sizing: border-box; }
        body {
            font-family: 'Inter', sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #e4e8ec 100%);
            min-height: 100vh;
            margin: 0;
            padding: 20px;
        }
        .contact-container {
            max-width: 900px;
            margin: 0 auto;
        }
        .contact-header {
            text-align: center;
            margin-bottom: 30px;
        }
        .contact-header h1 {
            color: var(--secondary);
            font-weight: 700;
            margin-bottom: 10px;
            font-size: 2rem;
        }
        .contact-header h1 i {
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .contact-header p {
            color: #64748b;
            font-size: 1.1rem;
        }
        .contact-card {
            background: white;
            border-radius: 20px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            display: grid;
            grid-template-columns: 1fr 1.5fr;
        }
        .contact-info {
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            color: white;
            padding: 40px;
        }
        .contact-info h3 {
            margin: 0 0 25px 0;
            font-weight: 600;
            font-size: 1.4rem;
        }
        .contact-info-item {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-bottom: 20px;
        }
        .contact-info-item i {
            width: 44px;
            height: 44px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 18px;
            flex-shrink: 0;
        }
        .contact-info-item div {
            flex: 1;
        }
        .contact-info-item .label {
            font-size: 12px;
            opacity: 0.8;
            margin-bottom: 2px;
        }
        .contact-info-item span, .contact-info-item a {
            font-size: 15px;
            color: white;
            text-decoration: none;
        }
        .contact-info-item a:hover {
            text-decoration: underline;
        }
        .social-links {
            margin-top: 30px;
            padding-top: 25px;
            border-top: 1px solid rgba(255,255,255,0.2);
        }
        .social-links h4 {
            font-size: 14px;
            margin-bottom: 15px;
            opacity: 0.9;
        }
        .social-icons {
            display: flex;
            gap: 12px;
        }
        .social-icons a {
            width: 40px;
            height: 40px;
            background: rgba(255,255,255,0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            text-decoration: none;
            transition: all 0.3s;
        }
        .social-icons a:hover {
            background: white;
            color: var(--primary);
            transform: translateY(-3px);
        }
        .contact-form {
            padding: 40px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-label {
            display: block;
            font-weight: 600;
            color: #334155;
            margin-bottom: 8px;
            font-size: 14px;
        }
        .form-label .required {
            color: #ef4444;
        }
        .form-control {
            width: 100%;
            padding: 14px 18px;
            border: 2px solid #e2e8f0;
            border-radius: 12px;
            font-size: 15px;
            font-family: inherit;
            transition: all 0.3s;
        }
        .form-control:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
        }
        textarea.form-control {
            min-height: 130px;
            resize: vertical;
        }
        .btn-submit {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            border: none;
            border-radius: 12px;
            color: white;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }
        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
        }
        .alert {
            padding: 16px 20px;
            border-radius: 12px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 12px;
        }
        .alert-success {
            background: #d1fae5;
            color: #065f46;
            border: 1px solid #a7f3d0;
        }
        .alert-danger {
            background: #fee2e2;
            color: #991b1b;
            border: 1px solid #fecaca;
        }
        .back-link {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            color: var(--secondary);
            text-decoration: none;
            font-weight: 500;
            margin-bottom: 20px;
            transition: color 0.3s;
        }
        .back-link:hover {
            color: var(--primary);
        }
        .row {
            display: flex;
            gap: 20px;
        }
        .col-half {
            flex: 1;
        }
        .partner-badge {
            margin-top: 25px;
            padding: 15px;
            background: rgba(255,255,255,0.1);
            border-radius: 12px;
            text-align: center;
        }
        .partner-badge small {
            display: block;
            opacity: 0.7;
            margin-bottom: 5px;
            font-size: 11px;
        }
        .partner-badge a {
            color: white;
            text-decoration: none;
            font-weight: 600;
        }

        @media (max-width: 768px) {
            .contact-card {
                grid-template-columns: 1fr;
            }
            .row {
                flex-direction: column;
                gap: 0;
            }
            .contact-info, .contact-form {
                padding: 30px;
            }
            .contact-header h1 {
                font-size: 1.5rem;
            }
        }
    </style>
</head>
<body>
    <div class="contact-container">
        <a href="<?php echo $is_logged_in ? SITE_URL . '/dashboard/' : SITE_URL; ?>" class="back-link">
            <i class="fas fa-arrow-left"></i>
            <?php echo $is_logged_in ? 'Back to Dashboard' : 'Back to Home'; ?>
        </a>

        <div class="contact-header">
            <h1><i class="fas fa-headset"></i> Contact Us</h1>
            <p>Have questions about LandSalesPro? We're here to help!</p>
        </div>

        <div class="contact-card">
            <div class="contact-info">
                <h3>Get in Touch</h3>

                <div class="contact-info-item">
                    <i class="fas fa-envelope"></i>
                    <div>
                        <div class="label">Email</div>
                        <a href="mailto:<?php echo SUPPORT_EMAIL; ?>"><?php echo SUPPORT_EMAIL; ?></a>
                    </div>
                </div>

                <div class="contact-info-item">
                    <i class="fas fa-phone"></i>
                    <div>
                        <div class="label">Phone</div>
                        <a href="tel:<?php echo SUPPORT_PHONE; ?>"><?php echo SUPPORT_PHONE; ?></a>
                    </div>
                </div>

                <div class="contact-info-item">
                    <i class="fab fa-whatsapp"></i>
                    <div>
                        <div class="label">WhatsApp</div>
                        <a href="https://wa.me/<?php echo WHATSAPP_NUMBER; ?>?text=Hello%2C%20I%20need%20help%20with%20LandSalesPro" target="_blank">
                            <?php echo SUPPORT_PHONE; ?>
                        </a>
                    </div>
                </div>

                <div class="contact-info-item">
                    <i class="fas fa-globe"></i>
                    <div>
                        <div class="label">Website</div>
                        <span>landsalespro.landsecure.co.tz</span>
                    </div>
                </div>

                <div class="contact-info-item">
                    <i class="fas fa-clock"></i>
                    <div>
                        <div class="label">Business Hours</div>
                        <span>Mon - Fri: 8:00 AM - 6:00 PM</span>
                    </div>
                </div>

                <div class="contact-info-item">
                    <i class="fas fa-map-marker-alt"></i>
                    <div>
                        <div class="label">Location</div>
                        <span>Dar es Salaam, Tanzania</span>
                    </div>
                </div>

                <div class="social-links">
                    <h4>Connect With Us</h4>
                    <div class="social-icons">
                        <a href="https://wa.me/<?php echo WHATSAPP_NUMBER; ?>" target="_blank" title="WhatsApp">
                            <i class="fab fa-whatsapp"></i>
                        </a>
                        <a href="https://landsecure.co.tz" target="_blank" title="LandSecure">
                            <i class="fas fa-globe"></i>
                        </a>
                    </div>
                </div>

                <div class="partner-badge">
                    <small>Part of</small>
                    <a href="https://landsecure.co.tz" target="_blank">LandSecure Tanzania</a>
                </div>
            </div>

            <div class="contact-form">
                <?php if($success): ?>
                <div class="alert alert-success">
                    <i class="fas fa-check-circle"></i>
                    <?php echo $success; ?>
                </div>
                <?php endif; ?>

                <?php if($error): ?>
                <div class="alert alert-danger">
                    <i class="fas fa-exclamation-circle"></i>
                    <?php echo $error; ?>
                </div>
                <?php endif; ?>

                <form method="POST">
                    <div class="row">
                        <div class="col-half">
                            <div class="form-group">
                                <label class="form-label">Your Name <span class="required">*</span></label>
                                <input type="text" name="name" class="form-control" placeholder="John Doe"
                                       value="<?php echo htmlspecialchars($name ?? $user_name); ?>" required>
                            </div>
                        </div>
                        <div class="col-half">
                            <div class="form-group">
                                <label class="form-label">Email Address <span class="required">*</span></label>
                                <input type="email" name="email" class="form-control" placeholder="john@example.com"
                                       value="<?php echo htmlspecialchars($email ?? $user_email); ?>" required>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-half">
                            <div class="form-group">
                                <label class="form-label">Phone Number</label>
                                <input type="tel" name="phone" class="form-control" placeholder="+255 xxx xxx xxx"
                                       value="<?php echo htmlspecialchars($phone ?? $user_phone); ?>">
                            </div>
                        </div>
                        <div class="col-half">
                            <div class="form-group">
                                <label class="form-label">Subject <span class="required">*</span></label>
                                <select name="subject" class="form-control" required>
                                    <option value="">Select a subject</option>
                                    <option value="General Inquiry" <?php echo ($subject ?? '') == 'General Inquiry' ? 'selected' : ''; ?>>General Inquiry</option>
                                    <option value="Technical Support" <?php echo ($subject ?? '') == 'Technical Support' ? 'selected' : ''; ?>>Technical Support</option>
                                    <option value="Subscription & Billing" <?php echo ($subject ?? '') == 'Subscription & Billing' ? 'selected' : ''; ?>>Subscription & Billing</option>
                                    <option value="Account Issue" <?php echo ($subject ?? '') == 'Account Issue' ? 'selected' : ''; ?>>Account Issue</option>
                                    <option value="Feature Request" <?php echo ($subject ?? '') == 'Feature Request' ? 'selected' : ''; ?>>Feature Request</option>
                                    <option value="Bug Report" <?php echo ($subject ?? '') == 'Bug Report' ? 'selected' : ''; ?>>Bug Report</option>
                                    <option value="Partnership" <?php echo ($subject ?? '') == 'Partnership' ? 'selected' : ''; ?>>Partnership</option>
                                    <option value="Other" <?php echo ($subject ?? '') == 'Other' ? 'selected' : ''; ?>>Other</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="form-label">Your Message <span class="required">*</span></label>
                        <textarea name="message" class="form-control" placeholder="How can we help you today?"
                                  required><?php echo htmlspecialchars($message ?? ''); ?></textarea>
                    </div>

                    <button type="submit" class="btn-submit">
                        <i class="fas fa-paper-plane"></i>
                        Send Message
                    </button>
                </form>
            </div>
        </div>
    </div>
</body>
</html>
