<?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;
  }

  ## Verify game log has been uploaded
  $gameLogFile=$lgpath."/game_logs/log_".$game_id.".html";
  echo "<a href='".$gameLogFile."'>".$gameLogFile."</a></br>\n";
  $ch = curl_init($gameLogFile);
  curl_setopt($ch, CURLOPT_NOBODY, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FILETIME, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  curl_exec($ch);
  $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // $retcode > 400 -> not found, $retcode = 200, found.
  if ($retcode!=200)
  {
      echo "<p>Unable to find HTML game log</p>\n";
      ### END OF PAGE
      echo " </div>   <!-- END contentpane DIV -->\n";
      page_foot($_SESSION['lgpath']);
      exit;
  }

  $handle = fopen($gameLogFile, "r");
  $cnt=1;
  $inGame=0;
  while ($line = fgets($handle))
  {
      if (strpos($line,"TOP OF THE 1ST"))
      {
          $inGame=1;
          echo "$cnt lines read - START</br>\n";
      }
      elseif (strpos($line,"START PAGE FOOTER"))
      {
          $inGame=0;
          echo "$cnt lines read - END</br>\n";
      }
      
      if ($inGame==1)
      {
          if (strpos($line,"boxtitle"))
          {
              echo "<div>".strip_tags($line)."</div>";
              echo "</br>\n";
          }
          elseif (
              (strpos($line," of the "))
              || (strpos($line," batting - "))
              || (strpos($line,"Pitching: "))
              || (strpos($line,"Batting: "))
              )
          {
              echo "<div>".$line."</div></br>\n";
          }
          elseif (
              (strpos($line,"0-0: "))
              )
          {
              echo "<div style='padding-left:50px;'>".$line."</div></br>\n";
          }
          else
          {
              echo "";
              //echo "$cnt lines read</br>\n";
          }
      }
      $cnt++;
  }
  fclose($handle);

  ################## 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";
  }

  mysqli_close($db);

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

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

  page_foot($_SESSION['lgpath']);

?>

