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

if(!isset($_SESSION['user_id'])){
    header("Location: ../login.php");
    exit;
}

$role = strtolower($_SESSION['role'] ?? '');
$username = $_SESSION['username'] ?? 'Executive';

$employeeId = isset($_GET['employee_id']) ? intval($_GET['employee_id']) : 0;

// Gather target history list
$res = $conn->query("
SELECT
    ea.*,
    ap.title AS period_name
FROM employee_appraisals ea
LEFT JOIN appraisal_periods ap ON ap.id = ea.period_id
WHERE ea.employee_id = $employeeId
ORDER BY ea.id DESC
");

// Fetch employee metadata if present to personalize layout heading frame
$empMeta = $conn->query("SELECT name FROM employees WHERE id = $employeeId")->fetch_assoc();
$employeeName = $empMeta['name'] ?? 'Personnel Registry Record';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Employee Appraisal History - Executive HR Boardroom</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="../components/sidebar.css">
</head>
<body class="bg-gradient-to-br from-slate-100 via-blue-50 to-indigo-100 min-h-screen flex flex-col md:flex-row overflow-x-hidden font-sans">

<?php include '../components/sidebar.php'; ?>

<div id="sidebar-overlay" class="fixed inset-0 bg-black/50 hidden z-40" onclick="toggleSidebar()"></div>

<!-- Responsive Mobile Navigation Bar -->
<div class="mobile-trigger-bar w-full bg-slate-900 text-white flex items-center justify-between p-4 md:hidden sticky top-0 z-30 shadow-md">
    <span class="font-bold tracking-wide">HR History</span>
    <button onclick="toggleSidebar()" class="bg-white/10 hover:bg-white/20 px-3 py-2 rounded-lg text-xl transition">
        ☰
    </button>
</div>

<!-- Main Content Area Wrapper -->
<div id="main-content" class="flex-1 content-expanded transition-all duration-300 w-full p-4 sm:p-6 lg:p-8 space-y-6 sm:space-y-8">

    <!-- Top Profile Header Frame -->
    <div class="bg-white/70 backdrop-blur-xl border border-white/40 p-5 rounded-3xl shadow-sm flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
        <div>
            <h1 class="text-2xl sm:text-3xl font-black text-gray-800">Appraisal History</h1>
            <p class="text-xs sm:text-sm text-gray-500 mt-1">
                Historical Lifecycle Ledger for <span class="font-extrabold text-blue-600"><?= htmlspecialchars($employeeName); ?></span>
            </p>
        </div>
        <div class="bg-slate-900 text-white px-4 py-2 rounded-xl font-bold text-xs sm:text-sm shadow-sm text-center">
            Signed in: <span class="font-black text-slate-300 uppercase"><?= htmlspecialchars($username); ?></span>
        </div>
    </div>

    <!-- Historical Ledger Table Block -->
    <div class="space-y-3">
        <h2 class="text-xs font-bold text-slate-500 uppercase tracking-widest px-1">Evaluation History Matrix</h2>
        <div class="bg-white rounded-3xl shadow-md border border-white/60 overflow-hidden">
            <div class="overflow-x-auto w-full">
                <table class="w-full text-left border-collapse whitespace-nowrap">
                    <thead>
                        <tr class="bg-slate-50 border-b border-slate-100 text-slate-400 text-[11px] font-bold uppercase tracking-wider">
                            <th class="p-4 sm:p-5">Evaluation Period Cycle</th>
                            <th class="p-4 sm:p-5 text-center">Self Rating</th>
                            <th class="p-4 sm:p-5 text-center">Manager Rating</th>
                            <th class="p-4 sm:p-5 text-center">HR Benchmarking</th>
                            <th class="p-4 sm:p-5 text-center">Status</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100 text-sm text-gray-700">
                        <?php if($res->num_rows == 0): ?>
                        <tr>
                            <td colspan="5" class="p-8 text-center text-gray-400 font-medium">
                                No historical validation cycles logged for this personnel profile.
                            </td>
                        </tr>
                        <?php else: ?>
                            <?php while($row = $res->fetch_assoc()): ?>
                            <tr class="hover:bg-slate-50/50 transition-colors">
                                <td class="p-4 sm:p-5 font-bold text-gray-800">
                                    <?= htmlspecialchars($row['period_name'] ?? 'Legacy Cycle Archive') ?>
                                </td>
                                <td class="p-4 sm:p-5 text-center">
                                    <span class="inline-block bg-slate-100 text-slate-700 px-3 py-1 rounded-lg font-mono text-xs font-bold">
                                        <?= number_format($row['self_score'] ?? 0.00, 2) ?>
                                    </span>
                                </td>
                                <td class="p-4 sm:p-5 text-center">
                                    <span class="inline-block bg-blue-50 text-blue-700 px-3 py-1 rounded-lg font-mono text-xs font-bold">
                                        <?= number_format($row['manager_score'] ?? 0.00, 2) ?>
                                    </span>
                                </td>
                                <td class="p-4 sm:p-5 text-center">
                                    <span class="inline-block bg-indigo-50 text-indigo-700 px-3 py-1 rounded-lg font-mono text-xs font-black">
                                        <?= number_format($row['hr_score'] ?? 0.00, 2) ?>
                                    </span>
                                </td>
                                <td class="p-4 sm:p-5 text-center">
                                    <?php 
                                    $status = htmlspecialchars($row['status'] ?? 'Draft');
                                    if($status == 'Completed'): 
                                    ?>
                                        <span class="inline-block bg-emerald-100 text-emerald-800 text-[10px] font-black px-2.5 py-1 rounded-md uppercase tracking-wide">
                                            Completed
                                        </span>
                                    <?php else: ?>
                                        <span class="inline-block bg-amber-100 text-amber-800 text-[10px] font-black px-2.5 py-1 rounded-md uppercase tracking-wide">
                                            <?= $status ?>
                                        </span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endwhile; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</div>

<script src="../components/sidebar.js"></script>
</body>
</html>