<?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_organization_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 Eligible";

  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='ruleFive.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.draft_team_id,
p.league_id,
p.position,
p.role,
prs.years_protected_from_rule_5,
prs.pro_service_years,
prs.pro_service_days,
prs.is_active,
prs.is_on_secondary,
prs.mlb_service_years,
prs.mlb_service_days,
prs.secondary_service_years,
prs.secondary_service_days,
prs.designated_for_assignment,
prs.days_on_dfa_left,
p.injury_is_injured,
prs.is_on_dl,
prs.is_on_dl60

FROM 
(SELECT player_id,first_name,last_name,age,team_id,organization_id,draft_team_id,league_id,position,role,injury_is_injured FROM 

players WHERE ";
	if (is_numeric($team_id)) {$query.="team_id IN (".$tmList.")";} else {$query.="league_id IN ($lgList)";}
	$query.=") as p,
(
	SELECT * FROM players_roster_status
	WHERE ";
	if (is_numeric($team_id)) {$query.="team_id IN (".$tmList.")";} else {$query.="league_id IN ($lgList)";}
	$query.="
	 AND is_on_secondary=0
	 AND (
		(years_protected_from_rule_5=0)
		OR
		(years_protected_from_rule_5=4 AND pro_service_years>=3)
		OR
		(years_protected_from_rule_5=5 AND pro_service_years>=4)
	     )
) as prs
WHERE p.player_id=prs.player_id
AND (injury_is_injured=0 OR (is_on_dl=0 AND is_on_dl60=0))
ORDER BY team_id,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 Exposed to Rule 5 Draft</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 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']);

?>

