<?php
include '../../includes/header.php';
require_once __DIR__ . '/_module.php';

payrollRequireAdmin($auth);

$payroll = new Payroll($conn);
$grades = $payroll->getSalaryGrade();

payrollModuleStyles();
?>

<div class="container-fluid px-4">
    <?php payrollModuleNav('pension'); ?>

    <div class="payroll-page-head">
        <div>
            <h1>Pension & Gratuities</h1>
            <p>Review pension deductions and benefits defined in salary grades.</p>
        </div>
        <div class="no-print">
            <a href="../dashboard/admin.php" class="btn btn-outline-secondary"><i class="fas fa-arrow-left"></i> Admin Dashboard</a>
            <a href="salaries.php" class="btn btn-primary"><i class="fas fa-money-check-alt"></i> Salary Grades</a>
        </div>
    </div>

    <div class="payroll-panel">
        <div class="payroll-panel-title">
            <h2>Pension by Salary Grade</h2>
            <button type="button" class="btn btn-outline-secondary btn-sm no-print" onclick="window.print()"><i class="fas fa-print"></i> Print</button>
        </div>
        <div class="table-responsive">
            <table class="table table-bordered payroll-table">
                <thead>
                    <tr>
                        <th>Grade</th>
                        <th>Basic Salary</th>
                        <th>Pension Rate</th>
                        <th>Monthly Pension</th>
                        <th>Other Benefits</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($grades)): ?>
                        <?php foreach ($grades as $grade): ?>
                            <?php $basic = (float)($grade['basic_salary'] ?? 0); ?>
                            <tr>
                                <td><?php echo payrollText($grade['grade_title'] ?? ''); ?></td>
                                <td><?php echo payrollMoney($basic); ?></td>
                                <td><?php echo payrollText($grade['pension'] ?? 0); ?>%</td>
                                <td><?php echo payrollMoney(((float)$grade['pension'] / 100) * $basic); ?></td>
                                <td><?php echo payrollMoney(((float)$grade['other'] / 100) * $basic); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr><td colspan="5" class="text-center">No salary grades found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <?php payrollModuleEnd(); ?>
</div>

<?php include '../../includes/footer.php'; ?>
