<?php
/**
 * Contact Page
 * Digital Marketplace
 */

require_once __DIR__ . '/config/database.php';

$page_title = 'Contact Us';

$errors = [];
$success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!verify_csrf_token($_POST['csrf_token'] ?? '')) {
        $errors[] = 'Invalid request. Please try again.';
    } else {
        $name = sanitize($_POST['name'] ?? '');
        $email = sanitize_email($_POST['email'] ?? '');
        $subject = sanitize($_POST['subject'] ?? '');
        $message = sanitize($_POST['message'] ?? '');
        
        if (empty($name)) {
            $errors[] = 'Please enter your name.';
        }
        
        if (empty($email) || !validate_email($email)) {
            $errors[] = 'Please enter a valid email address.';
        }
        
        if (empty($subject)) {
            $errors[] = 'Please enter a subject.';
        }
        
        if (empty($message)) {
            $errors[] = 'Please enter your message.';
        }
        
        if (empty($errors)) {
            // Send email to admin
            $admin_email = get_setting('site_email', 'admin@example.com');
            $email_subject = "Contact Form: {$subject}";
            $email_body = <<<HTML
<h2>New Contact Form Submission</h2>
<p><strong>Name:</strong> {$name}</p>
<p><strong>Email:</strong> {$email}</p>
<p><strong>Subject:</strong> {$subject}</p>
<p><strong>Message:</strong></p>
<p>{$message}</p>
HTML;
            
            send_email($admin_email, $email_subject, $email_body);
            
            $success = true;
            set_flash_message('Thank you for your message! We will get back to you soon.', 'success');
        }
    }
}

include __DIR__ . '/includes/header.php';
?>

