#! /usr/bin/perl

use ccbs;
use strict;
use warnings;

my $cgi = new CGI;
my $id = $cgi->param('id');
my $selected_player = $cgi->param('player');

my $dbh = ccbs::db_connect();

my $tournament = $dbh->selectrow_hashref('SELECT * FROM tournaments NATURAL JOIN seasons NATURAL JOIN countries NATURAL JOIN machines NATURAL JOIN scoringsystems WHERE tournament=?', undef, $id);
my $registered = ccbs::db_fetch_all($dbh, 'SELECT * FROM tournamentparticipation NATURAL JOIN players WHERE tournament=? ORDER BY LOWER(nick)', $id);
my $available = ccbs::db_fetch_all($dbh, 'SELECT * FROM players WHERE player NOT IN ( SELECT player FROM tournamentparticipation WHERE tournament=? ) ORDER BY LOWER(nick)', $id);
my $countries = ccbs::db_fetch_all($dbh, 'SELECT * FROM countries ORDER BY countrycode');
my $clubs = ccbs::db_fetch_all($dbh, 'SELECT * FROM clubs ORDER BY clubcode');

ccbs::print_header();
ccbs::process_template('registration.tmpl', $tournament->{'tournamentname'}, {
	tournament => $tournament,
	registered => $registered,
	available => $available,
	selected_player => $selected_player,
	countries => $countries,
	clubs => $clubs
});
$dbh->disconnect;
