<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];

  $lgpath=$_SESSION['lgpath'];

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

  $lgyear=$_SESSION['lgyear'];



  $hmid=$_SESSION['user_id'];

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

  $subleagueID=$_GET['sub_league_id'];

  $position=$_GET['position'];

  $year=$_GET['year'];

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

  $minYr=$lgyear;

  $maxYr=$lgyear-1;



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



  # Check if awards vote table exists

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

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

  if (!mysqli_num_rows($result))

  {

	  ##### Initialize Table #####

	  $query="CREATE TABLE IF NOT EXISTS awards_vote (player_id INT, league_id INT, sub_league_id INT, year SMALLINT, human_manager_id INT, award_id SMALLINT, position SMALLINT, vote INT DEFAULT 0, PRIMARY KEY (player_id, league_id, sub_league_id, year, human_manager_id, award_id));";

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

  }

  # Check Admin Table

  $module='All-Star';

  $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 'splitOF'    : $splitOF=$val;     break;

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

			  }

		  }

	  }

  }

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

  if (($DHconfig!=1)&&($DHconfig!=2)) {$DHconfig=0;}



  #Configure position array

  $query="SELECT year,position FROM awards_vote WHERE league_id=$lgid AND award_id=9";

  if ($year!="ALL") {$query.=" AND year=$year";}

  $query.=" GROUP BY year,position";

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

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

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $yr=$row['year'];

		  $pos=$row['position'];

		  $posList[$pos]=1;

		  $yrPos[$yr][$pos]=1;

	  }

  }

  $posArray=Array(2,3,4,5,6);

  if ((isset($posList[7]))||(isset($posList[8]))||(isset($posList[9])))

  {

	  array_push($posArray,7);

	  array_push($posArray,8);

	  array_push($posArray,9);

  }

  if (isset($posList[20])) {array_push($posArray,20);}

  if (isset($posList[10])) {array_push($posArray,10);}

  array_push($posArray,11);

  array_push($posArray,21);



  # Get leagues

  $showDH=0;

  $query="SELECT sub_league_id,name,designated_hitter as dh 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'];

	  $subleagues[$sub_league_id]['origDH']=$row['dh'];

	  $slName=str_replace(".","",$slName);

    	  $slName=str_replace(" ","_",$slName);

	  $subleagues[$sub_league_id]['imgname']=$slName;

	  switch ($DHconfig)

	  {

	  case 1: $subleagues[$sub_league_id]['dh']=1; break;

	  case 2: $subleagues[$sub_league_id]['dh']=0; break;

	  default: $subleagues[$sub_league_id]['dh']=$row['dh']; break;

	  }

	  if ($subleagues[$sub_league_id]['dh']==1) {$showDH=1;}

	  $SLcnt++;

  }



  mysqli_close($db);



  # Title Bar

  $title="All-Star Voting History";



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

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

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

  nav_toolbar("allstar","All-Star Voting");

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

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

  

  ################## 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='asVotingHist.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")&&($subleagueID!="")) {echo " selected";}

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

  }

  echo "     </select>\n";

  echo "    </td>\n";

  

  ## Show Position Filter

  echo "    <td><label for='position'>Position:</label></td>\n";

  echo "    <td>\n";

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

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

  foreach ($posArray as $key => $pos)

  {

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

	  if ($position==$pos) {echo " selected";}

       	  echo ">".get_pos($pos,1)."</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";

  $query="SELECT allstar_date,IF( (allstar_date<'".$_SESSION['lgdate']."') OR (YEAR(allstar_date)>$lgyear) ,1,0) as asDone FROM leagues WHERE league_id=$lgid";

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

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

  $row=mysqli_fetch_array($result);

  if ($row['asDone']==1)

  {

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

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

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

	  $maxYr=$lgyear;

  }

  $query="SELECT DISTINCT(year) as year FROM awards_vote WHERE league_id=$lgid AND year<$lgyear AND award_id=9 ORDER BY year DESC;";

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

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

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $yr=$row['year'];

		  $minYr=min($yr,$minYr);

		  $maxYr=max($yr,$maxYr);

		  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";



  ##### Close out if no filter defined

  if (($year=="")||($subleagueID=="")||($position==""))

  {

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

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

	  page_foot($_SESSION['lgpath']);

	  exit;

  }



  if (is_numeric($year))

  {

	  $minYr=$year;

	  $maxYr=$year;

  }



  $showGMVote=0;

  if ($hmid>0) {$showGMVote=1;}



  ##### Loop Through Years to Display #####

  for ($yr=$maxYr;$yr>=$minYr;$yr--)

  {

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

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

	  echo "  <tr class='title2'><td style='padding:2px;'>$yr</td></tr>\n";

	  echo "  <tr><td style='padding:10px;'>\n";



	  unset($maxPos);



	  # Votes

	  $query="SELECT first_name,last_name,team_id,organization_id,x.* FROM players as p,(SELECT position,sub_league_id,player_id,SUM(vote) as vote,MAX(who) as who FROM (SELECT player_id,sub_league_id,position,vote,if(human_manager_id=$hmid,1,0) as who FROM awards_vote WHERE league_id=$lgid AND year=$yr AND award_id=9";

	  if ($subleagueID!='ALL') {$query.=" AND sub_league_id=$subleagueID";}

	  $query.=") as t GROUP BY position,sub_league_id,player_id) as x WHERE p.player_id=x.player_id ORDER BY position,sub_league_id,vote DESC,last_name,first_name";

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

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

	  if (mysqli_num_rows($result))

	  {

		  while ($row=mysqli_fetch_array($result))

		  {

			  $pos=$row['position'];

			  $slid=$row['sub_league_id'];

			  $pid=$row['player_id'];

			  if ($row['who']==1) {$who='X';} else {$who="&nbsp;";}



			  # Determine max votes between leagues for a position

			  $subleagues[$slid][$pos]['asCnt']=$subleagues[$slid][$pos]['asCnt']+1;

			  $maxPos[$pos]=max($subleagues[$slid][$pos]['asCnt'],$maxPos[$pos]);



			  # Get Row Text

			  $cls='s'.(($subleagues[$slid][$pos]['asCnt']-1)%2+1);

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

			  $rowTxt.="<td class='".$cls."_l'><a href='./player.php?player_id=$pid'>".$row['first_name']." ".$row['last_name']."</a></td>";

			  $rowTxt.="<td>".$row['vote']."</td>";

			  if ($showGMVote==1) {$rowTxt.="<td>".$who."</td>";}

			  $rowTxt.="</tr>\n";

			  $subleagues[$slid][$pos]['posTxt']=$subleagues[$slid][$pos]['posTxt'].$rowTxt;

		  }



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

		  echo "  <tr>\n";

		  $slCnt=0;

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

		  {

			  if (($subleagueID!='ALL')&&($subleagueID!=$slid)) {continue;}



			  echo "   <td valign='top' style='border:1px solid black;'>\n";

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

			  echo "     <tr class='title'><td colspan=".(2+$showGMVote).">".$subleagues[$slid]['name']."</td></tr>\n";

	     		  echo "     <tr class='headline'><td class='hsc2_l'>Player</td><td class='hsc2'>Votes</td>";

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

			  echo "</tr>\n";



			  foreach ($posArray as $key => $pos)

			  {

				  if (($position!='ALL')&&($position!=$pos)) {continue;}



				  if (!isset($yrPos[$yr][$pos])) {continue;}



				  if (($showDH!=1)&&($pos==10)) {continue;}

				  echo "     <tr class='headline'><td colspan=".(2+$showGMVote).">".get_pos($pos)."</td></tr>\n";

				  if (!isset($subleagues[$slid][$pos]['asCnt']))

				  {

					  echo "     <tr class='s1_l'><td colspan=".(2+$showGMVote).">No votes were cast for this position</td></tr>\n";

					  for ($i=1;$i<$maxPos[$pos];$i++) {echo "     <tr><td colspan=".(2+$showGMVote).">&nbsp;</td></tr>\n";}

				  }

				  else

				  {

					  echo $subleagues[$slid][$pos]['posTxt'];

					  for ($i=$subleagues[$slid][$pos]['asCnt'];$i<$maxPos[$pos];$i++) {echo "     <tr><td colspan=".(2+$showGMVote).">&nbsp;</td></tr>\n";}

				  }



				  unset($subleagues[$slid][$pos]);

			  }



			  echo "    </table>\n";

			  echo "   </td>\n";

			  

			  $slCnt++;

			  if ($slCnt<count($subleagues)) {echo "   <td width=40>&nbsp;</td>\n";}

		  }

		  echo "  </tr>\n";

		  echo " </table>\n";

	  }



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

	  echo " </table>\n";

	  echo "</div>\n";



	  if (($yr>3000)||($yr<1500)) {echo "OUT OF BOUNDS!";exit;}

  }



  mysqli_close($db);



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

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

  page_foot($_SESSION['lgpath']);

  exit;



?>

