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

payrollRequireAdmin($auth);

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

payrollModuleStyles();
?>

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

    <div class="payroll-page-head">
        <div>
            <h1>Salary Grades</h1>
            <p>Define salary bands, deductions, and allowance percentages used by payroll.</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="add_pay_grade_form.php" class="btn btn-primary"><i class="fas fa-plus"></i> Add Grade</a>
        </div>
    </div>

    <div class="payroll-metrics">
        <div class="payroll-metric"><strong>Salary Grades</strong><span><?php echo number_format($summary['salary_grades']); ?></span></div>
        <div class="payroll-metric"><strong>Employee Payroll</strong><span><?php echo payrollMoney($summary['monthly_payroll']); ?></span></div>
    </div>

    <div class="payroll-panel">
        <div class="payroll-panel-title">
            <h2>Grade Structure</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 table-hover payroll-table">
                <thead>
                    <tr>
                        <th>Grade</th>
                        <th>Basic Salary</th>
                        <th>Pension</th>
                        <th>Health Insurance</th>
                        <th>House Allowance</th>
                        <th>Other</th>
                        <th class="no-print">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($grades)): ?>
                        <?php foreach ($grades as $grade): ?>
                            <?php $basic = (float)($grade['basic_salary'] ?? 0); ?>
                            <tr>
                                <td><strong><?php echo payrollText($grade['grade_title'] ?? ''); ?></strong></td>
                                <td><?php echo payrollMoney($basic); ?></td>
                                <td><?php echo payrollMoney(((float)$grade['pension'] / 100) * $basic); ?> <small class="text-muted">(<?php echo payrollText($grade['pension']); ?>%)</small></td>
                                <td><?php echo payrollMoney(((float)$grade['health_insurance'] / 100) * $basic); ?> <small class="text-muted">(<?php echo payrollText($grade['health_insurance']); ?>%)</small></td>
                                <td><?php echo payrollMoney(((float)$grade['house_allowance'] / 100) * $basic); ?> <small class="text-muted">(<?php echo payrollText($grade['house_allowance']); ?>%)</small></td>
                                <td><?php echo payrollMoney(((float)$grade['other'] / 100) * $basic); ?> <small class="text-muted">(<?php echo payrollText($grade['other']); ?>%)</small></td>
                                <td class="no-print">
                                    <a href="edit_pay_grade.php?id=<?php echo (int)$grade['grade_id']; ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-pen"></i> Edit</a>
                                    <a href="delete_grade.php?id=<?php echo (int)$grade['grade_id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete this salary grade?');"><i class="fas fa-trash"></i> Delete</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr><td colspan="7" class="text-center">No salary grades found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

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

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