<?

/*
 * This file is part of the 'Science Fair In A Box' project
 * SFIAB Website: http://www.sfiab.ca
 *
 * Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
 * Copyright (C) 2005 James Grant <james@lightbox.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
?>
<?
require_once ('../common.inc.php');
require_once ('../user.inc.php');
user_auth_required('committee', 'admin');
require_once ('judges.inc.php');

send_header('Judging Team Members',
	array('Committee Main' => 'committee_main.php',
		'Administration' => 'admin/index.php',
		'Judges' => 'admin/judges.php'));
?>
<script language="javascript" type="text/javascript">
function addbuttonclicked(team)
{
	document.forms.judges.action.value="add";
	document.forms.judges.team_num.value=team;
	document.forms.judges.submit();
}

function switchjudgeinfo()
{
	if(document.forms.judges["judgelist[]"].selectedIndex != -1)
	{
		currentname=document.forms.judges["judgelist[]"].options[document.forms.judges["judgelist[]"].selectedIndex].text;
		currentid=document.forms.judges["judgelist[]"].options[document.forms.judges["judgelist[]"].selectedIndex].value;

		document.forms.judges.judgeinfobutton.disabled=false;
		document.forms.judges.judgeinfobutton.value=currentname;

	}
	else
	{
		document.forms.judges.judgeinfobutton.disabled=true;
		document.forms.judges.judgeinfobutton.value="<? echo i18n('Judge Info') ?>";
	}

}

var mousex = 0, mousey = 0;
var selectedMemberId;

function showMemberDetails(judgeId){
	if(judgeId == undefined){
		judgeId = document.forms.judges["judgelist[]"].options[document.forms.judges["judgelist[]"].selectedIndex].value;
	}
	$('#infodiv').load("judges_info.php?id=" + judgeId,
		function(){ eval('doShowMemberDetails(' + judgeId + ');'); }
	);
}

function editMember(memberId){
	if(memberId == undefined) memberId = selectedMemberId;
	hideMemberDetails();
	window.open("user_editor_window.php?id="+memberId,"UserEditor","location=no,menubar=no,directories=no,toolbar=no,width=770,height=500,scrollbars=yes");
}

function hideMemberDetails(){
	$('#infodiv').css("display", "none");
	$('#infodivcover').css("display", "none");
}

function doShowMemberDetails(judgeId){
	selectedMemberId = judgeId;
	$('#infodiv').css("top", mousey + 5);
	$('#infodiv').css("left", mousex + 20);
	$('#infodiv').css("display", "inline");
	$('#infodivcover').css("top", mousey + 5);
	$('#infodivcover').css("left", mousex + 20);
	$('#infodivcover').css("display", "inline");
	$('#infodivcover').css("width", $('#infodiv').width());
	$('#infodivcover').css("height", $('#infodiv').height());
}

jQuery(document).ready(function(){
	$('#infodivcover').click(function(){ editMember(); });
	$(document).mousemove(function(e){
		mousex = e.pageX;
		mousey = e.pageY;
	}); 
});

</script>
<?

if (get_value_from_array($_POST, 'action') == 'add' && get_value_from_array($_POST, 'team_num') && count(get_value_from_array($_POST, 'judgelist', [])) > 0) {
	// first check if this team exists.
	$q = $pdo->prepare("SELECT id,name FROM judges_teams WHERE num=? AND year=?");
	$q->execute([$_POST['team_num'],$config['FAIRYEAR']]);
	if ($q->rowCount()) {
		$r = $q->fetch(PDO::FETCH_OBJ);
		$team_id = $r->id;
		$team_name = $r->name;

		// if the team is empty, we'll add the first person as the captain
		$team = getJudgingTeam($team_id);
		if (count(get_value_from_array($team, 'members', [])))
			$captain = 'no';
		else
			$captain = 'yes';
	}
	$added = 0;

	foreach ($_POST['judgelist'] AS $selectedjudge) {
		// before we insert them, we need to make sure they dont already belong to this team.  We can not have the same judge assigned to the same team multiple times.

		$q = $pdo->prepare("SELECT * FROM judges_teams_link WHERE users_id=? AND judges_teams_id=?");
		$q->execute([$selectedjudge,$team_id]);
		if ($q->rowCount()) {
			echo notice(i18n('Judge (%1) already belongs to judging team: %2', array($selectedjudge, $team_name)));
		} else {
			// lets make the first one we add a captain, the rest, non-captains :)
			$stmt = $pdo->prepare("INSERT INTO judges_teams_link (users_id,judges_teams_id,captain,year) VALUES (?,?,?,?)");
			$stmt->execute([$selectedjudge,$team_id,$captain,$config['FAIRYEAR']]);
			$added++;
		}
		// if this is alreayd no, then who cares, but if its the first one that is going into the new team, then
		// captain will be yes, and we only want the first one assigned to a new team to be the captain
		// sno now we can set this back to no
		$captain = 'no';
	}
	if ($added == 1)
		$j = i18n('judge');
	else
		$j = i18n('judges');

	echo happy(i18n('%1 %2 added to team #%3 (%4)', array($added, $j, $_POST['team_num'], $team_name)));
}

if (get_value_from_array($_GET, 'action') == 'del' && get_value_from_array($_GET, 'team_num') && get_value_from_array($_GET, 'team_id') && get_value_from_array($_GET, 'users_id')) {
	$stmt = $pdo->prepare("DELETE FROM judges_teams_link WHERE users_id=? AND judges_teams_id=? AND year=?");
	$stmt->execute([$_GET['users_id'],$_GET['team_id'],$config['FAIRYEAR']]);
	echo happy(i18n('Removed judge from team #%1 (%2)', array($_GET['team_num'], $_GET['team_name'])));

	// if there is still members left in the team, make sure we have a captain still
	$q = $pdo->prepare("SELECT * FROM judges_teams_link WHERE judges_teams_id=? AND year=?");
	$q->execute([$_GET['team_id'],$config['FAIRYEAR']]);
	if ($q->rowCount()) {
		// make sure the team still has a captain!
		// FIXME: this might best come from the "i am willing to be a team captain" question under the judges profile
		$gotcaptain = false;
		$first = true;
		while ($r = $q->fetch(PDO::FETCH_OBJ)) {
			if ($first) {
				$firstjudge = $r->users_id;
				$first = false;
			}

			if ($r->captain == 'yes') {
				$gotcaptain = true;
				break;
			}
		}
		if (!$gotcaptain) {
			// make the first judge the captain
			$stmt = $pdo->prepare("UPDATE judges_teams_link SET captain='yes' WHERE judges_teams_id=? AND users_id=? AND year=?");
			$stmt->execute([$_GET['team_id'],$firstjudge,$config['FAIRYEAR']]);
			echo notice(i18n('Team captain was removed. A new team captain has been automatically assigned'));
		}
	}
}

if (get_value_from_array($_GET, 'action') == 'empty' && get_value_from_array($_GET, 'team_num') && get_value_from_array($_GET, 'team_id')) {
	$stmt = $pdo->prepare("DELETE FROM judges_teams_link WHERE judges_teams_id=? AND year=?");
	$stmt->execute([$_GET['team_id'],$config['FAIRYEAR']]);
	echo happy(i18n('Emptied all judges from team #%1 (%2)', array($_GET['team_num'], $_GET['team_name'])));
}

if (get_value_from_array($_POST, 'action') == 'saveteamnames') {
	if (count($_POST['team_names'])) {
		foreach ($_POST['team_names'] AS $team_id => $team_name) {
			$stmt = $pdo->prepare("UPDATE judges_teams SET name=? WHERE id=?");
			$stmt->execute([stripslashes($team_name),$team_id]);
		}
		echo happy(i18n('Team names successfully saved'));
	}
}

if (get_value_from_array($_GET, 'action') == 'addcaptain') {
	// teams can have as many captains as they want, so just add it.
	$stmt = $pdo->prepare("UPDATE judges_teams_link SET captain='yes' WHERE judges_teams_id=? AND users_id=?");
	$stmt->execute([ $_GET['team_id'],$_GET['judge_id']]);
	echo happy(i18n('Team captain assigned'));
}

if (get_value_from_array($_GET, 'action') == 'removecaptain') {
	// teams must always have at least one captain, so if we only have one, and we are trying to remove it, dont let them!
	$q = $pdo->prepare("SELECT * FROM judges_teams_link WHERE captain='yes' AND judges_teams_id=?");
	$q->execute([$_GET['team_id']]);
	if ($q->rowCount() < 2) {
		echo error(i18n('A judge team must always have at least one captain'));
	} else {
		$pdo->prepare("UPDATE judges_teams_link SET captain='no' WHERE judges_teams_id=? AND users_id=?");
		$pdo->execute([$_GET['team_id'],$_GET['judge_id']]);
		echo happy(i18n('Team captain removed'));
	}
}

if (get_value_from_array($_GET, 'action') == 'autoassignspecial') {
	/* Load all the judges (judge_complete=yes, deleted=no, year=fairyear) */
	$judgelist = judges_load_all();

	/* Load all the teams */
	$teams = array();
	$q = $pdo->prepare("SELECT * FROM judges_teams WHERE year=?");
	$q->execute([$config['FAIRYEAR']]);
	while ($i = $q->fetch(PDO::FETCH_ASSOC)) {
		$teams[$i['id']] = $i;
	}

	/* And the links */
	$links = array();
	$q = $pdo->prepare("SELECT * FROM judges_teams_link WHERE year=?");
	$q->execute([$config['FAIRYEAR']]);
	while ($i = $q->fetch(PDO::FETCH_ASSOC)) {
		$judgelist[$i['users_id']]['teams_links'][] = $i;
	}

	$jlist = array();

	/* Remove all judges that have a link */
	foreach ($judgelist as $j) {
		if (count($j['teams_links']) == 0 && $j['special_award_only'] == 'yes')
			$jlist[] = $j['id'];
	}
	echo 'We have ' . count($jlist) . ' special awards judges to assign';
	foreach ($jlist AS $jid) {
		$j = $judgelist[$jid];
		if (is_array($j['special_award_selected']) && count($j['special_award_selected'])) {
			// assing them to ALL teams for ALL awards
			foreach ($j['special_award_selected'] AS $awardid) {
				echo "Looking for a team for award $awardid <br />";
				// find the award id linked to a team
				$q = $pdo->prepare("SELECT * FROM judges_teams_awards_link WHERE award_awards_id=? AND year=?");
				$q->execute([$awardid,$config['FAIRYEAR']]);
				if ($q->rowCount()) {
					while ($r = $q->fetch(PDO::FETCH_OBJ)) {
						$stmt = $pdo->prepare("INSERT INTO judges_teams_link (users_id,judges_teams_id,captain,year) VALUES (?,?,'yes',?)");
						$stmt->execute([$jid,$r->judges_teams_id,$config['FAIRYEAR']]);
						echo happy(i18n('%1 %2 to their special award(s) team(s)', array($j['firstname'], $j['lastname'])));
					}
				} else {
					echo error(i18n('%1 %2 not assigned - No team found that is judging award id %1', array($awardid)));
				}
			}
		} else {
			echo error(i18n('%1 %2 has indicated special awards only, but didnt selected any awards', array($j['firstname'], $j['lastname'])));
		}
	}
}

