<?php
require_once dirname(__DIR__) . '/bootstrap/chairman_production_core_bootstrap.php';
function pch($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
$raceDate = isset($_GET['race_date']) ? $_GET['race_date'] : date('Y-m-d');
$venue = isset($_GET['venue']) ? $_GET['venue'] : '';
$raceNum = isset($_GET['race_num']) ? intval($_GET['race_num']) : 0;
$mode = isset($_GET['mode']) ? strtoupper($_GET['mode']) : 'BALANCED';
$autoBuild = isset($_GET['auto_build']) ? ($_GET['auto_build']==='1') : true;
$payload = $ChairmanProductionCore->payload($raceDate,$venue,$raceNum,$mode,$autoBuild);
?>
<!doctype html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title>Chairman Production Core v3.1</title>
<style>
body{margin:0;background:#050914;color:#e7fbff;font-family:Arial,'Microsoft JhengHei';font-size:13px}.wrap{padding:22px;max-width:1700px;margin:auto}
.hero{border:1px solid #31486b;border-radius:16px;background:#0b1325;padding:20px;margin-bottom:16px}.title{font-size:28px;font-weight:900}.gold{color:#ffc107}.ok{color:#32ffa7}.bad{color:#ff5a86}
.panel{border:1px solid #283c59;border-radius:12px;background:#0c1423;margin-bottom:16px;overflow:hidden}.panel h3{margin:0;padding:12px;border-bottom:1px solid #283c59}.body{padding:12px}
.inp{background:#091222;color:#fff;border:1px solid #314966;padding:8px}.btn{background:#ffc107;color:#111;text-decoration:none;border:0;padding:9px 12px;font-weight:900}
.grid{display:grid;grid-template-columns:repeat(4,1fr);gap:12px}.stat{background:#101a2c;border:1px solid #283c59;border-radius:10px;padding:14px}.stat b{font-size:24px;display:block}
table{width:100%;border-collapse:collapse}th,td{border-bottom:1px solid #243650;padding:7px;font-size:12px;vertical-align:top}th{background:#101a2c;position:sticky;top:0}.scroll{max-height:620px;overflow:auto}
</style>
</head>
<body>
<div class="wrap">
<div class="hero"><div class="title">🏆 Chairman <span class="gold">Production Core v3.1</span></div><div>Stable entry point for future analysis.php integration.</div></div>

<div class="panel"><h3>Query</h3><div class="body">
<form method="get">
Race Date <input class="inp" type="date" name="race_date" value="<?=pch($raceDate)?>">
Venue <input class="inp" name="venue" value="<?=pch($venue)?>">
Race # <input class="inp" type="number" name="race_num" value="<?=pch($raceNum)?>">
Mode <select class="inp" name="mode"><option <?=$mode==='BALANCED'?'selected':''?>>BALANCED</option><option <?=$mode==='AGGRESSIVE'?'selected':''?>>AGGRESSIVE</option><option <?=$mode==='CONSERVATIVE'?'selected':''?>>CONSERVATIVE</option><option <?=$mode==='DEFENSIVE'?'selected':''?>>DEFENSIVE</option></select>
<input type="hidden" name="auto_build" value="1">
<button class="btn">Load / Auto Build</button>
<a class="btn" href="../cache/build_production_core_cache.php?race_date=<?=pch($raceDate)?>&venue=<?=pch($venue)?>&race_num=<?=pch($raceNum)?>&mode=<?=pch($mode)?>">Build Cache</a>
</form>
</div></div>

<div class="grid">
<div class="stat"><small>Status</small><b class="ok"><?=pch($payload['status'])?></b></div>
<div class="stat"><small>Summary</small><b class="gold"><?=pch($payload['counts']['summary'])?></b></div>
<div class="stat"><small>Ranking</small><b class="gold"><?=pch($payload['counts']['ranking'])?></b></div>
<div class="stat"><small>Top By Race</small><b class="gold"><?=pch($payload['counts']['top_by_race'])?></b></div>
</div>

<div class="panel"><h3>Top By Race</h3><div class="scroll"><table>
<tr><th>Race</th><th>Top</th><th>Name</th><th>Score</th><th>Role</th><th>Decision</th></tr>
<?php foreach($payload['top_by_race'] as $r): ?>
<tr><td>R<?=pch($r['race_num'])?></td><td class="gold"><?=pch($r['top_horse'])?></td><td><?=pch($r['top_name'])?></td><td class="gold"><?=number_format((float)$r['top_score'],1)?></td><td><?=pch($r['role'])?></td><td><?=pch($r['decision'])?></td></tr>
<?php endforeach; ?>
</table></div></div>

<div class="panel"><h3>Production Ranking</h3><div class="scroll"><table>
<tr><th>Race</th><th>Rank</th><th>Horse</th><th>Name</th><th>Role</th><th>Decision</th><th>Final</th><th>Win</th><th>Place</th><th>Q</th><th>Trio</th><th>Risk</th></tr>
<?php foreach($payload['ranking'] as $r): ?>
<tr><td>R<?=pch($r['race_num'])?></td><td class="gold">#<?=pch($r['race_rank'])?></td><td class="gold"><?=pch($r['horse_num'])?></td><td><?=pch($r['horse_name'])?></td><td><?=pch($r['role_tag'])?></td><td><?=pch($r['final_decision'])?></td><td class="gold"><?=number_format((float)$r['calibrated_score'],1)?></td><td><?=number_format((float)$r['win_score'],1)?></td><td><?=number_format((float)$r['place_score'],1)?></td><td><?=number_format((float)$r['q_score'],1)?></td><td><?=number_format((float)$r['trio_score'],1)?></td><td class="bad"><?=number_format((float)$r['risk_score'],1)?></td></tr>
<?php endforeach; ?>
</table></div></div>
</div>
</body>
</html>
