<div class="row gy-4">
    <div class="col-12">
        <h2 class="h4">Pending Requests</h2>
        <?php if (!empty($message)): ?>
            <div class="alert alert-success"><?= htmlspecialchars($message) ?></div>
        <?php endif; ?>
        <?php if (!empty($error)): ?>
            <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
        <?php endif; ?>
    </div>
    <?php
    $sections = [
        'advance' => ['title' => 'Advance Salary', 'data' => $advance],
        'leave' => ['title' => 'Leave Requests', 'data' => $leave],
        'claim' => ['title' => 'Invoice & Claims', 'data' => $claims],
    ];
    ?>
    <?php foreach ($sections as $type => $section): ?>
        <div class="col-12">
            <div class="card shadow-sm">
                <div class="card-body">
                    <h5 class="card-title"><?= $section['title'] ?></h5>
                    <?php if (empty($section['data'])): ?>
                        <p class="text-muted">No pending records.</p>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-striped align-middle">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Nama</th>
                                        <th>Amount / Info</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php foreach ($section['data'] as $item): ?>
                                    <tr>
                                        <td>#<?= $item['id'] ?></td>
                                        <td><?= htmlspecialchars($item['nama'] ?? $item['nama_staff']) ?></td>
                                        <td>
                                            <?php if ($type === 'advance'): ?>
                                                RM <?= number_format($item['jumlah'], 2) ?>
                                            <?php elseif ($type === 'leave'): ?>
                                                <?= htmlspecialchars($item['tarikh_mula']) ?> → <?= htmlspecialchars($item['tarikh_akhir']) ?>
                                            <?php else: ?>
                                                RM <?= number_format($item['amount'], 2) ?>
                                                <?php if (!empty($item['file_path'])): ?>
                                                    <br><a target="_blank" href="<?= $baseUrl ?>/uploads/claims/<?= htmlspecialchars($item['file_path']) ?>">Invoice</a>
                                                <?php endif; ?>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <form method="post" action="index.php?route=approver/pending" class="d-flex gap-2">
                                                <?= $csrf ?>
                                                <input type="hidden" name="type" value="<?= $type ?>">
                                                <input type="hidden" name="id" value="<?= $item['id'] ?>">
                                                <input type="text" name="remarks" class="form-control form-control-sm" placeholder="Remarks" required>
                                                <button class="btn btn-success btn-sm" name="action" value="approved">Approve</button>
                                                <button class="btn btn-danger btn-sm" name="action" value="rejected">Reject</button>
                                            </form>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
</div>

