#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""科研秋季促销 · 课题组维度统计 HTML（含两个窗口完整数据）"""
import json
import os
import sys

sys.path.insert(0, '/Users/liuxinyuan/Desktop/Hermes输出-工作类/脚本')
import brand_kit as BK  # noqa: E402

W = '/Users/liuxinyuan/Desktop/Hermes输出-工作类'
d = json.load(open(os.path.join(W, '数据/promo_opp_stats.json'), encoding='utf-8'))
OUT = os.path.join(W, '营销活动/科研秋季促销_课题组维度统计_2026.html')
CLS = {'10W': 't10', '5W': 't5', '2W': 't2', '1W': 't1', '5K': 't5k', '2K': 't2k', '1K': 't1k', '未达标': 'none'}

proj_rows = ''.join(
    '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td class="n">%d</td><td class="n">%s</td>'
    '<td><span class="tier %s">%s</span></td><td class="n">%s</td><td class="n">%s</td></tr>' % (
        r[0], r[1], r[2], r[3], r[4], '{:,.2f}'.format(r[5]), CLS.get(r[6], 'none'), r[6],
        '{:,}'.format(r[7]) if r[7] else '—', '{:,}'.format(r[8]) if r[8] else '—')
    for r in d['proj'])

cust_rows = ''.join(
    '<tr><td>%s</td><td>%s</td><td class="n">%d</td><td class="n">%d</td><td class="n">%s</td>'
    '<td><span class="tier %s">%s</span></td></tr>' % (
        r[0], r[1], r[2], r[3], '{:,.2f}'.format(r[4]), CLS.get(r[5], 'none'), r[5])
    for r in d['cust'])

sales_rows = ''.join(
    '<tr><td>%s</td><td>%s</td><td class="n">%d</td><td class="n">%d</td><td class="n">%d</td>'
    '<td class="n">%s</td><td><span class="tier %s">%s</span></td></tr>' % (
        r[0], r[1], r[2], r[3], r[4], '{:,.2f}'.format(r[5]), CLS.get(r[6], 'none'), r[6])
    for r in d['sales'])

GD, ZJ = d['summary']['广东'], d['summary']['浙江']