if (!$_SESSION['viewstate']['judges_teams_list_show'])
	$_SESSION['viewstate']['judges_teams_list_show'] = 'unassigned';
// now update the judges_teams_list_show viewstate
if (get_value_from_array($_GET, 'judges_teams_list_show'))
	$_SESSION['viewstate']['judges_teams_list_show'] = $_GET['judges_teams_list_show'];

echo '<form name="judges" method="post" action="judges_teams_members.php">';
echo '<input type="hidden" name="action">';
echo '<input type="hidden" name="team_id">';
echo '<input type="hidden" name="team_num">';
echo '<input type="hidden" name="team_name">';
echo '<input type="hidden" name="users_id">';
echo '<table>';
echo '<tr>';
echo '<th>' . i18n('Judges List');
echo '<br />';
echo '<input disabled="true" name="judgeinfobutton" id="judgeinfobutton" onclick="showMemberDetails()" type="button" value="' . i18n('Judge Info') . '">';
echo '</th>';
echo '<th>' . i18n('Judge Teams') . '</th>';
echo '</tr>';
echo '<tr><td valign="top">';
echo '<table width="100%"><tr>';
if ($_SESSION['viewstate']['judges_teams_list_show'] == 'all') {
	echo '<td align=left><a href="judges_teams_members.php?judges_teams_list_show=unassigned">' . i18n('show unassigned') . '</a></td>';
	echo '<td align=right><b>' . i18n('show all') . '</b></td>';
} else {
	echo '<td align=left><b>' . i18n('show unassigned') . '</b></td>';
	echo '<td align=right><a href="judges_teams_members.php?judges_teams_list_show=all">' . i18n('show all') . '</a></td>';
}
echo '</tr></table>';

