<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];

  $lgyear=$_SESSION['lgyear'];



  if (($_SESSION['user_team_id']>0)&&(!isset($_GET['team_id'])))

  {

	  if ($_SESSION['user_league_id']==$lgid) {$team_id=$_SESSION['user_team_id'];}

	  else

	  {

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

		  $query="SELECT team_id FROM teams WHERE parent_team_id=".$_SESSION['user_team_id']." AND league_id=$lgid";

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

		  if (mysqli_num_rows($result))

		  {

			  $row=mysqli_fetch_array($result);

			  $team_id=$row['team_id'];

		  }

		  mysqli_close($db);

	  }

  }

  elseif (isset($_GET['team_id'])) {$team_id=$_GET['team_id'];}

  if ($team_id=="") {$team_id=-1;}



  ##### Get Team Names #####

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

  $query="SELECT team_id,name,nickname FROM teams WHERE league_id=$lgid";

  if ($team_id>0) {$query.=" AND team_id=$team_id";}

  $query.=" AND allstar_team=0 ORDER BY name,nickname LIMIT 1;";

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

  while ($row=mysqli_fetch_array($result))

  {

	  $team_id=$row['team_id'];

	  $tname=$row['name']." ".$row['nickname'];

  }

  mysqli_close($db);





  ##### Get Title #####

  $title=$tname.' History - Postseason Table';

  

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

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

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

  nav_toolbar("history","History");

  nav_toolbar("teams","Teams","","",$team_id,$tyear);

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

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



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

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



  ## Get Team Info

  $tlist="";

  $tcnt=0;

  $query="SELECT t.team_id,t.sub_league_id,t.division_id,name,nickname,abbr,text_color_id,background_color_id,if(level=7,1,level) as level FROM teams as t,team_relations as tr WHERE t.team_id=tr.team_id AND (t.team_id=$team_id OR parent_team_id=$team_id) AND allstar_team=0 ORDER BY team_id,sub_league_id,division_id";

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

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

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

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

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

	  $tlist.=",".$tid;

	  $tcnt++;

  }

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



  ## Get Min/Max Years

  $query="SELECT MIN(year) as minYr,MAX(year) as maxYr FROM team_history WHERE league_id=$lgid";

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

  $row=mysqli_fetch_array($result);

  $minYr=$row['minYr'];

  $maxYr=$row['maxYr'];



  ## Check for Wild Cards

  $query="SELECT MAX(thr.pos) as maxPos FROM team_history as th,team_history_record as thr WHERE th.team_id=thr.team_id AND th.year=thr.year AND th.league_id=$lgid";

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

  $row=mysqli_fetch_array($result);

  if ($row['maxPos']>1) {$wcFnd=1;} else {$wcFnd=0;} 



  ## Get Playoff Results

  $query="SELECT th.team_id,th.year,made_playoffs,won_playoffs,thr.pos,w,l FROM team_history as th,team_history_record as thr WHERE th.team_id=thr.team_id AND th.year=thr.year AND th.team_id IN ($tlist) AND made_playoffs=1";

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

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

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $tid=$row['team_id'];

		  $yr=$row['year'];



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

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

		  $teams[$tid][$yr]['pos']=$row['pos'];

		  $teams[$tid][$yr]['w']=$row['w'];

		  $teams[$tid][$yr]['l']=$row['l'];

		  $teams[$tid][$yr]['pct']=stat_format('winpct',$row['w']/($row['w']+$row['l']));

	

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

		  $totTitles=$totTitles+$row['won_playoffs'];

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

		  $totPS=$totPS+$row['made_playoffs'];

		  if ($row['pos']==1)

		  {

			  $teams[$tid]['totals']['division']=$teams[$tid]['totals']['division']+1;

	  		  $totDiv=$totDiv+1;

		  }

		  else

		  {

			  $teams[$tid]['totals']['wildcard']=$teams[$tid]['totals']['wildcard']+1;

	  		  $totWC=$totWC+1;

		  }



	  }

  }

  else

  {

	  echo "No playoff history found";

	  mysqli_close($db);

	

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

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



	  echo "<script type='text/javascript'>\n";

	  echo " <!--\n";

	  echo "   document.getElementById('year').disabled=1;\n";

	  echo " -->\n";

	  echo "</script>\n";



	  page_foot($_SESSION['lgpath']);

	  exit;

  }



  //echo "<div class='textbox' style='margin-left:auto;margin-right:auto;width:".(100+50*$tcnt)."px'>\n";

  echo " <table cellpadding=2 cellspacing=0 border=0 style='margin-left:auto;margin-right:auto;width:".(100+50*$tcnt)."px;border:1px solid black;'>\n";

  echo "  <tr class='title2'><td colspan=".(1+count($teams)).">Franchise Postseason Appearances</td></tr>\n";

  echo "  <tr class='headline'><td class='hsc2'>&nbsp;</td>";

  foreach ($teams as $tid => $val)

  {

	  echo "<td class='hsc2' title='".$teams[$tid]['name'];

	  if ($teams[$tid]['level']!=1) {echo " (".get_level($teams[$tid]['level']).")";}

	  echo "'>".$teams[$tid]['abbr']."</td>";

  }

  echo "</tr>\n";

  $rowCnt=0;

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

  echo "  <tr class='$cls'><td style='font-weight:bold;border-right:1px dotted #666666;' title='$totTitles Championships Won'>Won</td>";

  foreach ($teams as $tid => $val) {echo "<td>".max(0,$teams[$tid]['totals']['won_playoffs'])."</td>";}

  echo "</tr>\n";

  $rowCnt++;

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

  echo "  <tr class='$cls'><td style='font-weight:bold;border-right:1px dotted #666666;' title='$totPS Postseasons Appeared In'>PS</td>";

  foreach ($teams as $tid => $val) {echo "<td>".max(0,$teams[$tid]['totals']['made_playoffs'])."</td>";}

  echo "</tr>\n";

  $rowCnt++;

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

  echo "  <tr class='$cls'><td style='font-weight:bold;border-right:1px dotted #666666;";

  if ($wcFnd!=1) {echo "border-bottom:1px solid black;";}

  echo "' title='$totDiv Division Titles Won'>Div</td>";

  foreach ($teams as $tid => $val)

  {

	  echo "<td";

	  if ($wcFnd!=1) {echo " style='border-bottom:1px solid black;'";}

	  echo ">".max(0,$teams[$tid]['totals']['division'])."</td>";

  }

  echo "</tr>\n";

  $rowCnt++;

  if ($wcFnd==1)

  {

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

	  echo "  <tr class='$cls'><td style='font-weight:bold;border-right:1px dotted #666666;border-bottom:1px solid black;' title='$totWC Wild Cards Won'>WC</td>";

	  foreach ($teams as $tid => $val) {echo "<td style='border-bottom:1px solid black;'>".max(0,$teams[$tid]['totals']['wildcard'])."</td>";}

	  echo "</tr>\n";

	  $rowCnt++;

  }



  for ($yr=$minYr;$yr<=$maxYr;$yr++)

  {

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

	  echo "  <tr class='$cls'><td style='font-weight:bold;border-right:1px dotted #666666;'>$yr</td>";

	  foreach ($teams as $tid => $val)

	  {

		  if ($teams[$tid][$yr]['made_playoffs']==1)

		  {

			  echo "<td style='color:".$teams[$tid]['text_color_id'].";background-color:".$teams[$tid]['background_color_id'].";";

			  if ($teams[$tid][$yr]['won_playoffs']==1) {echo "text-align:left;font-weight:bold;border:2px solid ".$teams[$tid]['text_color_id'].";";}

			  echo "'";

			  echo " title='".$teams[$tid][$yr]['w']."-".$teams[$tid][$yr]['l'].", ".$teams[$tid][$yr]['pct']."'";

			  echo ">";

			  echo ordinal_suffix($teams[$tid][$yr]['pos']);

			  echo "</td>";

		  }

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

	  }

	  echo "</tr>\n";

	  $rowCnt++;

  }





  echo " </table>\n";

  //echo "</div>\n";





  mysqli_close($db);



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

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



  echo "<script type='text/javascript'>\n";

  echo " <!--\n";

  echo "   document.getElementById('yearnav').disabled=1;\n";

  echo " -->\n";

  echo "</script>\n";



  page_foot($_SESSION['lgpath']);





?>

