39773-vm/athletes.php
2026-04-22 14:26:14 +00:00

135 lines
6.3 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/app.php';
app_boot();
require_login();
$user = current_user();
$q = trim((string) ($_GET['q'] ?? ''));
$sport = trim((string) ($_GET['sport'] ?? ''));
$club = trim((string) ($_GET['club'] ?? ''));
$status = trim((string) ($_GET['status'] ?? ''));
$sql = 'SELECT * FROM athletes WHERE user_id = :user_id';
$params = ['user_id' => (int) $user['id']];
if ($q !== '') {
$sql .= ' AND (first_name LIKE :q OR last_name LIKE :q OR nationality LIKE :q OR position_name LIKE :q)';
$params['q'] = '%' . $q . '%';
}
if ($sport !== '') {
$sql .= ' AND sport_name = :sport';
$params['sport'] = $sport;
}
if ($club !== '') {
$sql .= ' AND club_name = :club';
$params['club'] = $club;
}
if ($status !== '') {
$sql .= ' AND status = :status';
$params['status'] = $status;
}
$sql .= ' ORDER BY created_at DESC';
$stmt = db()->prepare($sql);
$stmt->execute($params);
$athletes = $stmt->fetchAll();
$sports = db()->prepare('SELECT DISTINCT sport_name FROM athletes WHERE user_id = :user_id ORDER BY sport_name ASC');
$sports->execute(['user_id' => (int) $user['id']]);
$clubs = db()->prepare('SELECT DISTINCT club_name FROM athletes WHERE user_id = :user_id ORDER BY club_name ASC');
$clubs->execute(['user_id' => (int) $user['id']]);
render_header('Liste des sportifs', ['description' => 'Filtrer, parcourir et consulter les fiches sportifs enregistrées.']);
?>
<main class="container-xxl py-5">
<div class="d-flex flex-wrap justify-content-between align-items-end gap-3 mb-4">
<div>
<p class="section-kicker mb-1">Registre principal</p>
<h1 class="h3 mb-0">Liste des sportifs</h1>
</div>
<a href="athlete_new.php" class="btn btn-dark">Ajouter un sportif</a>
</div>
<div class="panel-card p-4 mb-4">
<form class="row g-3 align-items-end" method="get">
<div class="col-md-4">
<label class="form-label" for="q">Recherche</label>
<input class="form-control" id="q" name="q" value="<?= e($q) ?>" placeholder="Nom, poste, nationalité">
</div>
<div class="col-md-3">
<label class="form-label" for="sport">Sport</label>
<select class="form-select" id="sport" name="sport">
<option value="">Tous</option>
<?php foreach ($sports->fetchAll() as $item): ?>
<option value="<?= e((string) $item['sport_name']) ?>" <?= $sport === $item['sport_name'] ? 'selected' : '' ?>><?= e((string) $item['sport_name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-3">
<label class="form-label" for="club">Club</label>
<select class="form-select" id="club" name="club">
<option value="">Tous</option>
<?php foreach ($clubs->fetchAll() as $item): ?>
<option value="<?= e((string) $item['club_name']) ?>" <?= $club === $item['club_name'] ? 'selected' : '' ?>><?= e((string) $item['club_name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label class="form-label" for="status">Statut</label>
<select class="form-select" id="status" name="status">
<option value="">Tous</option>
<?php foreach (['actif' => 'Actif', 'blesse' => 'Blessé', 'suspendu' => 'Suspendu', 'retraite' => 'Retraité'] as $value => $label): ?>
<option value="<?= e($value) ?>" <?= $status === $value ? 'selected' : '' ?>><?= e($label) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-12 d-flex gap-2">
<button class="btn btn-dark" type="submit">Filtrer</button>
<a href="athletes.php" class="btn btn-outline-secondary">Réinitialiser</a>
</div>
</form>
</div>
<div class="panel-card p-0 overflow-hidden">
<?php if ($athletes): ?>
<div class="table-responsive">
<table class="table align-middle app-table mb-0">
<thead>
<tr>
<th>Sportif</th>
<th>Sport</th>
<th>Club actuel</th>
<th>Statut</th>
<th>Stats</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($athletes as $athlete): ?>
<tr>
<td>
<strong><?= e($athlete['first_name'] . ' ' . $athlete['last_name']) ?></strong>
<div class="text-secondary small"><?= e((string) ($athlete['position_name'] ?: 'Poste non renseigné')) ?></div>
</td>
<td><?= e((string) $athlete['sport_name']) ?></td>
<td><?= e((string) $athlete['club_name']) ?></td>
<td><span class="badge text-bg-<?= e(stat_badge_class((string) $athlete['status'])) ?>"><?= e(ucfirst((string) $athlete['status'])) ?></span></td>
<td class="small text-secondary"><?= e((string) $athlete['matches_played']) ?> MJ · <?= e((string) $athlete['goals_scored']) ?> PTS · <?= e((string) $athlete['assists_count']) ?> AST</td>
<td class="text-end"><a href="athlete.php?id=<?= e((string) $athlete['id']) ?>" class="btn btn-sm btn-outline-secondary">Voir la fiche</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div class="empty-card m-4">
<h2 class="h5">Aucun résultat</h2>
<p class="mb-3">Aucune fiche ne correspond à vos filtres actuels.</p>
<a href="athlete_new.php" class="btn btn-dark">Créer la première fiche</a>
</div>
<?php endif; ?>
</div>
</main>
<?php render_footer(); ?>