<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];

  $lgyear=$_SESSION['lgyear'];

  $yr=$_GET['year'];

  $slid=$_GET['sub_league_id'];



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

  $query="SELECT name,designated_hitter FROM sub_leagues WHERE league_id=$lgid AND sub_league_id=$slid;";

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

  $row=mysqli_fetch_array($result);

  $slName=$row['name'];

  $useDH=$row['designated_hitter'];

  $query="SELECT value FROM ootp_sql_admin WHERE setting='dh$slid';";

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

  $row=mysqli_fetch_array($result);

  $dhYr=$row['value'];

  mysqli_close($db);

  if ($dhYr!="")

  {

	  if (($useDH==1)&&($dhYr>$yr)) {$useDH=0;}

	  elseif (($useDH==0)&&($dhYr>$yr)) {$useDH=1;}

  }



  $title="$yr $slName Register";



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

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

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

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

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



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

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



  #Get Team Names

  $query="SELECT team_id,name,nickname,abbr";

  if ($lgyear==$yr) {$query.=" FROM teams WHERE league_id=$lgid AND allstar_team=0";}

  else {$query.=",made_playoffs,won_playoffs FROM team_history WHERE league_id=$lgid AND year=$yr";}

  $query.=" AND sub_league_id=$slid;";

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

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

  $tlist="";

  $tmCnt=0;

  while ($row=mysqli_fetch_array($result))

  {

	  $tid=$row['team_id'];

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

	  $teams[$tid]['city']=$row['name'];

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

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

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

	  $tlist.="$tid,";

	  $tmCnt++;

  }

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



  ## Get Batting Stats

  $statArray=Array("g","gs","pa","ab","r","h","d","t","hr","rbi","bb","k","sb","cs","avg","obp","slg","ops","war");

  $query="SELECT p.player_id,first_name,last_name,bats,throws,x.team_id";

  foreach ($statArray as $key => $statLbl) {$query.=",".$statLbl;}

  $query.=" FROM players as p,(SELECT player_id,team_id";

  foreach ($statArray as $key => $statLbl) {$query.=",".stat_query($statLbl)." as $statLbl";}

  $query.=" FROM players_career_batting_stats WHERE league_id=$lgid AND split_id=1 AND year=$yr AND team_id IN ($tlist) GROUP BY player_id) as x";

  $query.=" WHERE p.player_id=x.player_id ORDER BY pa DESC,last_name,first_name";

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

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



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

  echo " <table border=0 cellpadding=0 cellspacing=0 width='100%' style='border=1px solid black;margin-left:auto;margin-right:auto;'>   <!-- open batters -->\n";

  echo "  <tr class='title2'><td style='padding:4px;'>Batting Registry (".mysqli_num_rows($result)." players)</td></tr>\n";

  echo "  <tr>\n";

  echo "   <td>\n";

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

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

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

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

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

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

  foreach ($statArray as $key => $statLbl) {echo "<td class='hsc2'>".stat_text($statLbl,2)."</td>";}

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



  $rowCnt=0;

  while ($row=mysqli_fetch_array($result))

  {

	  $pid=$row['player_id'];

	  $tid=$row['team_id'];



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

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

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

	  echo "<td class='".$cls."_l'><a href='./teamHist.php?page=year&year=$yr&team_id=$tid'>".$teams[$tid]['abbr']."</a></td>";

	  echo "<td class='".$cls."_l'>".stat_format('bats',$row['bats'])."</td>";

	  echo "<td class='".$cls."_l'>".stat_format('throws',$row['throws'])."</td>";

	  foreach ($statArray as $key => $statLbl) {echo "<td>".stat_format($statLbl,$row[$statLbl])."</td>";}

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

	  $rowCnt++;

  }



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

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

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

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

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

  foreach ($statArray as $key => $statLbl) {echo "<td class='hsc2'>".stat_text($statLbl,2)."</td>";}

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

  echo "    </table>\n";

  echo "   </td>\n";

  echo "  </tr>\n";

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

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











  mysqli_close($db);



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

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

  page_foot($_SESSION['lgpath']);





?>

