# -*- coding: utf-8 -*-
"""验证：修复后的距离与认领（桦枫居→石桥支行 不再 0 km；海上海=余杭区/余杭辖区）。"""
import io, os, sys, html as H

BUILD = r'D:\workbbbbbb\build'
SRC = r'D:\workbbbbbb\排班工具.html'
OUT = os.path.join(BUILD, '_z_e2e.html')

TEST = r'''
<script>
(function () {
  function out(t) {
    var d = document.getElementById('E2E');
    if (!d) { d = document.createElement('div'); d.id = 'E2E'; d.style.display = 'none'; document.body.appendChild(d); }
    d.appendChild(document.createTextNode(t + ' || '));
  }
  var done = false;
  function run() {
    if (done) return; done = true;
    try {
      var PB = window.__PB;
      if (!PB) { out('NO_PB'); return; }
      var st = PB.state();
      var ps = st.people;
      out('people=' + ps.length);
      for (var i = 0; i < ps.length; i++) {
        var p = ps[i];
        out(p.n + '|' + p.home + '|' + (p.district || '-') + '|auto=' + (p.autoArea || '-')
            + '|areas=' + ((p.areas && p.areas.length) ? p.areas.join(',') : (p.area || '-'))
            + '|石桥=' + PB.distP2O(p, 'T710950000', '下城辖区')
            + '|拱宸桥=' + PB.distP2O(p, 'T710220000', '拱墅辖区'));
      }
      var geo = PB.geo();
      var hk = geo.homes.filter(function (x) { return x.n === '桦枫居'; })[0];
      out('bHome=' + (hk ? hk.lat + ',' + hk.lng + ',' + (hk.src || '-') : 'none'));
      var org = geo.orgs.filter(function (x) { return x.c === 'T710950000'; })[0];
      out('bOrg=' + (org ? org.lat + ',' + org.lng + ' ' + org.a : 'none'));
      out('cover=' + document.getElementById('areaCover').textContent);
      out('stat2=' + document.getElementById('areaStat2').textContent);
      out('ERRS=' + ((window.__ERRS || []).join(' ; ') || 'none'));
    } catch (e) { out('ERR ' + e.message); }
  }
  if (document.readyState === 'complete') setTimeout(run, 300);
  else window.addEventListener('load', function () { setTimeout(run, 300); });
  setTimeout(run, 4000);
})();
</script>
'''

ERRTRAP = ('<script>window.__ERRS=[];window.addEventListener("error",function(e){'
           'window.__ERRS.push((e.message||"")+" @line"+e.lineno);});</script>\n')

s = io.open(SRC, encoding='utf-8').read()
i = s.find('<script>window.__PAIBAN_DATA__')
assert i > 0
s = s[:i] + ERRTRAP + s[i:]
j = s.rfind('</body>')
s = s[:j] + TEST + s[j:]
io.open(OUT, 'w', encoding='utf-8').write(s)
print('wrote', OUT, len(s))
