<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];

  $lgpath=$_SESSION['lgpath'];



  # Check Admin Table

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

  $query="SHOW TABLES LIKE 'ootp_sql_admin';";

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

  if (mysqli_num_rows($result))

  {

	  # Get Settings

	  $query="SELECT scope,setting,value FROM ootp_sql_admin WHERE setting LIKE '%nable'";

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

	  if (mysqli_num_rows($result))

	  {

		  while ($row=mysqli_fetch_array($result))

		  {

			  $scope=$row['scope'];

			  $setting=$row['setting'];

			  $value=$row['value'];



			  $settings[$scope][$setting]=$value;

		  }

	  }

  }



  # Get Leagues

  $query="SELECT league_id,name,abbr,parent_league_id,league_level,rules_fa_minimum_years,rules_salary_arbitration_minimum_years,rules_min_service_days FROM leagues WHERE league_id=$lgid ORDER BY league_id";

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

  $lgCnt=0;

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $league_id=$row['league_id'];

		  $leagues[$league_id]['name']=$row['name'];

		  $leagues[$league_id]['abbr']=$row['abbr'];

		  $leagues[$league_id]['parent']=$row['parent_league_id'];

		  $leagues[$league_id]['level']=$row['league_level'];

		  $leagues[$league_id]['rules_min_service_days']=$row['rules_min_service_days'];

		  $leagues[$league_id]['rules_salary_arbitration_minimum_years']=$row['rules_salary_arbitration_minimum_years'];

		  $leagues[$league_id]['rules_fa_minimum_years']=$row['rules_fa_minimum_years'];

		  $leagues[$lgCnt]=$league_id;

		  $lgCnt++;

	  }

	  $lgTotCnt=count($leagues)/2;

  }



  # Get Teams

  $query="SELECT team_id,name,nickname,abbr FROM teams WHERE 1 ORDER BY team_id";

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

		  $teams[$tid]['abbr']=$row['abbr'];

	  }

  }



  mysqli_close($db);



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

  $title='Contract Extension Review';



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

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

  main_titlebar($_SESSION['lgid'],$_SESSION['lgpath'],$title);



  if ($_SESSION['user_is_commish']!=1)

  {

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

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

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

  }



  if ($lgTotCnt==0)

  {

	  echo "<p>No leagues defined.  Verify leagues table exists.</p>\n";

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

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

	  page_foot($_SESSION['lgpath']);

	  exit;

  }

  else

  {

	  if ($lgTotCnt>$maxCols) {$lgCnt=$maxCols;}

	  else {$lgCnt=$lgTotCnt;}

  }



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

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



  $module="Contracts";

  /*

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

  echo "   <FORM ACTION=\"save_settings.php\" METHOD=\"post\">\n";

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

  echo "    <table cellpadding=2 cellspacing=0 style='width:935px;'>";

  echo "     <TR class='title'><TD colspan=".($lgCnt+1).">Enabled Pages</TD></TR>\n";



    echo "     <TR><TD colspan=$lgCnt>&nbsp;</TD><TD><INPUT type=\"submit\" value=\"Save\"></TD></TR>\n";

  echo "    </table>";

  echo "   </FORM>\n";

  echo " </div>\n";

  */

  

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

  $query="SELECT MAX(years) as maxLen FROM players_contract_extension WHERE salary0>0";

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

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

  if (mysqli_num_rows($result))

  {

	  $row=mysqli_fetch_array($result);

	  $maxLen=$row['maxLen'];

  }

  $query="SELECT first_name,last_name,age,organization_id,x.* FROM players as p JOIN (SELECT pce.*,mlb_service_days FROM players_roster_status as prs JOIN (SELECT * FROM players_contract_extension WHERE salary0>0) as pce ON prs.player_id=pce.player_id) as x WHERE p.player_id=x.player_id ORDER BY last_name,first_name,salary0 DESC";

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

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

  if (mysqli_num_rows($result))

  {

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

	echo "      <TR class='title'><TD style='padding:2px;'>Contract Extensions</TD></TR>\n";

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

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

	echo "        <thead>\n";

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

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

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

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

	echo "<td class='hsc2_l' title='Service Time'>Svc Time</td>";

	echo "<td class='hsc2_l' title='Contract Length'>Lgth</td>";

	for ($i=1;$i<=$maxLen;$i++) {echo "<td class='hsc2_l'>Yr $i</td>";}

	echo "<td class='hsc2_l'>No Trade?</td>";

	echo "</tr>\n";

	echo "        <thead>\n";

	echo "        <tbody>\n";

	$rowCnt=0;

	while ($row=mysqli_fetch_array($result))

	{

		$pid=$row['player_id'];

		$orgID=$row['organization_id'];

		$mlb_service_days=$row['mlb_service_days'];

		

		$lPO=$row['last_year_player_option'];

		$lTO=$row['last_year_team_option'];

		$lVO=$row['last_year_vesting_option'];

		if (($lPO==1)&&($lTO==1)) {$lOpt="M";}

		elseif ($lPO==1) {$lOpt="P";}

		elseif ($lTO==1) {$lOpt="T";}

		elseif ($lVO==1) {$lOpt="V";}

		else {$lOpt="N";}

		

		$nlPO=$row['next_last_year_player_option'];

		$nlTO=$row['next_last_year_team_option'];

		$nlVO=$row['next_last_year_vesting_option'];

		if (($nlPO==1)&&($nlTO==1)) {$nlOpt="M";}

		elseif ($nlPO==1) {$nlOpt="P";}

		elseif ($nlTO==1) {$nlOpt="T";}

		elseif ($nlVO==1) {$nlOpt="V";}

		else {$nlOpt="N";}



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

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

		echo "<td class=\"".$cls."_l\" sorttable_customkey=\"".$row['last_name'].", ".$row['first_name']."\"><a href=\"./payroll.php?player=$pid\">".$row['first_name']." ".$row['last_name']."</a></td>";

		echo "<td class=\"".$cls."_l\" title=\"".$teams[$orgID]['name']."\"><a href=\"$lgpath/teams/team_$orgID.html\">".$teams[$orgID]['abbr']."</a></td>";

		echo "<td>".$row['age']."</td>";

		echo "<td sorttable_customkey='".$mlb_service_days."'>".floor($mlb_service_days/$leagues[$lgid]['rules_min_service_days'])." yrs, ".$mlb_service_days%$leagues[$lgid]['rules_min_service_days']." days</td>";

		echo "<td>".$row['years']."</td>";

		for ($i=0;$i<$maxLen;$i++)

		{

			$key='salary'.$i;

			echo "<td sorttable_customkey=\"$row[$key]\"";

			// Next to last year

			if (($i==$row['years']-2)&&($nlOpt!="N"))

			{				

				$buyout=$row['next_last_year_option_buyout'];

				switch ($nlOpt)

				{

					case 'M': echo " title='Mutual Option".($buyout>0?", $".number_format($buyout):"")."'"; break;

					case 'T': echo " title='Team Option".($buyout>0?", $".number_format($buyout):"")."'"; break;

					case 'P': echo " title='Player Option".($buyout>0?", $".number_format($buyout):"")."'"; break;

					case 'V': echo " title='Vesting Option".($buyout>0?", $".number_format($buyout):"")."'"; break;

				}

				echo">$".number_format($row[$key]);

				switch ($nlOpt)

				{

					case 'M': echo "<sup>mo</sup>"; break;

					case 'T': echo "<sup>to</sup>"; break;

					case 'P': echo "<sup>po</sup>"; break;

					case 'V': echo "<sup>vo</sup>"; break;

				}

			}

			// Last year

			elseif (($i==$row['years']-1)&&($lOpt!="N"))

			{				

				$buyout=$row['next_last_year_option_buyout'];

				switch ($lOpt)

				{

					case 'M': echo " title='Mutual Option".($buyout>0?", $".number_format($buyout):"")."'"; break;

					case 'T': echo " title='Team Option".($buyout>0?", $".number_format($buyout):"")."'"; break;

					case 'P': echo " title='Player Option".($buyout>0?", $".number_format($buyout):"")."'"; break;

					case 'V': echo " title='Vesting Option".($buyout>0?", $".number_format($buyout):"")."'"; break;

				}

				echo">$".number_format($row[$key]);

				switch ($lOpt)

				{

					case 'M': echo "<sup>mo</sup>"; break;

					case 'T': echo "<sup>to</sup>"; break;

					case 'P': echo "<sup>po</sup>"; break;

					case 'V': echo "<sup>vo</sup>"; break;

				}

			}

			else

			{

				echo">$".number_format($row[$key]);

			}

			echo "</td>";

		}

		echo "<td>".($row['no_trade']==1?"Yes":"No")."</td>";

		echo "</tr>\n";

		$rowCnt++;

	}

	echo "        </tbody>\n";

	echo "      </table>\n";

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

	echo "    </table>\n";

  }

  else

  {

	  echo "<p>No contract extenstions signed.</p>\n";

  }

  echo " </div>\n";

  

  mysqli_close($db);



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

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

  page_foot($_SESSION['lgpath']);





?>

