<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];

  $lgpath=$_SESSION['lgpath'];

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

  $lgyear=$_SESSION['lgyear'];

  

  ini_set("memory_limit","64M");

  

	  

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

  ##### Get Report Settings #####

  $team_id=$_GET['team_id'];

  if ($team_id=="All") {$team_id="";}

  $position=$_GET['pos'];

  $dyear=$_GET['year'];

  if ($dyear=="") {$dyear=$lgyear+1;}



  if (($_SESSION['user_team_id']!=$team_id)&&($view=='admin')) {$view='all_blocks';}

  if ($view!="admin")

  {

	  $view='all_blocks';

  }



  ##### Get Teams In League #####

  $query="SELECT team_id,name,nickname,abbr,league_id,level,parent_team_id FROM teams WHERE";

  $query.=" (league_id=$lgid OR parent_team_id IN (SELECT team_id FROM teams WHERE league_id=$lgid AND allstar_team=0))";

  $query.=" AND allstar_team=0 ORDER BY name,nickname;";

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

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

  $tmList="";

  while ($row=mysqli_fetch_array($result))
  {
	  $tid=$row['team_id'];
	  $teams[$tid]['abbr']=$row['abbr'];
	  $teams[$tid]['league_id']=$row['league_id'];
	  $teams[$tid]['name']=$row['name']." ".$row['nickname'];
	  $teams[$tid]['level']=get_level($row['level']);
	  if ((($team_id!="")&&(($tid==$team_id)||($team_id==$row['parent_team_id']))) || (($team_id=="")&&($lgid==$row['league_id']))) {$tmList.=",$tid";}
  }
  $tmList=trim($tmList,",");

  mysqli_close($db);

  # Title Bar
  $title="Rule 5 Draft Ineligible";

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

  echo " <div id='topbar'>\n";
  main_titlebar($lgid,$_SESSION['lgpath'],$title);
  nav_toolbar('front_office',"Front Office");
  echo " </div>   <!-- END topbar DIV -->\n";

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



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

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



##### Display Filters #####
echo " <div class='textbox'>\n";
echo "  <form method='get' action='ruleFiveIneligible.php' style='clear:left;'>\n";
echo "  <table cellspacing=0 cellpadding=2 border=0>\n";
echo "   <tr class='title'><td colspan=5>Filters</td></tr>\n";
echo "   <tr>\n";


## Show Team Filter
echo "    <td><label for='team_id'><b>Team:</b></label></td>\n";
echo "    <td>\n";
echo "     <select name='team_id' id='team_id'>\n";
echo "      <option value='All'>All Teams</option>\n";
foreach ($teams as $tid => $val)
 {
   if ($teams[$tid]['league_id']!=$lgid) {continue;}
   echo "      <option value='$tid'";
   if ($team_id==$tid) {echo " selected";}
   echo ">".$teams[$tid]['name']."</option>\n";
 }
echo "     </select>\n";
echo "    </td>\n";


## Close Form
if ($view!="")
 {
   ## Submission button
   echo "    <td><input type='submit' value='Go'></input></td>\n";
 }
echo "   </tr>\n";
echo "  </table>\n";
echo "  </form>\n";
echo " </div>\n";



##### Get League Settings #####
$lgList="";
$query="SELECT league_id,parent_league_id,league_level FROM leagues WHERE (league_id=$lgid OR parent_league_id=$lgid OR league_id=(SELECT parent_league_id FROM leagues WHERE league_id=$lgid)) and league_level<10 ORDER BY league_level,parent_league_id DESC;";
if ($_GET['showq']==1) {echo $query."<br/>\n";}
$result=mysqli_query($db,$query);
while ($row=mysqli_fetch_array($result))
{
	$league_id=$row['league_id'];
	$parent_league_id=$row['parent_league_id'];
	
	$lgList.=",".$league_id;

	if (($league_id==$lgid)&&($parent_league_id>0)) {$getParentAbbr=$parent_league_id;}
}
$lgList=trim($lgList,",");


if ($getParentAbbr>0)
{
	$query="SELECT team_id,abbr FROM teams WHERE league_id=$getParentAbbr;";
	if ($_GET['showq']==1) {echo $query."<br/>\n";}
	$result=mysqli_query($db,$query);
	while ($row=mysqli_fetch_array($result))
	{
		$tid=$row['team_id'];
		$teams[$tid]['abbr']=$row['abbr'];
	}
}


##### Get Players #####

$query="SELECT p.player_id,
first_name,
last_name,
age,
p.team_id,
p.organization_id,
p.league_id,
p.position,
p.role

FROM players as p,z_rule_5_ineligible as zr5 WHERE p.player_id=zr5.player_id";
    if (is_numeric($team_id)) {$query.=" AND p.organization_id=".$team_id;}
    $query.=" ORDER BY last_name,first_name";

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

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

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

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

echo "   <tr class='title2'><td style='padding:2px;'>Players Currently Rule 5 Draft Ineligible</td></tr>\n";

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

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

$cnt=0;

if (mysqli_num_rows($result))

 {

   echo "     <tr class='headline'>";
   echo "<td class='hsc2_l'>Player</td>";
   echo "<td class='hsc2'>Pos</td>";
   echo "<td class='hsc2'>Age</td>";
   echo "<td class='hsc2'>Org</td>";
   echo "<td class='hsc2'>Tm</td>";
   echo "<td class='hsc2'>Level</td>";
   echo "<td class='hsc2'>Notes</td>";
   echo "</tr>\n";   

   $salYr=$dyear-1;

   while ($row=mysqli_fetch_array($result))

    {

      $pid=$row['player_id'];
      $name=$row['first_name']." ".$row['last_name'];
      $sortname=$row['last_name'].", ".$row['first_name'];
      $age=$row['age'];
      $pos=get_pos($row['position'],1);
      $sortpos=$row['position'];
      $tid=$row['team_id'];
      $orgID=$row['organization_id'];

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

      echo "     <tr class='$cls'>";
      echo "<td class='".$cls."_l' sorttable_customkey=\"".$sortname."\"><a href='$lgpath/players/player_$pid.html'>$name</a></td>";
      echo "<td sorttable_customkey=\"".$sortpos."\">$pos</td>";
      echo "<td>$age</td>";
      echo "<td><a href='$lgpath/teams/team_$orgID.html'>".$teams[$orgID]['abbr']."</a></td>";
      echo "<td><a href='$lgpath/teams/team_$tid.html'>".$teams[$tid]['abbr']."</a></td>";
      echo "<td><a href='$lgpath/teams/team_$tid.html'>".$teams[$tid]['level']."</a></td>";
	  echo "<td>";
	  if ($row['is_on_dl60']==1) {echo "On 60-day DL, now healthy";}
	  elseif ($row['is_on_dl']==1) {echo "On DL, now healthy";}
	  elseif ($row['designated_for_assignment']==1) {echo "DFA, ".$row['days_on_dfa_left']." day".($row['days_on_dfa_left']>1?"s":"")." remaining";}
	  else {echo "&nbsp;";}
	  echo "</td>";
      echo "</tr>\n";

      $cnt++;

    }

 }

if ($cnt==0) {echo "<tr><td>No players are currently Rule 5 draft in    eligible.";}

echo "    </table>\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']);

?>

