<?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'] ?? 'User';
$role = $_SESSION['role'] ?? 'Staff';

$totalJobs = $conn->query("
SELECT COUNT(*) total
FROM jobs
")->fetch_assoc()['total'] ?? 0;

$openJobs = $conn->query("
SELECT COUNT(*) total
FROM jobs
WHERE status='Open'
")->fetch_assoc()['total'] ?? 0;

$totalApplicants = $conn->query("
SELECT COUNT(*) total
FROM applicants
")->fetch_assoc()['total'] ?? 0;

$hiredApplicants = $conn->query("
SELECT COUNT(*) total
FROM applicants
WHERE status='Hired'
")->fetch_assoc()['total'] ?? 0;

$interviews = $conn->query("
SELECT COUNT(*) total
FROM interviews
WHERE status='Scheduled'
")->fetch_assoc()['total'] ?? 0;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ATS Dashboard - 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">Applicant Tracking System</h1>
                        <p class="text-[10px] text-indigo-700 font-bold uppercase tracking-wider">Recruitment Metrics & Overview</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); ?></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">

                <!-- STATS CARDS GRID -->
                <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 gap-4">
                    
                    <!-- Total Jobs -->
                    <div class="bg-white border border-slate-300/80 rounded-xl p-4 shadow-sm hover:border-indigo-400 transition-all">
                        <div class="flex items-start justify-between">
                            <div class="space-y-1">
                                <p class="text-[10px] font-extrabold text-slate-500 uppercase tracking-wider">Total Positions</p>
                                <h4 class="text-2xl font-black tracking-tight text-slate-950"><?= $totalJobs ?></h4>
                                <p class="text-[10px] text-slate-600 font-semibold">Created vacancy records.</p>
                            </div>
                            <div class="w-8 h-8 rounded-lg bg-indigo-50 border border-indigo-200 flex items-center justify-center text-sm shrink-0">💼</div>
                        </div>
                    </div>

                    <!-- Open Jobs -->
                    <div class="bg-white border border-slate-300/80 rounded-xl p-4 shadow-sm hover:border-emerald-400 transition-all">
                        <div class="flex items-start justify-between">
                            <div class="space-y-1">
                                <p class="text-[10px] font-extrabold text-slate-500 uppercase tracking-wider">Active Openings</p>
                                <h4 class="text-2xl font-black tracking-tight text-emerald-950"><?= $openJobs ?></h4>
                                <p class="text-[10px] text-slate-600 font-semibold">Currently accepting applicants.</p>
                            </div>
                            <div class="w-8 h-8 rounded-lg bg-emerald-50 border border-emerald-200 flex items-center justify-center text-sm shrink-0">🟢</div>
                        </div>
                    </div>

                    <!-- Total Applicants -->
                    <div class="bg-white border border-slate-300/80 rounded-xl p-4 shadow-sm hover:border-sky-400 transition-all">
                        <div class="flex items-start justify-between">
                            <div class="space-y-1">
                                <p class="text-[10px] font-extrabold text-slate-500 uppercase tracking-wider">Total Candidates</p>
                                <h4 class="text-2xl font-black tracking-tight text-slate-950"><?= $totalApplicants ?></h4>
                                <p class="text-[10px] text-slate-600 font-semibold">Submitted applications.</p>
                            </div>
                            <div class="w-8 h-8 rounded-lg bg-sky-50 border border-sky-200 flex items-center justify-center text-sm shrink-0">👥</div>
                        </div>
                    </div>

                    <!-- Interviews -->
                    <div class="bg-white border border-slate-300/80 rounded-xl p-4 shadow-sm hover:border-amber-400 transition-all">
                        <div class="flex items-start justify-between">
                            <div class="space-y-1">
                                <p class="text-[10px] font-extrabold text-slate-500 uppercase tracking-wider">Interviews</p>
                                <h4 class="text-2xl font-black tracking-tight text-amber-950"><?= $interviews ?></h4>
                                <p class="text-[10px] text-slate-600 font-semibold">Scheduled candidate sessions.</p>
                            </div>
                            <div class="w-8 h-8 rounded-lg bg-amber-50 border border-amber-200 flex items-center justify-center text-sm shrink-0">📅</div>
                        </div>
                    </div>

                    <!-- Hired -->
                    <div class="bg-white border border-slate-300/80 rounded-xl p-4 shadow-sm hover:border-purple-400 transition-all">
                        <div class="flex items-start justify-between">
                            <div class="space-y-1">
                                <p class="text-[10px] font-extrabold text-slate-500 uppercase tracking-wider">Hired Applicants</p>
                                <h4 class="text-2xl font-black tracking-tight text-purple-950"><?= $hiredApplicants ?></h4>
                                <p class="text-[10px] text-slate-600 font-semibold">Successfully placed candidates.</p>
                            </div>
                            <div class="w-8 h-8 rounded-lg bg-purple-50 border border-purple-200 flex items-center justify-center text-sm shrink-0">🎉</div>
                        </div>
                    </div>

                </div>

                <!-- QUICK ACCESS NAVIGATION -->
                <div class="bg-white border border-slate-300/80 rounded-xl p-4 shadow-sm space-y-4">
                    <div class="flex items-center justify-between border-b border-slate-200 pb-2">
                        <h3 class="text-[10px] font-black text-slate-500 uppercase tracking-wider">Recruitment Control Panel</h3>
                        <span class="text-[9px] font-bold text-indigo-700 uppercase">Quick Actions</span>
                    </div>

                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                        
                        <a href="job_positions.php" class="group bg-stone-50/70 border border-slate-300/80 hover:border-indigo-400 hover:bg-white rounded-xl p-4 transition-all shadow-xs flex flex-col justify-between">
                            <div class="space-y-2">
                                <div class="flex items-center justify-between">
                                    <div class="w-8 h-8 rounded-lg bg-indigo-50 border border-indigo-200 flex items-center justify-center text-sm">
                                        📌
                                    </div>
                                    <span class="text-[10px] font-black text-indigo-700 uppercase group-hover:translate-x-0.5 transition-transform">Manage &rarr;</span>
                                </div>
                                <div>
                                    <h2 class="font-extrabold text-sm text-slate-900 group-hover:text-indigo-950">Job Positions</h2>
                                    <p class="text-[11px] text-slate-600 font-medium leading-relaxed mt-0.5">Create, adjust, and monitor active job vacancies across departments.</p>
                                </div>
                            </div>
                        </a>

                        <a href="applicants.php" class="group bg-stone-50/70 border border-slate-300/80 hover:border-indigo-400 hover:bg-white rounded-xl p-4 transition-all shadow-xs flex flex-col justify-between">
                            <div class="space-y-2">
                                <div class="flex items-center justify-between">
                                    <div class="w-8 h-8 rounded-lg bg-sky-50 border border-sky-200 flex items-center justify-center text-sm">
                                        📄
                                    </div>
                                    <span class="text-[10px] font-black text-indigo-700 uppercase group-hover:translate-x-0.5 transition-transform">Review &rarr;</span>
                                </div>
                                <div>
                                    <h2 class="font-extrabold text-sm text-slate-900 group-hover:text-indigo-950">Applicants Roster</h2>
                                    <p class="text-[11px] text-slate-600 font-medium leading-relaxed mt-0.5">Filter, evaluate candidate profiles, and manage current pipeline stages.</p>
                                </div>
                            </div>
                        </a>

                        <a href="interview_calendar.php" class="group bg-stone-50/70 border border-slate-300/80 hover:border-indigo-400 hover:bg-white rounded-xl p-4 transition-all shadow-xs flex flex-col justify-between">
                            <div class="space-y-2">
                                <div class="flex items-center justify-between">
                                    <div class="w-8 h-8 rounded-lg bg-amber-50 border border-amber-200 flex items-center justify-center text-sm">
                                        🗓️
                                    </div>
                                    <span class="text-[10px] font-black text-indigo-700 uppercase group-hover:translate-x-0.5 transition-transform">Calendar &rarr;</span>
                                </div>
                                <div>
                                    <h2 class="font-extrabold text-sm text-slate-900 group-hover:text-indigo-950">Interview Schedule</h2>
                                    <p class="text-[11px] text-slate-600 font-medium leading-relaxed mt-0.5">View scheduled interview slots, update agendas, and coordinate meetings.</p>
                                </div>
                            </div>
                        </a>

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