<?php

$paymentRecord = $paymentRecord ?? null;
$formErrors = $formErrors ?? [];
$clubOptions = club_options();
$seasonOptions = season_list();
$fieldValue = static function (string $field, mixed $default = '') use ($paymentRecord) {
    if (isset($_SESSION['_old']) && array_key_exists($field, $_SESSION['_old'])) {
        return old($field, $default);
    }

    return $paymentRecord[$field] ?? $default;
};
?>
<div class="row g-3">
    <div class="col-md-6">
        <label class="form-label" for="club_id">Club</label>
        <select class="form-select <?php echo validation_error($formErrors, 'club_id') ? 'is-invalid' : ''; ?>" id="club_id" name="club_id" required>
            <option value="">Select club</option>
            <?php foreach ($clubOptions as $club): ?>
                <option value="<?php echo e((string) $club['id']); ?>" <?php echo (int) $fieldValue('club_id') === (int) $club['id'] ? 'selected' : ''; ?>><?php echo e($club['name']); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'club_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'club_id')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-6">
        <label class="form-label" for="season_id">Season</label>
        <select class="form-select <?php echo validation_error($formErrors, 'season_id') ? 'is-invalid' : ''; ?>" id="season_id" name="season_id" required>
            <option value="">Select season</option>
            <?php foreach ($seasonOptions as $season): ?>
                <option value="<?php echo e((string) $season['id']); ?>" <?php echo (int) $fieldValue('season_id') === (int) $season['id'] ? 'selected' : ''; ?>><?php echo e($season['name']); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'season_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'season_id')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="payment_type">Payment type</label>
        <select class="form-select <?php echo validation_error($formErrors, 'payment_type') ? 'is-invalid' : ''; ?>" id="payment_type" name="payment_type" required>
            <?php foreach (payment_type_options() as $paymentType): ?>
                <option value="<?php echo e($paymentType); ?>" <?php echo (string) $fieldValue('payment_type', 'registration') === $paymentType ? 'selected' : ''; ?>><?php echo e(payment_type_label($paymentType)); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'payment_type')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'payment_type')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="amount">Amount due</label>
        <input class="form-control <?php echo validation_error($formErrors, 'amount') ? 'is-invalid' : ''; ?>" id="amount" name="amount" type="number" min="0.01" step="0.01" value="<?php echo e((string) $fieldValue('amount')); ?>" required>
        <?php if (validation_error($formErrors, 'amount')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'amount')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="amount_paid">Amount paid</label>
        <input class="form-control <?php echo validation_error($formErrors, 'amount_paid') ? 'is-invalid' : ''; ?>" id="amount_paid" name="amount_paid" type="number" min="0" step="0.01" value="<?php echo e((string) $fieldValue('amount_paid', '0.00')); ?>">
        <?php if (validation_error($formErrors, 'amount_paid')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'amount_paid')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-6">
        <label class="form-label" for="payment_reference">Payment reference</label>
        <input class="form-control <?php echo validation_error($formErrors, 'payment_reference') ? 'is-invalid' : ''; ?>" id="payment_reference" name="payment_reference" type="text" maxlength="100" value="<?php echo e((string) $fieldValue('payment_reference')); ?>" required>
        <?php if (validation_error($formErrors, 'payment_reference')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'payment_reference')); ?></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($formErrors, 'payment_date') ? 'is-invalid' : ''; ?>" id="payment_date" name="payment_date" type="date" value="<?php echo e((string) $fieldValue('payment_date')); ?>">
        <?php if (validation_error($formErrors, 'payment_date')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'payment_date')); ?></div><?php endif; ?>
    </div>
</div>