<?php 
require APPROOT . '/app/Views/layouts/header.php'; 
$settings = getSettings(); 

$hasEmail = !empty($settings['contact_email']);
$hasPhone = !empty($settings['contact_landline']) || !empty($settings['contact_mobile']);
$addressParts = array_filter([
    $settings['contact_address_street'] ?? '',
    $settings['contact_address_suburb'] ?? '',
    $settings['contact_address_city'] ?? '',
    $settings['contact_address_state'] ?? '',
    $settings['contact_address_zip'] ?? '',
    $settings['contact_address_country'] ?? ''
]);
$hasAddress = !empty($addressParts);
$hasContactInfo = $hasEmail || $hasPhone || $hasAddress;
?>

<div class="max-w-6xl mx-auto my-10 px-4">
    <div class="text-center mb-10">
        <h1 class="text-4xl font-bold text-gray-900 dark:text-white mb-4"><?php echo $data['title']; ?></h1>
        <p class="text-gray-600 dark:text-gray-400">Have questions? We'd love to hear from you.</p>
    </div>
    
    <div class="grid grid-cols-1 <?php echo $hasContactInfo ? 'lg:grid-cols-3' : 'max-w-3xl mx-auto'; ?> gap-8">
        
        <?php if ($hasContactInfo): ?>
        <div class="lg:col-span-1 space-y-6">
            
            <?php if ($hasEmail): ?>
            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 p-6 text-center">
                <div class="text-primary mb-3 flex justify-center">
                    <svg class="w-8 h-8" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path></svg>
                </div>
                <h3 class="text-lg font-bold text-gray-900 dark:text-white mb-2">Email Us</h3>
                <a href="mailto:<?php echo htmlspecialchars($settings['contact_email']); ?>" class="text-primary hover:underline font-medium">
                    <?php echo htmlspecialchars($settings['contact_email']); ?>
                </a>
            </div>
            <?php endif; ?>
            
            <?php if ($hasPhone): ?>
            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 p-6 text-center">
                <div class="text-primary mb-3 flex justify-center">
                    <svg class="w-8 h-8" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"></path></svg>
                </div>
                <h3 class="text-lg font-bold text-gray-900 dark:text-white mb-2">Call Us</h3>
                <?php if(!empty($settings['contact_landline'])): ?>
                    <p class="text-gray-600 dark:text-gray-300 font-medium mb-2">
                        <span class="text-[10px] text-gray-400 uppercase tracking-wider font-bold block mb-0.5">Landline</span>
                        <?php echo htmlspecialchars($settings['contact_landline']); ?>
                    </p>
                <?php endif; ?>
                <?php if(!empty($settings['contact_mobile'])): ?>
                    <p class="text-gray-600 dark:text-gray-300 font-medium">
                        <span class="text-[10px] text-gray-400 uppercase tracking-wider font-bold block mb-0.5">Mobile</span>
                        <?php echo htmlspecialchars($settings['contact_mobile']); ?>
                    </p>
                <?php endif; ?>
            </div>
            <?php endif; ?>
            
            <?php if ($hasAddress): ?>
            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 p-6 text-center">
                <div class="text-primary mb-3 flex justify-center">
                    <svg class="w-8 h-8" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
                </div>
                <h3 class="text-lg font-bold text-gray-900 dark:text-white mb-2">Visit Us</h3>
                <p class="text-gray-600 dark:text-gray-300 font-medium leading-relaxed text-sm">
                    <?php echo nl2br(htmlspecialchars(implode(', ', $addressParts))); ?>
                </p>
            </div>
            <?php endif; ?>
            
        </div>
        <?php endif; ?>

        <div class="<?php echo $hasContactInfo ? 'lg:col-span-2' : ''; ?> bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 p-8 md:p-10">
            <h2 class="text-2xl font-bold text-gray-900 dark:text-white mb-6">Send us a message</h2>
            
            <form action="<?php echo URLROOT; ?>/pages/contact" method="POST" class="space-y-6">
                
                <?php echo csrfField(); ?>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-2">Your Name</label>
                        <input type="text" name="name" value="<?php echo $data['name'] ?? ''; ?>" class="w-full px-4 py-2.5 rounded-lg border <?php echo (!empty($data['name_err'])) ? 'border-red-500' : 'border-gray-300 dark:border-gray-700'; ?> bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors">
                        <span class="text-xs text-red-500 mt-1 block"><?php echo $data['name_err'] ?? ''; ?></span>
                    </div>
                    <div>
                        <label class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-2">Your Email</label>
                        <input type="email" name="email" value="<?php echo $data['email'] ?? ''; ?>" class="w-full px-4 py-2.5 rounded-lg border <?php echo (!empty($data['email_err'])) ? 'border-red-500' : 'border-gray-300 dark:border-gray-700'; ?> bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors">
                        <span class="text-xs text-red-500 mt-1 block"><?php echo $data['email_err'] ?? ''; ?></span>
                    </div>
                </div>
                
                <div>
                    <label class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-2">Subject</label>
                    <input type="text" name="subject" value="<?php echo $data['subject'] ?? ''; ?>" class="w-full px-4 py-2.5 rounded-lg border <?php echo (!empty($data['subject_err'])) ? 'border-red-500' : 'border-gray-300 dark:border-gray-700'; ?> bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors">
                    <span class="text-xs text-red-500 mt-1 block"><?php echo $data['subject_err'] ?? ''; ?></span>
                </div>

                <div>
                    <label class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-2">Message</label>
                    <textarea name="message" rows="6" class="w-full px-4 py-3 rounded-lg border <?php echo (!empty($data['message_err'])) ? 'border-red-500' : 'border-gray-300 dark:border-gray-700'; ?> bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none resize-y transition-colors"><?php echo $data['message'] ?? ''; ?></textarea>
                    <span class="text-xs text-red-500 mt-1 block"><?php echo $data['message_err'] ?? ''; ?></span>
                </div>

                <div class="p-5 bg-gray-50 dark:bg-gray-900/30 rounded-xl border <?php echo (!empty($data['captcha_err'])) ? 'border-red-500' : 'border-gray-200 dark:border-gray-700/50'; ?>">
                    <label class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-3">Spam Protection Verification</label>
                    <div class="flex items-center space-x-4">
                        <div class="px-4 py-2.5 bg-white dark:bg-gray-800 rounded-lg shadow-sm font-mono text-lg font-bold text-primary tracking-widest whitespace-nowrap border border-gray-200 dark:border-gray-700/50">
                            <?php echo $data['captcha_num1'] . ' + ' . $data['captcha_num2'] . ' = ?'; ?>
                        </div>
                        <input type="number" name="captcha" placeholder="Answer" class="w-32 px-4 py-2.5 rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none font-mono transition-colors">
                    </div>
                    <span class="text-xs text-red-500 mt-2 block"><?php echo $data['captcha_err'] ?? ''; ?></span>
                </div>

                <button type="submit" class="w-full md:w-auto bg-primary hover:opacity-90 text-white font-bold py-3.5 px-10 rounded-lg shadow-sm transition-all focus:ring-4 focus:ring-primary/50 flex items-center justify-center">
                    <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"></path></svg>
                    Send Message
                </button>
            </form>
        </div>
    </div>
</div>
<?php require APPROOT . '/app/Views/layouts/footer.php'; ?>