';
return;
}
// Sort by updatedAt desc
ids.sort((a, b) => sessions[b].updatedAt - sessions[a].updatedAt);
sessionList.innerHTML = ids.map(id => {
const s = sessions[id];
const tmpl = TEMPLATES[s.template] || TEMPLATES.surat_lamaran;
const isActive = id === activeId;
const time = new Date(s.updatedAt).toLocaleString('id-ID', { day:'numeric', month:'short', hour:'2-digit', minute:'2-digit' });
return `
${s.name || tmpl.label}
${tmpl.label} ยท ${time}
`;
}).join('');
// Click session = buka (populate) sesi yang dipilih
sessionList.querySelectorAll('.side-link').forEach(el => {
el.addEventListener('click', () => {
const id = el.dataset.id;
if (!sessions[id]) return;
switchSession(id);
});
});
}
// โโ Switch to session โโ
function switchSession(id) {
if (!sessions[id]) return;
activeId = id;
const s = sessions[id];
templateType.value = s.template || 'surat_lamaran';
renderForm(s.template || 'surat_lamaran');
renderPreview();
renderSidebar();
emptyState.classList.add('hidden-el');
editorArea.classList.remove('hidden-el');
}
// โโ Create new session โโ
function createNew() {
const id = createSession('surat_lamaran');
saveSessions();
switchSession(id);
// Scroll form into view
editorArea.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
// โโ Theme โโ
const isDark = () => document.documentElement.classList.contains('dark');
function setTheme(t) {
if (t === 'dark') document.documentElement.classList.add('dark');
else document.documentElement.classList.remove('dark');
localStorage.setItem('theme', t);
$('themeIcon').textContent = isDark() ? '๐' : 'โ๏ธ';
if (window.__i18n) window.__i18n.apply();
}
$('themeToggle').addEventListener('click', () => setTheme(isDark() ? 'light' : 'dark'));
setTheme(localStorage.getItem('theme') || (isDark() ? 'dark' : 'light'));
// โโ Rating โโ
fetch('https://app.dexcort.net/api/feedback.php', {
method: 'POST',
headers: {'Content-Type':'application/json'},
body: JSON.stringify({action:'stats', domain:window.location.hostname})
}).then(r=>r.json()).then(d=>{
if(d.avg>0){$('raiaAvg').textContent=d.avg;$('raiaTotal').textContent=d.total;$('raiaRating').classList.remove('hidden-el');}
}).catch(()=>{});
// โโ Event bindings โโ
$('createBtn').addEventListener('click', createNew);
$('startCreateBtn').addEventListener('click', createNew);
$('duplicateBtn').addEventListener('click', () => {
if (!activeId) return;
const newId = duplicateSession(activeId);
if (newId) { saveSessions(); switchSession(newId); }
});
$('deleteBtn').addEventListener('click', () => {
if (!activeId || !sessions[activeId]) return;
if (!confirm(window.__('confirm_delete') + ' "' + (sessions[activeId].name || 'Template') + '"? ' + window.__('confirm_delete_body'))) return;
deleteSession(activeId);
const ids = Object.keys(sessions);
activeId = null;
if (ids.length > 0) {
switchSession(ids[0]);
} else {
editorArea.classList.add('hidden-el');
emptyState.classList.remove('hidden-el');
renderSidebar();
}
});
templateType.addEventListener('change', () => {
if (!activeId) return;
sessions[activeId].template = templateType.value;
renderForm(templateType.value);
renderPreview();
scheduleSave();
});
previewBtn.addEventListener('click', () => {
const prev = $('previewContent');
prev.scrollIntoView({ behavior: 'smooth', block: 'start' });
});
$('printBtn').addEventListener('click', () => {
const content = previewContent.textContent;
const win = window.open('', '_blank');
if (!win) { alert(window.__('popup_alert')); return; }
const isDarkMode = isDark();
win.document.write(`
${window.__('print_title')}
${content.replace(/\n/g, '
')}
`);
win.document.close();
setTimeout(() => { win.focus(); win.print(); }, 300);
});
// โโ Init โโ
loadSessions();
const ids = Object.keys(sessions);
if (ids.length > 0) {
switchSession(ids[0]);
}
renderSidebar();
console.log('Template Generator ready โ
');