Hattan Fleet Registry — QR / Barcode Record Tool
`);
w.document.close();
w.focus();
setTimeout(()=>w.print(),300);
}
function handleScannedText(text){
lastScan = text || "";
$("hf-scan-output").textContent = lastScan || "Nothing scanned.";
$("hf-copy-scan").disabled = !lastScan;
$("hf-open-result").disabled = true;
let record = null;
try{
const url = new URL(lastScan);
const encoded = url.searchParams.get("record");
if(encoded) record = decodeRecord(encoded);
}catch(e){}
if(record){
currentRecord = record;
$("hf-open-result").disabled = false;
renderRecord($("hf-scan-record"), record);
$("hf-scan-record").scrollIntoView({behavior:"smooth", block:"start"});
}else{
$("hf-scan-record").classList.add("hf-hidden");
}
}
async function startScanner(){
if(scanner) return;
scanner = new Html5Qrcode("hf-reader");
try{
await scanner.start(
{ facingMode:"environment" },
{ fps:10, qrbox:{width:250,height:250} },
decodedText => handleScannedText(decodedText),
() => {}
);
}catch(e){
$("hf-scan-output").textContent = "Camera could not start. Check camera permission or use Scan Image.";
scanner = null;
}
}
async function stopScanner(){
if(!scanner) return;
try{ await scanner.stop(); }catch(e){}
try{ scanner.clear(); }catch(e){}
scanner = null;
}
// Tabs
document.querySelectorAll(".hf-tab").forEach(btn => {
btn.addEventListener("click", ()=>showTab(btn.dataset.tab));
});
$("hf-generate").addEventListener("click", ()=>generate(collectForm(), false));
$("hf-save").addEventListener("click", ()=>generate(collectForm(), true));
$("hf-form").addEventListener("reset", ()=>{
setTimeout(()=>{
currentRecord = null;
currentURL = "";
$("hf-qr").innerHTML = "";
$("hf-url-wrap").classList.add("hf-hidden");
$("hf-created-result").classList.add("hf-hidden");
$("hf-download-qr").disabled = true;
$("hf-copy-url").disabled = true;
$("hf-print").disabled = true;
$("hf-qr-status").textContent = "No QR code generated yet.";
},0);
});
$("hf-download-qr").addEventListener("click", downloadQR);
$("hf-copy-url").addEventListener("click", async ()=>{
if(await copyText(currentURL)) alert("Record URL copied.");
});
$("hf-print").addEventListener("click", ()=>printRecord(currentRecord));
$("hf-start-scan").addEventListener("click", startScanner);
$("hf-stop-scan").addEventListener("click", stopScanner);
$("hf-copy-scan").addEventListener("click", async ()=>{
if(lastScan && await copyText(lastScan)) alert("Scanned result copied.");
});
$("hf-open-result").addEventListener("click", ()=>{
if(currentRecord) renderRecord($("hf-scan-record"), currentRecord);
$("hf-scan-record").scrollIntoView({behavior:"smooth"});
});
$("hf-file-scan").addEventListener("change", async function(){
const file = this.files && this.files[0];
if(!file) return;
const temp = new Html5Qrcode("hf-reader");
try{
const result = await temp.scanFile(file, true);
handleScannedText(result);
}catch(e){
$("hf-scan-output").textContent = "No readable QR/barcode was found in this image.";
}finally{
try{ temp.clear(); }catch(e){}
this.value = "";
}
});
$("hf-history").addEventListener("click", e=>{
const btn = e.target.closest("button[data-action]");
if(!btn) return;
const records = getRecords();
const index = Number(btn.dataset.index);
const record = records[index];
if(!record) return;
if(btn.dataset.action === "load"){
fillForm(record);
generate(record, false);
showTab("create");
window.scrollTo({top:0, behavior:"smooth"});
}
if(btn.dataset.action === "copy"){
copyText(buildRecordURL(record)).then(ok=>{if(ok) alert("Record URL copied.");});
}
if(btn.dataset.action === "delete"){
records.splice(index,1);
localStorage.setItem(STORAGE_KEY, JSON.stringify(records));
renderHistory();
}
});
$("hf-delete-all").addEventListener("click", ()=>{
if(confirm("Delete all saved records from this browser?")){
localStorage.removeItem(STORAGE_KEY);
renderHistory();
}
});
// IMPORTANT: If ?record=... is present, this is the shareable record view.
function openURLRecord(){
const params = new URLSearchParams(window.location.search);
const encoded = params.get("record");
if(!encoded) return;
const record = decodeRecord(encoded);
if(!record) return;
currentRecord = record;
renderRecord($("hf-created-result"), record);
$("hf-created-result").scrollIntoView({behavior:"smooth", block:"start"});
$("hf-created-result").classList.remove("hf-hidden");
document.querySelector(".hf-panel.active")?.classList.remove("active");
$("hf-create").classList.add("active");
}
renderHistory();
openURLRecord();
})();