<?php

require '../config.php';
session_start();
session_write_close();

if(!isset($_SESSION['user_id'])){
    header("Location: ../login.php");
    exit;
}

$username = $_SESSION['username'] ?? 'HR Admin';
$role = $_SESSION['role'] ?? 'Administrator';

$jobs = $conn->query("
SELECT

j.*,
d.name department

FROM jobs j

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

ORDER BY j.id DESC
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Job Positions - 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">

    <div class="flex">
        
        <?php include '../components/sidebar.php'; ?>

        <div id="sidebar-overlay" class="fixed inset-0 bg-slate-900/40 hidden z-40 transition-opacity duration-300" onclick="toggleSidebar()"></div>

        <div id="main-content" class="flex-1 min-w-0 md:pl-60 transition-all duration-300 min-h-screen 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">
                    <button onclick="toggleSidebar()" class="p-1.5 rounded-lg text-slate-600 hover:bg-slate-200/80 transition active:scale-95" aria-label="Toggle Side Area Menu">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 12h16M4 18h16"></path>
                        </svg>
                    </button>
                    <div>
                        <h1 class="text-sm font-black tracking-tight text-slate-950">Recruitment Portal</h1>
                        <p class="text-[10px] text-indigo-700 font-bold uppercase tracking-wider">Job Positions & Requisitions</p>
                    </div>
                </div>
                
                <div class="flex items-center gap-4">
                    <div class="hidden sm:flex flex-col text-right">
                        <span class="text-[11px] font-extrabold text-slate-950 leading-tight"><?= htmlspecialchars($username); ?></span>
                        <span class="text-[9px] text-indigo-700 uppercase font-black tracking-wider leading-none mt-0.5"><?= htmlspecialchars($role ?: 'Staff'); ?></span>
                    </div>
                    <div class="w-7 h-7 bg-slate-200 border border-slate-300 rounded-lg flex items-center justify-center font-black text-xs text-slate-700 select-none uppercase">
                        <?= strtoupper(substr($username, 0, 1)); ?>
                    </div>
                </div>
            </header>

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

                <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4 bg-white border border-slate-300/80 rounded-xl p-4 shadow-sm">
                    <div>
                        <h2 class="text-base font-black text-slate-950 uppercase tracking-tight">Active Requisitions</h2>
                        <p class="text-[10px] text-slate-600 font-semibold mt-0.5">Manage job openings, monitor application pipelines, and configure positions.</p>
                    </div>
                    <a href="create_job.php" class="bg-slate-900 hover:bg-slate-800 active:scale-[0.98] transition text-white px-4 py-2 rounded-lg font-bold text-xs shadow-xs border border-slate-950 flex items-center justify-center gap-2 self-start sm:self-auto">
                        <span>+ Create Vacancy</span>
                    </a>
                </div>

                <div class="bg-white border border-slate-300/80 rounded-xl shadow-sm overflow-hidden">
                    
                    <div class="px-4 py-3 bg-stone-50/50 border-b border-slate-300/80 flex items-center justify-between">
                        <div>
                            <h3 class="text-xs font-black text-slate-950 uppercase tracking-tight">Job Requisition Directory</h3>
                            <p class="text-[10px] text-slate-600 font-semibold mt-0.5">Real-time status of all created job openings.</p>
                        </div>
                        <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 ?> Listed
                        </span>
                    </div>

                    <div class="overflow-x-auto w-full">
                        <table class="w-full min-w-[800px] border-collapse">
                            <thead>
                                <tr class="bg-stone-50 text-slate-500 uppercase text-[9px] font-black tracking-wider border-b border-slate-300/80">
                                    <th class="p-3 text-left w-[250px]">Job Title & Department</th>
                                    <th class="p-3 text-center">Employment Type</th>
                                    <th class="p-3 text-center">Vacancies</th>
                                    <th class="p-3 text-center">Status</th>
                                    <th class="p-3 text-center">Applicants</th>
                                    <th class="p-3 text-center">Action</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-slate-200/80 text-xs font-bold">

                            <?php if (!$jobs || $jobs->num_rows === 0): ?>
                            <tr>
                                <td colspan="6" class="text-center py-12">
                                    <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 Job Requisitions Found</h4>
                                        <p class="text-[10px] text-slate-600 max-w-xs">Create your first job vacancy to start receiving applicant profiles.</p>
                                    </div>
                                </td>
                            </tr>
                            <?php else: ?>
                                <?php while($job = $jobs->fetch_assoc()): ?>
                                    <?php
                                        $applicantsStmt = $conn->prepare("SELECT COUNT(*) total FROM applicants WHERE job_id = ?");
                                        $applicantsStmt->bind_param("i", $job['id']);
                                        $applicantsStmt->execute();
                                        $applicants = $applicantsStmt->get_result()->fetch_assoc()['total'];
                                    ?>
                                    <tr class="hover:bg-stone-50/50 transition text-slate-800">
                                        <td class="p-3 whitespace-nowrap">
                                            <div class="space-y-0.5">
                                                <p class="font-extrabold text-slate-900 leading-tight"><?= htmlspecialchars($job['title']) ?></p>
                                                <p class="text-[9px] text-indigo-700 uppercase tracking-wider font-black"><?= htmlspecialchars($job['department'] ?? 'General') ?></p>
                                            </div>
                                        </td>

                                        <td class="p-3 text-center whitespace-nowrap">
                                            <span class="inline-flex items-center px-2.5 py-1 rounded-md text-[10px] font-black bg-slate-100 border border-slate-300 text-slate-700 font-mono">
                                                <?= htmlspecialchars($job['employment_type'] ?? 'Full-Time') ?>
                                            </span>
                                        </td>

                                        <td class="p-3 text-center whitespace-nowrap font-mono text-xs">
                                            <?= (int)$job['vacancies'] ?>
                                        </td>

                                        <td class="p-3 text-center whitespace-nowrap">
                                            <?php if($job['status'] == 'Open'): ?>
                                                <span class="bg-emerald-50 text-emerald-800 border border-emerald-300 px-2 py-1 rounded-md font-black text-[10px] inline-block min-w-[75px] text-center">Open</span>
                                            <?php else: ?>
                                                <span class="bg-rose-50 text-rose-900 border border-rose-300 px-2 py-1 rounded-md font-black text-[10px] inline-block min-w-[75px] text-center">Closed</span>
                                            <?php endif; ?>
                                        </td>

                                        <td class="p-3 text-center whitespace-nowrap">
                                            <span class="bg-indigo-50 text-indigo-800 border border-indigo-200 px-2 py-0.5 rounded font-black text-[10px] font-mono">
                                                <?= $applicants ?>
                                            </span>
                                        </td>

                                        <td class="p-3 text-center whitespace-nowrap">
                                            <a href="applicants.php?job=<?= $job['id'] ?>" class="bg-slate-900 hover:bg-slate-800 active:scale-[0.98] transition text-white px-3 py-1.5 rounded-lg font-bold text-[11px] shadow-xs border border-slate-950 inline-flex items-center gap-1">
                                                <span>Applicants</span>
                                                <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
                                                    <path stroke-linecap="round" stroke-linejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5"></path>
                                                </svg>
                                            </a>
                                        </td>
                                    </tr>
                                <?php endwhile; ?>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                </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>
        </div>
    </div>

    <script src="../components/sidebar.js"></script>
</body>
</html>