page = """<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>科研秋季促销 · 课题组维度统计</title><style>
*{box-sizing:border-box}body{margin:0;background:#f4f6fa;font-family:-apple-system,"PingFang SC",sans-serif;color:#1b2430;font-size:13px;line-height:1.6}
.wrap{max-width:1280px;margin:0 auto;padding:0 16px 40px}
h1{font-size:21px;margin:14px 0 4px}.s0{color:#5f6b7a;font-size:12.4px;margin-bottom:12px}
.kpis{display:grid;grid-template-columns:1fr 1fr;gap:12px;margin-bottom:14px}
.k{background:#fff;border:1px solid #e3e8f1;border-radius:10px;padding:12px 14px}
.k .t{font-size:12.5px;color:#5f6b7a}.k b{display:block;font-size:24px;color:#1852B6;margin:2px 0}
.k span{font-size:11.3px;color:#8a94a3}.k .sub{font-size:11.8px;color:#43505f;margin-top:4px}
.card{background:#fff;border:1px solid #e3e8f1;border-radius:10px;padding:13px 15px;margin-bottom:13px}
.card h2{font-size:14.5px;margin:0 0 9px;padding-left:9px;border-left:3px solid #1F4E79}
table{width:100%%;border-collapse:collapse;font-size:12px}
th{background:#1F4E79;color:#fff;padding:6px 7px;text-align:left;position:sticky;top:0}
td{padding:5px 7px;border-bottom:1px solid #eef1f6;vertical-align:top}
td.n{text-align:right;font-variant-numeric:tabular-nums}
.scroll{max-height:620px;overflow:auto;border:1px solid #eef1f6;border-radius:8px}
.tier{display:inline-block;border-radius:4px;padding:1px 7px;font-size:10.8px;font-weight:700}
.tier.t10{background:#1F4E79;color:#fff}.tier.t5{background:#1852B6;color:#fff}.tier.t2{background:#4FA3E3;color:#fff}
.tier.t1{background:#A8C8EA;color:#0E2E63}.tier.t5k{background:#CFE0F5;color:#0E2E63}
.tier.t2k{background:#E3F4EA;color:#1B7A43}.tier.t1k{background:#F0F7E9;color:#3d6b1f}.tier.none{background:#EEF1F6;color:#8a94a3}
.note{background:#FFF6E5;border:1px solid #F3D9A6;border-radius:8px;padding:9px 11px;font-size:12.1px;margin-top:9px}
@media(max-width:900px){.kpis{grid-template-columns:1fr}.bior-tagwrap{display:none!important}}
</style></head><body><div class="wrap">
<h1>科研秋季促销 · 课题组（商机）维度统计</h1>
<div class="s0">广东 9/1–10/16 ｜浙江 8/20–9/30 ｜口径：标准销售订单，剔除测试单、赠品、已作废 ｜数据源：科研促销统计-20260820-1030</div>

<div class="kpis">
<div class="k"><div class="t">广东（9/1–10/16）</div><b>%(gd_amt)s</b><span>采购额（元）</span>
<div class="sub">课题组 %(gd_proj)d 个 ｜客户 %(gd_cust)d ｜订单 %(gd_ord)d ｜达档课题组 %(gd_hit)d<br>赠品预算 %(gd_lo)s–%(gd_hi)s 元</div></div>
<div class="k"><div class="t">浙江（8/20–9/30）</div><b>%(zj_amt)s</b><span>采购额（元）</span>
<div class="sub">课题组 %(zj_proj)d 个 ｜客户 %(zj_cust)d ｜订单 %(zj_ord)d ｜达档课题组 %(zj_hit)d<br>赠品预算 %(zj_lo)s–%(zj_hi)s 元</div></div>
</div>

<div class="card"><h2>按课题组明细 <span>%(n_proj)d 行，按采购额降序</span></h2>
<div class="scroll"><table><thead><tr><th style="width:5%%">区域</th><th style="width:9%%">销售员</th><th style="width:20%%">客户</th>
<th style="width:17%%">课题组（商机）</th><th style="width:6%%">订单</th><th style="width:11%%">采购额(元)</th>
<th style="width:8%%">档位</th><th style="width:8%%">赠品下限</th><th style="width:8%%">赠品上限</th></tr></thead>
<tbody>%(proj)s</tbody></table></div>
<div class="note">礼品按课题组发放：同一客户多个课题组分别计档、分别发礼；“按客户汇总”仅作对照，不作为发放依据。</div></div>

<div class="card"><h2>按客户汇总 <span>仅对照，不用于发礼</span></h2>
<div class="scroll"><table><thead><tr><th style="width:6%%">区域</th><th style="width:36%%">客户</th>
<th style="width:9%%">课题组数</th><th style="width:8%%">订单</th><th style="width:13%%">采购额(元)</th><th>合并档位</th></tr></thead>
<tbody>%(cust)s</tbody></table></div></div>

<div class="card"><h2>按销售员</h2>
<div class="scroll"><table><thead><tr><th style="width:6%%">区域</th><th style="width:10%%">销售员</th>
<th style="width:8%%">客户数</th><th style="width:8%%">课题组数</th><th style="width:8%%">订单</th>
<th style="width:13%%">采购额(元)</th><th>合并档位</th></tr></thead>
<tbody>%(sales)s</tbody></table></div></div>

<div class="card"><h2>口径</h2><div class="note">
<ul style="margin:4px 0 0 18px;padding:0">
<li>统计单元＝课题组（飞书商机）；金额为 ERP 价税合计，按订单去重后汇总。</li>
<li>仅标准销售订单，剔除测试销售订单、赠品行、已作废单据。</li>
<li>档位：1K≥1,000；2K≥2,000；5K≥5,000；1W≥10,000；2W≥20,000；5W≥50,000；10W≥100,000 元。</li>
<li>赠品价值按活动方案 5%%–10%% 折算，取档位上下限。</li>
<li>区域按订单销售员归属（目标基础数据_2026），不按客户注册地。</li>
</ul></div></div>
%(footer)s
</div></body></html>""" % {
    'gd_amt': '{:,.2f}'.format(GD['amount']), 'gd_proj': GD['proj'], 'gd_cust': GD['cust'],
    'gd_ord': GD['orders'], 'gd_hit': GD['hit'], 'gd_lo': '{:,}'.format(GD['lo']), 'gd_hi': '{:,}'.format(GD['hi']),
    'zj_amt': '{:,.2f}'.format(ZJ['amount']), 'zj_proj': ZJ['proj'], 'zj_cust': ZJ['cust'],
    'zj_ord': ZJ['orders'], 'zj_hit': ZJ['hit'], 'zj_lo': '{:,}'.format(ZJ['lo']), 'zj_hi': '{:,}'.format(ZJ['hi']),
    'proj': proj_rows, 'n_proj': len(d['proj']), 'cust': cust_rows, 'sales': sales_rows, 'footer': '__F__',
}
page = BK.inject(page, theme='dark', tag='科研秋季促销 · 课题组维度',
                 footer_extra='内部资料 · 不对外 ｜ 课题组＝飞书商机')
page = page.replace('__F__', '')
open(OUT, 'w', encoding='utf-8').write(page)
print('✅', OUT, '%.0f KB' % (len(page.encode()) / 1024))
