<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];

  

  $lgpath=$_SESSION['lgpath'];

  $adminpath=$_SESSION['adminpath'];

  $htmlpath=$_SESSION['htmlpath'];

  $lgyear=$_SESSION['lgyear'];



  $module='HOF';



  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

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

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

  if (mysqli_num_rows($result)) 

  {

	  # Fix hofEnable setting

	  $query="UPDATE ootp_sql_admin SET setting='hofVoteEnable' WHERE module='HOF' AND setting='hofEnable'";

	  mysqli_query($db,$query);



	  # Get Settings

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

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

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

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

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

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

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

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

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

	  

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

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

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

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

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

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

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

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

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

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

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

	  

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

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

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

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

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

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

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

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

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

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

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

	      

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

			  }

		  }

	  }

  }

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

  if ($minSea=="")     {$minSea=1;}

  if ($maxYrsBlt=="")  {$maxYrsBlt=15;}

  if ($numVotes=="")   {$numVotes=10;}

  if ($numVotes<1)     {$numVotes=10;}

  if ($maxPerc=="")    {$maxPerc=75;}

  elseif ($maxPerc<0) {$maxPerc=75;}

  elseif ($maxPerc>100) {$maxPerc=100;}

  if ($minPerc=="")    {$minPerc=5;}

  elseif ($minPerc<0) {$minPerc=5;}

  elseif ($minPerc>100) {$minPerc=100;}



  if ($bminHs=="")     {$bminHs=2000;}

  if ($bminHRs=="")    {$bminHRs=300;}

  if ($bminSBs=="")    {$bminSBs=300;}

  if ($bminAVG=="")    {$bminAVG=0.300;}

  if ($bminAVGPA=="")  {$bminAVGPA=1500;}

  if ($bminOPS=="")    {$bminOPS=0.750;}

  if ($bminOPSPA=="")  {$bminOPSPA=1500;}

  if ($bminHOFs=="")   {$bminHOFs=35;}

  if ($bminPA=="")     {$bminPA=2000;}

  $incBs=trim($incBs,", ");

  $excBs=trim($excBs,", ");



  if ($pminWs=="")     {$pminWs=150;}

  if ($pminKs=="")     {$pminKs=2000;}

  if ($pminSVs=="")    {$pminSVs=300;}

  if ($pminWPct=="")   {$pminWPct=0.525;}

  if ($pminWPctIP=="") {$pminWPctIP=800;}

  if ($pminHOFs=="")   {$pminHOFs=35;}

  if ($pmaxERA=="")    {$pmaxERA=5.00;}

  if ($pminIPs=="")    {$pminIPs=1000;}

  if ($pminIPr=="")    {$pminIPr=500;}

  $incPs=trim($incPs,", ");

  $excPs=trim($excPs,", ");



  mysqli_close($db);



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

  $title='Hall of Fame Voting Admin';



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

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

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



  if ($_SESSION['user_is_commish']!=1)

  {

	  nav_toolbar("hof","Hall of Fame Voting");

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

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

	  echo "You must be a commisioner to access this page";

	

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

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

	  page_foot($_SESSION['lgpath']);

	  exit;

  }

  else

  {

	  nav_toolbar('admin','Admin Pages');

	  nav_toolbar("hof","Hall of Fame Voting");

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

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

  }



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

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



  echo "   <FORM ACTION=\"save_settings.php\" METHOD=\"post\">\n";

  echo "    <input type='hidden' name='module' value='HOF'>\n";

  if ($_GET['debug']==1) {echo "    <input type='hidden' name='debug' value='1'>\n";}

  echo "    <table cellpadding=2 cellspacing=0>";

  echo "     <TR class='title2'><TD colspan=5>Hall of Fame Voting Settings</TD></TR>\n";

  echo "     <TR><TD colspan=3>Enable Voting:</TD><TD colspan=2 align='left'><INPUT type='checkbox' name='hofVoteEnable' value=1";

  if ($hofVoteEnable==1) {echo " checked";}

  echo "></TD></TR>\n";

  echo "     <TR><TD colspan=3>Show Results To All (else, only after voting):</TD><TD colspan=2 align='left'><INPUT type='checkbox' name='hofResults' value=1";

  if ($hofResults==1) {echo " checked";}

  echo "></TD></TR>\n";

  echo "     <TR class='title'><TD colspan=5>General Settings</TD></TR>\n";

  echo "     <TR><TD colspan=3>Minimum Years Since Retirement:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='minYrsRet' value='".$minYrsRet."'></TD></TR>\n";

  echo "     <TR><TD colspan=3>Minimum Seasons Appeared In:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='minSea' value='".$minSea."'></TD></TR>\n";

  echo "     <TR><TD colspan=3>Maximum Number of Years on Ballot:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='maxYrsBlt' value='".$maxYrsBlt."'></TD></TR>\n";

  echo "     <TR><TD colspan=3>Maximum Number of Votes per Ballot:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='numVotes' value='".$numVotes."'></TD></TR>\n";

  echo "     <TR><TD colspan=3>Percentage of Votes Required for Election:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=3 name='maxPerc' title='Enter a value 0-100' value='".$maxPerc."'></TD></TR>\n";

  echo "     <TR><TD colspan=3>Minimum Percentage of Votes to Stay on Ballot:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=3 name='minPerc' title='Enter a value 0-100' value='".$minPerc."'></TD></TR>\n";



  echo "     <TR class='title'><TD colspan=5>Batter Qualifications</TD></TR>\n";

  echo "     <TR class='headline'><TD class='hsc2_l' colspan=5>Include batters with any of the following</TD></TR>\n";

  echo "     <TR><TD colspan=3>Hits:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='bminHs' value='".$bminHs."'></TD></TR>\n";

  echo "     <TR><TD colspan=3>Home Runs:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='bminHRs' value='".$bminHRs."'></TD></TR>\n";

  echo "     <TR><TD colspan=3>Stolen Bases:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='bminSBs' value='".$bminSBs."'></TD></TR>\n";

  echo "     <TR><td>Minimum</td><td><INPUT type='text' size=5 maxlength=5 name='bminAVG' value='".$bminAVG."'></td><td>Batting Average in</td><td><INPUT type='text' size=5 maxlength=5 name='bminAVGPA' value='".$bminAVGPA."'></td><td>plate appearances</td></TD></TR>\n";

  echo "     <TR><td>Minimum</td><td><INPUT type='text' size=5 maxlength=5 name='bminOPS' value='".$bminOPS."'></td><td>OPS in</td><td><INPUT type='text' size=5 maxlength=5 name='bminOPSPA' value='".$bminOPSPA."'></td><td>plate appearances</td></TD></TR>\n";

