#!/usr/bin/env python3
"""W2 终选 5 锁区收敛页 — 5→1 定妆终审 (8098 单选+优良中差双槽)"""
import os, time

ROOT = "/home/kai/shared/virtual-idol/week1"
PAGE_DIR = f"{ROOT}/review/w2_final5_1022"

FINAL5 = [
    (66,  "A3", "B2", "C1", "霓虹不对称短发 × 白袍电路绣 × 壁画墙", "短发灵动型 · 经典白袍"),
    (102, "A4", "B3", "C2", "机械发簪盘发 × LED旗袍 × 飘带霓虹舞", "古典盘发 · 科技旗袍 · 舞台感最强"),
    (125, "A5", "B1", "C5", "粒子长发 × 全息汉服 × 星空穹顶", "长发仙气型 · 双验证轴 A5×C5+B1 汉服"),
    (140, "A5", "B4", "C5", "粒子长发 × 光织墨裙 × 星空穹顶", "长发仙气型 · 水墨科技混搭"),
    (191, "A7", "B3", "C1", "辫顶剃侧 × LED旗袍 × 壁画墙", "个性剃侧 · 中性酷感"),
]

cards = ""
for cid, a, b, c, label, tag in FINAL5:
    pick = f"pick{cid:03d}"
    grade = f"grade{cid:03d}"
    votes = ''.join(f'<label><input type="radio" name="{grade}" value="{v}">{v}</label>' for v in ("优","良","中","差"))
    cards += f'''<section><h2>T2_{cid:03d} <small>{label}</small><span class="tag">{tag}</span></h2>
  <div class="row">
    <div class="card ref"><div class="slothead">◈ 种子（禁评）</div><img src="seed_{cid:03d}.png"></div>
    <div class="card"><div class="slothead">Col1 近身</div><img src="f{cid:03d}_col1.png"></div>
    <div class="card"><div class="slothead">Col2 全身正</div><img src="f{cid:03d}_col2.png"></div>
    <div class="card big"><div class="slothead">v14 五列整板</div><img src="f{cid:03d}_board.png"></div>
  </div>
  <div class="ctl">终选单选: <label><input type="radio" name="{pick}" value="定妆">选此定妆</label>
   &nbsp;|&nbsp; 评级: {votes}</div>
</section>'''

html = f'''<!DOCTYPE html><html lang="zh"><head><meta charset="utf-8">
<title>W2 终选 5 锁区收敛 · 1022</title><style>
body{{background:#0d1117;color:#e6edf3;font-family:system-ui;margin:0;padding:20px}}
h1{{font-size:20px}} h2{{font-size:16px;margin:26px 0 10px;border-left:4px solid #58a6ff;padding-left:10px}}
h2 small{{color:#8b949e;font-weight:normal;margin-left:8px}}
.tag{{font-size:12px;color:#d29922;margin-left:10px}}
.meta{{color:#8b949e;font-size:13px;margin:6px 0}}
.row{{display:grid;grid-template-columns:minmax(160px,220px) minmax(160px,220px) minmax(160px,220px) 1fr;gap:12px;align-items:start}}
@media(max-width:900px){{.row{{grid-template-columns:1fr 1fr}}}}
.card{{background:#161b22;border:1px solid #30363d;border-radius:10px;padding:8px}}
.card.ref{{border:1px dashed #d29922}}
.slothead{{font-size:12px;color:#8b949e;margin-bottom:5px}}
.card img{{width:100%;border-radius:6px;display:block}}
.ctl{{margin:10px 0 0;font-size:14px;display:flex;gap:14px;flex-wrap:wrap}}
.ctl label{{cursor:pointer}}
#bar{{position:sticky;top:0;background:#0d1117e0;padding:8px 12px;border-bottom:1px solid #30363d;z-index:9;font-size:14px}}
</style></head><body>
<div id="bar"><b>W2 终选 · 5 优收敛 → 1 定妆</b> · 每区: 单选「选此定妆」+ 优良中差评级 · 自动入箱 <span id="vbstat"></span></div>
<div class="meta">{time.strftime('%Y-%m-%d %H:%M')} · 5 候选全部通过锚门 fd&lt;25 · TR v14 板 + 裁格</div>
{cards}
<script>
const VB_URL='http://100.124.72.88:8098/report', VB_PAGE='w2_final5_1022';
let VB_TIMER;
function reportBox(){{try{{const votes={{}},notes={{}};
document.querySelectorAll('input[type=radio]:checked').forEach(r=>{{votes[r.name]=r.value;}});
fetch(VB_URL,{{method:'POST',headers:{{'Content-Type':'application/json'}},
body:JSON.stringify({{page:VB_PAGE,votes:votes,notes:notes,client_ts:Date.now()}})}})
.then(r=>r.json()).then(j=>{{const el=document.getElementById('vbstat');if(el)el.textContent=j.ok?'✓ 已自动入箱':'⚠ 上报失败';}})
.catch(()=>{{const el=document.getElementById('vbstat');if(el)el.textContent='⚠ 上报失败';}});}}catch(e){{}}}}
document.querySelectorAll('input[type=radio]').forEach(r=>r.addEventListener('change',()=>{{clearTimeout(VB_TIMER);VB_TIMER=setTimeout(reportBox,400);}}));
</script></body></html>'''

out = f"{PAGE_DIR}/w2_final5_review.html"
open(out, "w").write(html)

import urllib.request, json as J
imgs = [f for f in os.listdir(PAGE_DIR) if f.endswith('.png')]
missing = [f for f in imgs if f not in html]
ok = 0
for f in imgs:
    try:
        r = urllib.request.urlopen(f'http://127.0.0.1:8082/virtual-idol/week1/review/w2_final5_1022/{f}', timeout=5)
        ok += 1 if r.status == 200 else 0
    except Exception: pass
funnel = urllib.request.urlopen('https://kais-engine.taile7d1c8.ts.net/virtual-idol/week1/review/w2_final5_1022/w2_final5_review.html', timeout=8).status
req = urllib.request.Request('http://100.124.72.88:8098/report',
    data=J.dumps({'page':'w2_final5_1022','votes':{'__smoke':'probe'},'notes':{},'client_ts':1}).encode(),
    headers={'Content-Type':'application/json'})
rep = urllib.request.urlopen(req, timeout=5).read().decode()
print(J.dumps({"page": out, "imgs": len(imgs), "missing": missing or "NONE", "http": f"{ok}/{len(imgs)}",
               "funnel": funnel, "votebox": rep}))
