<?php
session_start();
require_once __DIR__ . '/db/config.php';

if (!isset($_SESSION['user_id']) || !isset($_SESSION['company_id']) || $_SESSION['role'] !== 'admin') {
    header('Location: /login.php');
    exit;
}

$company_id = $_SESSION['company_id'];

// Fetch payroll runs for the company
$stmt = db()->prepare("SELECT * FROM payroll_runs WHERE company_id = ? ORDER BY pay_period_year DESC, pay_period_month DESC");
$stmt->execute([$company_id]);
$payroll_runs = $stmt->fetchAll();

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payroll History - GPTPayroll</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 font-sans leading-normal tracking-normal">
    <div class="flex md:flex-row-reverse flex-wrap">

        <!-- Main Content -->
        <div class="w-full md:w-4/5 bg-gray-100">
            <div class="container bg-gray-100 pt-16 px-6 mx-auto">
                <div class="bg-white shadow-md rounded p-8">
                    <h1 class="text-2xl font-bold text-gray-800 mb-6">Payroll Run History</h1>
                    <div class="overflow-x-auto">
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Pay Period</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Run Date</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">Actions</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                <?php if (empty($payroll_runs)): ?>
                                    <tr>
                                        <td colspan="4" class="px-6 py-4 text-center text-sm text-gray-500">No payroll history found.</td>
                                    </tr>
                                <?php else: ?>
                                    <?php foreach ($payroll_runs as $run): ?>
                                        <tr>
                                            <td class="px-6 py-4 text-sm font-medium text-gray-900"><?= htmlspecialchars(date('F Y', mktime(0,0,0, (int)$run['pay_period_month'], 1, (int)$run['pay_period_year']))) ?></td>
                                            <td class="px-6 py-4 text-sm text-gray-500"><span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800"><?= htmlspecialchars(ucfirst($run['status'])) ?></span></td>
                                            <td class="px-6 py-4 text-sm text-gray-500"><?= htmlspecialchars(date('d M Y, H:i', strtotime($run['run_date']))) ?></td>
                                            <td class="px-6 py-4 text-right text-sm font-medium">
                                                <a href="/payslips.php?run_id=<?= $run['id'] ?>" class="text-indigo-600 hover:text-indigo-900">View Payslips</a>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Sidebar -->
        <div class="w-full md:w-1/5 bg-gray-800 md:min-h-screen">
            <div class="md:relative mx-auto lg:float-right lg:px-6">
                <ul class="list-reset flex flex-row md:flex-col text-center md:text-left">
                    <li class="mr-3 flex-1"><a href="/dashboard.php" class="block py-4 px-4 text-gray-400 hover:text-white no-underline">Dashboard</a></li>
                    <li class="mr-3 flex-1"><a href="/employees.php" class="block py-4 px-4 text-gray-400 hover:text-white no-underline">Employees</a></li>
                    <li class="mr-3 flex-1"><a href="/payroll.php" class="block py-4 px-4 text-gray-400 hover:text-white no-underline">Payroll</a></li>
                    <li class="mr-3 flex-1"><a href="/payroll_history.php" class="block py-4 px-4 text-white font-bold no-underline">Payroll History</a></li>
                    <li class="mr-3 flex-1"><a href="/settings.php" class="block py-4 px-4 text-gray-400 hover:text-white no-underline">Settings</a></li>
                    <li class="mr-3 flex-1"><a href="/logout.php" class="block py-4 px-4 text-gray-400 hover:text-white no-underline">Logout</a></li>
                </ul>
            </div>
        </div>

    </div>
</body>
</html>