/* Load all the judges (judge_complete=yes, deleted=no, year=fairyear) */
$judgelist = judges_load_all();

/* Load all the teams */
$teams = array();
$q = $pdo->prepare("SELECT * FROM judges_teams WHERE year=?");
$q->execute([$config['FAIRYEAR']]);
while ($i = $q->fetch(PDO::FETCH_ASSOC)) {
	$teams[$i['id']] = $i;
}

/* And the links */
$links = array();
$q = $pdo->prepare("SELECT * FROM judges_teams_link WHERE year=?");
$q->execute([$config['FAIRYEAR']]);
while ($i = $q->fetch(PDO::FETCH_ASSOC)) {
	$judgelist[$i['users_id']]['teams_links'][] = $i;
}

$jlist = array();
if ($_SESSION['viewstate']['judges_teams_list_show'] == 'unassigned') {
	/* Remove all judges that have a link */
	foreach ($judgelist as $j) {
		if (count(get_value_from_array($j, 'teams_links', [])) == 0)
			$jlist[] = $j['id'];
	}
} else {
	$jlist = array_keys($judgelist);
}

echo '<center>';
echo i18n('Listing %1 judges', array(count($jlist)));
echo '<br />';
echo '</center>';
show_pdo_errors_if_any($pdo);
echo '<select name="judgelist[]" onchange="switchjudgeinfo()" multiple="multiple" style="width: 250px; height: 600px;">';