//  echo "     <TR><TD colspan=3>HOF Standards:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='bminHOFs' value='".$bminHOFs."'></TD></TR>\n";

  echo "     <TR class='headline'><TD class='hsc2_l' colspan=5>Batters must meet each of the following</TD></TR>\n";

  echo "     <TR><TD colspan=3>Minimum Plate Appearances:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='bminPA' value='".$bminPA."'></TD></TR>\n";

  echo "     <TR class='headline'><TD class='hsc2_l' colspan=5>Players not meeting above criteria</TD></TR>\n";

  echo "     <TR><TD colspan=3>Always include these players:</TD><TD colspan=2 align='left'><INPUT type='text' size=30 title='comma separated player ID list' name='incBs' value='".$incBs."'></TD></TR>\n";

  echo "     <TR><TD colspan=3>Always exclude these players:</TD><TD colspan=2 align='left'><INPUT type='text' size=30 title='comma separated player ID list' name='excBs' value='".$excBs."'></TD></TR>\n";



  echo "     <TR class='title'><TD colspan=5>Pitcher Qualifications</TD></TR>\n";

  echo "     <TR class='headline'><TD class='hsc2_l' colspan=5>Include Pitchers with any of the following</TD></TR>\n";

  echo "     <TR><TD colspan=3>Wins:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='pminWs' value='".$pminWs."'></TD></TR>\n";

  echo "     <TR><TD colspan=3>Strikeouts:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='pminKs' value='".$pminKs."'></TD></TR>\n";

  echo "     <TR><TD colspan=3>Saves:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='pminSVs' value='".$pminSVs."'></TD></TR>\n";

  echo "     <TR><td>Minimum</td><td><INPUT type='text' size=5 maxlength=5 name='pminWPct' value='".$pminWPct."'></td><td>Winning Percentage in</td><td><INPUT type='text' size=5 maxlength=5 name='pminWPctIP' value='".$pminWPctIP."'></td><td>innings pitched</td></TD></TR>\n";

