<?php

error_reporting(E_ALL);
ini_set('display_errors',1);

require '../config.php';

$jobs = $conn->query("
SELECT

j.*,
d.name department

FROM jobs j

LEFT JOIN departments d
ON d.id=j.department_id

WHERE j.status='Open'

ORDER BY j.created_at DESC
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Career Portal - HR System</title>
    
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Mulish:ital,wght=0,200..1000;1,200..1000&display=swap" rel="stylesheet">
    
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    fontFamily: {
                        sans: ['Mulish', 'sans-serif'],
                    }
                }
            }
        }
    </script>
    <link rel="stylesheet" href="components/sidebar.css">
</head>
<body class="bg-stone-100/60 min-h-screen text-slate-800 antialiased selection:bg-indigo-600 selection:text-white font-sans flex flex-col">

    <header class="sticky top-0 bg-white/90 backdrop-blur-md border-b border-slate-300/80 h-14 flex items-center justify-between px-4 sm:px-6 z-30 shadow-xs">
        <div class="flex items-center gap-3">
            <div>
                <h1 class="text-sm font-black tracking-tight text-slate-950">Career Portal</h1>
                <p class="text-[10px] text-indigo-700 font-bold uppercase tracking-wider">Open Positions</p>
            </div>
        </div>
        
        <div class="flex items-center gap-2">
            <span class="bg-indigo-50 text-indigo-800 border border-indigo-200 px-2.5 py-1 rounded-md font-extrabold text-[10px] font-mono shadow-xs">
                <?= $jobs ? $jobs->num_rows : 0 ?> Roles Open
            </span>
        </div>
    </header>

    <main class="flex-1 p-4 sm:p-6 max-w-7xl w-full mx-auto space-y-6">

        <div class="bg-gradient-to-br from-slate-900 to-indigo-950 border border-slate-800 rounded-xl p-6 shadow-sm text-white flex flex-col sm:flex-row sm:items-center justify-between gap-4">
            <div class="space-y-1">
                <p class="text-[9px] font-black text-indigo-300 uppercase tracking-widest">Join Our Team</p>
                <h2 class="text-xl font-black tracking-tight text-slate-100">Explore Career Opportunities</h2>
                <p class="text-xs text-slate-300 font-semibold max-w-xl">Find your next role with us. Review open requisitions below and submit your application directly into our HR system.</p>
            </div>
            <div class="text-3xl shrink-0 self-start sm:self-center">🚀</div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">

            <?php if (!$jobs || $jobs->num_rows === 0): ?>
                <div class="col-span-full bg-white border border-slate-300/80 rounded-xl p-12 text-center shadow-sm">
                    <div class="flex flex-col items-center justify-center space-y-2">
                        <div class="text-3xl">📭</div>
                        <h4 class="text-xs font-extrabold text-slate-800">No Open Opportunities Right Now</h4>
                        <p class="text-[10px] text-slate-600 max-w-xs">Check back later for new openings and available positions.</p>
                    </div>
                </div>
            <?php else: ?>
                <?php while($job = $jobs->fetch_assoc()): ?>
                    <div class="bg-white border border-slate-300/80 rounded-xl p-4 shadow-sm hover:border-indigo-400 transition-all flex flex-col justify-between space-y-4">
                        <div class="space-y-2.5">
                            <div class="flex items-start justify-between gap-2">
                                <div>
                                    <h3 class="text-base font-black tracking-tight text-slate-950 leading-snug">
                                        <?= htmlspecialchars($job['title']) ?>
                                    </h3>
                                    <p class="text-[10px] font-bold text-slate-500 uppercase tracking-wider mt-0.5">
                                        <?= htmlspecialchars($job['department'] ?? 'General') ?>
                                    </p>
                                </div>
                                <span class="shrink-0 bg-indigo-50 text-indigo-800 border border-indigo-200 px-2 py-0.5 rounded text-[10px] font-black tracking-wider uppercase font-mono">
                                    <?= htmlspecialchars($job['employment_type'] ?? 'Full-Time') ?>
                                </span>
                            </div>

                            <p class="text-xs text-slate-600 leading-relaxed line-clamp-3 font-semibold">
                                <?= nl2br(htmlspecialchars(substr($job['description'], 0, 200))) ?><?= strlen($job['description']) > 200 ? '...' : '' ?>
                            </p>
                        </div>

                        <div class="pt-3 border-t border-slate-200/80 flex items-center justify-between">
                            <div class="space-y-0.5">
                                <p class="text-[9px] text-slate-500 font-black uppercase tracking-wider">Vacancies</p>
                                <span class="bg-emerald-50 text-emerald-800 border border-emerald-200 px-2 py-0.5 rounded font-black text-[10px] font-mono inline-block">
                                    <?= (int)$job['vacancies'] ?> Open
                                </span>
                            </div>

                            <a href="job_application.php?id=<?= $job['id'] ?>" class="bg-slate-900 hover:bg-slate-800 active:scale-[0.98] transition text-white px-3 py-2 rounded-lg font-bold text-xs shadow-xs border border-slate-950 inline-flex items-center gap-1.5">
                                Apply Now
                                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2.5">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M13.5 4.5L21 12m0 0l-7.5 7.5M21 12H3"></path>
                                </svg>
                            </a>
                        </div>
                    </div>
                <?php endwhile; ?>
            <?php endif; ?>

        </div>

    </main>

    <footer class="mt-auto border-t border-slate-300 bg-white px-6 py-4 text-center">
        <p class="text-[9px] text-slate-500 font-mono tracking-wider">&copy; 2026 Core HR Engine System Platform • Dynamic Secure Architecture Protocol.</p>
    </footer>

</body>
</html>