<?php
$clubRecord = $clubRecord ?? null;
$formErrors = $formErrors ?? [];

$fieldValue = static function (string $field, mixed $default = '') use ($clubRecord) {
    if (isset($_SESSION['_old']) && array_key_exists($field, $_SESSION['_old'])) {
        return old($field, $default);
    }

    return $clubRecord[$field] ?? $default;
};
?>
<div class="row g-3">
    <div class="col-md-6">
        <label class="form-label" for="name">Club name</label>
        <input class="form-control <?php echo validation_error($formErrors, 'name') ? 'is-invalid' : ''; ?>" id="name" name="name" value="<?php echo e((string) $fieldValue('name')); ?>" required>
        <?php if (validation_error($formErrors, 'name')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'name')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-3">
        <label class="form-label" for="short_name">Short name</label>
        <input class="form-control <?php echo validation_error($formErrors, 'short_name') ? 'is-invalid' : ''; ?>" id="short_name" name="short_name" maxlength="25" value="<?php echo e((string) $fieldValue('short_name')); ?>" required>
        <?php if (validation_error($formErrors, 'short_name')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'short_name')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-3">
        <label class="form-label" for="founded_year">Founded year</label>
        <input class="form-control <?php echo validation_error($formErrors, 'founded_year') ? 'is-invalid' : ''; ?>" id="founded_year" name="founded_year" type="number" min="1800" max="<?php echo e(date('Y')); ?>" value="<?php echo e((string) $fieldValue('founded_year')); ?>">
        <?php if (validation_error($formErrors, 'founded_year')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'founded_year')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-6">
        <label class="form-label" for="stadium_name">Stadium name</label>
        <input class="form-control" id="stadium_name" name="stadium_name" value="<?php echo e((string) $fieldValue('stadium_name')); ?>">
    </div>
    <div class="col-md-6">
        <label class="form-label" for="contact_person">Contact person</label>
        <input class="form-control" id="contact_person" name="contact_person" value="<?php echo e((string) $fieldValue('contact_person')); ?>">
    </div>
    <div class="col-md-4">
        <label class="form-label" for="phone">Phone</label>
        <input class="form-control <?php echo validation_error($formErrors, 'phone') ? 'is-invalid' : ''; ?>" id="phone" name="phone" value="<?php echo e((string) $fieldValue('phone')); ?>">
        <?php if (validation_error($formErrors, 'phone')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'phone')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="email">Email</label>
        <input class="form-control <?php echo validation_error($formErrors, 'email') ? 'is-invalid' : ''; ?>" id="email" name="email" type="email" value="<?php echo e((string) $fieldValue('email')); ?>">
        <?php if (validation_error($formErrors, 'email')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'email')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-4">
        <label class="form-label" for="logo">Club logo</label>
        <input class="form-control" id="logo" name="logo" type="file" accept=".jpg,.jpeg,.png,.webp,.gif">
        <?php if ($clubRecord && !empty($clubRecord['logo'])): ?><div class="form-text">Uploading a new logo will replace the current one.</div><?php endif; ?>
    </div>
    <?php if (!is_club_admin()): ?>
        <div class="col-md-4">
            <label class="form-label" for="license_status">License status</label>
            <select class="form-select <?php echo validation_error($formErrors, 'license_status') ? 'is-invalid' : ''; ?>" id="license_status" name="license_status">
                <?php foreach (club_license_options() as $status): ?>
                    <option value="<?php echo e($status); ?>" <?php echo (string) $fieldValue('license_status', 'pending') === $status ? 'selected' : ''; ?>><?php echo e(ucfirst($status)); ?></option>
                <?php endforeach; ?>
            </select>
            <?php if (validation_error($formErrors, 'license_status')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'license_status')); ?></div><?php endif; ?>
        </div>
        <div class="col-md-4">
            <label class="form-label" for="payment_status">Payment status</label>
            <select class="form-select <?php echo validation_error($formErrors, 'payment_status') ? 'is-invalid' : ''; ?>" id="payment_status" name="payment_status">
                <?php foreach (club_payment_options() as $status): ?>
                    <option value="<?php echo e($status); ?>" <?php echo (string) $fieldValue('payment_status', 'unpaid') === $status ? 'selected' : ''; ?>><?php echo e(ucfirst($status)); ?></option>
                <?php endforeach; ?>
            </select>
            <?php if (validation_error($formErrors, 'payment_status')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'payment_status')); ?></div><?php endif; ?>
        </div>
        <div class="col-md-4">
            <label class="form-label" for="status">Club status</label>
            <select class="form-select <?php echo validation_error($formErrors, 'status') ? 'is-invalid' : ''; ?>" id="status" name="status">
                <?php foreach (club_status_options() as $status): ?>
                    <option value="<?php echo e($status); ?>" <?php echo (string) $fieldValue('status', 'active') === $status ? 'selected' : ''; ?>><?php echo e(ucfirst($status)); ?></option>
                <?php endforeach; ?>
            </select>
            <?php if (validation_error($formErrors, 'status')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'status')); ?></div><?php endif; ?>
        </div>
    <?php endif; ?>
</div>