<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

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

// 1. Enforce Authentication Guard
$userId = $_SESSION['user_id'] ?? null;
if (!$userId) {
    // If not logged in, gracefully inform or kill process before rendering HTML
    die("CRITICAL ACCESS ERROR: Please log into the portal to view this profile.");
}

// 2. Fetch Last 10 MOD Submissions for History Ledger
$reports = [];
$sql = "SELECT id, report_date, manager_name, shift_timeline, fo_roll_call, created_at 
        FROM mod_reports 
        ORDER BY report_date DESC, id DESC 
        LIMIT 10";

if ($result = $conn->query($sql)) {
    while ($row = $result->fetch_assoc()) {
        $reports[] = $row;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Management Dashboard Hub</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-50 min-h-screen text-slate-800 antialiased">

    <header class="bg-slate-950 text-white p-4 shadow-sm">
        <div class="max-w-6xl mx-auto flex justify-between items-center">
            <div>
                <h1 class="text-sm font-black tracking-wider uppercase">BWP Operations Gateway</h1>
                <p class="text-[10px] text-slate-400 font-mono">Control Identity Panel</p>
            </div>
            <div class="flex items-center gap-4">
                <span class="text-xs bg-slate-800 px-3 py-1.5 rounded-lg font-mono text-slate-300 border border-slate-700">
                    UID: #<?php echo htmlspecialchars($userId); ?>
                </span>
            </div>
        </div>
    </header>

    <main class="max-w-6xl mx-auto p-4 sm:p-6 lg:p-8">
        
        <?php if (isset($_SESSION['success_flash'])): ?>
            <div class="bg-emerald-50 border border-emerald-200 text-emerald-800 p-4 rounded-xl mb-6 text-xs font-bold flex justify-between items-center shadow-sm">
                <span>🎉 <?php echo $_SESSION['success_flash']; unset($_SESSION['success_flash']); ?></span>
                <button onclick="this.parentElement.remove()" class="text-emerald-400 hover:text-emerald-600 font-bold">×</button>
            </div>
        <?php endif; ?>

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            
            <div class="space-y-4">
                <div class="bg-white p-6 rounded-2xl border border-slate-100 shadow-sm text-center">
                    <div class="w-16 h-16 bg-blue-50 text-blue-600 rounded-full flex items-center justify-center text-xl mx-auto mb-3 font-bold shadow-inner">
                        💼
                    </div>
                    <h2 class="text-base font-black text-slate-900 tracking-tight uppercase">Shift Manager Control</h2>
                    <p class="text-xs text-slate-400 mb-6 font-mono">Operational Command Role</p>
                    
                    <div class="space-y-2">
                        <a href="mod_checklist.html" class="block w-full bg-slate-900 hover:bg-slate-800 text-white font-bold py-3 px-4 rounded-xl text-xs uppercase tracking-widest transition shadow">
                            📝 New MOD Checklist Run
                        </a>
                        <a href="logout.php" class="block w-full bg-red-50 hover:bg-red-100 text-red-600 font-bold py-2.5 px-4 rounded-xl text-xs uppercase tracking-wider transition">
                            Sign Out of Session
                        </a>
                    </div>
                </div>
            </div>

            <div class="lg:col-span-2">
                <div class="bg-white rounded-2xl border border-slate-100 shadow-sm overflow-hidden">
                    <div class="bg-slate-900 text-white px-5 py-3.5 text-xs font-black uppercase tracking-wider flex justify-between items-center">
                        <span>📋 Historical MOD Logbook Index</span>
                        <span class="text-[10px] font-mono text-slate-400">Database Live Feed</span>
                    </div>
                    
                    <div class="p-2 overflow-x-auto">
                        <table class="w-full text-left text-xs">
                            <thead>
                                <tr class="text-slate-400 uppercase tracking-wider border-b border-slate-100">
                                    <th class="p-3 font-bold">Audit Date</th>
                                    <th class="p-3 font-bold">Manager Name</th>
                                    <th class="p-3 font-bold">Shift Frame</th>
                                    <th class="p-3 font-bold">Staff Count</th>
                                    <th class="p-3 text-right font-bold">Actions</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-slate-50 font-medium text-slate-700">
                                <?php if (empty($reports)): ?>
                                    <tr>
                                        <td colspan="5" class="p-8 text-center text-slate-400 font-mono">
                                            No tracking records identified inside the active schema ledger.
                                        </td>
                                    </tr>
                                <?php else: ?>
                                    <?php foreach ($reports as $report): ?>
                                        <tr class="hover:bg-slate-50/80 transition">
                                            <td class="p-3 font-mono font-bold text-slate-900">
                                                <?php echo htmlspecialchars($report['report_date']); ?>
                                            </td>
                                            <td class="p-3 font-semibold">
                                                <?php echo htmlspecialchars($report['manager_name']); ?>
                                            </td>
                                            <td class="p-3 text-slate-500 font-mono">
                                                <?php echo htmlspecialchars($report['shift_timeline']); ?>
                                            </td>
                                            <td class="p-3">
                                                <span class="bg-slate-100 text-slate-700 font-bold px-2 py-0.5 rounded text-[10px]">
                                                    <?php echo htmlspecialchars($report['fo_roll_call']); ?>
                                                </span>
                                            </td>
                                            <td class="p-3 text-right">
                                                <a href="view_report.php?id=<?php echo $report['id']; ?>" class="text-blue-600 hover:text-blue-800 font-bold underline">
                                                    Inspect
                                                </a>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        </div>
    </main>

</body>
</html>