<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/iscms_layout.php';

$conn = iscms_init();
$pageTitle = 'Fee schedules';
$activeNav = 'fee_schedules';

$sql = "SELECT id, service_type, application_type, fee_amount, currency, valid_from, valid_to, is_active, created_at
        FROM fee_schedules
        ORDER BY valid_from DESC, service_type ASC
        LIMIT 300";
$result = $conn->query($sql);
$rows = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$dbError = $result ? null : $conn->error;

iscms_render_head($pageTitle);
iscms_render_sidebar($activeNav);
iscms_render_topbar($pageTitle);
?>
    <div class="page-header">
        <h1>Fee schedules</h1>
        <p>Service fees by application type and effective period.</p>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-money-dollar-circle-line"></i> Schedules</h2>
            <span class="text-muted" style="font-size:.75rem"><?= count($rows) ?> shown</span>
        </div>
        <div class="table-responsive">
            <?php if ($dbError): ?>
                <div class="alert alert-danger m-3" style="font-size:.85rem">Unable to load data: <?= iscms_h($dbError) ?></div>
            <?php else: ?>
            <table class="ssbs-table">
                <thead>
                    <tr>
                        <th>Service</th>
                        <th>Application type</th>
                        <th>Amount</th>
                        <th>Valid from</th>
                        <th>Valid to</th>
                        <th>Active</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="6"><i class="ri-inbox-line me-2"></i>No fee schedules</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <tr>
                            <td><small><?= iscms_h($r['service_type']) ?></small></td>
                            <td class="iscms-mono"><?= iscms_h(str_replace('_', ' ', (string) ($r['application_type'] ?? ''))) ?></td>
                            <td style="font-weight:600"><?= iscms_h((string) ($r['currency'] ?? 'KES')) ?> <?= iscms_h(number_format((float) ($r['fee_amount'] ?? 0), 2)) ?></td>
                            <td><small><?= iscms_val($r['valid_from'] ?? null) ?></small></td>
                            <td><small><?= iscms_val($r['valid_to'] ?? null) ?></small></td>
                            <td><?= !empty($r['is_active']) ? '<span class="badge bg-success-subtle text-success" style="font-size:.68rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.68rem">No</span>' ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
