<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];

  $rnd=$_GET['rnd'];

  $supp=$_GET['supp'];

  $pick=$_GET['pick'];



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

  $title='Draft - Manual Pick 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("draft","Draft");

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

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

  }



  ################## PAGE CONTENT #########################

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



  ## Get Pick Detail

  $query="SELECT t.team_id,ad.player_id,name,nickname FROM amateur_draft as ad,teams as t WHERE ad.team_id=t.team_id AND ad.league_id=$lgid AND round=$rnd AND supplemental=$supp AND pick_round=$pick";

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

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

  if (mysqli_num_rows($result))

  {

	  $row=mysqli_fetch_array($result);

	  $tid=$row['team_id'];

	  $player_id=$row['player_id'];

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

  }



  ##### Generate Selection Form

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

  echo " <form method='GET' action='./draftProcess.php'>\n";

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

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

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

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

  echo "  <input type='hidden' name='mode' value='Edit' />\n";



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

  echo "  <tr class='title'><td colspan=2>Player Selection</td></tr>\n";

  $rndLbl="Round $rnd";

  if ($supp==1) {$rndLbl="Supplemental to ".$rndLbl;}

  echo "  <tr><td colspan=2>$rndLbl, Pick $pick - <a href='".$_SESSION['lgpath']."/teams/team_$tid.html'>".$teams[$tid]['name']."</a></td></tr>\n";

  echo "  <tr>\n";

  echo "   <td>\n";

  echo "    Draft Player:";

  echo "   </td>\n";

  echo "   <td>\n";



  ## Display Available Players

  $query="SELECT p.player_id,first_name,last_name,position,role,date_of_birth,age FROM players as p,(SELECT dp.player_id FROM ootp_sql_draft_pool as dp LEFT JOIN amateur_draft as ad ON dp.player_id=ad.player_id WHERE dp.league_id=$lgid AND (ad.player_id IS NULL";

  if (isset($player_id)) {$query.=" OR ad.player_id=$player_id";}

  $query.=")) as x WHERE p.player_id=x.player_id ORDER BY last_name,first_name";

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

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

  if (mysqli_num_rows($result))

  {

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

	  echo " <option value=0>Please select a player</option>\n";

	  while ($row=mysqli_fetch_array($result))

	  {

		  $pid=$row['player_id'];

		  $pos=$row['position'];

		  if ($pos==1) {$pos=$row['role'];}



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

		  if ($pid==$player_id) {echo " selected";}

		  echo ">";



		  echo get_pos($pos,1)." ".$row['first_name']." ".$row['last_name'];

		  echo " - ".$row['age']." yrs old (".strftime("%m/%d/%Y",strtotime($row['date_of_birth'])).")";

		  echo "</option>\n";

	  }

	  echo "</select>\n";

  }

  echo "   </td>\n";

  echo "  </tr>\n";

  echo "  <tr><td colspan=2><input type='submit' value='Select Player' /></td></tr>\n";

  echo " </table>\n";



  echo " </form>\n";

  echo "</div>\n";



  mysqli_close($db);



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

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

  page_foot($_SESSION['lgpath']);





?>

