# -*- coding: utf-8 -*-
import io, os
SRC = r"D:\workbbbbbb\排班工具.html"
OUT = r"D:\workbbbbbb\build\_geo_e2e.html"

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

TEST = r'''
<div id="E2E"></div>
<script>
(function () {
  var done = false;
  function push(s) {
    var d = document.getElementById('E2E');
    d.textContent = (d.textContent ? d.textContent + ' || ' : '') + s;
  }
  function q(sel) { return document.querySelector(sel); }
  function click(el) { if (el) el.click(); }
  function run() {
    if (done) return; done = true;
    try {
      var D = window.__PAIBAN_DATA__ || {};
      var G = D.geo || {};
      push('geoOrgs=' + (G.orgs || []).length);
      push('geoHomes=' + (G.homes || []).length);
      push('areaCenters=' + Object.keys(G.areaCenters || {}).length);
      var d0 = (G.dist || {})['桦枫居'] || {};
      var ks = Object.keys(d0);
      push('distN=' + ks.length);
      push('sample=' + (ks.length ? ks[0] + ':' + d0[ks[0]] : '-'));

      var tb = q('[data-tab="v-set"]');
      if (tb) tb.click();
      push('tab=' + (q('.view.active') || {}).id);
      push('geoStat=' + (q('#geoStat') ? q('#geoStat').textContent : '-'));
      push('geoOrgsBtn=' + !!q('#btnGeoOrgs'));
      push('geoDistBtn=' + !!q('#btnGeoDist'));

      var pb = window.__PB;
      if (pb && pb.state) {
        var st = pb.state();
        var p = st.people.filter(function (x) { return x.n === '孙银刚'; })[0];
        push('sunHome=' + (p ? p.home : '-'));
      }

      click(q('[data-tab="v-plan"]'));
      var sug = q('#orgSug');
      var inp = q('#orgInput');
      inp.value = '拱宸桥';
      inp.dispatchEvent(new Event('input', { bubbles: true }));
      push('sugN=' + (sug ? sug.children.length : -1));
      var hasAddr = sug && sug.children.length ? sug.children[0].textContent : '';
      push('sugText=' + hasAddr.replace(/\s+/g, ' ').slice(0, 60));
      if (sug && sug.children.length) click(sug.children[0]);
      push('picked=' + (q('#pickedName') ? q('#pickedName').textContent : '-'));
      push('pickedArea=' + (q('#pickedArea') ? q('#pickedArea').textContent : '-'));
      click(q('#btnAddPlan'));
      click(q('#btnGen'));
      var r = (window.__PB && window.__PB.state) ? window.__PB.state().lastResult : null;
      var rows = (r && r.rows) || [];
      push('rows=' + rows.length);
      if (rows.length) {
        var x = rows[0];
        push('row0=' + x.area + '/' + x.org + ' d=' + JSON.stringify(x.dists || []));
        push('members=' + (x.members || []).join(','));
        var m0 = (x.members || [])[0];
        var pp = (window.__PB.state().people.filter(function (q) { return q.n === m0; })[0]) || {};
        var pre = ((G.dist || {})[pp.home] || {})['T710220000'];
        push('expect=' + pre + ' home=' + pp.home);
      }
      var tag = q('.tag');
      push('tagText=' + (tag ? tag.textContent : '-'));
      push('ERRS=' + ((window.__ERRS || []).join(' ; ') || 'none'));
    } catch (err) {
      push('ERR ' + err.message + ' @' + (err.stack || '').split('\n')[1]);
      push('ERRS=' + ((window.__ERRS || []).join(' ; ') || 'none'));
    }
  }
  if (document.readyState === 'complete') setTimeout(run, 300);
  else window.addEventListener('load', function () { setTimeout(run, 300); });
  setTimeout(run, 4000);
})();
</script>
'''

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