//  echo "     <TR><TD colspan=3>HOF Standards:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='pminHOFs' value='".$pminHOFs."'></TD></TR>\n";

  echo "     <TR class='headline'><TD class='hsc2_l' colspan=5>Pitchers must meet each of the following</TD></TR>\n";

  echo "     <TR><TD colspan=3>Maximum ERA:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='pmaxERA' value='".$pmaxERA."'></TD></TR>\n";

  echo "     <TR><TD colspan=3>Minimum Innings Pitched for Starters:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='pminIPs' value='".$pminIPs."'></TD></TR>\n";

  echo "     <TR><TD colspan=3>Minimum Innings Pitched for Relievers:</TD><TD colspan=2 align='left'><INPUT type='text' size=5 maxlength=5 name='pminIPr' value='".$pminIPr."'></TD></TR>\n";

  echo "     <TR><TD colspan=5><i>(Reliever definition: (G-GS)>GS and (IP/G)<4.00)</i></TD></TR>\n";

  echo "     <TR class='headline'><TD class='hsc2_l' colspan=5>Players not meeting above criteria</TD></TR>\n";

  echo "     <TR><TD colspan=3>Always include these players:</TD><TD colspan=2 align='left'><INPUT type='text' size=30 title='comma separated player ID list' name='incPs' value='".$incPs."'></TD></TR>\n";

  echo "     <TR><TD colspan=3>Always exclude these players:</TD><TD colspan=2 align='left'><INPUT type='text' size=30 title='comma separated player ID list' name='excPs' value='".$excPs."'></TD></TR>\n";



  echo "     <TR class='title'><TD colspan=5>&nbsp;</TD></TR>\n";

  echo "     <TR><TD colspan=5><INPUT type=\"submit\" value=\"Save\"></TD></TR>\n";

  echo "   </FORM>\n";



  echo "     <TR><TD></TD><TD colspan=4>\n";

  echo "     <TR><TD colspan=3></TD><TD colspan=2>\n";

  echo "      <form action=\"save_settings.php\" method=\"post\">\n";

  echo "       <input type='hidden' name='module' value='HOF Generate'>\n";

  if ($_GET['debug']==1) {echo "    <input type='hidden' name='debug' value='1'>\n";}

  echo "       <input type='submit' value='(Re)Generate HOF Ballot'>\n";

  echo "      </form>\n";

  echo "     </TD></TR>\n";

  echo "     <TR><TD colspan=3></TD><TD colspan=2>\n";

  echo "      <form method='get' action='./hofSubmit.php'>\n";

  echo "       <input type='hidden' name='clear_votes' value=1 />\n";

  echo "       <input type='hidden' name='lgid' value='$lgid' />\n";

  echo "       <input type='hidden' name='year' value=$lgyear />\n";

  echo "       <INPUT type='submit' value=\"Clear This Year's Votes\">\n";

  echo "      </form>\n";

  echo "     </TD></TR>\n";

  echo "     <TR><TD colspan=3></TD><TD colspan=2>\n";

  echo "      <form method='get' action='./hofSubmit.php'>\n";

  echo "       <input type='hidden' name='clear_votes' value=1 />\n";

  echo "       <input type='hidden' name='lgid' value='$lgid' />\n";

  echo "       <INPUT type='submit' value='Clear All Voting History'>\n";

  echo "      </form>\n";

  echo "     </TD></TR>\n";

  echo "    </table>";

  echo "  </div>\n";



  ##### Display Current Ballot List #####

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



  echo "  <div class='textbox' style='float:right;clear:right;'>\n";

  echo "    <table cellpadding=0 cellspacing=0>";

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

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

  echo "      <table cellpadding=2 cellspacing=0 border=0 style='width:400px;' class='sortable'>\n";

  $query="SELECT p.player_id,if(p.position=1,role,p.position) as position,if(p.position=1,2,1) as BorP,first_name,last_name FROM players as p,awards_vote as av WHERE p.player_id=av.player_id AND av.award_id=100 AND av.league_id=$lgid AND av.year=$lgyear AND human_manager_id=-1 ORDER BY BorP,last_name,first_name";

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

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

  if (mysqli_num_rows($result)>0)

  {

	  echo "       <thead><tr class='headline'><td class='hsc2_l'>PID</td><td class='hsc2_l'>Pos</td><td class='hsc2_l'>Player</td></tr></thead>\n";



	  $rowCnt=0;

	  while ($row=mysqli_fetch_array($result))

	  {

		  $pid=$row['player_id'];



		  $cls='s'.($rowCnt%2+1)."_l";

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

		  echo "<td>$pid</td>";

		  echo "<td sorttable_customkey=".$row['position'].">".get_pos($row['position'],1)."</td>";

		  echo "<td sorttable_customkey=\"".$row['last_name'].", ".$row['first_name']."\"><a href='./player.php?player_id=$pid'>".$row['first_name']." ".$row['last_name']."</a></td></tr>\n";



		  $rowCnt++;

	  }

  }

  else {echo "       <tr><td colspan=3 style='text-align:center;'>- No players on $lgyear ballot -</td></tr>\n";}

  echo "      </table>\n";

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

  echo "    </table>\n";

  echo "  </div>\n";





  ##### Display Summary of Users #####

  ## Get leagues

  $query="SELECT l.league_id,name FROM leagues as l,human_managers as hm WHERE l.league_id=hm.league_id";

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

  if (mysqli_num_rows($result)>0)

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $uLeagueID=$row['league_id'];

		  $leagues[$uLeagueID]['name']=$row['name'];

	  }

  }

  ## Get users

  $query="SELECT hm.human_manager_id,t.team_id,t.name,t.nickname,first_name,last_name,hm.league_id FROM human_managers AS hm,teams AS t WHERE hm.team_id=t.team_id UNION SELECT human_manager_id,0 as team_id,IF(is_commish=1,'Commissioner','No team') as name,'' as nickname,first_name,last_name,9999-is_commish as league_id FROM human_managers WHERE team_id=0 ORDER BY league_id,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))

	  {

	    	  if (($showAllUsers!=1)&&($row['name']=='No team')) {continue;}

		  $hmid=$row['human_manager_id'];

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

		  $managers[$hmid]['team']=$row['name']." ".$row['nickname'];

		  $managers[$hmid]['lgid']=$row['league_id'];

		  $managers[$hmid]['tid'] =$row['team_id'];

	  }

  }

  ## Get results

  $query="SELECT human_manager_id,COUNT(DISTINCT player_id) AS cnt,MIN(player_id) as minPid FROM awards_vote WHERE league_id=$lgid AND year=$lgyear AND award_id=100 AND human_manager_id>0 GROUP BY human_manager_id";

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

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

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $hmid=$row['human_manager_id'];

		  if ($row['minPid']>0) {$managers[$hmid]['vote']=$row['cnt'];}

		  else {$managers[$hmid]['vote']=0;}

	  }

  }



  ## Display

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

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

  echo "    <tr class='title2'><td colspan=4>Voting Detail</td></tr>\n";

  echo "    <tr class='headline'><td class='hsc2_l'>User</td><td class='hsc2_l'>Team</td><td class='hsc2'>Voted</td><td class='hsc2'>Ballot Size</td></tr>\n";

  $mCnt=1;

  $oldHMLeagueID=-1;

  foreach ($managers as $hmid => $val)

  {

	  $hmLeagueID=$managers[$hmid]['lgid'];



	  if ($hmLeagueID!=$oldHMLeagueID)

	  {

		  $mCnt=1;

		  echo "     <tr class='headline'><td class='hsc2_l' colspan=4>";

		  if ($hmLeagueID==9998) {echo "Unaffiliated Commissioners";}

		  elseif ($hmLeagueID==9999) {echo "Unaffiliated Users";}

		  else {echo $leagues[$hmLeagueID]['name'];}

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

	  }



	  $voteCnt=$managers[$hmid]['vote'];

	  if (isset($managers[$hmid]['vote']))

	  {

		  $ballotText="<a href='./hall_of_fame_vote.php?page=results&human_manager_id=$hmid'>Yes</a> (<a title='Clear ballot' href='./hofSubmit.php?clear_votes=1&human_manager_id=$hmid&lgid=$lgid&year=$lgyear'>X</a>)";

	  }

	  else

	  {

		  $ballotText="No";

		  $voteCnt="-";

	  }



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

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

	  echo "<td class='".$cls."_l'";

	  if (!is_numeric($voteCnt)) {echo " style='color:red;'";}

	  echo ">".$managers[$hmid]['name']."</td>";

	  echo "<td class='".$cls."_l'";

	  if (!is_numeric($voteCnt)) {echo " style='color:red;'";}

	  echo ">".$managers[$hmid]['team']."</td>";

	  echo "<td class='$cls'";

	  if (!is_numeric($voteCnt)) {echo " style='color:red;'";}

	  echo ">$ballotText</td>";

	  echo "<td class='$cls'";

	  if (!is_numeric($voteCnt)) {echo " style='color:red;'";}

	  echo ">$voteCnt</td>";

	  echo "</tr>\n";

	  $mCnt++;

	  $oldHMLeagueID=$hmLeagueID;

  }



  echo "   </table>\n";

  echo "  </div>\n";



  mysqli_close($db);



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

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

  page_foot($_SESSION['lgpath']);





?>

