<?php

  include("html_functions.php");



  ################## PAGE INITIALIZATION #########################

  $lgid=$_SESSION['lgid'];

  $lgpath=$_SESSION['lgpath'];

  $date=new DateTime($_SESSION['lgdate']);

  $lgyear=$_SESSION['lgyear'];



  include($_SESSION['adminpath']."/dbopen.php");



  # Check Admin Table

  $module='Awards';

  $query="SHOW TABLES LIKE 'ootp_sql_admin';";

  $result=mysqli_query($db,$query);

  if (mysqli_num_rows($result)) 

  {

	  # Get Settings

	  $query="SELECT setting,value FROM ootp_sql_admin WHERE scope IN ('ALL',$lgid) AND module='$module'";

	  if ($_GET['showq']==1) {echo "$query<br/>\n";}

	  $result=mysqli_query($db,$query);

	  if (mysqli_num_rows($result))

	  {

		  while ($row=mysqli_fetch_array($result))

		  {

			  $field=$row['setting'];

		 	  $val=$row['value'];

			  $val=stripslashes($val);

		 	  switch ($field)

			  {

	  		  case 'awResults'  : $awResults=$val;   break;

			  case 'boyVotes'   : $boyVotes=$val;    break;

	  		  case 'poyVotes'   : $poyVotes=$val;    break;

			  case 'royVotes'   : $royVotes=$val;    break;

	  		  case 'ggEnable'   : $ggEnable=$val;    break;

			  case 'splitOF'    : $splitOF=$val;     break;

	  		  case 'cmEnable'   : $cmEnable=$val;    break;

			  case 'cmAwardName': $cmAwardName=$val; break;

	  		  case 'cmVotes'    : $cmVotes=$val;     break;

			  case 'cmQual'     : $cmQual=$val;      break;

	  		  case 'cmWho'      : $cmWho=$val;       break;

			  case 'gmEnable'   : $gmEnable=$val;    break;

	  		  case 'gmVotes'    : $gmVotes=$val;     break;

			  }

		  }

	  }

  }

  if ($splitOF=="") {$splitOF=0;}

  $awList=Array ("5"=>"boy","4"=>"poy","6"=>"roy");

  if ($ggEnable==1) {array_push($awList,"gg");}

  if ($cmEnable==1) {array_push($awList,"cm");}

  $votes['boy']=$boyVotes;

  $votes['poy']=$poyVotes;

  $votes['roy']=$royVotes;

  $votes['cm']=$cmVotes;

  $votes['gm']=$gmVotes;

  

  ##### Get Parameters #####

  $hmid=$_SESSION['user_id'];

  if ($hmid=="") {$hmid=-1;}

  $subleagueID=$_GET['sub_league_id'];

  if ($subleagueID=="") {$subleagueID="ALL";}

  $awardToView=$_GET['award'];

  if ($awardToView=="") {$awardToView="ALL";}

  $year=$_GET['year'];

  if ($year=="") {$year="ALL";}

    

  ##### Get leagues #####

  $query="SELECT sub_league_id,name FROM sub_leagues WHERE league_id=$lgid ORDER BY sub_league_id;";

  $result=mysqli_query($db,$query);

  $prevSL=0;

  $SLcnt=0;

  while ($row=mysqli_fetch_array($result))

  {

	  $sub_league_id=$row['sub_league_id'];

	  $subleagues[$sub_league_id]['name']=$row['name'];

	  $SLcnt++;

  }



  ##### Get Award Info #####

  $query="SELECT mvp_award_name,pitcher_award_name,rookie_award_name,defense_award_name FROM leagues WHERE league_id=$lgid;";

  $result=mysqli_query($db,$query);

  $row=mysqli_fetch_array($result);

  $awardName['boy']=$row['mvp_award_name']." (BOY)";

  $awardName['poy']=$row['pitcher_award_name']." (POY)";

  $awardName['roy']=$row['rookie_award_name']." (ROY)";

  $awardName['gg']=$row['defense_award_name']." (GG)";

  $awardName['cm']=$cmAwardName;

  $awardName['gm']="General Manager of the Year Award";



  $awardsAbbr[4]='poy';

  $awardsAbbr[5]='boy';

  $awardsAbbr[6]='roy';

  $awardsAbbr[7]='gg';

  $awardsAbbr[200]='cm';

  $awardsAbbr[300]='gm';

  

  ##### Get years and awards in DB #####

  $query="SHOW TABLES LIKE 'awards_vote';";

  $result=mysqli_query($db,$query);

  if (mysqli_num_rows($result))

  {

	  ## Get History of Voting

	  $query="SELECT DISTINCT(year) as year FROM awards_vote WHERE league_id=$lgid AND award_id IN (4,5,6,7,200,300) ORDER BY year DESC;";

	  $result=mysqli_query($db,$query);

       	  while ($row=mysqli_fetch_array($result))

	  {

	    	  $yr=$row['year'];

		  $years[$yr]=1;

      	  }

	  $query="SELECT DISTINCT(award_id) as award_id FROM awards_vote WHERE league_id=$lgid AND award_id IN (4,5,6,7,200,300) ORDER BY award_id;";

       	  $result=mysqli_query($db,$query);

	  while ($row=mysqli_fetch_array($result))

      	  {

		  $awID=$row['award_id'];

	    	  $awards[$awID]=1;

	  }

  }

  else {$noHist=1;}



  mysqli_close($db);



  # Title Bar

  $title='Awards Voting History';



  page_head($title,$_SESSION['lgpath']);

  echo " <div id='topbar'>\n";

  main_titlebar($lgid,$_SESSION['lgpath'],$title);

  nav_toolbar("awards","Awards Voting");

  echo " </div>   <!-- END topbar DIV -->\n";

  echo " <div id='contentpane'>\n";



  if ($noHist==1)

  {

	  echo "<div class='textbox'>No awards voting history yet</div>\n";    

       	  ##### End Page #####

	  echo " </div>   <!-- END contentpane DIV -->\n";

       

	  page_foot($lgpath);

	  exit;

  }

 

  ################## START OF PAGE CONTENT #########################

  include($_SESSION['adminpath']."/dbopen.php");



