// Phase 6.2 — Summaries UI // Handles save-summary button and summary history panel. function initSummaries() { _bindSaveSummaryBtn(); _bindHistoryTab(); } /* ── Save button (appears after generating a summary) ── */ function _bindSaveSummaryBtn() { const btn = document.getElementById('save-summary-btn'); if (!btn) return; btn.addEventListener('click', _handleSaveSummary); } async function _handleSaveSummary() { const summaryEl = document.getElementById('summary-text'); const editorText = (typeof getEditorText === 'function') ? getEditorText() : ''; const summaryText = summaryEl ? (summaryEl.innerText || summaryEl.textContent || '').trim() : ''; if (!summaryText) { if (typeof showDocToast === 'function') showDocToast('لا يوجد ملخص لحفظه', 'info'); return; } const isAuthenticated = window.__bayanAuth && window.__bayanAuth.userId && !window.__bayanAuth.isOfflineMode; if (!isAuthenticated) { if (typeof showDocToast === 'function') showDocToast('سجّل دخولك لحفظ الملخصات', 'info'); return; } const result = await saveSummary(editorText, summaryText); if (result) { if (typeof showDocToast === 'function') showDocToast('تم حفظ الملخص ✓', 'success'); await _loadAndRenderHistory(); } else { if (typeof showDocToast === 'function') showDocToast('تعذّر حفظ الملخص', 'error'); } } /* ── History tab toggle ── */ function _bindHistoryTab() { const toggleBtn = document.getElementById('summary-history-toggle'); const panel = document.getElementById('summary-history-panel'); if (!toggleBtn || !panel) return; toggleBtn.addEventListener('click', async () => { const isOpen = panel.classList.toggle('is-open'); toggleBtn.setAttribute('aria-expanded', isOpen ? 'true' : 'false'); if (isOpen) await _loadAndRenderHistory(); }); } /* ── Load & render history ── */ async function _loadAndRenderHistory() { const listEl = document.getElementById('summary-history-list'); if (!listEl) return; const isAuthenticated = window.__bayanAuth && window.__bayanAuth.userId && !window.__bayanAuth.isOfflineMode; if (!isAuthenticated) { listEl.innerHTML = `
سجّل دخولك لعرض سجل الملخصات
`; return; } listEl.innerHTML = `جاري التحميل...
`; const items = await loadSummaries(); if (!items.length) { listEl.innerHTML = `لم يتم حفظ أي ملخصات بعد
`; return; } listEl.innerHTML = items.map(item => _buildHistoryItemHTML(item)).join(''); _bindHistoryItemEvents(listEl); } function _buildHistoryItemHTML(item) { const preview = (item.summary_text || '').slice(0, 80) + (item.summary_text.length > 80 ? '...' : ''); const date = new Date(item.created_at).toLocaleDateString('ar-EG', { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }); return `${_escapeSummaryHtml(preview)}
${date}