<?php
require_once dirname(__DIR__) . '/safety/ChairmanSafeLoader.php';
require_once dirname(__DIR__) . '/presenter/ChairmanGatewayPresenter.php';

function vh($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }

$raceDate = isset($_GET['race_date']) ? $_GET['race_date'] : date('Y-m-d');
$payload = ChairmanSafeLoader::payload($raceDate);
$view = array();
if (isset($payload['ok']) && $payload['ok']) {
    $view = (new ChairmanGatewayPresenter())->present($payload);
}
?>
<!doctype html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title>Chairman Gateway Presenter V46</title>
<style>
body{margin:0;background:#050914;color:#e7fbff;font-family:Arial,'Microsoft JhengHei';padding:22px}
.hero,.panel{background:#0c1423;border:1px solid #31486b;border-radius:14px;padding:16px;margin-bottom:14px}
.gold{color:#ffc107}.green{color:#32ffa7}.red{color:#ff657f}
.grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:12px}
.card{background:#09111f;border:1px solid #2b405e;border-radius:12px;padding:13px}
.races{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:12px}
.pill{display:inline-block;border-radius:999px;border:1px solid #6a5a11;padding:3px 8px;margin:2px;color:#ffe681}
table{width:100%;border-collapse:collapse}td,th{border-bottom:1px solid #243650;padding:7px;text-align:left}
@media(max-width:1100px){.grid,.races{grid-template-columns:1fr 1fr}}
@media(max-width:700px){.grid,.races{grid-template-columns:1fr}}
</style>
</head>
<body>
<div class="hero">
<h1>🏆 Chairman Gateway Presenter <span class="gold">V46.0</span></h1>
<form>Race Date <input type="date" name="race_date" value="<?=vh($raceDate)?>"><button>Load</button></form>
<p>Status: <span class="<?=isset($payload['ok'])&&$payload['ok']?'green':'red'?>"><?=isset($payload['ok'])&&$payload['ok']?'OK':'ERROR'?></span></p>
</div>

<?php if(isset($payload['ok']) && $payload['ok']): ?>
<div class="grid">
<div class="card"><small>Best Banker</small><h2 class="gold">R<?=vh(isset($view['best_banker']['race_num'])?$view['best_banker']['race_num']:'-')?> #<?=vh(isset($view['best_banker']['horse_num'])?$view['best_banker']['horse_num']:'-')?></h2><p><?=vh(isset($view['best_banker']['horse_name'])?$view['best_banker']['horse_name']:'')?>｜Score <?=vh(isset($view['best_banker']['score'])?$view['best_banker']['score']:0)?></p></div>
<div class="card"><small>Race Count</small><h2 class="green"><?=vh($view['race_count'])?></h2><p>Ranking <?=vh($view['ranking_count'])?></p></div>
<div class="card"><small>Presenter</small><h2 class="gold"><?=vh($view['presenter_version'])?></h2><p>Gateway Mapping Ready</p></div>
</div>

<div class="panel">
<h2>Race Cards</h2>
<div class="races">
<?php foreach($view['races'] as $race): ?>
<div class="card">
<h3 class="gold">R<?=vh($race['race_num'])?>｜#<?=vh(isset($race['top']['horse_num'])?$race['top']['horse_num']:'-')?> <?=vh(isset($race['top']['horse_name'])?$race['top']['horse_name']:'')?></h3>
<p>Score <?=vh(isset($race['top']['score'])?$race['top']['score']:0)?>｜<?=vh(isset($race['top']['role'])?$race['top']['role']:'')?>｜<?=vh(isset($race['top']['decision'])?$race['top']['decision']:'')?></p>
<p>Mode <?=vh($race['race_mode'])?>｜RCI <?=vh($race['rci'])?></p>
<p>Top4:
<?php foreach($race['top4'] as $h): ?>
<span class="pill">#<?=vh($h['horse_num'])?> <?=vh($h['score'])?></span>
<?php endforeach; ?>
</p>
</div>
<?php endforeach; ?>
</div>
</div>
<?php else: ?>
<div class="panel red"><?=vh(isset($payload['error'])?$payload['error']:(isset($payload['status'])?$payload['status']:'Unknown error'))?></div>
<?php endif; ?>
</body>
</html>
