<?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="Projected Free Agents";



  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 Year Filter #####

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

echo "  <form method='get' action='projFreeAgents.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 Year Filter

echo "    <td><label for='year'><b>Year:</b></label></td>\n";

echo "    <td>\n";

echo "     <select name='year' id='year'>\n";

for ($yr=($lgyear+1);$yr<=($lgyear+10);$yr++)

 {

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

   if ($dyear==$yr) {echo " selected";}

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

 }

echo "     </select>\n";

echo "    </td>\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 #####

$query="SELECT league_id,parent_league_id,rules_min_service_days,rules_fa_minimum_years,rules_salary_arbitration_minimum_years,rules_minor_league_fa_minimum_years FROM leagues WHERE league_id=$lgid OR league_id=(SELECT parent_league_id FROM leagues WHERE league_id=$lgid) ORDER BY 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'];



	if ($parent_league_id==0)

	{

		$minService=$row['rules_min_service_days'];

		$minFAyrs=$row['rules_fa_minimum_years'];

		$minARByrs=$row['rules_salary_arbitration_minimum_years'];

		$minMLFAyrs=$row['rules_minor_league_fa_minimum_years'];

	}



	if (($league_id==$lgid)&&($parent_league_id>0)) {$getParentAbbr=$parent_league_id;}

}



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 Arbitration Eligible Players #####

$query="SELECT player_id,mlb_service_days,mlb_service_years FROM players_roster_status WHERE mlb_service_days<=($minService*$minFAyrs)";

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

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

while ($row=mysqli_fetch_array($result))

 {

   $pid=$row['player_id'];

   $mlbServ=$row['mlb_service_days'];

   $mlbServYrs=$row['mlb_service_years'];

   $mlbYrs=floor($mlbServ/$minService);

   $mlbDays=$mlbServ%$minService;

   $players[$pid]['arb']=1;

   $players[$pid]['days']=$minService-$mlbDays;

   $players[$pid]['yrs']=$mlbServYrs;

 }



##### Get Player Contracts #####

$query="SELECT player_id,season_year,last_year_team_option,last_year_player_option,salary0,salary1,salary2,salary3,salary4,salary5,salary6,salary7,salary8,salary9 FROM players_contract WHERE season_year!=0 AND team_id IN ($tmList);";

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

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

if (mysqli_num_rows($result))

 {

   $numFields=mysqli_num_fields($result);

   while ($row=mysqli_fetch_array($result))

    {

      $pid=$row['player_id'];

      $yrOne=$row['season_year'];

      $players[$pid]['last_year_team_option']=$row['last_year_team_option'];

      $players[$pid]['last_year_player_option']=$row['last_year_player_option'];

   

      for ($i=4;$i<$numFields;$i++)

       {

				$fieldInfo = mysqli_fetch_field_direct( $result, $i);

				$fieldname = $fieldInfo->name;

	 $yr=$yrOne+$i-4;

   	 $salary=$row[$fieldname];

	 $players[$pid][$yr]['salary']=$salary;

   	 if ($salary!=0)

	  {

	    $players[$pid]['finalYr']=$yr;

  	  }

       }

    }

 }



##### Get Player Contract Extensions #####

$query="SELECT player_id,season_year,last_year_team_option,last_year_player_option,salary0,salary1,salary2,salary3,salary4,salary5,salary6,salary7,salary8,salary9 FROM players_contract_extension WHERE season_year!=0 AND team_id IN ($tmList);";

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

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

if (mysqli_num_rows($result))

 {

   $numFields=mysqli_num_fields($result);

   while ($row=mysqli_fetch_array($result))

    {

      $pid=$row['player_id'];

      if (isset($players[$pid]['finalYr'])) {$yrOne=$players[$pid]['finalYr']+1;}

       else {$yrOne=$row['season_year']+1;}

      $players[$pid]['last_year_team_option']=$row['last_year_team_option'];

      $players[$pid]['last_year_player_option']=$row['last_year_player_option'];

   

      for ($i=4;$i<$numFields;$i++)

       {

				$fieldInfo = mysqli_fetch_field_direct( $result, $i);

				$fieldname = $fieldInfo->name;

	 $yr=$yrOne+$i-4;

   	 $salary=$row[$fieldname];

	 $players[$pid][$yr]['salary']=$salary;

   	 if ($salary!=0)

	  {

	    $players[$pid]['finalYr']=$yr;

  	  }

       }

    }

 }



