<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

finance_module_boot();

$paymentRecord = find_finance_payment_or_abort((int) ($_GET['id'] ?? 0));
$pageTitle = 'View Payment';
$balance = max(0, (float) $paymentRecord['amount'] - (float) $paymentRecord['amount_paid']);
$recordErrors = flash('errors') ?? [];

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>
            <p class="eyebrow mb-1">Finance record</p>
            <h2 class="h4 mb-0"><?php echo e($paymentRecord['payment_reference']); ?></h2>
        </div>
        <div class="d-flex gap-2">
            <?php if (can_manage_finance()): ?>
                <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/finance/edit.php?id=' . (int) $paymentRecord['id'])); ?>">Edit</a>
                <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/finance/delete.php?id=' . (int) $paymentRecord['id'])); ?>">Delete</a>
            <?php endif; ?>
        </div>
    </div>
    <div class="row g-3">
        <div class="col-md-3">
            <div class="feature-tile"><i class="bi bi-building"></i>
                <div>
                    <h4>Club</h4>
                    <p><?php echo e($paymentRecord['club_name']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="feature-tile"><i class="bi bi-calendar-range"></i>
                <div>
                    <h4>Season</h4>
                    <p><?php echo e($paymentRecord['season_name']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="feature-tile"><i class="bi bi-receipt"></i>
                <div>
                    <h4>Payment type</h4>
                    <p><?php echo e(payment_type_label((string) $paymentRecord['payment_type'])); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="feature-tile"><i class="bi bi-flag"></i>
                <div>
                    <h4>Status</h4>
                    <p><span class="badge <?php echo e(payment_status_badge_class((string) $paymentRecord['payment_status'])); ?>"><?php echo e(ucfirst((string) $paymentRecord['payment_status'])); ?></span></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-wallet2"></i>
                <div>
                    <h4>Amount due</h4>
                    <p><?php echo e(number_format((float) $paymentRecord['amount'], 2)); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-cash-stack"></i>
                <div>
                    <h4>Amount paid</h4>
                    <p><?php echo e(number_format((float) $paymentRecord['amount_paid'], 2)); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-exclamation-diamond"></i>
                <div>
                    <h4>Balance</h4>
                    <p><?php echo e(number_format($balance, 2)); ?></p>
                </div>
            </div>
        </div>
    </div>
</section>

<section class="panel-card mb-4">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Reference</p>
            <h3 class="h5 mb-0">Payment details</h3>
        </div>
    </div>
    <dl class="row mb-0">
        <dt class="col-sm-3">Reference</dt>
        <dd class="col-sm-9"><?php echo e($paymentRecord['payment_reference']); ?></dd>
        <dt class="col-sm-3">Payment date</dt>
        <dd class="col-sm-9"><?php echo e($paymentRecord['payment_date'] ?: 'Not recorded'); ?></dd>
        <dt class="col-sm-3">Created</dt>
        <dd class="col-sm-9"><?php echo e(date('d M Y H:i', strtotime((string) $paymentRecord['created_at']))); ?></dd>
    </dl>
</section>

<?php if (can_record_payments()): ?>
    <section class="panel-card">
        <div class="panel-card-header mb-3">
            <div>
                <p class="eyebrow mb-1">Collection</p>
                <h3 class="h5 mb-0">Record payment</h3>
            </div>
        </div>
        <form method="post" action="<?php echo e(base_url('app/modules/finance/actions.php')); ?>" novalidate>
            <?php echo csrf_input(); ?>
            <input type="hidden" name="action" value="record">
            <input type="hidden" name="id" value="<?php echo e((string) $paymentRecord['id']); ?>">
            <div class="row g-3">
                <div class="col-md-6">
                    <label class="form-label" for="amount_paid">Amount paid</label>
                    <input class="form-control <?php echo validation_error($recordErrors, 'amount_paid') ? 'is-invalid' : ''; ?>" id="amount_paid" name="amount_paid" type="number" min="0" max="<?php echo e((string) $paymentRecord['amount']); ?>" step="0.01" value="<?php echo e((string) old('amount_paid', (string) $paymentRecord['amount_paid'])); ?>" required>
                    <?php if (validation_error($recordErrors, 'amount_paid')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($recordErrors, 'amount_paid')); ?></div><?php endif; ?>
                </div>
                <div class="col-md-6">
                    <label class="form-label" for="payment_date">Payment date</label>
                    <input class="form-control <?php echo validation_error($recordErrors, 'payment_date') ? 'is-invalid' : ''; ?>" id="payment_date" name="payment_date" type="date" value="<?php echo e((string) old('payment_date', (string) ($paymentRecord['payment_date'] ?? ''))); ?>">
                    <?php if (validation_error($recordErrors, 'payment_date')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($recordErrors, 'payment_date')); ?></div><?php endif; ?>
                </div>
            </div>
            <button class="btn btn-jlfc mt-4" type="submit">Save payment update</button>
        </form>
    </section>
<?php endif; ?>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>