#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""验收档位礼品表：行数 / 数字 / 筛选 / 导出 / 0 报错。"""
import sys
from playwright.sync_api import sync_playwright

F = ('/Users/liuxinyuan/Desktop/Hermes输出-工作类/营销活动/2026-10实验加速季/'
     '科研促销期_档位达成与礼品发放表_按客户.html')
ok = True
def chk(label, cond, detail=''):
    global ok
    print('  %s %-40s %s' % ('✅' if cond else '❌', label, detail))
    if not cond:
        ok = False

with sync_playwright() as p:
    br = p.chromium.launch(channel='chrome', headless=True)
    pg = br.new_page(viewport={'width': 1400, 'height': 1080})
    errs = []
    pg.on('pageerror', lambda e: errs.append(str(e)))
    pg.goto('file://' + F, wait_until='load')
    pg.wait_for_timeout(1200)
    chk('品牌页眉/页脚', pg.evaluate("() => !!document.querySelector('.bior-header') && !!document.querySelector('.bior-footer')"))
    n = pg.evaluate("() => document.querySelectorAll('#t tbody tr').length")
    chk('37 行客户', n == 37, str(n))
    chk('达档行高亮 25 行', pg.evaluate("() => document.querySelectorAll('tr.hit').length") >= 25)
    txt = pg.evaluate("() => document.body.innerText.replace(/\\s+/g,' ')")
    for kw in ['99,599', '3,300', '6,600', '按促销期内采购额套用', '未达 1,000']:
        chk('含「%s」' % kw, kw in txt)
    pg.fill('#q', '大学')
    pg.wait_for_timeout(500)
    vis = pg.evaluate("() => [...document.querySelectorAll('#t tbody tr')].filter(r=>r.style.display!=='none').length")
    chk('筛选「大学」有结果', 0 < vis < 37, '%d 行' % vis)
    chk('行数提示已更新', '/' in pg.evaluate("() => document.getElementById('n').textContent"))
    pg.fill('#q', '')
    pg.wait_for_timeout(400)
    chk('0 JS 错误', len(errs) == 0, str(errs[:2]))
    pg.screenshot(path='/tmp/tier_cust.png', full_page=True)
    br.close()

print('\n%s' % ('✅ 全部通过' if ok else '❌ 有失败项'))
sys.exit(0 if ok else 1)
