34109-vm/match_view.php
2025-09-19 12:49:02 +00:00

144 lines
5.1 KiB
PHP

<?php
require_once './db/config.php';
session_start();
// Redirect to login if not authenticated
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
// Check if match ID is provided
if (!isset($_GET['id'])) {
header('Location: matches.php');
exit;
}
$match_id = $_GET['id'];
$user_id = $_SESSION['user_id'];
$pdo = db();
// Fetch match details
$stmt = $pdo->prepare("SELECT * FROM matches WHERE id = ?");
$stmt->execute([$match_id]);
$match = $stmt->fetch();
if (!$match) {
header('Location: matches.php');
exit;
}
// Fetch teams for the match
$stmt = $pdo->prepare('SELECT t.name FROM teams t JOIN match_teams mt ON t.id = mt.team_id WHERE mt.match_id = ?');
$stmt->execute([$match_id]);
$teams = $stmt->fetchAll(PDO::FETCH_COLUMN);
// Fetch user's current vote
$stmt = $pdo->prepare("SELECT vote FROM match_votes WHERE match_id = ? AND user_id = ?");
$stmt->execute([$match_id, $user_id]);
$current_vote = $stmt->fetchColumn();
// Handle vote submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['vote'])) {
$vote = $_POST['vote'];
if ($current_vote) {
// Update vote
$stmt = $pdo->prepare("UPDATE match_votes SET vote = ? WHERE match_id = ? AND user_id = ?");
$stmt->execute([$vote, $match_id, $user_id]);
} else {
// Insert new vote
$stmt = $pdo->prepare("INSERT INTO match_votes (match_id, user_id, vote) VALUES (?, ?, ?)");
$stmt->execute([$match_id, $user_id, $vote]);
}
// Refresh the page to show the updated vote status
header('Location: match_view.php?id=' . $match_id);
exit;
}
// Fetch all votes for this match to display who has voted
$stmt = $pdo->prepare('
SELECT u.nickname, mv.vote
FROM users u
JOIN match_votes mv ON u.id = mv.user_id
WHERE mv.match_id = ?
ORDER BY mv.created_at DESC
');
$stmt->execute([$match_id]);
$votes = $stmt->fetchAll();
$match_date = new DateTime($match['match_datetime']);
$formatted_date = $match_date->format('D, M j, Y ');
$formatted_time = $match_date->format('g:i A');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View Match</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<?php include './includes/header.php'; ?>
<div class="container mt-4">
<div class="card">
<div class="card-header">
<h2><?php echo htmlspecialchars($match['match_type']); ?></h2>
</div>
<div class="card-body">
<p><strong>Location:</strong> <?php echo htmlspecialchars($match['location']); ?></p>
<p><strong>Date:</strong> <?php echo $formatted_date; ?></p>
<p><strong>Time:</strong> <?php echo $formatted_time; ?></p>
<?php if (count($teams) > 0): ?>
<p><strong>Teams:</strong> <?php echo htmlspecialchars(implode(', ', $teams)); ?></p>
<?php endif; ?>
</div>
</div>
<div class="card mt-4">
<div class="card-header">
<h3>Your Availability</h3>
</div>
<div class="card-body">
<form method="POST">
<div class="form-check">
<input class="form-check-input" type="radio" name="vote" id="vote_yes" value="yes" <?php echo ($current_vote === 'yes') ? 'checked' : ''; ?>>
<label class="form-check-label" for="vote_yes">Yes, I can make it</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="vote" id="vote_no" value="no" <?php echo ($current_vote === 'no') ? 'checked' : ''; ?>>
<label class="form-check-label" for="vote_no">No, I can't make it</label>
</div>
<button type="submit" class="btn btn-primary mt-3">Submit Vote</button>
</form>
</div>
</div>
<div class="card mt-4">
<div class="card-header">
<h3>Who's Coming</h3>
</div>
<ul class="list-group list-group-flush">
<?php foreach ($votes as $vote): ?>
<li class="list-group-item d-flex justify-content-between align-items-center">
<?php echo htmlspecialchars($vote['nickname']); ?>
<span class="badge bg-<?php echo $vote['vote'] === 'yes' ? 'success' : 'danger'; ?>"><?php echo ucfirst($vote['vote']); ?></span>
</li>
<?php endforeach; ?>
<?php if (count($votes) === 0): ?>
<li class="list-group-item">No one has voted yet.</li>
<?php endif; ?>
</ul>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>