<?php
ini_set( "display_errors", "Off");

require_once './FlCommon.php';

define("XML", "FlScheduleXml.php");

$status = 1;
$flCommon = new FlCommon();

try {
	if (!empty($_GET["tid"]) && !empty($_GET["team"])){
		$taikai_hold_id = $_GET["tid"];
		$team_id = $_GET["team"];

		$opts = array(
				'http' => array(
						'method' => "GET",
						'header' => "Referer: ".$_SERVER["HTTP_REFERER"]."\r\n".
						"Access-key: ".ACCESS_KEY."\r\n"
				)
		);
		$context = stream_context_create($opts);

		$xmlstr = file_get_contents(URL.XML.'?tid='.$taikai_hold_id.'&team='.$team_id.'&taikaikey='.TAIKAI_KEY, false, $context);

		$flschedule = new SimpleXMLElement($xmlstr);

		$status = $flschedule->{'status'};

		$roman = $flschedule->{'team-data'}->{'roman'};

		$scheduleData = $flschedule->{'schedule-data'}->{'schedule'};

		$scheduleArray = Array();

		foreach ($scheduleData as $data) {
			$schedule = Array();

			$schedule["game_id"] = $data->{'game-id'};
			$schedule["term_no"] = $data->{'term-no'};
			$schedule["game_form"] = $data->{'game-form'};
			$schedule["home_name"] = $data->{'home-name'};
			$schedule["away_name"] = $data->{'away-name'};
			$schedule["home_sht_name"] = $data->{'home-sht-name'};
			$schedule["away_sht_name"] = $data->{'away-sht-name'};
			$schedule["away_roman"] = $data->{'away-roman'};
			
			$schedule["home_logo_roman"] = $flCommon->getTeamLogoRoman($data->{'home-team-id'});
			$schedule["away_logo_roman"] = $flCommon->getTeamLogoRoman($data->{'away-team-id'});
			
			$schedule["ground_name"] = $data->{'ground-sht-name'};
			if (empty($data->{'ground-sht-name'})) {
				$schedule["ground_name"] = $data->{'ground-name'};
			}
			$schedule["note"] = $data->{'game-note'};

			$gameDate = "";
			$date = $data->{'game-date'};
			if (!empty($date)) {
				$gameDate = substr($date,0,4)."/".substr($date,4,2)."/".substr($date,6,2);
				$gameDate2 = substr($date,0,4).".".substr($date,4,2).".".substr($date,6,2);
			}

			$gameTime = "";
			$time = $data->{'game-time'};
			if (!empty($time)) {
				$gameTime = substr($time,0,2).":".substr($time,2,2);
			}
			
			$schedule["youbi"] = $flCommon->getYoubiJ($gameDate);
			
			
			$schedule["game_date"] = $gameDate2;
			$schedule["game_time"] = $gameTime;

			$schedule["game_status"] = $data->{'game-status'};
			$schedule["home_score"] = $data->{'home-score'};
			$schedule["away_score"] = $data->{'away-score'};
			
			$scheduleArray[] = $schedule;
		}
	}
} catch (Exception $e) {
	$status = 1;
}

?>

<?php if ($status == 0): ?>
			<table>

	<?php foreach ($scheduleArray as $schedule): ?>
				<tr>

					<th class="term"><?=$schedule["term_no"]?>節</th>

				<?php if($schedule["game_form"] == 2): ?>
					<td class="location">Central</td>
				<?php elseif($schedule["game_form"] == 9): ?>
					<td class="location">6クラブ<br />共同開催</td>
				<?php else: ?>
					<td class="location">H&A</td>
				<?php endif; ?>

					<td class="days"><?=$schedule["game_date"]?>（<?=$schedule["youbi"]?>）</td>

<?php if ($schedule["game_time"] == 0000): ?>
					<td class="time">未定</td>
<?php else: ?>
					<td class="time"><?=$schedule["game_time"]?></td>
<?php endif; ?>

					<td class="teamLeft"><?=$schedule["home_sht_name"]?> <img src="../../share/img/logo_<?=$schedule["home_logo_roman"]?>.png" width="25" height="26" alt="<?=$schedule["home_name"]?>" /></td>

				<?php if($schedule["game_status"] >= 7): ?>
					<td class="vs">
						<a href="../../score/score01.html?gid=<?=$schedule["game_id"]?>"><?=$schedule["home_score"]?> － <?=$schedule["away_score"]?></a>
					</td>
				<?php else: ?>
					<td class="vs">－</td>
				<?php endif; ?>
					<td class="teamRight"><img src="../../share/img/logo_<?=$schedule["away_logo_roman"]?>.png" width="25" height="26" alt="<?=$schedule["away_name"]?>" /> <?=$schedule["away_sht_name"]?></td>

					<td class="venue"><?=$schedule["ground_name"]?></td>
					<td class="tv"><?=$schedule["note"]?></td>

				</tr>
	<?php endforeach; ?>
			</table>
<?php else: ?>
	<p>日程・結果はございません。</p>
<?php endif; ?>

