<?php

//--------------------------------------------
// config
//--------------------------------------------
include("sub_config.php");
$refresh = $conf_refresh_time;
$step = $conf_refresh_step;

include("sub_league_2020_final.php");

//--------------------------------------------
// players
//  $g_players
//  $g_gnum
//--------------------------------------------
//include("sub_player_20171126.php");
include("sub_player_2020_final.php");
//print "<pre>";
//print_r($g_players);
//print "</pre>";

//--------------------------------------------
// Sheet No ( Group No )
//--------------------------------------------
$no = (isset($_GET["no"])) ? $_GET["no"] : "1";
$sheetNo = intval($no);
$sheetNo = ($sheetNo == 0) ? 1 : $sheetNo;
$preSheetNo  = ($sheetNo <= 1) ? $g_gnum :$sheetNo - 1;
$nextSheetNo = ($sheetNo >= $g_gnum) ? 1 :$sheetNo + 1;

//--------------------------------------------
// result
//  $g_results
//--------------------------------------------
//include("sub_result_20171126.php");
include("sub_result_2020_final.php");
//print "<pre>";
//print_r($g_results);
//print "</pre>";

//--------------------------------------------
// result (high run)
//  $g_highrun
//--------------------------------------------
include("sub_highrun.php");

//--------------------------------------------
// summary
//  $g_summary
//--------------------------------------------
include("sub_summary.php");
//print "<pre>";
//print_r($g_summary);
//print "</pre>";

$widthper = floor(60 / $conf_league_cols);
$headerWidth = "{$widthper}%";
$headerWidth = "80px";

//--------------------------------------------
// All Sheets
//--------------------------------------------
$tables = "<div id='tables'>\n";
for($groupNo = 1; $groupNo <=$g_gnum; $groupNo++)
{
	$sheetNo = $groupNo;
	$head1 = "";
	for($col=0;$col<$conf_league_cols;$col++)
	{
		$cc = $col+1;
		$shortname = "";
		$i_cc = "";
		if($col < $conf_league_nums)
		{
			$zn = "";
			if(isset($g_players[$sheetNo][$cc]))
			{
				$shortname = $g_players[$sheetNo][$cc][4];
				$zn = $g_players[$sheetNo][$cc][6];
			}
			$i_cc = ($zn) ? $zn : $cc;
		}

		$head1 .= <<<EOH
    <td rowspan="2" width="$headerWidth" class="headerName">
      $shortname
      <div class="headerNum">$i_cc</div>
    </td>

EOH;
	}

	$rankColorClass = "";

	$rows = "";
	for($row=0;$row<$conf_league_rows;$row++)
	{
		$rr = $row+1;
		$nm = "";
		$cm = "";
		$hd = "";
		$sn = "";
		$i_rr = "";
		if($row < $conf_league_nums)
		{
			$zn = "";
			if(isset($g_players[$sheetNo][$rr]))
			{
				$nm = $g_players[$sheetNo][$rr][0];
				$cm = $g_players[$sheetNo][$rr][1];
				$hd = $g_players[$sheetNo][$rr][2];
				$sn = $g_players[$sheetNo][$rr][4];
				$zn = $g_players[$sheetNo][$rr][6];
			}
			$i_rr = ($zn) ? $zn : $rr;
		}
		$nm = ($conf_league_nums > 15) ? $sn : $nm;
		$cols = "";
		for($col=0;$col<$conf_league_cols;$col++)
		{
			$cc = $col+1;
			if($cc == $rr)
			{
				$cols .= <<<EOH
    <td class="self" rowspan="2">
          <img src="adam-s.jpg" class="self-image">
    </td>

EOH;
			}
			else if(!isset($g_players[$sheetNo][$rr]))
			{
				$batsu = ($row < $conf_league_nums) ? '×' : '&nbsp;';
				$cols .= <<<EOH
    <td class="loss" rowspan="2">$batsu</td>

EOH;
			}
			else if(!isset($g_players[$sheetNo][$cc]))
			{
				$maru = ($col < $conf_league_nums) ? '○' : '&nbsp;';
				$cols .= <<<EOH
    <td class="score" rowspan="2">$maru</td>

EOH;
			}
			else
			{
				$highrun = "&nbsp;";
				$score   = "&nbsp;";
				$inning  = "&nbsp;";
				$colorClass = "";
				if(isset($g_results[$sheetNo][$rr][$cc]))
				{
					$highrun = $g_results[$sheetNo][$rr][$cc]["highrun"];
					$sc = $g_results[$sheetNo][$rr][$cc]["score"];
					$inning  = $g_results[$sheetNo][$rr][$cc]["inning"];
					$wl = $g_results[$sheetNo][$rr][$cc]["winner"];
					if($wl == "W")
					{
						$score = "W";
						$colorClass = "";
					}
					elseif($wl == "D")
					{
						$score = "D";
						$colorClass = " fgBlue";
					}
					else
					{
						$score = $sc;
						$colorClass = " fgRed";
					}
				}

				$cols .= <<<EOH
    <td rowspan=2 class="score$colorClass">$score
      <div class="highrun$colorClass">$highrun</div>
      <div class="inning$colorClass">$inning</div>
    </td>

EOH;
			}
		}

		$cmm = "";
		if(isset($g_players[$sheetNo][$rr]))
		{
			$hhd = ($hd) ? $hd : $conf_league_raceto;
			$hhh = ($hhd == $conf_league_raceto) ? "" : " ($hhd)";
			$cmm = ($hhh) ? "$cm{$hhh}" : $cm;
		}
	
		$w  = "&nbsp;";
		$l  = "&nbsp;";
		$d  = "&nbsp;";
		$a  = "";
		$wp = "";
		$hr = "";
		$bg = "";
		$tp = "";
		$ti = "";
		$ga = "";
		$ha = "";
		$ra = "";
		if(isset($g_summary[$sheetNo][$rr]["totalInning"]) &&
	   		$g_summary[$sheetNo][$rr]["totalInning"] > 0 )
		{
			$w  = $g_summary[$sheetNo][$rr]["win"];
			$l  = $g_summary[$sheetNo][$rr]["loss"];
			$d  = $g_summary[$sheetNo][$rr]["draw"];
			$a  = $g_summary[$sheetNo][$rr]["avewin"];
			$wp = $g_summary[$sheetNo][$rr]["winningPoint"];
			$hr = $g_summary[$sheetNo][$rr]["highrun"];
			$bg = $g_summary[$sheetNo][$rr]["bestgame"];
			$tp = $g_summary[$sheetNo][$rr]["totalPoint"];
			$ti = $g_summary[$sheetNo][$rr]["totalInning"];
			$ga = $g_summary[$sheetNo][$rr]["average"];
			$ha = $g_summary[$sheetNo][$rr]["ha"];
			$ra = $g_summary[$sheetNo][$rr]["ranking"];

			$rankColorClass = "";
			if($ra <= $conf_league_rankBlu)
			{
				$rankColorClass = " fgCyan";
			}
			if($ra <= $conf_league_rankRed)
			{
				$rankColorClass = " fgRed";
			}
			$hr = ($hr == 0)  ? "-" : $hr;
			$bg = ($bg == 99) ? "-" : $bg;
		}

		$gaha = $ga;
		if($is_handicap_match)
		{
			if($ha)
			{
				$gaha.= "<br/>($ha)";
			}
		}

		$rows .= <<<EOH
  <tr>
    <td rowspan=2 class="pname">$nm
      <div class="pnumber">$i_rr</div>
      <div class="pmemo">$cmm</div>
    </td>
$cols
    <td class="result">$w</td>
    <td class="result">$l</td>
    <td rowspan=2 class="result">$wp</td>
    <td class="result">$hr</td>
    <td class="result">$tp</td>
    <td rowspan=2 class="resultAve">$gaha</td>
    <td rowspan=2 class="resultRank$rankColorClass">$ra</td>
  </tr>
  <tr>
    <td colspan=2 class="result">$d</td>
    <td class="result">$bg</td>
    <td class="result">$ti</td>
  </tr>

EOH;
	}

	$gahaTitle = "GA";
	if($is_handicap_match)
	{
		$gahaTitle .= "<br/>(HA)";
	}

	$table = <<<EOH
<table class="sheet">
  <tr>
    <th width="150px">試合点数</th>
$head1
    <th class="headerHalf">W</th>
    <th class="headerHalf">L</th>
    <th rowspan="2" class="header">WP</th>
    <th class="header">HR</th>
    <th class="header">TP</th>
    <th rowspan="2" class="headerAve">$gahaTitle</th>
    <th rowspan="2" class="headerRank">R</th>
  </tr>
  <tr>
    <th>$conf_league_raceto 点</th>
    <th colspan="2">D</th>
    <th class="header">BG</th>
    <th class="header">TI</th>
  </tr>
$rows
</table>
EOH;

	$groupname = $sheetNo;
	if( $g_groups_extra ) {
		$groupname = $g_groups[$sheetNo]["name"];
	}

	$tables .= <<<EOH
<div class="floattable" id="t$groupNo">
$table
</div>
EOH;

}

