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

payrollRequireAdmin($auth);

$payroll = new Payroll($conn);
$leaveRequests = $payroll->getLeaveRequests();

payrollModuleStyles();
?>

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

    <div class="payroll-page-head">
        <div>
            <h1>Leave Management</h1>
            <p>Track annual leave, sick leave, unpaid leave, approvals, and balances.</p>
        </div>
        <a href="../dashboard/admin.php" class="btn btn-outline-secondary no-print"><i class="fas fa-arrow-left"></i> Admin Dashboard</a>
    </div>

    <?php payrollMigrationNotice($payroll, ['hr_leave_types', 'hr_leave_requests'], 'Leave balances and approvals require the professional HR schema.'); ?>

    <div class="payroll-panel">
        <div class="payroll-panel-title"><h2>Leave Requests</h2></div>
        <div class="table-responsive">
            <table class="table table-bordered payroll-table">
                <thead>
                    <tr>
                        <th>Employee</th>
                        <th>Leave Type</th>
                        <th>Dates</th>
                        <th>Days</th>
                        <th>Status</th>
                        <th>Reason</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($leaveRequests)): ?>
                        <?php foreach ($leaveRequests as $request): ?>
                            <tr>
                                <td><?php echo payrollText($request['employee_fullname'] ?? ''); ?></td>
                                <td><?php echo payrollText($request['leave_name'] ?? ''); ?></td>
                                <td><?php echo payrollText($request['start_date']); ?> to <?php echo payrollText($request['end_date']); ?></td>
                                <td><?php echo number_format((float)$request['total_days'], 2); ?></td>
                                <td><?php echo payrollStatusBadge($request['status'] ?? 'pending'); ?></td>
                                <td><?php echo payrollText($request['reason'] ?? ''); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr><td colspan="6" class="text-center">No leave requests found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

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

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