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

require_once './FlCommon.php';

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

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

try {
	if (!empty($_GET["team"])){
		$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='.TOTAL_TAIKAI_ID.'&team='.$team_id.'&taikaikey='.TAIKAI_KEY.'&teamkey='.TEAM_KEY, false, $context);

		$flData = new SimpleXMLElement($xmlstr);

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

		$record_date = $flData->{'record-data'}->{'record-dt'};

		$playerData = $flData->{'appearrances-data'}->{'appearrances'};

		$playerArray = Array();
		foreach ($playerData as $data) {
			$player = Array();

			$player["player_id"] = $data->{'player-id'};
			$player["uniform_num"] = $data->{'uniform-num'};

			$potition = $data->{'position'};
			if ($potition == 1) {
				$player["position"] = "GK";
			} else {
				$player["position"] = "FP";
			}
			$player["player_nm"] = $data->{'player-nm'};
			$player["player_eng_nm"] = $data->{'player-eng-nm'};

			$birthday = $data->{'player-birthday'};
			if (!empty($birthday)) {
				$player["birthday"] = substr($birthday,0,4).".".substr($birthday,4,2).".".substr($birthday,6,2);
			}



			$player["syutujo_num"] = $data->{'syutujo-num'};

			$player["first_game_id"] = $data->{'first-game-id'};

			$firstGameDate = $data->{'first-game-date'};
			if ($firstGameDate != "") {
				$player["first_game_date"] = substr($firstGameDate,0,4).".".substr($firstGameDate,4,2).".".substr($firstGameDate,6,2);
			} else {
				$player["first_game_date"] = "-";
			}

			$player["first_game_time"] = $data->{'first-game-time'};

			$player["first_home_team_id"] = $data->{'first-home-team-id'};
			$player["first_home_team_nm"] = $data->{'first-home-team-nm'};
			if ($player["first_home_team_nm"] == "") {
				$player["first_home_team_nm"] = "-";
			}
			$player["first_home_team_sht_nm"] = $data->{'first-home-team-sht-nm'};
			if ($player["first_home_team_sht_nm"] == "") {

				$player["first_home_team_sht_nm"] = "-";

			}

			$player["first_away_team_id"] = $data->{'first-away-team-id'};
			$player["first_away_team_nm"] = $data->{'first-away-team-nm'};
			if ($player["first_away_team_nm"] == "") {
				$player["first_away_team_nm"] = "-";
			}
			$player["first_away_team_sht_nm"] = $data->{'first-away-team-sht-nm'};

			if ($player["first_away_team_sht_nm"] == "") {

				$player["first_away_team_sht_nm"] = "-";

			}



			$playerArray[] = $player;
		}
	}
} catch (Exception $e) {
	$status = 1;
}

?>

		<div class="set">
			<!-- <p>情報が入るまでしばらくお待ちください。</p> -->
			<p class="tableNote"><?php echo substr($record_date,0,4)."年".substr($record_date,4,2)."月".substr($record_date,6,2)."日" ?></p>
			<div class="table-frame">
				<table class="table01">
					<tr>
						<th>No.<br />Pos.</th>
						<th>氏名</th>
						<th>生年月日</th>
						<th>出場数</th>
						<th>初出場日</th>
						<th>当時のクラブ</th>
						<th>対戦クラブ</th>
					</tr>
				<?php foreach ($playerArray as $player): ?>
					<tr>
						<td><?=$player["uniform_num"]?><br /><?=$player["position"]?></td>
						<td><?=$player["player_nm"]?><br /><span><?=$player["player_eng_nm"]?></span></td>
						<td><?=$player["birthday"]?></td>

						<?php if ($player["player_id"] == '1997122113'): ?>
										<td><?php echo $player["syutujo_num"] + 19; ?></td>
										<td>2018.06.17</td>
										<td>Ｆ選抜</td>
										<td>大阪</td>
										
						<?php elseif ($player["player_id"] == '1990013106'): ?>
										<td><?php echo $player["syutujo_num"] + 23; ?></td>
										<td>2014.11.02</td>
										<td>湘南</td>
										<td>仙台</td>
										
						<?php else: ?>
										<td><?php echo $player["syutujo_num"]; ?></td>
										<td><?=$player["first_game_date"]?></td>
										<td><?=$player["first_home_team_sht_nm"]?></td>
										<td><?=$player["first_away_team_sht_nm"]?></td>
						<?php endif; ?>

					</tr>
				<?php endforeach; ?>
				</table>
			</div>
		</div>