$tables .= <<<EOH
<div class="floattableclear">
</div>
EOH;

$t = (isset($_GET["t"])) ? $_GET["t"] : 1;
$t = ($t+$step+1 > $g_gnum) ? 1 : $t + $step;

print <<<EOH
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8" />
    <title>全日本プロ３Ｃ選手権大会</title>
    <link rel="stylesheet" href="css/sheet.css?x=1" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Pragma" content="no-cache">

    <!-- Bootstrap
    <link href="css2/bootstrap.css?x=2" rel="stylesheet">
    <!-- Bootstrap -->

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
<script type="text/javascript">
// <![CDATA[

//キー押下時に関数を呼び出す
document.onkeydown = keydownfunc;
function keydownfunc()
{
  if(event.keyCode == 48)
  {
    // 0:48
    var click0 = document.getElementById("link-to-sheets");
    click0.click();
    return false;
  }
  if(event.keyCode == 49)
  {
    // 1:49
    var click1 = document.getElementById("link-to-matchup");
    click1.click();
    return false;
  }
  return true;
}
// ]]>
</script>

</head>
<body>
    <div id="contents">
      <div id="header">
        <h1 class="">
<div class="main-title">
第31回 全日本プロスリークッション選手権大会</div>
<div class="sub-title">
ＡＤＡＭ ＪＡＰＡＮ杯</div>
        </h1>
<div>決勝
      (<a href="qualify.php">最終予選の結果はこちら</a>)</div>
      </div>
$tables
      <div id="footer">
          <img src="adamlogo.jpg" class="logo img-rounded">
      </div>
    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed
    <script src="js2/bootstrap.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
</body>
</html>

EOH;

//print "<pre>\n";
//print_r($_GET);
//print "</pre>\n";
//print "<pre>\n";
//print_r($summary);
//print "</pre>\n";

?>
