<?php 
require 'leave_rollover_script.php'; 

$rawRole  = $_SESSION['role'] ?? 'Staff';
$userRole = strtoupper(trim($rawRole));
$username = $_SESSION['username'] ?? 'User';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Monthly Cycle Rollover & Leave Matrix - 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">Monthly Circle Carryover Engine</h1>
                        <p class="text-[10px] text-indigo-700 font-bold uppercase tracking-wider">Cycle-Based Leave Allocation Rollover</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($userRole); ?></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">

                <?php if (isset($_GET['msg'])): ?>
                <div class="bg-emerald-50 border border-emerald-300 text-emerald-900 p-4 rounded-xl shadow-xs flex items-start gap-3 transition">
                    <div class="text-base leading-none mt-0.5">✅</div>
                    <div class="space-y-0.5">
                        <h3 class="font-black text-xs text-emerald-950">Circle Rollover Completed</h3>
                        <p class="text-[11px] text-emerald-700 font-semibold leading-tight"><?= htmlspecialchars($_GET['msg']) ?></p>
                    </div>
                </div>
                <?php endif; ?>

                <?php if (isset($_GET['err'])): ?>
                <div class="bg-rose-50 border border-rose-300 text-rose-900 p-4 rounded-xl shadow-xs flex items-start gap-3 transition">
                    <div class="text-base leading-none mt-0.5">❌</div>
                    <div class="space-y-0.5">
                        <h3 class="font-black text-xs text-rose-950">Process Fault</h3>
                        <p class="text-[11px] text-rose-700 font-semibold leading-tight"><?= htmlspecialchars($_GET['err']) ?></p>
                    </div>
                </div>
                <?php endif; ?>

                <!-- Active Payroll Circles & End-of-Cycle Execution List -->
                <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">Active & Historical Payroll Circles</h3>
                            <p class="text-[10px] text-slate-600 font-semibold mt-0.5">End-of-circle automated leave allocation carryovers (21st to 20th period).</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">
                            <?= count($payrollCircles) ?> Circles
                        </span>
                    </div>

                    <div class="overflow-x-auto">
                        <table class="w-full border-collapse text-left">
                            <thead>
                                <tr class="bg-slate-50/70 border-b border-slate-200 text-[10px] font-black text-slate-400 uppercase tracking-wider">
                                    <th class="p-4">Circle Identifier</th>
                                    <th class="p-4 text-center">Circle Month/Year</th>
                                    <th class="p-4 text-center">Period Frame</th>
                                    <th class="p-4 text-center">Processing Status</th>
                                    <th class="p-4 text-right">Rollover Action</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-slate-200/80 text-xs text-slate-700">
                                <?php if (!empty($payrollCircles)): ?>
                                    <?php foreach($payrollCircles as $circle): ?>
                                        <tr class="hover:bg-stone-50/60 transition duration-150">
                                            <td class="p-4 font-black text-slate-900">
                                                <?= htmlspecialchars($circle['name']) ?>
                                                <span class="block text-[10px] font-mono text-slate-400">ID: #<?= intval($circle['id']) ?></span>
                                            </td>
                                            <td class="p-4 text-center font-bold">
                                                Month <?= intval($circle['payroll_month']) ?> (<?= intval($circle['payroll_year']) ?>)
                                            </td>
                                            <td class="p-4 text-center font-mono text-[11px] text-slate-600">
                                                <?= date('d M Y', strtotime($circle['start_date'])) ?> — <?= date('d M Y', strtotime($circle['end_date'])) ?>
                                            </td>
                                            <td class="p-4 text-center">
                                                <span class="inline-block px-2.5 py-0.5 rounded font-black text-[10px] uppercase border tracking-wide <?= $circle['status'] === 'completed' ? 'bg-emerald-50 text-emerald-800 border-emerald-200' : 'bg-amber-50 text-amber-800 border-amber-200' ?>">
                                                    <?= htmlspecialchars($circle['status']) ?>
                                                </span>
                                            </td>
                                            <td class="p-4 text-right">
                                                <?php if ($circle['status'] !== 'completed'): ?>
                                                    <form method="POST" action="leave_rollover.php" class="inline-flex items-center gap-2">
                                                        <input type="hidden" name="action" value="close_cycle_and_rollover">
                                                        <input type="hidden" name="cycle_id" value="<?= $circle['id'] ?>">
                                                        <input type="number" step="0.5" name="max_cap" placeholder="Max Cap (Days)" class="w-28 border border-slate-300 bg-stone-50 rounded px-2 py-1 text-[10px] font-bold">
                                                        <button type="submit" onclick="return confirm('Close payroll circle #<?= $circle['id'] ?> and carry forward unused leave balances to the next period?');" class="bg-indigo-600 hover:bg-indigo-700 text-white font-extrabold px-3 py-1 rounded text-[10px] uppercase tracking-wider border border-indigo-700 transition">
                                                            Close & Carry Over
                                                        </button>
                                                    </form>
                                                <?php else: ?>
                                                    <span class="text-slate-400 font-mono text-[10px]">Rollover Executed</span>
                                                <?php endif; ?>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="5" class="p-8 text-center text-xs text-slate-400 font-bold uppercase tracking-wider bg-white">No payroll circles populated.</td>
                                    </tr>
                                <?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>