<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];
  $lgdate=new Date($_SESSION['lgdate']);
  $lgpath=$_SESSION['lgpath'];
  if (!isset($_GET['showq'])) {$_GET['showq']=0;}
  $title='Game Log';

  if (isset($_GET['game_id'])&&(is_numeric($_GET['game_id']))&&($_GET['game_id']>0)) {$game_id=$_GET['game_id'];}
  else {$game_id=0;}

  if ($game_id>0)
  {
      include($_SESSION['adminpath']."/dbopen.php");
      
      ## Get Game Details
      $query="SELECT * FROM games WHERE game_id=".$game_id;
      if ($_GET['showq']==1) {echo "$query</br>\n";}
      $result=mysqli_query($db,$query);
      if (mysqli_num_rows($result)!=1)
      {
          $gameFound=0;
      }
      else
      {
          $gameFound=1;
          
          $row=mysqli_fetch_array($result);
          $hid=$row['home_team'];
          $aid=$row['away_team'];
          $attendance=$row['attendance'];
          $gmDate=new Date($row['date']);
          $gmTime=$row['time'];
          $game_type=$row['game_type'];

          $query="SELECT team_id,name,abbr,nickname,logo_file_name,sub_league_id,division_id,parent_team_id,background_color_id,text_color_id FROM teams WHERE team_id IN ($hid,$aid) OR parent_team_id IN ($hid,$aid)";
          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]['name']=$row['name'];
              $teams[$tid]['abbr']=$row['abbr'];
              $teams[$tid]['nickname']=$row['nickname'];
              $teams[$tid]['logo_file_name']=$row['logo_file_name'];
              $teams[$tid]['sub_league_id']=$row['sub_league_id'];
              $teams[$tid]['division_id']=$row['division_id'];
              $teams[$tid]['parent_team_id']=$row['parent_team_id'];
              $teams[$tid]['background_color_id']=$row['background_color_id'];
              $teams[$tid]['text_color_id']=$row['text_color_id'];
          }
          
          $title.=" - ".$gmDate->format("M j, Y");
          $title.=" - ".$teams[$aid]['name']." @ ".$teams[$hid]['name'];
      }
      
      mysqli_close($db);
  }
  else {$title.=" - No Game Selected";}

  
  # Title Bar

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

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

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

  nav_toolbar("season","Season");

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

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

  ## Verify game ID
  if (($game_id==0)||($gameFound==0))
  {
      if ($game_id==0) {echo "<p>No game selected</p>\n";}
      else {echo "<p>Invalid game selected</p>\n";}
      ### END OF PAGE
      echo " </div>   <!-- END contentpane DIV -->\n";
      page_foot($_SESSION['lgpath']);
      exit;
  }

  ################## START OF PAGE CONTENT #########################
  include($_SESSION['adminpath']."/dbopen.php");

  ## Get Players
  $query="SELECT player_id,first_name,last_name,bats,throws FROM players WHERE player_id IN (";
  $query.="SELECT DISTINCT player_id FROM players_at_bat_batting_stats WHERE game_id=".$game_id;
  $query.=") OR player_id IN (";
  $query.="SELECT DISTINCT opponent_player_id FROM players_at_bat_batting_stats WHERE game_id=".$game_id;
  $query.=") GROUP BY player_id";
  if ($_GET['showq']==1) {echo "$query</br>\n";}
  $result=mysqli_query($db,$query);
  while ($row=mysqli_fetch_array($result))
  {
      $pid=$row['player_id'];
      $players[$pid]['first_name']=$row['first_name'];
      $players[$pid]['last_name']=$row['last_name'];
      $players[$pid]['name']=$row['first_name']." ".$row['last_name'];
      $players[$pid]['bats']=get_hand($row['bats'])."HB";
      $players[$pid]['throws']=get_hand($row['throws'])."HP";
  }

  ## Get At-Bat Results
  $query="SELECT *,if(team_id=".$hid.",1,0) as bottomHalf,if(team_id=".$hid.",1,-1)*run_diff as runSort,100*base3+10*base2+base1 as baseSort FROM players_at_bat_batting_stats WHERE game_id=".$game_id." ORDER BY inning,bottomHalf,outs,runSort,baseSort,spot;";
  if ($_GET['showq']==1) {echo "$query</br>\n";}
  $result=mysqli_query($db,$query);
  $cnt=1;
  $prevTid=-1;
  $prevInn=-1;
  $prevHalf=-1;
  $prevPid[$aid]=-1;
  $prevPid[$hid]=-1;
  echo "<table>\n";
  while ($row=mysqli_fetch_array($result))
  {
      $inning=$row['inning'];
      $bottomHalf=$row['bottomHalf'];
      $outs=$row['outs'];
      $spot=$row['spot'];
      $tid=$row['team_id'];
      
      $bid=$row['player_id'];
      $pid=$row['opponent_player_id'];
      $balls=$row['balls'];
      $strikes=$row['strikes'];
      $abResult=$row['result'];
            
      if ($prevHalf!=$bottomHalf)
      {
          echo "<tr><td colspan=2 style='font-weight:bold;font-size:1.5em;'>";
          if ($bottomHalf==1) {echo "Bottom";} else {echo "Top";}
          echo " of the ".ordinal_suffix($inning);
          echo "</td></tr>\n";
          
          echo "<tr><td colspan=2 style='background-color:#CCCCCC;padding-left:2em;'>";
          if ($bottomHalf==1)
          {
              echo "<a href='./team_summary.php?team_id=".$hid."&year=".$lgdate->format("Y")."'>";
              echo $teams[$hid]['name']." ".$teams[$hid]['nickname']."</a>";
              echo " batting";
              echo " - ";
              
              if ($prevPid[$aid]<0) {$pitcherID=$pid;} else {$pitcherID=$prevPid[$aid];}
              echo "Pitching for ";
              echo "<a href='./team_summary.php?team_id=".$aid."&year=".$lgdate->format("Y")."'>";
              echo $teams[$aid]['name']."</a>";
              echo ": ".$players[$pitcherID]['throws']." <a href='./player.php?player_id=".$pitcherID."'>".$players[$pitcherID]['name']."</a>";
          }
          else
          {
              echo "<a href='./team_summary.php?team_id=".$aid."&year=".$lgdate->format("Y")."'>";
              echo $teams[$aid]['name']." ".$teams[$aid]['nickname']."</a>";
              echo " batting";
              echo " - ";
              
              if ($prevPid[$hid]<0) {$pitcherID=$pid;} else {$pitcherID=$prevPid[$hid];}
              echo "Pitching for ";
              echo "<a href='./team_summary.php?team_id=".$hid."&year=".$lgdate->format("Y")."'>";
              echo $teams[$hid]['name']."</a>";
              echo ": ".$players[$pitcherID]['throws']." <a href='./player.php?player_id=".$pitcherID."'>".$players[$pitcherID]['name']."</a>";
          }
          echo "</td>";
          echo "</tr>\n";
      }
      ## New Pitcher Line
      if ($bottomHalf==1)
      {
          if ($pid!=$prevPid[$aid])
          {
              echo "<tr><td colspan=2>Now pitching: ".$players[$pid]['throws']." <a href='./player.php?player_id=$pid'>".$players[$pid]['name']."</a></td></tr>\n";
          }
      }
      else
      {
          if ($pid!=$prevPid[$hid])
          {
              echo "<tr><td colspan=2>Now pitching: ".$players[$pid]['throws']." <a href='./player.php?player_id=$pid'>".$players[$pid]['name']."</a></td></tr>\n";
          }
      }
      
      ## Batter Line
      echo "<tr>";
      echo "<td>Batting: ".$players[$bid]['bats']." <a href='./player.php?player_id=$bid'>".$players[$bid]['name']."</a></td>";
      echo "<td>";
      echo $balls."-".$strikes;
      echo ": ".get_ab_result($abResult);
      echo "</td>";
      echo "</tr>\n";
            
      $prevTid=$tid;
      $prevInn=$inning;
      $prevHalf=$bottomHalf;
      if ($bottomHalf==1) {$prevPid[$aid]=$pid;} else {$prevPid[$hid]=$pid;}

      $cnt++;
  }
  echo "</table>\n";

  print_r($abOrder[$aid]);
  echo "</br></br></br>";
  print_r($abOrder[$hid]);

  

  mysqli_close($db);

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

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

  page_foot($_SESSION['lgpath']);

?>

