<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];

  $lgdate=$_SESSION['lgdate'];

  $lgpath=$_SESSION['lgpath'];

  if ((!isset($_GET['team_id']))&&($_SESSION['user_league_id']==$lgid)&&($_SESSION['user_team_id']>0)) {$_GET['team_id']=$_SESSION['user_team_id'];}

  $page=$_GET['page'];

  $pageLimit=200;

  if ($page=="") {$page=0;}



  if (($_GET['year']=='career')&&(!isset($_GET['player_id']))) {unset($_GET['year']);}

  if (!isset($_GET['year'])) {$_GET['year']='past0';}



  # Title Bar

  $title='Development Tracker';

  if (isset($_GET['team_id']))

  {

	  if ($_GET['team_id']==0) {$title.=" - Free Agents";}

	  else

	  {

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

		  $query="SELECT name,nickname FROM teams WHERE team_id=".$_GET['team_id'];

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

		  $row=mysqli_fetch_array($result);

		  $title.=" - ".$row['name']." ".$row['nickname'];

		  mysqli_close($db);

	  }

  }

  if (isset($_GET['player_id']))

  {

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

	  $query="SELECT first_name,last_name,p.team_id,level,t.abbr FROM players as p LEFT JOIN teams as t ON p.team_id=t.team_id WHERE player_id=".$_GET['player_id'];

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

	  $row=mysqli_fetch_array($result);

	  $playerTitle.=" - ".$row['first_name']." ".$row['last_name'];

	  if ($row['team_id']==0) {$playerTitle.=" (FA)";}

	  else 

	  {

		  $playerTitle.=" (".$row['abbr'];

		  if (($row['level']!=1)&&($row['level']!=8)) {$playerTitle.=" - ".get_level($row['level']);}

		  $playerTitle.=")";

	  }

	  $title.=$playerTitle;

	  mysqli_close($db);

  }





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



  ##### Check for development table

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

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

  if (!mysqli_num_rows($result))

  {

	  echo "<div class='textbox' style='border:0;'><p>No development history.</p></div>\n";

	  mysqli_close($db);

	

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

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

	  page_foot($_SESSION['lgpath']);

	  exit;

  }



  ##### Filters

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

  echo " <form method='get' action='development.php' class='inline'>\n";

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

  echo "  <tr class='title'><td colspan=9>Filters</td></tr>\n";

  $query="SELECT team_id,name,nickname,abbr,level FROM teams WHERE league_id=$lgid AND allstar_team!=1 ORDER BY name,nickname;";

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

  echo "  <tr>\n";

  echo "   <td><label for='team_id'>Team:</label></td>\n";

  echo "   <td>\n";

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

  echo "     <option value=0>Free Agents</option>\n";

  while ($row=mysqli_fetch_array($result))

  {

	  $tid=$row['team_id'];

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

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

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



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

	  if ($_GET['team_id']==$tid) {echo " selected";}

     	  echo ">".$row['name']." ".$row['nickname']."</option>\n";

   }

  echo "    </select>\n";

  echo "   </td>\n";

  echo "   <td><label for='team_id'>Time Period:</label></td>\n";

  echo "   <td>\n";

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

  echo "     <option value='past0'>This Sim</option>\n";

  echo "     <option value='past30'";

  if ($_GET['year']=='past30') {echo " selected";}

  echo ">Past 30 Days</option>\n";

  echo "     <option value='past60'";

  if ($_GET['year']=='past60') {echo " selected";}

  echo ">Past 60 Days</option>\n";

  $query="SELECT DISTINCT YEAR(dev_date) as year FROM players_development ORDER BY year;";

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

  while ($row=mysqli_fetch_array($result))

   {

     echo "     <option value='".$row['year']."'";

     if ($_GET['year']==$row['year']) {echo " selected";}

     echo ">".$row['year']."</option>\n";

   }

  echo "    </select>\n";

  echo "   </td>\n";

  echo "   <td><label for='devFilter'>Display:</label></td>\n";

  echo "   <td>\n";

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

  echo "     <option value='all'>All Ratings</option>\n";

  $ratFilters=array("batting"=>"Batting","running"=>"Running","fielding"=>"Fielding","pitching"=>"Pitching","pitch"=>"Pitch");

  foreach ($ratFilters as $key => $val)

   {

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

     if ($_GET['devFilter']==$key) {echo " selected";}

     echo ">".$val."</option>\n";

   }

  echo "    </select>\n";

  echo "   </td>\n";

  echo "   <td><label for='split'>Split:</label></td>\n";

  echo "   <td>\n";

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

  echo "     <option value='all'>All Ratings</option>\n";

  $ratFilters=array("overall"=>"Overall","vsl"=>"Vs Left","vsr"=>"Vs Right","talent"=>"Potential");

  foreach ($ratFilters as $key => $val)

   {

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

     if ($_GET['split']==$key) {echo " selected";}

     echo ">".$val."</option>\n";

   }

  echo "    </select>\n";

  echo "   </td>\n";

  echo "   <td><input type='submit' class='submitButton' value='Go' /></td>\n";

  echo "  </tr>\n";

  echo " </table>\n";

  echo " </form>\n";

  echo "</div>\n";





  ##### Display Development

  if ((isset($_GET['team_id']))||(isset($_GET['player_id'])))

  {

	  # Get Scouts Config

	  $query="SELECT value FROM ootp_sql_admin WHERE scope='ALL' AND setting='scoutsOn'";

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

	  if (mysqli_num_rows($result))

	  $row=mysqli_fetch_array($result);

	  $settings['scoutsOn']=$row['value'];



	  # Display Player Info

	  if (isset($_GET['player_id']))

	  {

		  # Get settings

		  # Get basic player info

		  # Get ratings

	  }





	  # Get Minor League Teams

	  if (isset($_GET['team_id']))

	  {

		  $query="SELECT team_id,name,nickname,abbr,level FROM teams WHERE parent_team_id=".$_GET['team_id']." AND allstar_team!=1 ORDER BY name,nickname;";

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

		  if (mysqli_num_rows($result))

		  {

			  while ($row=mysqli_fetch_array($result))

			  {

				  $tid=$row['team_id'];

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

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

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

			  }

		  }

	  }



	  # Compose Dev Query

	  $query="SELECT p.player_id,first_name,last_name,p.position,role,(YEAR(dev_date)-YEAR(date_of_birth)-(RIGHT(date_of_birth,5)>RIGHT(dev_date,5))) as age,p.team_id,p.organization_id,dev_date,rating,old_value,new_value FROM players as p,players_development as pd WHERE p.player_id=pd.player_id";

	  if ((isset($_GET['team_id']))&&(!isset($_GET['player_id']))) {$query.=" AND (p.organization_id=".$_GET['team_id']." OR p.team_id=".$_GET['team_id'].")";}

	  if (isset($_GET['player_id'])) {$query.=" AND p.player_id=".$_GET['player_id'];}

	  if (isset($_GET['year']))

	  {

		  switch ($_GET['year'])

		  {

		  case 'career':

			  break;

		  case 'past0':

			  $query.=" AND dev_date='".$lgdate."'";

			  break;

		  case 'past30':

			  $timeFrame=30;

			  $query.=" AND DATEDIFF(dev_date,'".$lgdate."')<1 AND DATEDIFF(dev_date,'".$lgdate."')>-".$timeFrame;

			  break;

		  case 'past60':

			  $timeFrame=60;

			  $query.=" AND DATEDIFF(dev_date,'".$lgdate."')<1 AND DATEDIFF(dev_date,'".$lgdate."')>-".$timeFrame;

			  break;

		  default:

			  $query.=" AND YEAR(dev_date)=".$_GET['year'];

			  break;

		  }

	  }

	  switch ($_GET['devFilter'])

	  {

	  case 'batting' : $query.=" AND rating LIKE 'batting_%'"; break;

	  case 'running' : $query.=" AND rating LIKE 'running_%'"; break;

	  case 'fielding': $query.=" AND rating LIKE 'fielding_%'"; break;

	  case 'pitching': $query.=" AND rating LIKE 'pitching_%'"; break;

	  case 'pitch'   : $query.=" AND rating LIKE '%_pitches_%'"; break;

	  }

	  switch ($_GET['split'])

	  {

	  case 'overall' : $query.=" AND (rating LIKE '%_overall_%' OR (rating LIKE '%pitches%' AND rating NOT LIKE '%_talent_%'))"; break;

	  case 'vsl'     : $query.=" AND rating LIKE '%_vsl_%'"; break;

	  case 'vsr'     : $query.=" AND rating LIKE '%_vsr_%'"; break;

	  case 'talent'  : $query.=" AND rating LIKE '%_talent_%'"; break;

	  }

	  $query.=" ORDER BY dev_date DESC,last_name,first_name";



	  # Get Count

	  $cntQuery="SELECT COUNT(*) as cnt FROM ($query) as t";

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

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

	  $row=mysqli_fetch_array($result);

	  $totCnt=$row['cnt'];

	  $pgCnt=ceil($totCnt/$pageLimit);



	  # Run Query

  	  $query.=" LIMIT ".($page*$pageLimit).",$pageLimit";

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

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



	  # Display Results

  	  echo "<div class='textbox' style='width:100%;'>   <!-- open development table -->\n";

	  echo " <table border=0 cellpadding=4 cellspacing=0 width='100%' style='margin-left:auto;margin-right:auto;'>   <!-- open development table -->\n";

	  echo "  <tr class='title2'><td style='text-align:left;'>";

	  echo "Development";

	  if (isset($_GET['player_id'])) {echo $playerTitle;}

	  if ($settings['scoutsOn']==1) {echo " - Scouted by StatsLab";}

	  echo "</td>\n";

	  if ($pgCnt>1)

	  {

		  echo "   <td style='text-align:right;vertical-align:top;margin-bottom:0px;padding-bottom:0px;'>\n";

		  echo "    <table border=0 cellpadding=0 cellspacing=0 style='float:right;'>\n";

		  echo "     <tr class='title2'><td>Page</td>\n";

		  echo "     <td>\n";

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

		  echo "       <input type='hidden' name='team_id' value='".$_GET['team_id']."' >\n";

		  echo "       <input type='hidden' name='year' value='".$_GET['year']."' >\n";

		  echo "       <input type='hidden' name='devFilter' value='".$_GET['devFilter']."' >\n";

		  echo "       <input type='hidden' name='split' value='".$_GET['split']."' >\n";

		  echo "       <select name='page' onchange='this.form.submit()' style='float:right;clear:none;'>\n";

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

		  {

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

			  if ($page==$i) {echo " selected='selected'";}

			  echo ">".($i+1)."</option>\n";

		  }

		  echo "       </select>\n";

		  echo "      </form>\n";

		  echo "     </td>\n";

		  echo "     <td style='text-align:left;'>of $pgCnt</td></tr>\n";

		  echo "    </table>\n";

		  echo "   </td>\n";

	  }

	  else {echo "   <td>&nbsp;</td>\n";}

	  echo "  </tr>\n";

	  if (isset($_GET['player_id'])) {echo "  <tr><td colspan=2><a href='./player.php?player_id=".$_GET['player_id']."' class='hsc2'>StatsLab Player Page</a>, <a href='$lgpath/players/player_".$_GET['player_id'].".html' class='hsc2'>OOTP Player Page</a></td></tr>";}

	  echo "  <tr><td colspan=2 style='padding:0px;'>\n";

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

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

	  if (!isset($_GET['player_id']))

	  {

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

		  echo "<td class='hsc2_l' title='Current team'>Tm</td>";

		  echo "<td class='hsc2_l' title='Current level'>Lvl</td>";

		  echo "<td class='hsc2_l' title='Age at development change'>Age</td>";

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

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

	  }

	  else

	  {

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

		  echo "<td class='hsc2_l' title='Age at development change'>Age</td>";

	  }

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

	  echo "<td class='hsc2'>Trend</td>";

	  echo "<td class='hsc2'>From</td>";

	  echo "<td class='hsc2'>To</td>";

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

	  $rowCnt=0;

	  while ($row=mysqli_fetch_array($result))

	  {

		  $pid=$row['player_id'];

		  $tid=$row['team_id'];

		  $pos=$row['position'];

		  $rating=$row['rating'];



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

		  $e=explode("_",$rating);

		  $ratChg=$row['new_value']-$row['old_value'];



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



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

		  if (!isset($_GET['player_id']))

		  {

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

			  echo "&team_id=".$_GET['team_id'];

			  echo "&year=career";

			  echo "&devFilter=".$_GET['devFilter'];

			  echo "&split=".$_GET['split'];

			  echo "'>".$row['first_name']." ".$row['last_name']."</a></td>";

			  if ($tid!=0)

			  {

				  echo "<td class='".$cls."_l' sorttable_customkey='".$teams[$tid]['abbr']."'><a href=''>".$teams[$tid]['abbr']."</a></td>";

				  echo "<td class='".$cls."_l' sorttable_customkey='".$teams[$tid]['level']."'>".get_level($teams[$tid]['level'])."</td>";

			  }

			  else {echo "<td class='".$cls."_l'>FA</td>\n<td class='".$cls."_l'>-</td>\n";}

			  echo "<td class='".$cls."_l'>".$row['age']."</td>";

			  echo "<td class='".$cls."_l' sorttable_customkey='$pos'>".get_pos($pos,1)."</td>";

			  echo "<td class='".$cls."_l' sorttable_customkey='".strtotime($row['dev_date'])."'>".$row['dev_date']."</td>";

		  }

		  else

		  {

			  echo "<td class='".$cls."_l' sorttable_customkey='".strtotime($row['dev_date'])."'>".$row['dev_date']."</td>";

			  echo "<td class='".$cls."_l'>".$row['age']."</td>";

		  }

		  echo "<td class='".$cls."_l'>";

		  if (($e[2]=='talent')||($e[3]=='talent')) {echo "<b>";}

		  if ($ratChg>0) {echo "<span style='color:#006600;'>";} else {echo "<span style='color:#990000;'>";}

		  echo stat_text($rating);

		  echo "</span>";

		  if (($e[2]=='talent')||($e[3]=='talent')) {echo "</b>";}

		  echo "</td>";

		  if ($ratChg>0) {$img='up';} else {$img='down';}

		  echo "<td sorttable_customkey='$ratChg'><img src='./images/$img.png'></td>";

		  echo "<td";

		  switch ($rating)

		  {

		  case 'pitching_ratings_misc_velocity':

			  echo " sortable_customkey='".$row['old_value']."'>".get_velocity($row['old_value']);

			  break;

		  case 'pitching_ratings_misc_ground_fly':

			  echo " sortable_customkey='".$row['new_value']."'>".stat_format($rating,$row['old_value'],0);

			  break;

		  default:

			  echo ">".$row['old_value'];

			  break;

		  }

		  echo "</td>";

		  echo "<td";

		  switch ($rating)

		  {

		  case 'pitching_ratings_misc_velocity':

			  echo " sortable_customkey='".$row['new_value']."'>".get_velocity($row['new_value']);

			  break;

		  case 'pitching_ratings_misc_ground_fly':

			  echo " sortable_customkey='".$row['new_value']."'>".stat_format($rating,$row['new_value'],0);

			  break;

		  default:

			  echo ">".$row['new_value'];

			  break;

		  }

		  echo "</td>";

		  echo "</tr>\n";

		  $rowCnt++;

	  }



	  if ($rowCnt==0)

	  {

		  echo "    <tr>";

		  echo "<td colspan=10 style='text-align:center;'> - No development history matches specified filters - </td>";

		  echo "</tr>\n";

	  }



	  echo "   </table>\n";

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

	  echo " </table>   <!-- close development table -->\n";

	  echo "</div>   <!-- close development table -->\n";

  }







  mysqli_close($db);



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

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

  page_foot($_SESSION['lgpath']);





?>

