Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 Support — Staff Console</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{color:var(--brand);font-weight:600;}
  15. .who{color:#dbe4f5;font-size:.85rem;margin-right:.7rem;}
  16. main{max-width:1020px;margin:1.8rem auto;padding:0 1rem;}
  17. .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);}
  18. h2{font-size:1rem;margin:0 0 1rem;color:var(--blue);}
  19. 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;}
  20. input:focus,select:focus{outline:none;border-color:var(--brand);}
  21. button{padding:.6rem 1.1rem;background:var(--brand);color:var(--ink);border:none;border-radius:10px;font-weight:700;cursor:pointer;font-size:.92rem;}
  22. button:hover{background:var(--brand-d);}
  23. button.ghost{background:transparent;color:#dbe4f5;border:1px solid #46598c;font-weight:600;}
  24. button.ghost:hover{background:var(--blue-d);}
  25. button.mini{padding:.32rem .6rem;font-size:.76rem;font-weight:600;background:#eef1f6;color:var(--blue);border:1px solid var(--line);}
  26. button.mini:hover{background:var(--blue-soft);}
  27. button.mini.danger{color:var(--red);}
  28. .row{display:flex;gap:.5rem;align-items:center;}
  29. .muted{color:var(--muted);font-size:.85rem;}
  30. table{width:100%;border-collapse:collapse;font-size:.88rem;}
  31. th{color:var(--muted);font-weight:600;font-size:.76rem;text-transform:uppercase;letter-spacing:.04em;}
  32. th,td{text-align:left;padding:.55rem .5rem;border-bottom:1px solid var(--line);}
  33. .pill{font-size:.74rem;font-weight:600;padding:.15rem .55rem;border-radius:99px;}
  34. .pill.on{background:#ecfdf3;color:#15803d;} .pill.off{background:#fee2e2;color:var(--red);}
  35. .hidden{display:none;}
  36. .tabs{display:flex;gap:.5rem;margin-bottom:1.2rem;}
  37. .tabs button{background:#eef1f6;color:var(--muted);font-weight:600;}
  38. .tabs button.active{background:var(--blue);color:#fff;}
  39. .quick{display:flex;align-items:center;justify-content:space-between;gap:1rem;background:linear-gradient(120deg,var(--blue),var(--blue-d));color:#fff;border:none;}
  40. .quick h2{color:#fff;margin:0 0 .25rem;}
  41. .quick p{margin:0;color:#cdd7ee;font-size:.88rem;}
  42. .quick a{background:var(--brand);color:var(--ink);text-decoration:none;font-weight:700;padding:.7rem 1.3rem;border-radius:10px;white-space:nowrap;}
  43. .quick a:hover{background:var(--brand-d);}
  44. .lbl{display:block;font-size:.74rem;color:var(--muted);text-transform:uppercase;letter-spacing:.06em;margin:.7rem 0 .15rem;}
  45. .filters{display:flex;gap:.6rem;align-items:flex-end;flex-wrap:wrap;margin-bottom:1rem;}
  46. .filters .f{flex:1;min-width:140px;}
  47. .filters .lbl{margin:.1rem 0 .15rem;}
  48. .profile{position:relative}
  49. .profile .pbtn{display:flex;align-items:center;gap:.4rem;background:rgba(255,255,255,.14);color:#fff;border:1px solid #46598c;border-radius:10px;padding:.45rem .85rem;font-weight:600;font-size:.88rem;cursor:pointer}
  50. .profile .pbtn:hover{background:rgba(255,255,255,.24)}
  51. .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:190px;overflow:hidden;z-index:5000;display:none}
  52. .profile .pmenu.open{display:block}
  53. .profile .pmenu a{display:block;padding:.6rem .9rem;color:#1f2430;text-decoration:none;font-size:.9rem;cursor:pointer}
  54. .profile .pmenu a:hover{background:#f1f5f9}
  55. .profile .pmenu a.danger{color:#b91c1c;border-top:1px solid #eef1f6}
  56. </style>
  57. </head>
  58. <body>
  59. <header>
  60. <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>Support</span> <span style="color:#8ea3cf;font-weight:500;font-size:.85rem">· Console</span></div></div>
  61. <div class="row" id="hdrRight"></div>
  62. </header>
  63. <main id="app"></main>
  64. <script>
  65. function pEsc(s){return String(s==null?'':s).replace(/[&<>"]/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;'}[c]));}
  66. function profileHTML(name){return '<div class="profile"><button class="pbtn" id="pbtn">'+pEsc(name)+' <span style="font-size:.65rem">&#9662;</span></button><div class="pmenu" id="pmenu"><a href="/console">Console / Dashboard</a><a id="plogout" class="danger">Logout</a></div></div>';}
  67. 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='/';};}
  68. function makeBrandClickable(){document.querySelectorAll('.brandrow,.wordmark').forEach(el=>{el.style.cursor='pointer';el.addEventListener('click',()=>{location.href='/';});});}
  69. makeBrandClickable();
  70. const app = document.getElementById('app');
  71. const hdrRight = document.getElementById('hdrRight');
  72. async function api(path, body, method = 'POST') {
  73. const opt = { method, headers: { 'Content-Type': 'application/json' } };
  74. if (body) opt.body = JSON.stringify(body);
  75. const r = await fetch(path, opt);
  76. const data = await r.json().catch(() => ({}));
  77. if (!r.ok) throw new Error(data.error || 'request failed');
  78. return data;
  79. }
  80. 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(); } }); }); }
  81. function view(html) { app.innerHTML = html; }
  82. // ---------- Auth ----------
  83. async function authView() {
  84. hdrRight.innerHTML = '';
  85. let regOpen = false;
  86. try { regOpen = (await api('/api/setup-state', null, 'GET')).registrationOpen; } catch {}
  87. view(`
  88. <div class="card" style="max-width:420px;margin:3rem auto">
  89. <div class="tabs">
  90. <button id="tabLogin" class="active">Sign in</button>
  91. ${regOpen ? '<button id="tabReg">Register team</button>' : ''}
  92. </div>
  93. <div id="loginForm">
  94. <span class="lbl">Email</span>
  95. <input id="li_email" placeholder="you@bizgaze.com" type="email">
  96. <span class="lbl">Password</span>
  97. <input id="li_pw" placeholder="password" type="password">
  98. <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>
  99. <button id="li_btn" style="width:100%;margin-top:.5rem">Sign in</button>
  100. <p id="li_err" class="muted"></p>
  101. </div>
  102. ${regOpen ? `<div id="regForm" class="hidden">
  103. <span class="lbl">Team name</span>
  104. <input id="rg_team" placeholder="e.g. BizGaze Support">
  105. <span class="lbl">Email</span>
  106. <input id="rg_email" placeholder="you@bizgaze.com" type="email">
  107. <span class="lbl">Password</span>
  108. <input id="rg_pw" placeholder="min 8 characters" type="password">
  109. <button id="rg_btn" style="width:100%;margin-top:1rem">Create team</button>
  110. <p id="rg_err" class="muted"></p>
  111. </div>` : ''}
  112. </div>`);
  113. document.getElementById('li_btn').onclick = doLogin;
  114. onEnter(['li_email','li_pw'], doLogin);
  115. if (regOpen) {
  116. document.getElementById('tabLogin').onclick = () => toggle(true);
  117. document.getElementById('tabReg').onclick = () => toggle(false);
  118. document.getElementById('rg_btn').onclick = doRegister;
  119. onEnter(['rg_team','rg_email','rg_pw'], doRegister);
  120. }
  121. function toggle(login) {
  122. document.getElementById('loginForm').classList.toggle('hidden', !login);
  123. document.getElementById('regForm').classList.toggle('hidden', login);
  124. document.getElementById('tabLogin').classList.toggle('active', login);
  125. const rt = document.getElementById('tabReg'); if (rt) rt.classList.toggle('active', !login);
  126. }
  127. }
  128. async function doLogin() {
  129. try {
  130. const rem = document.getElementById('li_remember');
  131. await api('/api/login', { email: li_email.value, password: li_pw.value, remember: rem ? rem.checked : false });
  132. location.reload();
  133. } catch (e) { li_err.textContent = e.message; }
  134. }
  135. async function doRegister() {
  136. try {
  137. await api('/api/register', { email: rg_email.value, password: rg_pw.value, teamName: rg_team.value });
  138. await api('/api/login', { email: rg_email.value, password: rg_pw.value });
  139. location.reload();
  140. } catch (e) { rg_err.textContent = e.message; }
  141. }
  142. // ---------- Dashboard ----------
  143. let ME = null;
  144. async function dashboard(me) {
  145. ME = me;
  146. hdrRight.innerHTML = profileHTML((me.name||me.email)+' · '+me.role); wireProfile();
  147. view(`
  148. <div class="card quick">
  149. <div><h2>Start a support session</h2><p>Customer gives you their 6-digit code from the share page.</p></div>
  150. <a href="/connect">Open connect page →</a>
  151. </div>
  152. <div class="card" id="agentsCard">
  153. <h2>Agents</h2>
  154. <table id="agents"><thead><tr><th>Email</th><th>Display name</th><th>Role</th><th>Status</th><th style="width:280px"></th></tr></thead><tbody></tbody></table>
  155. <div class="row" style="margin-top:1rem;flex-wrap:wrap">
  156. <input id="agEmail" placeholder="agent email" style="max-width:200px">
  157. <input id="agName" placeholder="display name (from BizGaze)" style="max-width:210px">
  158. <input id="agPw" placeholder="temporary password" style="max-width:170px">
  159. <select id="agRole" style="max-width:140px">
  160. <option value="technician">technician</option><option value="admin">admin</option><option value="viewer">view-only</option>
  161. </select>
  162. <button id="agAdd">Add agent</button>
  163. </div>
  164. <p id="agOut" class="muted"></p>
  165. </div>
  166. <div class="card">
  167. <h2>Session report</h2>
  168. <div class="filters">
  169. <div class="f"><span class="lbl">Agent</span><select id="fAgent"><option value="">All agents</option></select></div>
  170. <div class="f"><span class="lbl">From</span><input id="fFrom" type="date"></div>
  171. <div class="f"><span class="lbl">To</span><input id="fTo" type="date"></div>
  172. <button id="fApply">Apply</button>
  173. <button id="fExcel" class="mini" style="padding:.6rem .9rem">⬇ Excel</button>
  174. <button id="fPdf" class="mini" style="padding:.6rem .9rem">⬇ PDF</button>
  175. </div>
  176. <table id="report"><thead><tr><th>Date</th><th>Start time</th><th>Agent</th><th>Ticket</th><th>Time spent</th></tr></thead><tbody></tbody></table>
  177. <p id="repSummary" class="muted" style="margin-top:.6rem"></p>
  178. </div>`);
  179. if (me.role !== 'admin') document.getElementById('agentsCard').style.display = 'none';
  180. else {
  181. document.getElementById('agAdd').onclick = addAgent;
  182. onEnter(['agEmail','agName','agPw'], addAgent);
  183. await loadAgents();
  184. }
  185. document.getElementById('fApply').onclick = loadReport;
  186. document.getElementById('fExcel').onclick = exportExcel;
  187. document.getElementById('fPdf').onclick = exportPdf;
  188. await populateAgentFilter();
  189. await loadReport();
  190. }
  191. async function addAgent() {
  192. try {
  193. const r = await api('/api/users', { email: agEmail.value, name: agName.value, password: agPw.value, role: agRole.value });
  194. agOut.textContent = `Agent ${r.email} added. Share the email + temporary password — they sign in at /connect.`;
  195. agEmail.value = ''; agName.value = ''; agPw.value = '';
  196. loadAgents(); populateAgentFilter();
  197. } catch (e) { agOut.textContent = e.message; }
  198. }
  199. async function loadAgents() {
  200. const rows = await api('/api/users', null, 'GET');
  201. document.querySelector('#agents tbody').innerHTML = rows.map((u) => `
  202. <tr>
  203. <td>${esc(u.email)}</td><td>${esc(u.name || '—')}</td><td>${esc(u.role)}</td>
  204. <td><span class="pill ${u.active === 0 ? 'off' : 'on'}">${u.active === 0 ? 'deactivated' : 'active'}</span></td>
  205. <td>
  206. <button class="mini" onclick="resetPw('${u.id}','${esc(u.email)}')">Reset password</button>
  207. <button class="mini" onclick="renameAgent('${u.id}','${esc(u.email)}')">Edit name</button>
  208. ${u.id === ME.id ? '' : (u.active === 0
  209. ? `<button class="mini" onclick="manage('${u.id}','activate')">Activate</button>`
  210. : `<button class="mini danger" onclick="manage('${u.id}','deactivate')">Deactivate</button>`)
  211. }
  212. ${u.id === ME.id ? '' : `<button class="mini danger" onclick="delAgent('${u.id}','${esc(u.email)}')">Delete</button>`}
  213. </td>
  214. </tr>`).join('');
  215. }
  216. window.resetPw = async (id, email) => {
  217. const pw = prompt(`New password for ${email} (min 8 characters):`);
  218. if (!pw) return;
  219. try { await api('/api/users/manage', { id, action: 'reset-password', password: pw }); agOut.textContent = `Password reset for ${email}. They were signed out everywhere.`; }
  220. catch (e) { agOut.textContent = e.message; }
  221. };
  222. window.renameAgent = async (id, email) => {
  223. const name = prompt(`Display name for ${email} (as in the BizGaze app):`);
  224. if (!name) return;
  225. try { await api('/api/users/manage', { id, action: 'rename', name }); loadAgents(); }
  226. catch (e) { agOut.textContent = e.message; }
  227. };
  228. window.manage = async (id, action) => {
  229. try { await api('/api/users/manage', { id, action }); loadAgents(); }
  230. catch (e) { agOut.textContent = e.message; }
  231. };
  232. window.delAgent = async (id, email) => {
  233. if (!confirm(`Delete ${email}? This cannot be undone. (Tip: Deactivate keeps their history.)`)) return;
  234. try { await api('/api/users/manage', { id, action: 'delete' }); loadAgents(); populateAgentFilter(); }
  235. catch (e) { agOut.textContent = e.message; }
  236. };
  237. // ---------- Session report ----------
  238. async function populateAgentFilter() {
  239. try {
  240. const rows = await api('/api/users', null, 'GET');
  241. const sel = document.getElementById('fAgent');
  242. const cur = sel.value;
  243. sel.innerHTML = '<option value="">All agents</option>' + rows.map(u => `<option value="${esc(u.email)}">${esc(u.name || u.email)}</option>`).join('');
  244. sel.value = cur;
  245. } catch { /* non-admins cannot list agents; filter stays "All" */ }
  246. }
  247. function fmtDuration(ms) {
  248. if (ms == null) return '—';
  249. const s = Math.round(ms / 1000);
  250. if (s < 60) return s + 's';
  251. const m = Math.floor(s / 60), r = s % 60;
  252. if (m < 60) return m + 'm ' + r + 's';
  253. return Math.floor(m / 60) + 'h ' + (m % 60) + 'm';
  254. }
  255. let REPORT_ROWS = [];
  256. async function loadReport() {
  257. const q = new URLSearchParams();
  258. if (fAgent.value) q.set('agent', fAgent.value);
  259. if (fFrom.value) q.set('from', fFrom.value);
  260. if (fTo.value) q.set('to', fTo.value);
  261. const rows = await api('/api/report?' + q.toString(), null, 'GET');
  262. REPORT_ROWS = rows;
  263. document.querySelector('#report tbody').innerHTML = rows.map((r) => {
  264. const d = new Date(r.started_at);
  265. const dur = r.ended_at ? (r.ended_at - r.started_at) : null;
  266. return `<tr>
  267. <td>${d.toLocaleDateString()}</td>
  268. <td class="muted">${d.toLocaleTimeString([], {hour:'2-digit',minute:'2-digit'})}</td>
  269. <td>${esc(r.agent_name || r.agent_email || '—')}</td>
  270. <td>${esc(r.ticket || 'Direct session')}</td>
  271. <td>${r.ended_at ? fmtDuration(dur) : '<span class="pill on">in progress</span>'}</td>
  272. </tr>`;
  273. }).join('') || '<tr><td colspan=5 class="muted">No sessions in this period.</td></tr>';
  274. const total = rows.reduce((a, r) => a + (r.ended_at ? r.ended_at - r.started_at : 0), 0);
  275. repSummary.textContent = rows.length ? `${rows.length} session(s) · total time ${fmtDuration(total)}` : '';
  276. }
  277. function reportData() {
  278. return REPORT_ROWS.map((r) => {
  279. const d = new Date(r.started_at);
  280. return {
  281. date: d.toLocaleDateString(), start: d.toLocaleTimeString([], {hour:'2-digit',minute:'2-digit'}),
  282. agent: r.agent_name || r.agent_email || '', ticket: r.ticket || 'Direct session',
  283. spent: r.ended_at ? fmtDuration(r.ended_at - r.started_at) : 'in progress',
  284. };
  285. });
  286. }
  287. function exportExcel() {
  288. const rows = reportData();
  289. if (!rows.length) { repSummary.textContent = 'Nothing to export for this period.'; return; }
  290. const head = ['Date','Start time','Agent','Ticket','Time spent'];
  291. const csvCell = (v) => '"' + String(v).replace(/"/g, '""') + '"';
  292. const csv = '\ufeff' + [head, ...rows.map(r => [r.date, r.start, r.agent, r.ticket, r.spent])]
  293. .map(line => line.map(csvCell).join(',')).join('\r\n');
  294. const a = document.createElement('a');
  295. a.href = URL.createObjectURL(new Blob([csv], { type: 'text/csv;charset=utf-8' }));
  296. a.download = 'session-report.csv';
  297. a.click(); URL.revokeObjectURL(a.href);
  298. }
  299. function exportPdf() {
  300. const rows = reportData();
  301. if (!rows.length) { repSummary.textContent = 'Nothing to export for this period.'; return; }
  302. const period = (fFrom.value || 'start') + ' to ' + (fTo.value || 'today');
  303. const agentSel = fAgent.value || 'All agents';
  304. const w = window.open('', '_blank');
  305. w.document.write('<html><head><title>Session report</title><style>' +
  306. 'body{font-family:Segoe UI,Arial,sans-serif;color:#1f2430;margin:32px}' +
  307. 'h1{font-size:18px;color:#1F3B73;border-bottom:3px solid #FFC708;padding-bottom:6px}' +
  308. '.meta{color:#6b7280;font-size:12px;margin-bottom:14px}' +
  309. 'table{width:100%;border-collapse:collapse;font-size:12px}' +
  310. 'th{background:#1F3B73;color:#fff;text-align:left;padding:6px 8px}' +
  311. 'td{padding:6px 8px;border-bottom:1px solid #e6e9ef}' +
  312. '</style></head><body>' +
  313. '<h1>BizGaze Support — Session report</h1>' +
  314. '<div class="meta">Agent: ' + esc(agentSel) + ' · Period: ' + esc(period) + ' · Generated ' + new Date().toLocaleString() + '</div>' +
  315. '<table><tr><th>Date</th><th>Start time</th><th>Agent</th><th>Ticket</th><th>Time spent</th></tr>' +
  316. rows.map(r => '<tr><td>' + [r.date, r.start, esc(r.agent), esc(r.ticket), r.spent].join('</td><td>') + '</td></tr>').join('') +
  317. '</table><div class="meta" style="margin-top:12px">' + esc(repSummary.textContent) + '</div></body></html>');
  318. w.document.close();
  319. w.onload = () => { w.print(); };
  320. }
  321. function esc(s) { return String(s == null ? '' : s).replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c])); }
  322. // ---------- Boot ----------
  323. (async function () {
  324. try { const me = await api('/api/me', null, 'GET'); dashboard(me); }
  325. catch { authView(); }
  326. })();
  327. </script>
  328. </body>
  329. </html>