<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

$transferModuleBoot = 'transfer_module_boot';
$findTransferMarketPlayerOrAbort = 'find_transfer_market_player_or_abort';
$transferBuyerClubOptions = 'transfer_buyer_club_options';
$transferOpenWindow = 'transfer_open_window';

$transferModuleBoot();

$playerRecord = $findTransferMarketPlayerOrAbort((int) ($_GET['player_id'] ?? 0));
$buyerClubOptions = $transferBuyerClubOptions();
$openWindow = $transferOpenWindow();
$pageTitle = 'Register Transfer Interest';
$formErrors = 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">
    <div class="panel-card-header mb-3">
        <div>
            <h2 class="h4 mb-0">Register interest</h2>
        </div>
        <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/transfers/index.php')); ?>">Back to market</a>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-person"></i>
                <div>
                    <h4>Player</h4>
                    <p><?php echo e(trim($playerRecord['first_name'] . ' ' . $playerRecord['last_name'])); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-buildings"></i>
                <div>
                    <h4>Current club</h4>
                    <p><?php echo e($playerRecord['club_name']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-door-open"></i>
                <div>
                    <h4>Open window</h4>
                    <p><?php echo e($openWindow['name'] ?? 'No open window'); ?></p>
                </div>
            </div>
        </div>
    </div>

    <form method="post" action="<?php echo e(base_url('app/modules/transfers/actions.php')); ?>" novalidate>
        <?php echo csrf_input(); ?>
        <input type="hidden" name="action" value="register_interest">
        <input type="hidden" name="player_id" value="<?php echo e((string) $playerRecord['id']); ?>">

        <?php if (!is_club_admin()): ?>
            <div class="mb-3 col-md-6 px-0">
                <label class="form-label" for="buying_club_id">Buying club</label>
                <select class="form-select <?php echo validation_error($formErrors, 'buying_club_id') ? 'is-invalid' : ''; ?>" id="buying_club_id" name="buying_club_id" required>
                    <option value="">Select buying club</option>
                    <?php foreach ($buyerClubOptions as $club): ?>
                        <option value="<?php echo e((string) $club['id']); ?>" <?php echo (string) old('buying_club_id') === (string) $club['id'] ? 'selected' : ''; ?>><?php echo e($club['name']); ?></option>
                    <?php endforeach; ?>
                </select>
                <?php if (validation_error($formErrors, 'buying_club_id')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'buying_club_id')); ?></div><?php endif; ?>
            </div>
        <?php endif; ?>

        <div class="mb-3">
            <label class="form-label" for="interest_message">Interest note</label>
            <textarea class="form-control <?php echo validation_error($formErrors, 'interest_message') ? 'is-invalid' : ''; ?>" id="interest_message" name="interest_message" rows="5" placeholder="Optional note to appear in the transfer timeline and internal workflow."><?php echo e((string) old('interest_message')); ?></textarea>
            <?php if (validation_error($formErrors, 'interest_message')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'interest_message')); ?></div><?php endif; ?>
        </div>

        <?php if ($openWindow === null): ?>
            <div class="alert alert-warning">An open transfer window is required before interest can be registered.</div>
        <?php endif; ?>

        <div class="d-flex gap-2 mt-4">
            <button class="btn btn-jlfc" type="submit" <?php echo $openWindow === null ? 'disabled' : ''; ?>>Register interest</button>
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/transfers/index.php')); ?>">Cancel</a>
        </div>
    </form>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>