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

// Get active jobs
try {
    $stmt = $db->query("SELECT * FROM jobs WHERE status = 'active' ORDER BY created_at DESC");
    $jobs = $stmt->fetchAll();
} catch (PDOException $e) {
    error_log("Error fetching jobs: " . $e->getMessage());
    $jobs = [];
}

$pageTitle = 'Careers';
include 'includes/header.php';
?>

<!-- Hero Section -->
<section class="py-12 bg-white">
    <div class="container mx-auto px-4 grid md:grid-cols-2 gap-8 items-center">
        <div>
            <h1 class="text-3xl md:text-4xl font-bold text-gray-900 mb-3">Build a Career with Purpose</h1>
            <p class="text-gray-700 mb-4">At Fantom Capital, your work becomes a catalyst for possibility. Every idea you share and every challenge you solve helps open doors for entrepreneurs, empower families, and strengthen communities. Here, you don't just grow a career — you shape a better financial future for Africa.</p>
            <h3 class="text-xl font-bold text-primary-blue mb-3">What Awaits You</h3>
            <ul class="list-disc pl-5 text-gray-700 space-y-2 mb-6">
                <li>Make a lasting difference, driving meaningful change in the African financial ecosystem</li>
                <li>Grow into your full potential through mentorship, continuous learning, and clear pathways for advancement</li>
                <li>Join a team that believes in you — supportive, ambitious, and united by shared values</li>
                <li>Thrive in an environment that prioritizes your well-being, offering flexibility, competitive rewards, and lifelong upskilling</li>
            </ul>
            <a href="#openings" class="inline-block bg-primary-blue text-white px-6 py-3 rounded-lg font-semibold hover:bg-opacity-90 transition">See Open Roles</a>
        </div>
        <div class="relative">
            <img src="https://images.unsplash.com/photo-1665686304026-4f50ed6e3a7a?q=80&w=1600&auto=format&fit=crop" alt="African colleagues discussing at work" class="w-full h-72 md:h-96 object-cover rounded-2xl shadow-md">
        </div>
    </div>
</section>

<!-- Why Join Us -->
<section class="py-12 bg-white">
        <div class="container mx-auto px-4">
            <h2 class="text-3xl font-bold text-center text-primary-blue mb-8">Why Work With Us?</h2>
            <div class="grid md:grid-cols-3 gap-8 max-w-5xl mx-auto">
                <div class="text-center p-6">
                    <div class="text-5xl mb-4">🚀</div>
                    <h3 class="text-xl font-bold mb-2">Career Growth</h3>
                    <p class="text-gray-600">Opportunities for professional development and advancement</p>
                </div>
                <div class="text-center p-6">
                    <div class="text-5xl mb-4">💼</div>
                    <h3 class="text-xl font-bold mb-2">Competitive Benefits</h3>
                    <p class="text-gray-600">Attractive compensation and comprehensive benefits package</p>
                </div>
                <div class="text-center p-6">
                    <div class="text-5xl mb-4">🤝</div>
                    <h3 class="text-xl font-bold mb-2">Great Culture</h3>
                    <p class="text-gray-600">Collaborative environment with passionate team members</p>
                </div>
            </div>
        </div>
    </section>

