<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

finance_module_boot();

$pageTitle = 'Finance';
$clubFilterId = (int) ($_GET['club_id'] ?? 0);
$filteredClub = $clubFilterId > 0 ? get_club_by_id($clubFilterId) : null;
$payments = finance_payment_list($clubFilterId > 0 ? $clubFilterId : null);
$summary = finance_summary_cards($clubFilterId > 0 ? $clubFilterId : null);

require dirname(__DIR__, 2) . '/includes/header.php';
require dirname(__DIR__, 2) . '/includes/sidebar.php';
require dirname(__DIR__, 2) . '/includes/topbar.php';
require dirname(__DIR__, 2) . '/includes/flash.php';
?>
<section class="panel-card mb-4">
    <div class="panel-card-header mb-3">
        <div>
            <h2 class="h4 mb-0">Club payments</h2>
            <?php if ($filteredClub): ?><div class="small text-muted mt-1">Filtered for <?php echo e($filteredClub['name']); ?></div><?php endif; ?>
        </div>
        <div class="d-flex gap-2">
            <?php if ($filteredClub): ?>
                <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/finance/index.php')); ?>">Clear filter</a>
            <?php endif; ?>
            <?php if (can_manage_finance()): ?>
                <a class="btn btn-jlfc" href="<?php echo e(base_url('app/modules/finance/create.php')); ?>"><i class="bi bi-cash-coin"></i> Add payment</a>
            <?php endif; ?>
        </div>
    </div>
    <div class="row g-3">
        <div class="col-md-3">
            <article class="metric-card h-100">
                <div class="metric-icon"><i class="bi bi-wallet2"></i></div>
                <span class="metric-label">Total due</span>
                <strong class="metric-value"><?php echo e(number_format((float) $summary['total_due'], 2)); ?></strong>
                <small class="text-muted">All billed charges</small>
            </article>
        </div>
        <div class="col-md-3">
            <article class="metric-card h-100">
                <div class="metric-icon"><i class="bi bi-cash-stack"></i></div>
                <span class="metric-label">Total paid</span>
                <strong class="metric-value"><?php echo e(number_format((float) $summary['total_paid'], 2)); ?></strong>
                <small class="text-muted">Collected payments</small>
            </article>
        </div>
        <div class="col-md-3">
            <article class="metric-card h-100">
                <div class="metric-icon"><i class="bi bi-exclamation-diamond"></i></div>
                <span class="metric-label">Outstanding</span>
                <strong class="metric-value"><?php echo e(number_format((float) $summary['outstanding'], 2)); ?></strong>
                <small class="text-muted">Balance remaining</small>
            </article>
        </div>
        <div class="col-md-3">
            <article class="metric-card h-100">
                <div class="metric-icon"><i class="bi bi-receipt"></i></div>
                <span class="metric-label">Open records</span>
                <strong class="metric-value"><?php echo e((string) $summary['unpaid_count']); ?></strong>
                <small class="text-muted">Not fully settled</small>
            </article>
        </div>
    </div>
</section>

<section class="panel-card">
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Club</th>
                    <th>Season</th>
                    <th>Type</th>
                    <th>Reference</th>
                    <th>Amounts</th>
                    <th>Status</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($payments === []): ?>
                    <tr>
                        <td colspan="7" class="text-center text-muted py-4">No club payment records found.</td>
                    </tr>
                <?php endif; ?>
                <?php foreach ($payments as $payment): ?>
                    <tr>
                        <td>
                            <strong><?php echo e($payment['club_name']); ?></strong>
                            <div class="small text-muted">Created <?php echo e(date('d M Y', strtotime((string) $payment['created_at']))); ?></div>
                        </td>
                        <td>
                            <?php echo e($payment['season_name']); ?>
                            <div class="small text-muted"><?php echo e(ucfirst((string) $payment['season_status'])); ?></div>
                        </td>
                        <td><?php echo e(payment_type_label((string) $payment['payment_type'])); ?></td>
                        <td><?php echo e($payment['payment_reference']); ?></td>
                        <td>
                            <div class="small">Due: <?php echo e(number_format((float) $payment['amount'], 2)); ?></div>
                            <div class="small">Paid: <?php echo e(number_format((float) $payment['amount_paid'], 2)); ?></div>
                        </td>
                        <td><span class="badge <?php echo e(payment_status_badge_class((string) $payment['payment_status'])); ?>"><?php echo e(ucfirst((string) $payment['payment_status'])); ?></span></td>
                        <td class="text-end">
                            <div class="btn-group btn-group-sm">
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/finance/view.php?id=' . (int) $payment['id'])); ?>">View</a>
                                <?php if (can_manage_finance()): ?>
                                    <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/finance/edit.php?id=' . (int) $payment['id'])); ?>">Edit</a>
                                    <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/finance/delete.php?id=' . (int) $payment['id'])); ?>">Delete</a>
                                <?php endif; ?>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>