##### Filters #####

echo "<div class='textbox'>\n";

echo " <table cellspacing=0 cellpadding=2 border=0>\n";

echo "  <tr class='title'><td colspan=7>Filters</td></tr>\n";

echo "  <form method='get' action='awVotingHist.php' class='inline'>\n";

echo "   <tr>\n";



## Show League Filter

echo "    <td><label for='sub_league_id'>League:</label></td>\n";

echo "    <td>\n";

echo "     <select name='sub_league_id' id='sub_league_id' onchange=\"if((this[this.selectedIndex].value!='ALL')&&(document.getElementById('award').value==300)) {document.getElementById('award').selectedIndex=0;}\">\n";

echo "      <option value='ALL'>All Leagues</option>\n";

foreach ($subleagues as $slid => $val)

 {

   echo "      <option value='$slid'";

   if (($subleagueID==$slid)&&($subleagueID!="ALL")) {echo " selected";}

   echo ">".$subleagues[$slid]['name']."</option>\n";

 }

echo "     </select>\n";

echo "    </td>\n";



## Show Award Filter

echo "    <td><label for='award'>Award:</label></td>\n";

echo "    <td>\n";

echo "     <select name='award' id='award' onchange=\"if(this[this.selectedIndex].value=='300') {document.getElementById('sub_league_id').selectedIndex=0;}\">\n";

echo "      <option value='ALL'>All Awards</option>\n";

foreach ($awards as $awID => $val)

 {

   switch ($awID)

    {

      case '4': $awName=$awardName['poy']; break;

      case '5': $awName=$awardName['boy']; break;

      case '6': $awName=$awardName['roy']; break;

      case '7': $awName=$awardName['gg']; break;

      case '200': $awName=$awardName['cm']; break;

      case '300': $awName=$awardName['gm']; break;

    }

   echo "      <option value='$awID'";

   if ($awardToView==$awID) {echo " selected";}

   echo ">$awName</option>\n";

 }

echo "     </select>\n";

echo "    </td>\n";



## Show Year Filter

echo "    <td><label for='year'>Year:</label></td>\n";

echo "    <td>\n";

echo "     <select name='year'>\n";

