<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

player_module_boot();

$pageTitle = 'Bulk Player Registration';
$formErrors = flash('errors') ?? [];
$bulkErrors = flash('bulk_errors') ?? [];
$clubOptions = player_club_options();

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">Players</p>
            <h2 class="h4 mb-0">Bulk player registration</h2>
        </div>
        <div class="d-flex gap-2">
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/players/template.php')); ?>">
                <i class="bi bi-download"></i>
                Download CSV template
            </a>
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/players/index.php' . (isset($_GET['club_id']) ? '?club_id=' . (int) $_GET['club_id'] : ''))); ?>">Back to players</a>
        </div>
    </div>

    <p class="text-muted mb-3">Upload one CSV per club to register multiple players at once. The CSV template opens in Excel, uses the player's NIN as the match key, and leaves passport or other document uploads for later on each player profile.</p>

    <?php if ($bulkErrors !== []): ?>
        <div class="alert alert-danger border-0 shadow-sm">
            <strong>Import issues found:</strong>
            <ul class="mb-0 mt-2">
                <?php foreach ($bulkErrors as $bulkError): ?>
                    <li><?php echo e((string) $bulkError); ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <form method="post" action="<?php echo e(base_url('app/modules/players/actions.php')); ?>" enctype="multipart/form-data" novalidate>
        <?php echo csrf_input(); ?>
        <input type="hidden" name="action" value="bulk_import">

        <div class="row g-3">
            <?php if (!is_club_admin()): ?>
                <div class="col-md-6">
                    <label class="form-label" for="bulk_club_id">Club</label>
                    <select class="form-select <?php echo validation_error($formErrors, 'club_id') ? 'is-invalid' : ''; ?>" id="bulk_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 (string) old('club_id', (string) ($_GET['club_id'] ?? '')) === (string) $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>
            <?php else: ?>
                <div class="col-md-6">
                    <label class="form-label">Club</label>
                    <div class="form-control bg-light d-flex align-items-center"><?php echo e($clubOptions[0]['name'] ?? 'Assigned club'); ?></div>
                </div>
            <?php endif; ?>
            <div class="col-md-6">
                <label class="form-label" for="bulk_file">CSV file</label>
                <input class="form-control <?php echo validation_error($formErrors, 'bulk_file') ? 'is-invalid' : ''; ?>" id="bulk_file" name="bulk_file" type="file" accept=".csv" required>
                <?php if (validation_error($formErrors, 'bulk_file')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'bulk_file')); ?></div><?php endif; ?>
                <div class="form-text">Use the downloaded CSV template. Save it as CSV after editing in Excel.</div>
            </div>
        </div>

        <div class="d-flex gap-2 mt-4">
            <button class="btn btn-jlfc" type="submit">Import players</button>
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/players/template.php')); ?>">Get template</a>
        </div>
    </form>
</section>

<section class="panel-card">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Template columns</p>
            <h3 class="h5 mb-0">Expected CSV format</h3>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table align-middle mb-0">
            <thead>
                <tr>
                    <?php foreach (player_bulk_template_headers() as $header): ?>
                        <th><?php echo e($header); ?></th>
                    <?php endforeach; ?>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <?php foreach (player_bulk_template_sample_row() as $value): ?>
                        <td><?php echo e((string) $value); ?></td>
                    <?php endforeach; ?>
                </tr>
            </tbody>
        </table>
    </div>
    <div class="form-text mt-3">The eligibility_status column is optional for club admins because league staff controls final approval. The player ID is generated automatically from the club short name, while photo, passport, medical certificate, and other documents can be uploaded later on the player profile.</div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>