##### Trim Player List Based on Year #####

foreach ($players as $pid => $val)

 {

   if ((isset($players[$pid]['yrs']))&&($players[$pid]['yrs']<($minFAyrs-1))&&($dyear==($lgyear+1))) {continue;}

   if ($players[$pid]['finalYr']==($dyear-1)) {$majList.=",$pid";}

    elseif (($players[$pid]['finalYr']==$dyear)&&(($players[$pid]['last_year_team_option']==1)||($players[$pid]['last_year_player_option']==1))) {$majList.=",$pid";}

 }

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

$pList=$majList;





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

$query="SELECT player_id,first_name,last_name,if(position=1,role,position) as position,($dyear-YEAR(date_of_birth)-(RIGHT(date_of_birth,5)>'07-01')) as age,team_id,organization_id FROM players WHERE retired=0 AND team_id!=0";

if ($pList!="") {$query.=" AND player_id IN ($pList)";}

 else {$query.=" AND 0";}

$query.=" ORDER BY last_name,first_name,age";

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;'>Contracts Expiring in $dyear</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'>Level</td>";

   echo "<td class='hsc2'>".($dyear-1)." Salary</td>";

   echo "<td class='hsc2_l' style='padding-left:20px;'>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]['level']."</a></td>";

      echo "<td><a href='./payroll.php?player=$pid'>$".number_format($players[$pid][$salYr]['salary'])."</a></td>";

      echo "<td class='".$cls."_l' style='padding-left:20px;'>";

      if (($players[$pid]['last_year_team_option']==1)&&($players[$pid]['last_year_player_option']==1))

       {

         if ($players[$pid]['finalYr']==$dyear) {echo "Only if mutual option is not picked up";}

	 else {echo "Only if mutual option is picked up";}

       }

       elseif ($players[$pid]['last_year_team_option']==1)

       {

         if ($players[$pid]['finalYr']==$dyear) {echo "Only if team option is not picked up";}

	 else {echo "Only if team option is picked up";}

       }

       elseif ($players[$pid]['last_year_player_option']==1)

       {

         if ($players[$pid]['finalYr']==$dyear) {echo "Only if player option is not picked up";}

	 else {echo "Only if player option is picked up";}

       }

       elseif (isset($players[$pid]['arb'])&&($dyear==($lgyear+1))) {echo "Needs ".$players[$pid]['days']." more days for FA";}

       else {echo "&nbsp;";}

      echo"</td>";

      echo "</tr>\n";

      $cnt++;

    }

 }

##### Get Minor League Contracts Expiring if Looking at Minor League #####

if ((($getParentAbbr>0)||(isset($_GET['team_id'])))&&(($lgyear+1)==$dyear))

{

	$query="SELECT p.player_id,first_name,last_name,if(position=1,role,position) as position,($lgyear-YEAR(date_of_birth)-(RIGHT(date_of_birth,5)>'07-01')) as age,p.team_id,organization_id FROM players as p,players_roster_status as prs WHERE p.player_id=prs.player_id AND pro_service_years>=$minMLFAyrs AND is_active=0 AND is_on_secondary=0 AND is_on_dl=0 AND is_on_dl60=0 AND p.team_id IN ($tmList)";

	if ($pList!="") {$query.=" AND p.player_id NOT IN ($pList)";}

	$query.=" ORDER BY last_name,first_name,age";

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

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

	if (mysqli_num_rows($result))

	{

		if ($cnt==0)

		{

			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'>Level</td>";

			echo "<td class='hsc2'>".($dyear-1)." Salary</td>";

		     	echo "<td class='hsc2_l' style='padding-left:20px;'>Notes</td>";

			echo "</tr>\n";   

		}

		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]['level']."</a></td>";

			echo "<td sorttable_customkey=0>Minor League Contract</td>";

			echo "<td class='".$cls."_l' style='padding-left:20px;'>&nbsp;</td>";

			echo "</tr>\n";

		  	$cnt++;

		}

	}

}



if ($cnt==0) {echo "<tr><td>No players are due to become free agents in $dyear yet.";}

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']);





?>