echo "      <option value='ALL'>All Years</option>\n";

foreach ($years as $yr => $val)

{

  if ($yr==$lgyear) {continue;}

   echo "      <option value='$yr'";

   if ($year==$yr) {echo " selected";}

   echo ">$yr</option>\n";

 }

echo "     </select>\n";

echo "    </td>\n";



## Close Form

echo "    <td align='right'>\n";

echo "     <input type='submit' class='submitButton' value='Go' />\n";

echo "    </td>\n";

echo "   </tr>\n";

echo "  </form>\n";

echo " </table>\n";

  

echo "</div>\n";



if (!isset($_GET['award'])) 

	{

		##### End Page #####

		echo " </div>   <!-- END contentpane DIV -->\n";

		

		page_foot($lgpath);

		exit;

	}





	$showGMVote=0;

	if ($hmid!="") {$showGMVote=1;}

	

	## Loop Through Years

	foreach ($years as $yr => $val)

	{

		if (($year!="ALL")&&($year!=$yr)) {continue;}

		if ($yr==$lgyear) {continue;}

		

		## Check voting history

		if (($awResults==0)&&($_SESSION['user_is_commish']!=1)&&($yr==$lgyear)&&($hmid>0))

		{

			$query="SELECT human_manager_id,award_id,sub_league_id FROM awards_vote WHERE league_id=$lgid AND year=$yr AND award_id IN (4,5,6,7,200,300) AND human_manager_id=$hmid GROUP BY human_manager_id,award_id,sub_league_id";

			$result=mysqli_query($db,$query);

			if (mysqli_num_rows($result))

			{

				while ($row=mysqli_fetch_array($result))

				{

					$awID=$row['award_id'];

					$slid=$row['sub_league_id'];

					$voted[$hmid][$awID][$slid]=1;

				}

			}			

		}



		## Start Tables		

		echo "<div class='textbox'>\n";

		echo " <table cellpadding=0 cellspacing=0 border=0 width='935px'>\n";

		echo "  <tr class='title2'><td>$yr</td></tr>\n";

		echo "  <tr><td>\n";

		

		## Loop Through Subleagues

		foreach ($subleagues as $slid => $val)

		{

			if ((($subleagueID!=$slid)&&($subleagueID!="ALL"))||($awardToView==300)) {continue;}

			echo "<div class='tablebox'>\n";

			echo " <table cellpadding=0 cellspacing=0 border=0>\n";

			echo "  <tr class='title2'><td style='padding:2px;'>".$subleagues[$slid]['name']."</td></tr>\n";

			echo "  <tr><td>\n";

			echo "   <table cellpadding=2 cellspacing=0 border=0 width='650px'>\n";

			echo "    <tr class='headline'><td class='hsc2_l'>Player</td><td class='hsc2'>1st</td><td class='hsc2'>2nd</td><td class='hsc2'>3rd</td><td class='hsc2'>4th</td><td class='hsc2'>5th</td><td class='hsc2'>6th</td><td class='hsc2'>7th</td><td class='hsc2'>8th</td><td class='hsc2'>9th</td><td class='hsc2'>10th</td><td class='hsc2'>Score</td>";

			if ($showGMVote==1) {echo "<td class='hsc2'>Your Vote</td>";}

			echo "</tr>\n";

			

			foreach ($awList as $awID => $award)

			{

				if (($awardToView!=$awID)&&($awardToView!='ALL')) {continue;}

				if ($award=='gg') {$awID=7;}

				if ($award=='cm') {$awID=200;}

				

				echo "    <tr class='title'><td colspan=".(12+$showGMVote).">".$awardName[$award]."</td></tr>\n";

				$cnt=0;

				

				$query="SELECT p.player_id,x.position,first_name,last_name,organization_id,first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth,score,who FROM players as p,(SELECT player_id,position,SUM(first) as first,SUM(second) as second,SUM(third) as third,SUM(fourth) as fourth,SUM(fifth) as fifth,SUM(sixth) as sixth,SUM(seventh) as seventh,SUM(eighth) as eighth,SUM(ninth) as ninth,SUM(tenth) as tenth,SUM(score) as score,MAX(who) as who FROM (SELECT player_id,position,if(vote=1,1,0) as first,if(vote=2,1,0) as second,if(vote=3,1,0) as third,if(vote=4,1,0) as fourth,if(vote=5,1,0) as fifth,if(vote=6,1,0) as sixth,if(vote=7,1,0) as seventh,if(vote=8,1,0) as eighth,if(vote=9,1,0) as ninth,if(vote=10,1,0) as tenth,".get_vote_query($votes[$award])." as score,if(human_manager_id=$hmid,vote,0) as who FROM awards_vote WHERE league_id=$lgid AND year=$yr AND sub_league_id=$slid AND award_id=$awID";

				if (($gmID!="")&&($_SESSION['user_is_commish'])) {$query.=" AND human_manager_id=$gmID";}

				$query.=") as t GROUP BY player_id) as x WHERE p.player_id=x.player_id ORDER BY position,score DESC";

				if ($_GET['showq']==1) {echo "$query<br/>\n";}

				$result=mysqli_query($db,$query);

				if (($lgyear==$yr)&&($voted[$hmid][$awID][$slid]!=1)&&($awResults==0)&&($_SESSION['user_is_commish']!=1)) {echo "    <tr class='s2'><td class='".$cls."_l' colspan=".(12+$showGMVote)." style='text-align:center;'>You have not voted for this award</td>";}

				elseif (mysqli_num_rows($result))

				{

					$lastPos=-1;

					while ($row=mysqli_fetch_array($result))

					{

						$pid=$row['player_id'];

						$position=$row['position'];

						$name=$row['first_name']." ".$row['last_name'];

						

						if (($award=='gg')&&($lastPos!=$position))

						{

							$cnt=0;

							echo "    <tr class='headline'><td class='hsc2_l' colspan=".(12+$showGMVote).">".get_pos($position)."</td>";

						}

						

						$cls='s'.($cnt%2+1);

						echo "    <tr class='$cls'>";

						echo "<td class='".$cls."_l'><a href='./player.php?player_id=$pid'>$name</a></td>";

						if ($row['first']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['first']."</td>";}

						if ($row['second']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['second']."</td>";}

						if ($row['third']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['third']."</td>";}

						if ($row['fourth']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['fourth']."</td>";}

						if ($row['fifth']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['fifth']."</td>";}

						if ($row['sixth']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['sixth']."</td>";}

						if ($row['seventh']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['seventh']."</td>";}

						if ($row['eighth']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['eighth']."</td>";}

						if ($row['ninth']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['ninth']."</td>";}

						if ($row['tenth']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['tenth']."</td>";}

						echo "<td>".$row['score']."</td>";

						if ($showGMVote==1)

						{

							if ($row['who']==0) {echo "<td>&nbsp;</td>";}

							elseif ($votes[$award]==1) {echo "<td>X</td>";}

							else {echo "<td>".$row['who']."</td>";}

						}

						echo "</tr>\n";

						

						$lastPos=$position;

						$cnt++;

					}

				}

				else {echo "    <tr class='s2'><td class='".$cls."_l' colspan=".(12+$showGMVote)." style='text-align:center;'>No votes recorded</td>";}

			}

			echo "   </table>\n";

			echo "  </td></tr>\n";

			echo " </table>\n";

			echo "</div>\n";

		}

		

		if (($gmEnable==1)&&(($awardToView==300)||($awardToView=='ALL')))

		{

			echo "<div class='tablebox'>\n";

			echo " <table cellpadding=0 cellspacing=0 border=0>\n";

			echo "  <tr class='title2'><td style='padding:2px;'>League-Wide</td></tr>\n";

			echo "  <tr><td>\n";

			echo "   <table cellpadding=2 cellspacing=0 border=0 width='650px'>\n";

			echo "    <tr class='headline'><td class='hsc2_l'>Manager</td><td class='hsc2'>1st</td><td class='hsc2'>2nd</td><td class='hsc2'>3rd</td><td class='hsc2'>4th</td><td class='hsc2'>5th</td><td class='hsc2'>6th</td><td class='hsc2'>7th</td><td class='hsc2'>8th</td><td class='hsc2'>9th</td><td class='hsc2'>10th</td><td class='hsc2'>Score</td>";

			if ($showGMVote==1) {echo "<td class='hsc2'>Your Vote</td>";}

			echo "</tr>\n";

			

			$award='gm';

			$awID=300;			

			$slid=0;

			echo "    <tr class='title'><td colspan=".(12+$showGMVote).">".$awardName[$award]."</td></tr>\n";

			$cnt=0;

			

			$query="SELECT hm.human_manager_id,first_name,last_name,first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth,score,who FROM human_managers as hm,(SELECT player_id,position,SUM(first) as first,SUM(second) as second,SUM(third) as third,SUM(fourth) as fourth,SUM(fifth) as fifth,SUM(sixth) as sixth,SUM(seventh) as seventh,SUM(eighth) as eighth,SUM(ninth) as ninth,SUM(tenth) as tenth,SUM(score) as score,MAX(who) as who FROM (SELECT player_id,position,if(vote=1,1,0) as first,if(vote=2,1,0) as second,if(vote=3,1,0) as third,if(vote=4,1,0) as fourth,if(vote=5,1,0) as fifth,if(vote=6,1,0) as sixth,if(vote=7,1,0) as seventh,if(vote=8,1,0) as eighth,if(vote=9,1,0) as ninth,if(vote=10,1,0) as tenth,".get_vote_query($votes[$award])." as score,if(human_manager_id=$hmid,vote,0) as who FROM awards_vote WHERE league_id=$lgid AND year=$yr AND sub_league_id=$slid AND award_id=$awID";

			if (($gmID!="")&&($_SESSION['user_is_commish'])) {$query.=" AND human_manager_id=$gmID";}

			$query.=") as t GROUP BY player_id) as x WHERE hm.human_manager_id=x.player_id ORDER BY position,score DESC";

			if ($_GET['showq']==1) {echo "$query<br/>\n";}

			$result=mysqli_query($db,$query);

			if (($lgyear==$yr)&&($voted[$hmid][$awID][$slid]!=1)&&($awResults==0)&&($_SESSION['user_is_commish']!=1)) {echo "    <tr class='s2'><td class='".$cls."_l' colspan=".(12+$showGMVote)." style='text-align:center;'>You have not voted for this award</td>";}

			elseif (mysqli_num_rows($result))

			{

				while ($row=mysqli_fetch_array($result))

				{

					$pid=$row['human_manager_id'];

					$name=$row['first_name']." ".$row['last_name'];

					

					$cls='s'.($cnt%2+1);

					echo "    <tr class='$cls'>";

					echo "<td class='".$cls."_l'><a href='$lgpath/coaches/human_manager_$pid.html'>$name</a></td>";

					if ($row['first']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['first']."</td>";}

					if ($row['second']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['second']."</td>";}

					if ($row['third']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['third']."</td>";}

					if ($row['fourth']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['fourth']."</td>";}

					if ($row['fifth']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['fifth']."</td>";}

					if ($row['sixth']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['sixth']."</td>";}

					if ($row['seventh']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['seventh']."</td>";}

					if ($row['eighth']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['eighth']."</td>";}

					if ($row['ninth']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['ninth']."</td>";}

					if ($row['tenth']==0) {echo "<td>&nbsp;</td>";} else {echo "<td>".$row['tenth']."</td>";}

					echo "<td>".$row['score']."</td>";

					if ($showGMVote==1)

					{

						if ($row['who']==0) {echo "<td>&nbsp;</td>";}

						elseif ($votes[$award]==1) {echo "<td>X</td>";}

						else {echo "<td>".$row['who']."</td>";}

					}

					echo "</tr>\n";

					

					$cnt++;

				}

			}

			else {echo "    <tr class='s2'><td class='".$cls."_l' colspan=".(12+$showGMVote)." style='text-align:center;'>No votes recorded</td>";}

			echo "   </table>\n";

			echo "  </td></tr>\n";

			echo " </table>\n";

			echo "</div>\n";

		}

		

		

		echo "  </td></tr>\n";

		echo " </table>\n";

		echo "</div>\n";		

	}

  



  mysqli_close($db);



  ################## END OF PAGE #########################

  echo " </div>   <!-- END contentpane DIV -->\n";

  page_foot($_SESSION['lgpath']);





?>

