Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dashboard.html 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>BizGaze Connect — Dashboard</title>
  7. <style>
  8. :root{ --brand:#FFC708; --brand-d:#E0AC00; --blue:#1F3B73; --blue-d:#16294f; --blue-soft:#EAF0FB; --ink:#1f2430; --muted:#6b7280; --bg:#f6f8fb; --card:#fff; --line:#e6e9ef; --green:#16a34a; --red:#b91c1c; }
  9. *{box-sizing:border-box;}
  10. body{font-family:'Segoe UI',system-ui,sans-serif;background:var(--bg);color:var(--ink);margin:0;}
  11. header{background:var(--blue);padding:.75rem 1.5rem;display:flex;justify-content:space-between;align-items:center;}
  12. .brandrow{display:flex;align-items:center;gap:.6rem;}
  13. .logo{width:30px;height:30px;border-radius:8px;background:var(--brand);display:grid;place-items:center;font-weight:800;color:var(--blue);}
  14. .brand{font-weight:700;color:#fff;font-size:1.05rem;} .brand span.y{color:var(--brand);font-weight:700;} .brand span.tag{color:#8ea3cf;font-weight:500;font-size:.85rem;}
  15. main{max-width:1020px;margin:1.8rem auto;padding:0 1rem;}
  16. .card{background:var(--card);border:1px solid var(--line);border-radius:14px;padding:1.5rem;margin-bottom:1.25rem;box-shadow:0 6px 18px rgba(20,30,60,.05);}
  17. h2{font-size:1rem;margin:0 0 1rem;color:var(--blue);}
  18. input,select{width:100%;padding:.6rem .7rem;border-radius:10px;border:2px solid var(--line);background:#fbfcfe;color:var(--ink);margin:.25rem 0;font-size:.92rem;}
  19. input:focus,select:focus{outline:none;border-color:var(--brand);}
  20. button{padding:.6rem 1.1rem;background:var(--brand);color:var(--ink);border:none;border-radius:10px;font-weight:700;cursor:pointer;font-size:.92rem;}
  21. button:hover{background:var(--brand-d);}
  22. button.mini{padding:.32rem .6rem;font-size:.76rem;font-weight:600;background:#eef1f6;color:var(--blue);border:1px solid var(--line);}
  23. button.mini:hover{background:var(--blue-soft);}
  24. .row{display:flex;gap:.5rem;align-items:center;}
  25. .muted{color:var(--muted);font-size:.85rem;}
  26. table{width:100%;border-collapse:collapse;font-size:.88rem;}
  27. th{color:var(--muted);font-weight:600;font-size:.76rem;text-transform:uppercase;letter-spacing:.04em;}
  28. th,td{text-align:left;padding:.55rem .5rem;border-bottom:1px solid var(--line);}
  29. .pill{font-size:.74rem;font-weight:600;padding:.15rem .55rem;border-radius:99px;}
  30. .pill.on{background:#ecfdf3;color:#15803d;}
  31. .hidden{display:none;}
  32. .tabs{display:flex;gap:.5rem;margin-bottom:1.2rem;}
  33. .tabs button{background:#eef1f6;color:var(--muted);font-weight:600;}
  34. .tabs button.active{background:var(--blue);color:#fff;}
  35. .lbl{display:block;font-size:.74rem;color:var(--muted);text-transform:uppercase;letter-spacing:.06em;margin:.7rem 0 .15rem;}
  36. .filters{display:flex;gap:.6rem;align-items:flex-end;flex-wrap:wrap;margin-bottom:1rem;}
  37. .filters .f{flex:1;min-width:140px;}
  38. .filters .lbl{margin:.1rem 0 .15rem;}
  39. .srch{max-width:320px;margin:.2rem 0 .9rem;}
  40. .pager{display:flex;gap:.5rem;align-items:center;justify-content:flex-end;margin-top:.8rem;font-size:.82rem;color:var(--muted);}
  41. .pager button{padding:.32rem .7rem;font-size:.8rem;font-weight:600;background:#eef1f6;color:var(--blue);border:1px solid var(--line);}
  42. .pager button:hover:not(:disabled){background:var(--blue-soft);}
  43. .pager button:disabled{opacity:.4;cursor:default;}
  44. .stats{display:flex;gap:1rem;flex-wrap:wrap;margin-bottom:1.25rem;}
  45. .stat{flex:1;min-width:150px;background:var(--card);border:1px solid var(--line);border-radius:14px;padding:1.1rem 1.3rem;box-shadow:0 6px 18px rgba(20,30,60,.05);}
  46. .stat .v{font-size:1.7rem;font-weight:800;color:var(--blue);line-height:1.1;}
  47. .stat .k{font-size:.78rem;color:var(--muted);text-transform:uppercase;letter-spacing:.05em;margin-top:.2rem;}
  48. .formerr{color:var(--red);font-weight:600;font-size:.88rem;margin:.7rem 0 0;min-height:1em;}
  49. .formerr.show{display:flex;align-items:center;gap:.5rem;background:#fee2e2;border:1px solid #fca5a5;border-radius:9px;padding:.6rem .75rem;animation:errShake .35s;}
  50. .formerr.show::before{content:"⚠";font-size:1rem;}
  51. @keyframes errShake{0%,100%{transform:translateX(0)}20%,60%{transform:translateX(-5px)}40%,80%{transform:translateX(5px)}}
  52. .pwwrap{position:relative;} .pwwrap input{padding-right:2.6rem;}
  53. .eye{position:absolute;right:.35rem;top:50%;transform:translateY(-50%);background:none;border:none;padding:.3rem;width:auto;color:var(--muted);display:inline-flex;align-items:center;cursor:pointer;}
  54. .eye:hover{background:none;color:var(--blue);}
  55. .profile{position:relative}
  56. .profile .pbtn{display:flex;align-items:center;gap:.5rem;background:rgba(255,255,255,.14);color:#fff;border:1px solid #46598c;border-radius:10px;padding:.4rem .85rem .4rem .5rem;font-weight:600;font-size:.88rem;cursor:pointer}
  57. .profile .pbtn:hover{background:rgba(255,255,255,.24)}
  58. .profile .pbtn .pav{width:28px;height:28px;border-radius:50%;background:var(--brand);color:var(--blue);display:grid;place-items:center;font-weight:800;font-size:.78rem}
  59. .profile .pmenu{position:absolute;right:0;top:calc(100% + 6px);background:#fff;border:1px solid #e6e9ef;border-radius:10px;box-shadow:0 10px 28px rgba(0,0,0,.18);min-width:210px;overflow:hidden;z-index:5000;display:none}
  60. .profile .pmenu.open{display:block}
  61. .profile .pmenu .phead{padding:.7rem .9rem;border-bottom:1px solid #eef1f6}
  62. .profile .pmenu .phead .n{font-weight:700;font-size:.9rem}
  63. .profile .pmenu .phead .e{color:var(--muted);font-size:.78rem}
  64. .profile .pmenu a{display:block;padding:.6rem .9rem;color:#1f2430;text-decoration:none;font-size:.9rem;cursor:pointer}
  65. .profile .pmenu a:hover{background:#f1f5f9}
  66. .profile .pmenu a.danger{color:#b91c1c;border-top:1px solid #eef1f6}
  67. </style>
  68. </head>
  69. <body>
  70. <header>
  71. <div class="brandrow"><img src="/logo.png" alt="" style="height:46px;width:auto;max-width:190px;border-radius:8px;object-fit:contain;background:#fff;padding:5px 12px;image-rendering:-webkit-optimize-contrast" onerror="this.replaceWith(Object.assign(document.createElement('div'),{className:'logo',textContent:'B'}))"><div class="brand">BizGaze <span class="y">Connect</span> <span class="tag">· Dashboard</span></div></div>
  72. <div class="row" id="hdrRight"></div>
  73. </header>
  74. <main id="app"></main>
  75. <script>
  76. const EYE_OFF='<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24"/><line x1="1" y1="1" x2="23" y2="23"/></svg>';
  77. const EYE_ON='<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>';
  78. function pwField(id,ph){return '<div class="pwwrap"><input id="'+id+'" type="password" placeholder="'+ph+'"><button type="button" class="eye" data-for="'+id+'" aria-label="Show password"></button></div>';}
  79. function wireEyes(){document.querySelectorAll('.eye').forEach(b=>{if(b._w)return;b._w=1;b.innerHTML=EYE_OFF;b.onclick=()=>{const inp=document.getElementById(b.getAttribute('data-for'));if(!inp)return;const show=inp.type==='password';inp.type=show?'text':'password';b.innerHTML=show?EYE_ON:EYE_OFF;};});}
  80. function pEsc(s){return String(s==null?'':s).replace(/[&<>"]/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;'}[c]));}
  81. function initials(name){const p=String(name||'?').trim().split(/\s+/);return ((p[0]||'?')[0]+(p[1]?p[1][0]:'')).toUpperCase();}
  82. function profileHTML(u){
  83. const display=u.name||u.email;
  84. return '<div class="profile"><button class="pbtn" id="pbtn">'
  85. + '<span class="pav">'+pEsc(initials(display))+'</span>'
  86. + pEsc(display)+' <span style="font-size:.65rem">&#9662;</span></button>'
  87. + '<div class="pmenu" id="pmenu">'
  88. + '<div class="phead"><div class="n">'+pEsc(display)+'</div><div class="e">'+pEsc(u.email)+(u.role?' · '+pEsc(u.role):'')+'</div></div>'
  89. + '<a href="/home">Home</a>'
  90. + '<a class="danger" id="plogout">Logout</a>'
  91. + '</div></div>';
  92. }
  93. function wireProfile(){const btn=document.getElementById('pbtn'),menu=document.getElementById('pmenu');if(!btn)return;btn.onclick=(e)=>{e.stopPropagation();menu.classList.toggle('open');};document.addEventListener('click',()=>menu.classList.remove('open'));const lo=document.getElementById('plogout');if(lo)lo.onclick=async()=>{try{await fetch('/api/logout',{method:'POST'});}catch(_){}location.href='/';};}
  94. const app = document.getElementById('app');
  95. const hdrRight = document.getElementById('hdrRight');
  96. async function api(path, body, method = 'POST') {
  97. const opt = { method, headers: { 'Content-Type': 'application/json' } };
  98. if (body) opt.body = JSON.stringify(body);
  99. const r = await fetch(path, opt);
  100. const data = await r.json().catch(() => ({}));
  101. if (!r.ok) throw new Error(data.error || 'request failed');
  102. return data;
  103. }
  104. function onEnter(ids, fn){ ids.forEach(id => { const el = document.getElementById(id); if (el) el.addEventListener('keydown', e => { if (e.key === 'Enter') { e.preventDefault(); fn(); } }); }); }
  105. function view(html) { app.innerHTML = html; }
  106. // ---------- Auth (login lives here; on success → home) ----------
  107. async function authView() {
  108. hdrRight.innerHTML = '';
  109. let regOpen = false;
  110. try { regOpen = (await api('/api/setup-state', null, 'GET')).registrationOpen; } catch {}
  111. view(`
  112. <div class="card" style="max-width:420px;margin:3rem auto">
  113. ${regOpen ? `<div class="tabs">
  114. <button id="tabLogin" class="active">Sign in</button>
  115. <button id="tabReg">Register team</button>
  116. </div>` : ''}
  117. <div id="loginForm">
  118. <span class="lbl">Email</span>
  119. <input id="li_email" placeholder="you@bizgaze.com" type="email">
  120. <span class="lbl">Password</span>
  121. ${pwField("li_pw","password")}
  122. <label style="display:flex;align-items:center;gap:.5rem;margin:.7rem 0;color:var(--ink);font-size:.9rem;cursor:pointer"><input type="checkbox" id="li_remember" style="width:18px;height:18px;accent-color:var(--blue);margin:0"> Remember me on this device</label>
  123. <button id="li_btn" style="width:100%;margin-top:.5rem">Sign in</button>
  124. <p id="li_err" class="formerr"></p>
  125. </div>
  126. ${regOpen ? `<div id="regForm" class="hidden">
  127. <span class="lbl">Team name</span>
  128. <input id="rg_team" placeholder="e.g. BizGaze Support">
  129. <span class="lbl">Email</span>
  130. <input id="rg_email" placeholder="you@bizgaze.com" type="email">
  131. <span class="lbl">Password</span>
  132. ${pwField("rg_pw","min 8 characters")}
  133. <button id="rg_btn" style="width:100%;margin-top:1rem">Create team</button>
  134. <p id="rg_err" class="formerr"></p>
  135. </div>` : ''}
  136. </div>`);
  137. document.getElementById('li_btn').onclick = doLogin;
  138. wireEyes();
  139. onEnter(['li_email','li_pw'], doLogin);
  140. if (regOpen) {
  141. document.getElementById('tabLogin').onclick = () => toggle(true);
  142. document.getElementById('tabReg').onclick = () => toggle(false);
  143. document.getElementById('rg_btn').onclick = doRegister;
  144. onEnter(['rg_team','rg_email','rg_pw'], doRegister);
  145. }
  146. function toggle(login) {
  147. document.getElementById('loginForm').classList.toggle('hidden', !login);
  148. document.getElementById('regForm').classList.toggle('hidden', login);
  149. document.getElementById('tabLogin').classList.toggle('active', login);
  150. const rt = document.getElementById('tabReg'); if (rt) rt.classList.toggle('active', !login);
  151. }
  152. }
  153. function showErr(id, msg) { const el = document.getElementById(id); el.textContent = msg; el.classList.add('show'); }
  154. function clearErr(id) { const el = document.getElementById(id); el.textContent = ''; el.classList.remove('show'); }
  155. async function doLogin() {
  156. clearErr('li_err');
  157. try {
  158. const rem = document.getElementById('li_remember');
  159. await api('/api/login', { email: li_email.value, password: li_pw.value, remember: rem ? rem.checked : false });
  160. location.href = '/home';
  161. } catch (e) {
  162. showErr('li_err', /invalid credentials/i.test(e.message) ? 'Incorrect email or password. Please try again.' : e.message);
  163. }
  164. }
  165. async function doRegister() {
  166. clearErr('rg_err');
  167. try {
  168. await api('/api/register', { email: rg_email.value, password: rg_pw.value, teamName: rg_team.value });
  169. await api('/api/login', { email: rg_email.value, password: rg_pw.value });
  170. location.href = '/home';
  171. } catch (e) { showErr('rg_err', e.message); }
  172. }
  173. // ---------- Dashboard ----------
  174. let ME = null, IS_ADMIN = false;
  175. async function dashboard(me) {
  176. ME = me; IS_ADMIN = (me.role === 'admin');
  177. hdrRight.innerHTML = profileHTML(me); wireProfile();
  178. view(`
  179. <div class="stats" id="stats"></div>
  180. <div class="card">
  181. <h2>${IS_ADMIN ? 'Connection report — all agents' : 'My connection report'}</h2>
  182. <div class="filters">
  183. ${IS_ADMIN ? '<div class="f"><span class="lbl">Agent</span><select id="fAgent"><option value="">All agents</option></select></div>' : ''}
  184. <div class="f"><span class="lbl">From</span><input id="fFrom" type="date"></div>
  185. <div class="f"><span class="lbl">To</span><input id="fTo" type="date"></div>
  186. <button id="fApply">Apply</button>
  187. <button id="fExcel" class="mini" style="padding:.6rem .9rem">⬇ Excel</button>
  188. <button id="fPdf" class="mini" style="padding:.6rem .9rem">⬇ PDF</button>
  189. </div>
  190. ${IS_ADMIN ? '<input id="repSearch" class="srch" placeholder="Search by agent or ticket">' : ''}
  191. <table id="report"><thead><tr><th>Date</th><th>Start time</th>${IS_ADMIN ? '<th>Agent</th>' : ''}<th>Ticket</th><th>Time spent</th><th>Recording / Transcript</th></tr></thead><tbody></tbody></table>
  192. <div id="repPager" class="pager"></div>
  193. <p id="repSummary" class="muted" style="margin-top:.6rem"></p>
  194. </div>`);
  195. document.getElementById('fApply').onclick = loadReport;
  196. document.getElementById('fExcel').onclick = exportExcel;
  197. document.getElementById('fPdf').onclick = exportPdf;
  198. if (IS_ADMIN) await populateAgentFilter();
  199. await loadReport();
  200. }
  201. const PER_PAGE = 5;
  202. function pagerHTML(page, pages, total, fn){
  203. if (total <= PER_PAGE) return total ? `<span>${total} total</span>` : '';
  204. return `<button ${page<=1?'disabled':''} onclick="${fn}(${page-1})">‹ Prev</button>`
  205. + `<span>Page ${page} of ${pages} · ${total} total</span>`
  206. + `<button ${page>=pages?'disabled':''} onclick="${fn}(${page+1})">Next ›</button>`;
  207. }
  208. async function populateAgentFilter() {
  209. try {
  210. const rows = await api('/api/users', null, 'GET');
  211. const sel = document.getElementById('fAgent'); if (!sel) return;
  212. const cur = sel.value;
  213. sel.innerHTML = '<option value="">All agents</option>' + rows.map(u => `<option value="${esc(u.email)}">${esc(u.name || u.email)}</option>`).join('');
  214. sel.value = cur;
  215. } catch { /* non-admins cannot list agents */ }
  216. }
  217. function fmtDuration(ms) {
  218. if (ms == null) return '—';
  219. const s = Math.round(ms / 1000);
  220. if (s < 60) return s + 's';
  221. const m = Math.floor(s / 60), r = s % 60;
  222. if (m < 60) return m + 'm ' + r + 's';
  223. return Math.floor(m / 60) + 'h ' + (m % 60) + 'm';
  224. }
  225. let REPORT_ROWS = [], reportPage = 1, reportSearch = '';
  226. function reportRowHTML(r){
  227. const d = new Date(r.started_at);
  228. const dur = r.ended_at ? (r.ended_at - r.started_at) : null;
  229. return `<tr>
  230. <td>${d.toLocaleDateString()}</td>
  231. <td class="muted">${d.toLocaleTimeString([], {hour:'2-digit',minute:'2-digit'})}</td>
  232. ${IS_ADMIN ? `<td>${esc(r.agent_name || r.agent_email || '—')}</td>` : ''}
  233. <td>${esc(r.ticket || 'Direct session')}</td>
  234. <td>${r.ended_at ? fmtDuration(dur) : '<span class="pill on">in progress</span>'}</td>
  235. <td>${[
  236. r.recording ? `<a class="mini" style="text-decoration:none;display:inline-block;padding:.32rem .6rem;margin:1px" href="/recordings/${esc(r.recording)}" download>⬇ Video</a>` : '',
  237. r.transcript ? `<a class="mini" style="text-decoration:none;display:inline-block;padding:.32rem .6rem;margin:1px" href="/transcripts/${esc(r.transcript)}" download>⬇ Text</a>` : ''
  238. ].join('') || '<span class="muted">—</span>'}</td>
  239. </tr>`;
  240. }
  241. async function loadReport() {
  242. const q = new URLSearchParams();
  243. const fa = document.getElementById('fAgent');
  244. if (fa && fa.value) q.set('agent', fa.value);
  245. if (fFrom.value) q.set('from', fFrom.value);
  246. if (fTo.value) q.set('to', fTo.value);
  247. REPORT_ROWS = await api('/api/report?' + q.toString(), null, 'GET');
  248. reportPage = 1;
  249. const s = document.getElementById('repSearch');
  250. if (s && !s._w){ s._w = 1; s.addEventListener('input', () => { reportSearch = s.value.trim().toLowerCase(); reportPage = 1; renderReport(); }); }
  251. renderStats();
  252. renderReport();
  253. }
  254. window.reportGo = (p) => { reportPage = p; renderReport(); };
  255. function filteredRows(){
  256. return reportSearch ? REPORT_ROWS.filter(r => ((r.agent_name||'')+' '+(r.agent_email||'')+' '+(r.ticket||'')).toLowerCase().includes(reportSearch)) : REPORT_ROWS;
  257. }
  258. function renderStats(){
  259. const el = document.getElementById('stats'); if (!el) return;
  260. const rows = REPORT_ROWS;
  261. const total = rows.length;
  262. const totalMs = rows.reduce((a, r) => a + (r.ended_at ? r.ended_at - r.started_at : 0), 0);
  263. const recs = rows.filter(r => r.recording).length;
  264. const cards = [
  265. { v: total, k: IS_ADMIN ? 'Total sessions' : 'My sessions' },
  266. { v: fmtDuration(totalMs), k: 'Time spent' },
  267. { v: recs, k: 'Recorded' },
  268. ];
  269. el.innerHTML = cards.map(c => `<div class="stat"><div class="v">${esc(String(c.v))}</div><div class="k">${esc(c.k)}</div></div>`).join('');
  270. }
  271. function renderReport(){
  272. const all = filteredRows();
  273. const pages = Math.max(1, Math.ceil(all.length / PER_PAGE));
  274. if (reportPage > pages) reportPage = pages;
  275. const slice = all.slice((reportPage-1)*PER_PAGE, (reportPage-1)*PER_PAGE + PER_PAGE);
  276. const cols = IS_ADMIN ? 6 : 5;
  277. document.querySelector('#report tbody').innerHTML = slice.map(reportRowHTML).join('') || `<tr><td colspan=${cols} class="muted">No sessions match.</td></tr>`;
  278. document.getElementById('repPager').innerHTML = pagerHTML(reportPage, pages, all.length, 'reportGo');
  279. const total = all.reduce((a, r) => a + (r.ended_at ? r.ended_at - r.started_at : 0), 0);
  280. repSummary.textContent = all.length ? `${all.length} session(s) · total time ${fmtDuration(total)}` : '';
  281. }
  282. function reportData() {
  283. return filteredRows().map((r) => {
  284. const d = new Date(r.started_at);
  285. return {
  286. date: d.toLocaleDateString(), start: d.toLocaleTimeString([], {hour:'2-digit',minute:'2-digit'}),
  287. agent: r.agent_name || r.agent_email || '', ticket: r.ticket || 'Direct session',
  288. spent: r.ended_at ? fmtDuration(r.ended_at - r.started_at) : 'in progress',
  289. };
  290. });
  291. }
  292. function exportExcel() {
  293. const rows = reportData();
  294. if (!rows.length) { repSummary.textContent = 'Nothing to export for this period.'; return; }
  295. const head = ['Date','Start time'].concat(IS_ADMIN ? ['Agent'] : []).concat(['Ticket','Time spent']);
  296. const csvCell = (v) => '"' + String(v).replace(/"/g, '""') + '"';
  297. const out = '' + [head, ...rows.map(r => [r.date, r.start].concat(IS_ADMIN ? [r.agent] : []).concat([r.ticket, r.spent]))]
  298. .map(line => line.map(csvCell).join(',')).join('\r\n');
  299. const a = document.createElement('a');
  300. a.href = URL.createObjectURL(new Blob([out], { type: 'text/csv;charset=utf-8' }));
  301. a.download = 'connection-report.csv';
  302. a.click(); URL.revokeObjectURL(a.href);
  303. }
  304. function exportPdf() {
  305. const rows = reportData();
  306. if (!rows.length) { repSummary.textContent = 'Nothing to export for this period.'; return; }
  307. const period = (fFrom.value || 'start') + ' to ' + (fTo.value || 'today');
  308. const fa = document.getElementById('fAgent');
  309. const agentSel = IS_ADMIN ? (fa && fa.value || 'All agents') : (ME.name || ME.email);
  310. const w = window.open('', '_blank');
  311. const headCells = ['Date','Start time'].concat(IS_ADMIN ? ['Agent'] : []).concat(['Ticket','Time spent']);
  312. w.document.write('<html><head><title>Connection report</title><style>' +
  313. 'body{font-family:Segoe UI,Arial,sans-serif;color:#1f2430;margin:32px}' +
  314. 'h1{font-size:18px;color:#1F3B73;border-bottom:3px solid #FFC708;padding-bottom:6px}' +
  315. '.meta{color:#6b7280;font-size:12px;margin-bottom:14px}' +
  316. 'table{width:100%;border-collapse:collapse;font-size:12px}' +
  317. 'th{background:#1F3B73;color:#fff;text-align:left;padding:6px 8px}' +
  318. 'td{padding:6px 8px;border-bottom:1px solid #e6e9ef}' +
  319. '</style></head><body>' +
  320. '<h1>BizGaze Connect — Connection report</h1>' +
  321. '<div class="meta">' + esc(IS_ADMIN ? 'Agent: ' + agentSel : 'Agent: ' + agentSel) + ' · Period: ' + esc(period) + ' · Generated ' + new Date().toLocaleString() + '</div>' +
  322. '<table><tr>' + headCells.map(h => '<th>' + esc(h) + '</th>').join('') + '</tr>' +
  323. rows.map(r => '<tr><td>' + [r.date, r.start].concat(IS_ADMIN ? [esc(r.agent)] : []).concat([esc(r.ticket), r.spent]).join('</td><td>') + '</td></tr>').join('') +
  324. '</table><div class="meta" style="margin-top:12px">' + esc(repSummary.textContent) + '</div></body></html>');
  325. w.document.close();
  326. w.onload = () => { w.print(); };
  327. }
  328. function esc(s) { return String(s == null ? '' : s).replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c])); }
  329. // ---------- Boot ----------
  330. // Login lives on /home — send logged-out visitors there.
  331. (async function () {
  332. try { const me = await api('/api/me', null, 'GET'); dashboard(me); }
  333. catch { location.href = '/home'; }
  334. })();
  335. </script>
  336. </body>
  337. </html>