<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];

  $lgpath=$_SESSION['lgpath'];

  $htmlpath=$_SESSION['htmlpath'];

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

  $year=$date->format("Y");

  $rnd=(isset($_GET['rnd'])?$_GET['rnd']:0);

  if ((!is_numeric($rnd))||($rnd<1)) {$rnd=1;}

  if (!isset($_GET['debug'])) {$_GET['debug']=0;}


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

  $title='Draft Order Editor';



  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("draft","Draft");

	  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("draft","Draft");

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

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

  }



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



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



  ##### Display Round Selector

  $query="SELECT MAX(round) as maxR FROM amateur_draft WHERE league_id=$lgid";

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

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

  if (mysqli_num_rows($result))

  {

	  $row=mysqli_fetch_array($result);

	  $maxR=$row['maxR'];



	  if ($maxR>1)

	  {

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

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

		  echo "    <tr class='title'><td colspan=2>Round to Display</td></tr>\n";

		  echo "    <tr>\n";

		  echo "     <td title='Supplemental rounds are included with the round they follow'>Round:</td>\n";

		  echo "     <td title='Supplemental rounds are included with the round they follow'>\n";

		  echo "      <select name='rnd' onchange=\"document.location.href = './draftOrder.php?rnd=' + this.value\">\n";

		  for ($i=1;$i<=$maxR;$i++)

		  {

			  echo "       <option value=$i";

			  if ($i==$rnd) {echo " selected";}

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

		  }

		  echo "      </select>\n";

		  echo "     </td>\n";

		  echo "    </tr>\n";

		  echo "   </table>\n";

		  echo "  </div>\n";

	  }

  }



  ##### Display Order Editor

  $module="Draft_Order";

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



  ## Get picks

  $query="SELECT supplemental,pick_round,team_id,player_id,pick_time FROM amateur_draft WHERE league_id=$lgid AND round=$rnd ORDER BY round,supplemental,pick_round";

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

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

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $supp=$row['supplemental'];

		  $pick=$row['pick_round'];

		  $tid=$row['team_id'];

		  $pid=$row['player_id'];

		  $due=$row['pick_time'];



		  $picks[$rnd][$supp][$pick]['tid']=$tid;

		  $picks[$rnd][$supp][$pick]['pid']=$pid;

		  $picks[$rnd][$supp][$pick]['due']=$due;

	  }

  }



  ## Cancel out if round is unscehduled.

  if (!isset($picks))

  {

	  echo "No draft picks found for Round $rnd.<br/><br/>";

	  echo "Either reschedule the draft or choose a round that has picks scheduled";

	  echo "  </div>\n";

	

	  mysqli_close($db);

	

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

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

	  page_foot($_SESSION['lgpath']);

	  exit;

  }



  ## Check If Timer Is Enabled

  $query="SELECT setting,value FROM ootp_sql_admin WHERE scope=$lgid AND module='Draft' AND setting='timerEnable'";

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

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $setting=$row['setting'];

		  $value=$row['value'];



		  switch ($setting)

		  {

		  case 'timerEnable':

			  $timerOn=$value;

			  break;

		  }

	  }

  }

	

  ## Get Teams

  $query="SELECT team_id,name,nickname FROM teams WHERE league_id=$lgid AND allstar_team=0 ORDER BY name,nickname";

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

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

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $tid=$row['team_id'];

		  $teams[$tid]['name']=$row['name']." ".$row['nickname'];

	  }

  }



  ## Display Pick Order

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

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

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

	

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

  echo "     <tr class='title'><td colspan=4>Draft Order</td></tr>\n";

  echo "     <tr class='headline'>";

  echo "<td class='hsc2'>Rnd</td>";

  echo "<td class='hsc2'>Pick</td>";

  echo "<td class='hsc2_l'>Team</td>";

  if ($timerOn==1) {echo "<td class='hsc2_l' title='24 HR Format: YYYY-MM-DD hh:mm:ss'>Due/Taken At</td>\n";}

  echo "</tr>\n";



  $lsupp=0;

  foreach ($picks as $rnd => $valr)

  {

	  foreach ($picks[$rnd] as $supp => $vals)

	  {

		  $cnt=0;

		  foreach ($picks[$rnd][$supp] as $pick => $valp)

		  {

			  $tid=$picks[$rnd][$supp][$pick]['tid'];

			  $pid=$picks[$rnd][$supp][$pick]['pid'];

			  $due=$picks[$rnd][$supp][$pick]['due'];



			  if ($supp!=$lsupp)

			  {

				  echo "     <tr class='headline'>";

				  echo "<td class='hsc2'>Rnd</td>";

				  echo "<td class='hsc2'>Pick</td>";

				  echo "<td class='hsc2_l'>Team</td>";

				  if ($timerOn==1) {echo "<td class='hsc2_l' title='24 HR Format: YYYY-MM-DD hh:mm:ss'>Due/Taken At</td>\n";}

				  echo "</tr>\n";

			  }

			  if ($supp==1) {$rndDisp="S".$rnd;} else {$rndDisp=$rnd;}



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

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

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

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

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

			  echo " <select name='team_".$rnd."_".$supp."_".$pick."'";

			  if ((is_numeric($pid))&&($pid>0)) {echo " disabled='disabled'";}

			  echo ">\n";

			  echo "  <option value=-1>-------- Delete This Pick --------</option>\n";

			  foreach ($teams as $team_id => $val)

			  {

				  echo "  <option value=$team_id";

				  if ($team_id==$tid) {echo " selected";}

				  echo ">".$teams[$team_id]['name']."</option>\n";

			  }

			  echo " </select>\n";

			  echo "</td>\n";

			  if ($timerOn==1)

			  {

				  echo "<td class='".$cls."_l' title='24 HR Format: YYYY-MM-DD hh:mm:ss'>\n";

				  echo " <input type='text' name='due_".$rnd."_".$supp."_".$pick."' value='$due'";

				  if ((is_numeric($pid))&&($pid>0)) {echo " disabled='disabled'";}

				  echo ">\n";

				  echo "</td>\n";

			  }

			  echo "</tr>\n";



			  $lsupp=$supp;

			  $cnt++;

		  }

	  }

  }



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

  echo "    </table>";

  echo "   </FORM>\n";

  echo "  </div>\n";



  mysqli_close($db);



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

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

  page_foot($_SESSION['lgpath']);





?>