foreach ($jlist as $jid) {
	$u = &$judgelist[$jid];
	if ($u['firstname'] && $u['lastname']) {
		if ($u['special_award_only'] == 'yes') {
			$sp = '[sp] ';
		} else
			$sp = '';
		echo "<option value=\"$jid\">{$sp}{$u['firstname']} {$u['lastname']} (" . implode(' ', $u['languages']) . ")</option>\n";
	}
}
unset($u);

echo '</select>';
echo '<br />';
echo "<a href=\"judges_teams_members.php?action=autoassignspecial\">Auto-Assign Special Awards Judges to Special Awards Teams</a>\n";
echo '</td>';
echo '<td valign="top">';

$teams = getJudgingTeams();

foreach ($teams AS $team) {
	echo '<hr>';

	echo '<table width="100%">';
	echo '<tr><td valign=top width="80">';
	echo '<input onclick="addbuttonclicked(\'' . $team['num'] . '\')" type="button" value="Add &gt;&gt;">';
	echo '</td><td>';

	echo "<table width=\"100%\">\n";
	echo '<tr><th colspan="2" align="left">#' . $team['num'] . ': ';
	echo $team['name'];
	echo "</th></tr>\n";
	echo '<tr><td colspan="2">';
	foreach ($team['rounds'] as $ts) {
		echo "{$ts['name']}: " . format_time($ts['starttime']) . ' - ' . format_time($ts['endtime']) . '<br />';
	}
	echo '</td></tr>';

	if (count(get_value_from_array($team, 'members', []))) {
		foreach ($team['members'] AS $member) {
			$j = &$judgelist[$member['id']];
			echo '<tr><td>';

			/*
			 * if($team['num']=="89") {
			 * 	echo "<pre>";
			 * 	print_r($team);
			 * 	print_r($j);
			 * 	echo "</pre>";
			 * }
			 */

			$langerr = false;
			$judgeerr = false;
			foreach ($team['languages'] AS $teamlang) {
				if (is_array($j['languages'])) {
					if (!in_array($teamlang, $j['languages'])) {
						$langerr = true;
						break;
					}
				} else {
					$langerr = true;
				}
			}

			if (!$j['id']) {
				$judgeerr = true;
			}

			echo '<a onclick="return confirmClick(\'Are you sure you want to remove this judge from this team?\')" href="judges_teams_members.php?action=del&team_id=' . $team['id'] . '&team_num=' . $team['num'] . '&users_id=' . $member['id'] . '&team_name=' . rawurlencode($team['name']) . '"><img border=0 src="' . $config['SFIABDIRECTORY'] . '/images/16/button_cancel.' . $config['icon_extension'] . '"></a>';
			echo '</td><td width="100%">';
			if ($langerr || $judgeerr)
				echo '<span class="error" style="width: 100%; display: block;">';
			if ($judgeerr) {
				echo 'ERROR: this judge is assigned to the team, but they are not an active/complete judge! <br />';
			}

			if ($member['captain'] == 'yes') {
				echo '<a title="Captain - Click to remove captain status" href="judges_teams_members.php?action=removecaptain&team_id=' . $team['id'] . '&judge_id=' . $member['id'] . '">';
				echo '<img border=0 src="' . $config['SFIABDIRECTORY'] . '/images/16/bookmark.' . $config['icon_extension'] . '">';
				echo '</a>&nbsp;';
			} else {
				echo '<a title="Non-Captain - Click to make a team captain" href="judges_teams_members.php?action=addcaptain&team_id=' . $team['id'] . '&judge_id=' . $member['id'] . '">';
				echo '<img border=0 src="' . $config['SFIABDIRECTORY'] . '/images/16/bookmark_disabled.' . $config['icon_extension'] . '">';
				echo '</a>&nbsp;';
			}
			echo '<a  onclick="showMemberDetails(' . $member['id'] . ');">';
			echo $member['firstname'] . ' ' . $member['lastname'];
			if (is_array($j['languages']))
				$l = is_array($j['languages']) ? join(' ', $j['languages']) : '';

			echo "</a>&nbsp;<span style=\"font-size: 1.0em;\">($l)</span>\n";
			if ($langerr || $judgeerr)
				echo "</span>\n";
			echo '</td></tr>';
		}

		echo '<tr><td colspan="2">';
		echo '<a onclick="return confirmClick(\'Are you sure you want to empty all judges from this team?\')" href="judges_teams_members.php?action=empty&team_id=' . $team['id'] . '&team_num=' . $team['num'] . '&team_name=' . rawurlencode($team['name']) . '">';
		echo ' ' . i18n('Empty All Members') . ' ';
		echo '<img border=0 src="' . $config['SFIABDIRECTORY'] . '/images/16/button_cancel.' . $config['icon_extension'] . '">';
		echo '</a>';
		echo '</td></tr>';
	} else {
		echo '<tr><td colspan="2">';
		echo error(i18n('Team has no members'), 'inline');
		echo '</td></tr>';
	}

	echo '</table>';

	echo '</td></tr></table>';
}

echo '<br />';

echo '</td></tr>';
echo '</table>';
echo '</form>';
echo '<div id="infodiv" style="font-size: 1.2em; background-color: #DDF; border:solid;'
	. ' border-width:1px;'
	. ' border-color: #000;'
	. ' position:absolute;'
	. ' top: 0px; left:0px;'
	. ' overflow:hidden; display:none;"'
	. '></div>';
echo '<div id="infodivcover" style="'
	. ' position:absolute;'
	. ' display:none;"'
	. ' onmouseout="hideMemberDetails();"'
	. '></div>';

send_footer();

?>