<!-- Job Openings -->
<section id="openings" class="py-12">
        <div class="container mx-auto px-4">
            <h2 class="text-3xl font-bold text-center text-primary-blue mb-8">Current Job Openings</h2>
            
            <?php if (empty($jobs)): ?>
                <div class="bg-white rounded-lg shadow-md p-12 text-center max-w-2xl mx-auto">
                    <div class="text-6xl mb-4">💼</div>
                    <h3 class="text-2xl font-bold text-gray-800 mb-2">No Open Positions</h3>
                    <p class="text-gray-600 mb-6">We don't have any openings at the moment, but we're always looking for talented individuals.</p>
                    <p class="text-gray-600">Send your CV to <a href="mailto:careers@fancap.co.ke" class="text-primary-blue hover:underline">careers@fancap.co.ke</a></p>
                </div>
            <?php else: ?>
                <div class="max-w-4xl mx-auto space-y-6">
                    <?php foreach ($jobs as $job): ?>
                    <div class="bg-white rounded-lg shadow-md hover:shadow-lg transition p-6">
                        <div class="flex justify-between items-start mb-4">
                            <div>
                                <h3 class="text-2xl font-bold text-gray-800 mb-2"><?php echo htmlspecialchars($job['title']); ?></h3>
                                <div class="flex flex-wrap gap-3 text-sm text-gray-600">
                                    <?php if ($job['department']): ?>
                                        <span class="flex items-center gap-1">
                                            📁 <?php echo htmlspecialchars($job['department']); ?>
                                        </span>
                                    <?php endif; ?>
                                    <?php if ($job['location']): ?>
                                        <span class="flex items-center gap-1">
                                            📍 <?php echo htmlspecialchars($job['location']); ?>
                                        </span>
                                    <?php endif; ?>
                                    <span class="flex items-center gap-1">
                                        ⏰ <?php echo htmlspecialchars($job['employment_type']); ?>
                                    </span>
                                </div>
                            </div>
                            <?php if ($job['deadline']): ?>
                                <span class="bg-red-100 text-red-800 text-xs font-semibold px-3 py-1 rounded-full">
                                    Apply by <?php echo formatDate($job['deadline']); ?>
                                </span>
                            <?php endif; ?>
                        </div>
                        
                        <p class="text-gray-700 mb-4 line-clamp-3"><?php echo nl2br(htmlspecialchars(substr($job['description'], 0, 200))); ?>...</p>
                        
                        <?php if ($job['salary_range']): ?>
                            <p class="text-primary-green font-semibold mb-4">💰 <?php echo htmlspecialchars($job['salary_range']); ?></p>
                        <?php endif; ?>
                        
                        <button onclick="openJobModal(<?php echo $job['id']; ?>)" 
                                class="bg-primary-blue text-white px-6 py-2 rounded-lg font-semibold hover:bg-opacity-90 transition">
                            View Details & Apply
                        </button>
                    </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </section>

    <!-- Job Details Modal -->
    <div id="jobModal" class="fixed inset-0 bg-black bg-opacity-50 z-50 hidden flex items-center justify-center p-4">
        <div class="bg-white rounded-2xl max-w-3xl w-full max-h-[90vh] overflow-y-auto p-6 md:p-8 relative">
            <button onclick="closeJobModal()" class="absolute top-4 right-4 text-gray-400 hover:text-gray-600 transition">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                </svg>
            </button>
            
            <div id="jobModalContent"></div>
        </div>
    </div>

    <script>
        const jobs = <?php echo json_encode($jobs); ?>;
        
        function openJobModal(jobId) {
            const job = jobs.find(j => j.id == jobId);
            if (!job) return;
            
            let content = `
                <h2 class="text-3xl font-bold text-primary-blue mb-4">${job.title}</h2>
                <div class="flex flex-wrap gap-4 mb-6 text-sm text-gray-600">
                    ${job.department ? `<span>📁 ${job.department}</span>` : ''}
                    ${job.location ? `<span>📍 ${job.location}</span>` : ''}
                    <span>⏰ ${job.employment_type}</span>
                    ${job.salary_range ? `<span class="text-primary-green font-semibold">💰 ${job.salary_range}</span>` : ''}
                </div>
                
                <div class="space-y-6">
                    <div>
                        <h3 class="text-xl font-bold text-gray-800 mb-2">Job Description</h3>
                        <p class="text-gray-700 whitespace-pre-line">${job.description}</p>
                    </div>
                    
                    ${job.responsibilities ? `
                    <div>
                        <h3 class="text-xl font-bold text-gray-800 mb-2">Key Responsibilities</h3>
                        <div class="text-gray-700 whitespace-pre-line">${job.responsibilities.split('\n').map(r => r.trim()).filter(r => r).map(r => `<p class="mb-1">• ${r}</p>`).join('')}</div>
                    </div>
                    ` : ''}
                    
                    ${job.requirements ? `
                    <div>
                        <h3 class="text-xl font-bold text-gray-800 mb-2">Requirements & Qualifications</h3>
                        <div class="text-gray-700 whitespace-pre-line">${job.requirements.split('\n').map(r => r.trim()).filter(r => r).map(r => `<p class="mb-1">• ${r}</p>`).join('')}</div>
                    </div>
                    ` : ''}
                    
                    ${job.deadline ? `
                    <div class="bg-red-50 border border-red-200 rounded-lg p-4">
                        <p class="text-red-800 font-semibold">⏰ Application Deadline: ${new Date(job.deadline).toLocaleDateString('en-US', {year: 'numeric', month: 'long', day: 'numeric'})}</p>
                    </div>
                    ` : ''}
                    
                    <div class="bg-primary-blue bg-opacity-5 rounded-lg p-6 text-center">
                        <h3 class="text-xl font-bold text-gray-800 mb-3">Ready to Apply?</h3>
                        <p class="text-gray-600 mb-4">Send your CV and cover letter to:</p>
                        <a href="mailto:careers@fancap.co.ke?subject=Application for ${encodeURIComponent(job.title)}" 
                           class="inline-block bg-primary-blue text-white px-8 py-3 rounded-lg font-semibold hover:bg-opacity-90 transition">
                            📧 Apply via Email
                        </a>
                        <p class="text-sm text-gray-500 mt-3">Or call us at <a href="tel:0723000500" class="text-primary-blue hover:underline">0723 000 500</a></p>
                    </div>
                </div>
            `;
            
            document.getElementById('jobModalContent').innerHTML = content;
            document.getElementById('jobModal').classList.remove('hidden');
            document.body.style.overflow = 'hidden';
        }
        
        function closeJobModal() {
            document.getElementById('jobModal').classList.add('hidden');
            document.body.style.overflow = '';
        }
    </script>

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