<?php
	// switch (true) {
	// 	case !isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']):
	// 	case $_SERVER['PHP_AUTH_USER'] !== 'edosaki':
	// 	case $_SERVER['PHP_AUTH_PW']   !== '27321189':
	// 	header('WWW-Authenticate: Basic realm="Enter username and password."');
	// 	header('Content-Type: text/plain; charset=utf-8');
	// 	die('このページを見るにはログインが必要です');
	// }

	require_once('common.php');
	if ($_POST['save']) {
		foreach ($_POST as $k => $v) {
			$_SESSION['scoreData'][$k] = $v;
		}
		file_put_contents('score.dat', serialize($_SESSION['scoreData']), LOCK_EX);
	} else {
		$_SESSION['scoreData'] = unserialize(file_get_contents('score.dat'));
		foreach ($_SESSION['scoreData'] as $k => $v) {
			$_POST[$k] = $v;
		}
	}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, user-scalable=no">
	<title>Score Operation</title>
	<style>
		body {
			font-size: 14px;
		}
		select {
			width: 98%;
			height: 44px;
			text-align: center;
		}
		table {
			border-collapse: collapse;
			width: 100%;
		}
		th, td {
			/*border: solid 1px #000;*/
			text-align: center;
		}
		th {
			white-space: nowrap;
		}
		input[type="text"] {
			width: 87%;
			height: 30px;
			font-size: 17px;
		}
		input[type="submit"] {
			width: 95%;
			height: 40px;
			margin: 10px 0;
		}
	</style>
</head>
<body>
	<div>
		<form action="" method="post">
		<table>
			<tr>
				<th>試合名</th>
				<td>
					<select name="gameStatus">
						<option value=""></option>
						<?php foreach($status as $id => $row){ ?>
						<option value="<?php h($id); ?>"<?php if($id == $_POST['gameStatus']) echo ' selected="selected"'; ?>><?php h($row); ?></option>
						<?php } ?>
					</select>
				</td>
				<td>
					<input type="text" name="gameName" value="<?php h($_POST['gameName']); ?>">
				</td>
			</tr>
		</table>
		<table>
			<tr>
				<th>チーム</th>
				<td><input type="text" name="aTeamNmae" value="<?php h($_POST['aTeamNmae']); ?>"></td>
				<td><input type="text" name="bTeamNmae" value="<?php h($_POST['bTeamNmae']); ?>"></td>
			</tr>
			<?php for($i = 1 ; $i <= 7 ; $i++){ ?>
			<tr>
				<th>
					<?php h($i); ?>
				</th>
				<td>
					<select name="aTeamScore<?php h($i); ?>">
						<option value=""></option>
						<option value="×"<?php if($_POST['aTeamScore'.$i] == '×') echo ' selected="selected"'; ?>>×</option>
						<?php for($s = 0 ; $s <= 99 ; $s++){ ?>
						<option value="<?php h($s); ?>"<?php if((String)$s === $_POST['aTeamScore'.$i]) echo ' selected="selected"'; ?>><?php h($s); ?></option>
						<?php } ?>
					</select>
				</td>
				<td>
					<select name="bTeamScore<?php h($i); ?>">
						<option value=""></option>
						<option value="×"<?php if($_POST['bTeamScore'.$i] == '×') echo ' selected="selected"'; ?>>×</option>
						<?php for($s = 0 ; $s <= 99 ; $s++){ ?>
						<option value="<?php h($s); ?>"<?php if((String)$s === $_POST['bTeamScore'.$i]) echo ' selected="selected"'; ?>><?php h($s); ?></option>
						<?php } ?>
					</select>
				</td>
			</tr>
			<?php } ?>
			<tr>
				<th>投手</th>
				<td colspan="2"><input type="text" name="aTeamToushu" value="<?php h($_POST['aTeamToushu']); ?>"></td>
				<!-- <td><input type="text" name="bTeamToushu" value="<?php h($_POST['bTeamToushu']); ?>"></td> -->
			</tr>
			<tr>
				<th>捕手</th>
				<td colspan="2"><input type="text" name="aTeamHoshu" value="<?php h($_POST['aTeamHoshu']); ?>"></td>
				<!-- <td><input type="text" name="bTeamHoshu" value="<?php h($_POST['bTeamHoshu']); ?>"></td> -->
			</tr>
			<tr>
				<th>本塁打</th>
				<td colspan="2"><input type="text" name="aTeamHonruida" value="<?php h($_POST['aTeamHonruida']); ?>"></td>
				<!-- <td><input type="text" name="bTeamHonruida" value="<?php h($_POST['bTeamHonruida']); ?>"></td> -->
			</tr>
			<tr>
				<th>三塁打</th>
				<td colspan="2"><input type="text" name="aTeamSanruida" value="<?php h($_POST['aTeamSanruida']); ?>"></td>
				<!-- <td><input type="text" name="bTeamSanruida" value="<?php h($_POST['bTeamSanruida']); ?>"></td> -->
			</tr>
			<tr>
				<th>二塁打</th>
				<td colspan="2"><input type="text" name="aTeamNiruida" value="<?php h($_POST['aTeamNiruida']); ?>"></td>
				<!-- <td><input type="text" name="bTeamNiruida" value="<?php h($_POST['bTeamNiruida']); ?>"></td> -->
			</tr>
		</table>
		<table>
			<tr>
				<th>試合開始時間</th>
				<td>
					<input type="text" name="gameStart" value="<?php h($_POST['gameStart']); ?>">
				</td>
			</tr>
			<tr>
				<th>試合終了時間</th>
				<td>
					<input type="text" name="gameEnd" value="<?php h($_POST['gameEnd']); ?>">
				</td>
			</tr>
		</table>
		<table>
			<tr><th colspan="3">スターティングメンバー</th></tr>
			<?php for($i = 1 ; $i <= 9 ; $i++){ ?>
			<tr>
				<th>
					　<?php h($i); ?>　
				</th>
				<td>
					<select name="memberPosition<?php h($i); ?>">
						<option value=""></option>
						<?php foreach($position as $id => $row){ ?>
						<option value="<?php h($id); ?>"<?php if($id == $_POST['memberPosition'.$i]) echo ' selected="selected"'; ?>><?php h($row); ?></option>
						<?php } ?>
					</select>
				</td>
				<td>
					<input type="text" name="memberName<?php h($i); ?>" value="<?php h($_POST['memberName'.$i]); ?>">
				</td>
			</tr>
			<?php } ?>
			<tr><td colspan="3" style="text-align:center;"><input type="submit" name="save" value="保存"></td></tr>
			<tr><td colspan="3" style="text-align:center;"><a href="./" target="_blank">確認</a></td></tr>
		</table>
		</form>
	</div>
</body>
</html>