<section class="section">
    <div class="container">
        <div class="row g-5">
            <!-- Contact Info -->
            <div class="col-lg-5" data-aos="fade-right">
                <h1 class="mb-4"><i class="fas fa-envelope me-2"></i>Contact Us</h1>
                <p class="text-muted mb-5">
                    Have a question or need assistance? We're here to help! Fill out the form and we'll get back to you as soon as possible.
                </p>
                
                <div class="contact-info">
                    <div class="contact-item mb-4">
                        <div class="contact-icon">
                            <i class="fas fa-map-marker-alt"></i>
                        </div>
                        <div>
                            <h6>Address</h6>
                            <p class="text-muted mb-0"><?php echo get_setting('site_address', '123 Digital Street, Tech City'); ?></p>
                        </div>
                    </div>
                    
                    <div class="contact-item mb-4">
                        <div class="contact-icon">
                            <i class="fas fa-envelope"></i>
                        </div>
                        <div>
                            <h6>Email</h6>
                            <p class="text-muted mb-0">
                                <a href="mailto:<?php echo get_setting('site_email', 'support@marketplace.com'); ?>">
                                    <?php echo get_setting('site_email', 'support@marketplace.com'); ?>
                                </a>
                            </p>
                        </div>
                    </div>
                    
                    <div class="contact-item mb-4">
                        <div class="contact-icon">
                            <i class="fas fa-phone"></i>
                        </div>
                        <div>
                            <h6>Phone</h6>
                            <p class="text-muted mb-0">
                                <a href="tel:<?php echo get_setting('site_phone', '+1 234 567 890'); ?>">
                                    <?php echo get_setting('site_phone', '+1 234 567 890'); ?>
                                </a>
                            </p>
                        </div>
                    </div>
                    
                    <div class="contact-item">
                        <div class="contact-icon">
                            <i class="fas fa-clock"></i>
                        </div>
                        <div>
                            <h6>Working Hours</h6>
                            <p class="text-muted mb-0">Mon - Fri: 9:00 AM - 6:00 PM</p>
                        </div>
                    </div>
                </div>
                
                <!-- Social Links -->
                <div class="mt-5">
                    <h6 class="mb-3">Follow Us</h6>
                    <div class="social-links">
                        <?php if ($facebook = get_setting('facebook_url')): ?>
                        <a href="<?php echo $facebook; ?>" target="_blank" class="social-link">
                            <i class="fab fa-facebook-f"></i>
                        </a>
                        <?php endif; ?>
                        
                        <?php if ($twitter = get_setting('twitter_url')): ?>
                        <a href="<?php echo $twitter; ?>" target="_blank" class="social-link">
                            <i class="fab fa-twitter"></i>
                        </a>
                        <?php endif; ?>
                        
                        <?php if ($instagram = get_setting('instagram_url')): ?>
                        <a href="<?php echo $instagram; ?>" target="_blank" class="social-link">
                            <i class="fab fa-instagram"></i>
                        </a>
                        <?php endif; ?>
                        
                        <?php if ($youtube = get_setting('youtube_url')): ?>
                        <a href="<?php echo $youtube; ?>" target="_blank" class="social-link">
                            <i class="fab fa-youtube"></i>
                        </a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            
            <!-- Contact Form -->
            <div class="col-lg-7" data-aos="fade-left">
                <div class="card">
                    <div class="card-body p-4 p-md-5">
                        <h4 class="mb-4">Send us a Message</h4>
                        
                        <?php if ($success): ?>
                            <div class="alert alert-success">
                                <i class="fas fa-check-circle me-2"></i>
                                Thank you for your message! We'll get back to you soon.
                            </div>
                        <?php else: ?>
                            <?php if (!empty($errors)): ?>
                                <div class="alert alert-danger">
                                    <?php foreach ($errors as $error): ?>
                                        <p class="mb-1"><i class="fas fa-exclamation-circle me-2"></i><?php echo $error; ?></p>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                            
                            <form method="POST">
                                <?php echo csrf_field(); ?>
                                
                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">Your Name <span class="text-danger">*</span></label>
                                        <input type="text" name="name" class="form-control" 
                                               value="<?php echo sanitize($_POST['name'] ?? ''); ?>" required>
                                    </div>
                                    
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">Email Address <span class="text-danger">*</span></label>
                                        <input type="email" name="email" class="form-control" 
                                               value="<?php echo sanitize($_POST['email'] ?? ''); ?>" required>
                                    </div>
                                </div>
                                
                                <div class="mb-3">
                                    <label class="form-label">Subject <span class="text-danger">*</span></label>
                                    <input type="text" name="subject" class="form-control" 
                                           value="<?php echo sanitize($_POST['subject'] ?? ''); ?>" required>
                                </div>
                                
                                <div class="mb-4">
                                    <label class="form-label">Message <span class="text-danger">*</span></label>
                                    <textarea name="message" class="form-control" rows="5" required><?php echo sanitize($_POST['message'] ?? ''); ?></textarea>
                                </div>
                                
                                <button type="submit" class="btn btn-primary btn-lg w-100">
                                    <i class="fas fa-paper-plane me-2"></i>Send Message
                                </button>
                            </form>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<style>
.contact-info {
    padding: 2rem;
    background: var(--gradient-card);
    border: 1px solid var(--dark-lighter);
    border-radius: var(--radius);
}

.contact-item {
    display: flex;
    align-items: flex-start;
    gap: 1rem;
}

.contact-icon {
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--gradient-primary);
    border-radius: var(--radius);
    color: var(--white);
    font-size: 1.25rem;
    flex-shrink: 0;
}

.contact-item h6 {
    color: var(--white);
    margin-bottom: 0.25rem;
}

.contact-item a {
    color: var(--gray-400);
    text-decoration: none;
}

.contact-item a:hover {
    color: var(--primary);
}

.social-links {
    display: flex;
    gap: 0.75rem;
}

.social-link {
    width: 45px;
    height: 45px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--dark-lighter);
    border: 1px solid var(--gray-700);
    border-radius: var(--radius);
    color: var(--gray-400);
    font-size: 1.125rem;
    transition: var(--transition);
}

.social-link:hover {
    background: var(--primary);
    border-color: var(--primary);
    color: var(--white);
    transform: translateY(-3px);
}
</style>

<?php include __DIR__ . '/includes/footer.php'; ?>
