<div id="vt-chat">
<div id="vt-login">
<p><strong>WFOA Valve Tech Assistant</strong> (staff only)</p>
<input id="vt-pass" type="password" placeholder="Staff passcode" autocomplete="off">
<button id="vt-enter">Enter</button>
<p id="vt-login-err" style="color:#b00020;display:none">Wrong passcode. Please try again.</p>
</div>
<div id="vt-main" style="display:none">
<div id="vt-log"></div>
<form id="vt-form">
<textarea id="vt-input" rows="2" placeholder="Describe the valve and the problem..."></textarea>
<button type="submit" id="vt-send">Send</button>
</form>
<button id="vt-new" type="button">New conversation</button>
</div>
</div>
<style>
#vt-chat{max-width:820px;margin:0 auto;font-family:inherit;border:1px solid #ddd;border-radius:10px;padding:16px;background:#fff}
#vt-log{height:520px;overflow-y:auto;padding:8px;background:#f7f8fa;border-radius:8px;margin-bottom:10px}
.vt-msg{margin:8px 0;padding:10px 12px;border-radius:8px;line-height:1.45;white-space:normal}
.vt-user{background:#00A2D6;color:#fff;margin-left:20%}
.vt-bot{background:#fff;border:1px solid #e3e3e3;margin-right:10%}
.vt-bot ol,.vt-bot ul{padding-left:22px;margin:6px 0}
#vt-form{display:flex;gap:8px}
#vt-input{flex:1;padding:8px;border:1px solid #ccc;border-radius:6px;font:inherit}
#vt-chat button{padding:8px 14px;border:0;border-radius:6px;background:#F96200;color:#fff;cursor:pointer}
#vt-chat button:disabled{opacity:.6;cursor:wait}
#vt-new{margin-top:8px;background:#777 !important}
#vt-pass{padding:8px;border:1px solid #ccc;border-radius:6px;margin-right:6px}
</style>
<script>
(function(){
var URL = 'https://wfoa-intelligence-bot.app.n8n.cloud/webhook/valve-tech-chat';
var $ = function(id){ return document.getElementById(id); };
function newSession(){ var s = 'vt-' + Date.now().toString(36) + Math.random().toString(36).slice(2,10); sessionStorage.setItem('vtSession', s); return s; }
var session = sessionStorage.getItem('vtSession') || newSession();
function esc(t){ return t.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); }
function md(t){
var lines = esc(t).split('\n'), out = '', list = null;
lines.forEach(function(l){
var ol = l.match(/^\s*\d+\.\s+(.*)/), ul = l.match(/^\s*[-*]\s+(.*)/);
var type = ol ? 'ol' : (ul ? 'ul' : null);
if (list && list !== type){ out += '</' + list + '>'; list = null; }
if (type){ if (!list){ out += '<' + type + '>'; list = type; } out += '<li>' + (ol||ul)[1] + '</li>'; }
else if (/^#{1,4}\s+/.test(l)){ out += '<p><strong>' + l.replace(/^#{1,4}\s+/,'') + '</strong></p>'; }
else if (l.trim()){ out += '<p>' + l + '</p>'; }
});
if (list) out += '</' + list + '>';
return out.replace(/\*\*(.+?)\*\*/g,'<strong>$1</strong>');
}
function add(text, who){
var d = document.createElement('div');
d.className = 'vt-msg ' + (who === 'user' ? 'vt-user' : 'vt-bot');
d.innerHTML = who === 'user' ? esc(text).replace(/\n/g,'<br>') : md(text);
$('vt-log').appendChild(d); $('vt-log').scrollTop = 1e9; return d;
}
function showChat(){ $('vt-login').style.display='none'; $('vt-main').style.display='block'; }
function showLogin(err){ sessionStorage.removeItem('vtPass'); $('vt-main').style.display='none'; $('vt-login').style.display='block'; $('vt-login-err').style.display = err ? 'block' : 'none'; }
function send(message){
return fetch(URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-Staff-Passcode': sessionStorage.getItem('vtPass') || '' },
body: JSON.stringify({ sessionId: session, message: message })
});
}
$('vt-enter').onclick = function(){
var p = $('vt-pass').value.trim(); if (!p) return;
sessionStorage.setItem('vtPass', p); $('vt-pass').value = '';
showChat();
if (!$('vt-log').children.length) add('Hi! Tell me the valve (manufacturer and model) and what the customer is seeing, and I will walk you through it.', 'bot');
};
$('vt-pass').addEventListener('keydown', function(e){ if (e.key === 'Enter') $('vt-enter').click(); });
$('vt-form').onsubmit = function(e){
e.preventDefault();
var msg = $('vt-input').value.trim(); if (!msg) return;
$('vt-input').value = ''; add(msg, 'user');
var wait = add('Thinking... (this can take 20-40 seconds)', 'bot');
$('vt-send').disabled = true;
send(msg).then(function(r){
if (r.status === 401 || r.status === 403){ wait.remove(); showLogin(true); throw null; }
return r.json();
}).then(function(data){
wait.innerHTML = md((data && data.reply) || 'Sorry, no answer came back. Please try again.');
}).catch(function(err){
if (err !== null) wait.innerHTML = md('Sorry, the assistant could not be reached. Please try again.');
}).then(function(){ $('vt-send').disabled = false; $('vt-input').focus(); });
};
$('vt-input').addEventListener('keydown', function(e){ if (e.key === 'Enter' && !e.shiftKey){ e.preventDefault(); $('vt-form').requestSubmit(); } });
$('vt-new').onclick = function(){ session = newSession(); $('vt-log').innerHTML = ''; add('New conversation started. What valve are you working on?', 'bot'); };
if (sessionStorage.getItem('vtPass')) { showChat(); add('Welcome back. What valve are you working on?', 'bot'); }
})();
</script>