<?php
session_start();
include 'config.php';

if (!isset($_SESSION['username']) || ($_SESSION['role'] != 'Admin' && $_SESSION['role'] != 'Super Admin')) {
    header("Location: index.php");
    exit();
}

// Fetch performance data
$sql = "SELECT pr.id, e.username AS employee, pr.review_period, pr.kpi_score, pr.okr_score, pr.kra_score, pr.total_score, u.username AS reviewer, pr.review_date 
        FROM performance_reviews pr
        JOIN employees e ON pr.employee_id = e.id
        JOIN users u ON pr.reviewer_id = u.id
        ORDER BY pr.review_date DESC";

$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Performance Reviews</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
    <h2>Performance Reviews</h2>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Employee</th>
                <th>Review Period</th>
                <th>KPI Score</th>
                <th>OKR Score</th>
                <th>KRA Score</th>
                <th>Total Score</th>
                <th>Reviewer</th>
                <th>Review Date</th>
            </tr>
        </thead>
        <tbody>
            <?php while ($row = $result->fetch_assoc()) { ?>
                <tr>
                    <td><?= $row['employee'] ?></td>
                    <td><?= $row['review_period'] ?></td>
                    <td><?= $row['kpi_score'] ?></td>
                    <td><?= $row['okr_score'] ?></td>
                    <td><?= $row['kra_score'] ?></td>
                    <td><strong><?= number_format($row['total_score'], 2) ?></strong></td>
                    <td><?= $row['reviewer'] ?></td>
                    <td><?= $row['review_date'] ?></td>
                </tr>
            <?php } ?>
        </tbody>
    </table>
</div>
</body>
</html>
