<?php

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

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 AS department,

    (
        SELECT COUNT(*)
        FROM applicants a
        WHERE a.job_id = j.id
    ) AS total_applicants,

    (
        SELECT COUNT(*)
        FROM applicants a
        WHERE a.job_id = j.id
        AND a.status = 'Hired'
    ) AS hired_count

FROM jobs j
LEFT JOIN departments d ON d.id = j.department_id
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>Job Vacancies - 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 Vacancies Registry</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 & Closed Vacancies</h2>
                        <p class="text-[10px] text-slate-600 font-semibold mt-0.5">Manage job listings, applicant flow, and recruitment metrics across departments.</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">
                        <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="M12 4.5v15m7.5-7.5h-15"></path>
                        </svg>
                        <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">Positions Overview</h3>
                            <p class="text-[10px] text-slate-600 font-semibold mt-0.5">Comprehensive list of posted positions and fulfillment statuses.</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">
                            Total: <?= $jobs ? $jobs->num_rows : 0 ?>
                        </span>
                    </div>

                    <div class="overflow-x-auto w-full">
                        <table class="w-full min-w-[750px] 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">Position</th>
                                    <th class="p-3 text-left">Department</th>
                                    <th class="p-3 text-center">Vacancies</th>
                                    <th class="p-3 text-center">Applicants</th>
                                    <th class="p-3 text-center">Hired</th>
                                    <th class="p-3 text-center">Status</th>
                                    <th class="p-3 text-center">Actions</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="7" 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 Vacancies Created</h4>
                                        <p class="text-[10px] text-slate-600 max-w-xs">Get started by creating a new position opening for candidates to apply.</p>
                                    </div>
                                </td>
                            </tr>
                            <?php else: ?>
                                <?php while($job = $jobs->fetch_assoc()): ?>
                                    <tr class="hover:bg-stone-50/50 transition text-slate-800">
                                        <td class="p-3 whitespace-nowrap">
                                            <span class="font-extrabold text-slate-950 block leading-tight"><?= htmlspecialchars($job['title']) ?></span>
                                            <span class="text-[9px] text-slate-400 font-mono font-normal">ID: #<?= $job['id'] ?></span>
                                        </td>

                                        <td class="p-3 whitespace-nowrap">
                                            <span class="text-slate-600 font-semibold"><?= htmlspecialchars($job['department'] ?? 'General / Unassigned') ?></span>
                                        </td>

                                        <td class="p-3 text-center whitespace-nowrap font-mono">
                                            <span class="text-slate-900 font-extrabold"><?= intval($job['vacancies']) ?></span>
                                        </td>

                                        <td class="p-3 text-center whitespace-nowrap font-mono">
                                            <span class="inline-flex items-center px-2 py-0.5 rounded text-[10px] font-extrabold bg-slate-100 border border-slate-200 text-slate-700">
                                                <?= intval($job['total_applicants']) ?>
                                            </span>
                                        </td>

                                        <td class="p-3 text-center whitespace-nowrap font-mono">
                                            <span class="inline-flex items-center px-2 py-0.5 rounded text-[10px] font-black bg-emerald-50 border border-emerald-200 text-emerald-800">
                                                <?= intval($job['hired_count']) ?>
                                            </span>
                                        </td>

                                        <td class="p-3 text-center whitespace-nowrap">
                                            <?php if ($job['status'] == 'Open'): ?>
                                                <span class="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-md text-[10px] font-black bg-emerald-50 border border-emerald-200 text-emerald-800">
                                                    <span class="w-1.5 h-1.5 rounded-full bg-emerald-600"></span>
                                                    Open
                                                </span>
                                            <?php else: ?>
                                                <span class="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-md text-[10px] font-black bg-rose-50 border border-rose-200 text-rose-800">
                                                    <span class="w-1.5 h-1.5 rounded-full bg-rose-600"></span>
                                                    Closed
                                                </span>
                                            <?php endif; ?>
                                        </td>

                                        <td class="p-3 text-center whitespace-nowrap">
                                            <div class="flex items-center justify-center gap-1.5">
                                                <a href="applicants.php?job_id=<?= $job['id'] ?>" class="bg-indigo-50 hover:bg-indigo-100 text-indigo-900 border border-indigo-200 active:scale-[0.98] transition px-2.5 py-1 rounded-md font-bold text-[10px]">
                                                    Applicants
                                                </a>
                                                <a href="job_positions.php?job_id=<?= $job['id'] ?>" class="bg-stone-100 hover:bg-stone-200 text-slate-700 border border-slate-300 active:scale-[0.98] transition px-2.5 py-1 rounded-md font-bold text-[10px]">
                                                    View
                                                </a>
                                            </div>
                                        </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>