No records yet. Save your first visitor above.
';
html += '
';
html += '';
headers.forEach(function (h) {
html += '| ' + escapeHtml(h) + ' | ';
});
html += '
';
records.forEach(function (r, i) {
html += '';
html += '| ' + (i + 1) + ' | ';
html += '' + escapeHtml(r.fullName) + ' | ';
html += '' + escapeHtml(r.phone) + ' | ';
html += '' + escapeHtml(r.company) + ' | ';
html += '' + escapeHtml(r.visitTime) + ' | ';
html += '' + escapeHtml(r.visitPurpose) + ' | ';
html += '' + escapeHtml(r.requirements) + ' | ';
html += '' + escapeHtml(r.followUp) + ' | ';
html += '' + escapeHtml(r.receptionist) + ' | ';
html += '' + escapeHtml(r.submittedAt) + ' | ';
html += '
';
});
html += '
';
var blob = new Blob(['\ufeff' + html], { type: 'application/vnd.ms-excel' });
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
var dateStr = new Date().toISOString().slice(0, 10);
a.download = 'Visitor_Records_' + dateStr + '.xls';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(function () { URL.revokeObjectURL(url); }, 100);
}
function escapeHtml(str) {
if (str === null || str === undefined) return '';
return String(str)
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"')
.replace(/\n/g, '
');
}
// ===== Format datetime for display =====
function formatDateTime(iso) {
if (!iso) return '-';
var d = new Date(iso);
if (isNaN(d.getTime())) return iso;
var mon = ('0' + (d.getMonth() + 1)).slice(-2);
var day = ('0' + d.getDate()).slice(-2);
var h = ('0' + d.getHours()).slice(-2);
var m = ('0' + d.getMinutes()).slice(-2);
return d.getFullYear() + '-' + mon + '-' + day + ' ' + h + ':' + m;
}
// ===== Render History =====
function renderHistory() {
var records = getRecords();
var total = records.length;
totalCountEl.textContent = total;
historyCount.textContent = total + ' total';
// today count
var today = new Date().toDateString();
var todayCount = records.filter(function (r) {
return new Date(r.submittedAt).toDateString() === today;
}).length;
todayCountEl.textContent = todayCount;
// show/hide download button
downloadAllBtn.style.display = total > 0 ? 'flex' : 'none';
if (total === 0) {
historyList.innerHTML = '
' +
'
' +
'
No records yet. Save your first visitor above.
';
return;
}
// show latest 20
var recent = records.slice(-20).reverse();
var html = '';
recent.forEach(function (r) {
html += '
';
html += '
' + escapeHtml(r.fullName) + '';
html += '' + formatDateTime(r.submittedAt) + '
';
html += '
' + escapeHtml(r.phone || '-');
if (r.company) html += ' · ' + escapeHtml(r.company);
html += '
';
if (r.visitPurpose) {
html += '
' + escapeHtml(r.visitPurpose) + '';
}
html += '
';
});
historyList.innerHTML = html;
}
// ===== Form Submit =====
form.addEventListener('submit', function (e) {
e.preventDefault();
var record = {
fullName: document.getElementById('fullName').value.trim(),
phone: document.getElementById('phone').value.trim(),
company: document.getElementById('company').value.trim(),
visitTime: document.getElementById('visitTime').value,
visitPurpose: document.getElementById('visitPurpose').value,
requirements: document.getElementById('requirements').value.trim(),
followUp: document.getElementById('followUp').value.trim(),
receptionist: document.getElementById('receptionist').value.trim(),
submittedAt: new Date().toISOString()
};
// basic validation
if (!record.fullName || !record.phone || !record.requirements) {
showToast('Please fill in all required fields', true);
return;
}
var records = getRecords();
records.push(record);
saveRecords(records);
// export to excel
exportToExcel(records);
showToast('Saved! Excel downloaded (' + records.length + ' records)');
// reset form but keep receptionist
var receptionist = record.receptionist;
form.reset();
document.getElementById('receptionist').value = receptionist;
// reset default time
var now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
document.getElementById('visitTime').value = now.toISOString().slice(0, 16);
renderHistory();
});
// ===== Clear Button =====
clearBtn.addEventListener('click', function () {
form.reset();
var now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
document.getElementById('visitTime').value = now.toISOString().slice(0, 16);
showToast('Form cleared');
});
// ===== Download All =====
downloadAllBtn.addEventListener('click', function () {
var records = getRecords();
if (records.length === 0) {
showToast('No records to download', true);
return;
}
exportToExcel(records);
showToast('Excel file downloaded');
});
// ===== Init =====
renderHistory();