<?php 
// Ensure this file is saved as leave_types.php
require 'leave_types_script.php'; 

// Fetch current session clearance metadata if available
$userRole = strtolower($_SESSION['role'] ?? 'Staff');
$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>Leave Types - 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">
            
            <!-- Structural Top Header Anchor Module -->
            <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">Leave Configurations</h1>
                        <p class="text-[10px] text-indigo-700 font-bold uppercase tracking-wider">System Operations Framework</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 Workspace Canvas Area -->
            <main class="flex-1 p-4 sm:p-6 max-w-7xl w-full mx-auto space-y-6">
                
                <!-- Settings Input/Modification Matrix Card -->
                <div class="bg-white border border-slate-300/80 rounded-xl p-4 shadow-sm space-y-4">
                    <div class="border-b border-slate-100 pb-2">
                        <p class="text-[9px] font-black text-indigo-700 uppercase tracking-widest">Parameters</p>
                        <h3 class="text-xs font-black text-slate-950 uppercase tracking-tight mt-0.5">
                            <?= $editMode ? 'Edit Corporate Leave Category' : 'Add Corporate Leave Category' ?>
                        </h3>
                    </div>

                    <form method="POST" action="leave_types.php<?= $editMode ? '?edit=' . $editData['id'] : '' ?>" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-3">
                        <?php if ($editMode): ?>
                            <input type="hidden" name="id" value="<?= htmlspecialchars($editData['id']) ?>">
                        <?php endif; ?>

                        <div class="relative">
                            <input
                                type="text"
                                name="name"
                                value="<?= $editMode ? htmlspecialchars($editData['name']) : '' ?>"
                                placeholder="Leave Type (e.g., Annual, Maternity)"
                                required
                                class="w-full border border-slate-300 bg-stone-50 rounded-lg px-3 py-1.5 focus:ring-2 focus:ring-indigo-100 focus:border-indigo-500 outline-none text-xs text-slate-800 font-bold placeholder:text-slate-400">
                        </div>

                        <div class="relative">
                            <input
                                type="number"
                                name="days_allowed"
                                value="<?= $editMode ? intval($editData['days_allowed']) : '' ?>"
                                placeholder="Baseline Days Allowed"
                                required
                                class="w-full border border-slate-300 bg-stone-50 rounded-lg px-3 py-1.5 focus:ring-2 focus:ring-indigo-100 focus:border-indigo-500 outline-none text-xs text-slate-800 font-bold placeholder:text-slate-400">
                        </div>

                        <div class="relative">
                            <select
                                name="is_paid"
                                class="w-full border border-slate-300 bg-stone-50 rounded-lg px-3 py-1.5 focus:ring-2 focus:ring-indigo-100 focus:border-indigo-500 outline-none text-xs text-slate-800 font-bold appearance-none cursor-pointer">
                                <option value="1" <?= ($editMode && $editData['is_paid'] == 1) ? 'selected' : '' ?>>Paid Leave Entitlement</option>
                                <option value="0" <?= ($editMode && $editData['is_paid'] == 0) ? 'selected' : '' ?>>Unpaid Leave Structure</option>
                            </select>
                        </div>

                        <div class="flex gap-2">
                            <button type="submit" class="flex-1 text-center <?= $editMode ? 'bg-amber-600 border-amber-700 hover:bg-amber-700' : 'bg-slate-900 border-slate-950 hover:bg-slate-800' ?> active:scale-[0.99] transition text-white rounded-lg font-bold py-1.5 px-4 text-xs border uppercase tracking-wider whitespace-nowrap">
                                <?= $editMode ? 'Update Type' : 'Add Leave Type' ?>
                            </button>
                            <?php if ($editMode): ?>
                                <a href="leave_types.php" class="bg-slate-100 hover:bg-slate-200 border border-slate-300 text-slate-700 font-bold px-3 py-1.5 rounded-lg transition text-center text-xs uppercase tracking-wider flex items-center justify-center">
                                    Cancel
                                </a>
                            <?php endif; ?>
                        </div>
                    </form>
                </div>

                <!-- Matrix Section Label -->
                <div class="px-1">
                    <h3 class="text-[10px] font-black text-slate-500 uppercase tracking-wider">Active System Parameters</h3>
                </div>

                <!-- Balanced Ledger Metrics Grid Table Portfolio -->
                <div class="bg-white border border-slate-300/80 rounded-xl shadow-sm overflow-hidden">
                    <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">Leave Classification</th>
                                    <th class="p-4 text-center">Baseline Entitlement</th>
                                    <th class="p-4 text-center">Compensation Structure</th>
                                    <th class="p-4 text-right">Actions Matrix</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-slate-200/80 text-xs text-slate-700">
                                <?php if (!empty($types)): ?>
                                    <?php foreach($types as $type): ?>
                                        <tr class="hover:bg-stone-50/60 transition duration-150">
                                            <td class="p-4 font-black text-slate-900"><?= htmlspecialchars($type['name']) ?></td>
                                            <td class="p-4 text-center">
                                                <span class="bg-indigo-50 border border-indigo-100 text-indigo-800 font-black px-2.5 py-1 rounded-md text-[11px]">
                                                    <?= intval($type['days_allowed']) ?> <span class="font-normal text-slate-400 lowercase text-[10px]">days</span>
                                                </span>
                                            </td>
                                            <td class="p-4 text-center">
                                                <span class="inline-block px-2 py-0.5 rounded font-black text-[10px] uppercase border tracking-wide <?= $type['is_paid'] ? 'bg-emerald-50 text-emerald-800 border-emerald-200' : 'bg-rose-50 text-rose-800 border-rose-200' ?>">
                                                    <?= $type['is_paid'] ? '✓ Paid' : '✕ Unpaid' ?>
                                                </span>
                                            </td>
                                            <td class="p-4 text-right space-x-1 whitespace-nowrap">
                                                <a href="leave_types.php?edit=<?= $type['id'] ?>" class="inline-block text-[10px] bg-white hover:bg-amber-50 text-amber-700 font-extrabold px-2.5 py-1 rounded border border-slate-200 hover:border-amber-300 transition uppercase tracking-wider">
                                                    Edit
                                                </a>
                                                <a href="leave_types.php?delete=<?= $type['id'] ?>" onclick="return confirm('Are you sure you want to permanently delete this leave type?');" class="inline-block text-[10px] bg-white hover:bg-rose-50 text-rose-700 font-extrabold px-2.5 py-1 rounded border border-slate-200 hover:border-rose-300 transition uppercase tracking-wider">
                                                    Delete
                                                </a>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="4" class="p-8 text-center text-xs text-slate-400 font-bold uppercase tracking-wider bg-white">No system configurations populated.</td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

            </main>

            <!-- Footer Module Architecture -->
            <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>

    <!-- Interface Controller Engine Blocks -->
    <script src="../components/sidebar.js"></script>
    <script>
    // Safeguard to prevent dual overlay triggering if sidebar.js has missing elements
    if (typeof toggleSidebar === 'undefined') {
        window.toggleSidebar = function() {
            const sidebar = document.getElementById('sidebar');
            const overlay = document.getElementById('sidebar-overlay');
            if (sidebar && overlay) {
                sidebar.classList.toggle('-translate-x-full');
                overlay.classList.toggle('hidden');
            }
        };
    }
    </script>
</body>
</html>