<?php
/** @var array $stats */
/** @var array $advance */
/** @var array $leaves */
/** @var array $claims */

$modules = [
    [
        'title' => 'Advance Salary',
        'items' => $advance,
        'columns' => ['Nama', 'Jumlah', 'Status', 'Updated'],
        'row' => function ($item) {
            return [
                htmlspecialchars($item['nama']),
                'RM ' . number_format($item['jumlah'], 2),
                ucfirst($item['status']),
                htmlspecialchars($item['updated_at'] ?? $item['created_at']),
            ];
        },
    ],
    [
        'title' => 'Leave Requests',
        'items' => $leaves,
        'columns' => ['Nama', 'Tarikh', 'Jumlah Hari', 'Status'],
        'row' => function ($item) {
            return [
                htmlspecialchars($item['nama']),
                htmlspecialchars($item['tarikh_mula']) . ' → ' . htmlspecialchars($item['tarikh_akhir']),
                (int) $item['jumlah_hari'] . ' hari',
                ucfirst($item['status']),
            ];
        },
    ],
    [
        'title' => 'Invoice & Claims',
        'items' => $claims,
        'columns' => ['Nama', 'Amount', 'Status', 'Updated'],
        'row' => function ($item) {
            return [
                htmlspecialchars($item['nama_staff']),
                'RM ' . number_format($item['amount'], 2),
                ucfirst($item['status']),
                htmlspecialchars($item['updated_at'] ?? $item['created_at']),
            ];
        },
    ],
];
?>

<?php if (!empty($stats)): ?>
    <div class="row g-3 mb-4">
        <?php foreach ($stats as $stat): ?>
            <div class="col-12 col-md-4">
                <div class="card summary-card shadow-sm border-0">
                    <div class="card-body">
                        <div class="text-muted text-uppercase small mb-1"><?= htmlspecialchars($stat['label']) ?></div>
                        <div class="display-6 fw-semibold text-<?= htmlspecialchars($stat['variant']) ?>">
                            <?= number_format((int)$stat['value']) ?>
                        </div>
                        <?php if (!empty($stat['subtitle'])): ?>
                            <div class="text-muted small mt-1">
                                <?= htmlspecialchars($stat['subtitle']) ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

<div class="row gy-4">
    <?php foreach ($modules as $module): ?>
        <div class="col-12">
            <div class="card shadow-sm">
                <div class="card-body">
                    <h5><?= $module['title'] ?></h5>
                    <?php if (empty($module['items'])): ?>
                        <p class="text-muted">No records yet.</p>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-hover align-middle">
                                <thead>
                                <tr>
                                    <?php foreach ($module['columns'] as $column): ?>
                                        <th><?= $column ?></th>
                                    <?php endforeach; ?>
                                </tr>
                                </thead>
                                <tbody>
                                <?php foreach ($module['items'] as $item): ?>
                                    <tr>
                                        <?php foreach ($module['row']($item) as $index => $value): ?>
                                            <?php if ($module['columns'][$index] === 'Status'): ?>
                                                <?php
                                                $statusLower = strtolower($item['status']);
                                                if ($statusLower === 'approved') {
                                                    $color = 'success';
                                                } elseif ($statusLower === 'rejected') {
                                                    $color = 'danger';
                                                } else {
                                                    $color = 'secondary';
                                                }
                                                ?>
                                                <td><span class="badge bg-<?= $color ?>"><?= $value ?></span></td>
                                            <?php else: ?>
                                                <td><?= $value ?></td>
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                    </tr>
                                <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
</div>

