Memuat...

` : ''} `; document.getElementById('viewContent').innerHTML = html; } async function showCategory(category) { currentCategory = category; currentView = 'category'; const pasalList = allPasal.filter(p => p.category === category); let html = `

${category}

`; if (currentUser && currentUser.role === 'admin') { html += ``; } html += `
`; if (pasalList.length === 0) { html += '

Belum ada pasal.

'; } else { html += '
'; for (let law of pasalList) { const snippet = law.content.substring(0, 120) + '...'; html += `
${law.number} ${law.title}
${notes[law.id] ? '' : ''}

${snippet}

${currentUser && currentUser.role === 'admin' ? ` ` : ''}
`; } html += '
'; } document.getElementById('viewContent').innerHTML = html; } async function showMK(mkName) { currentMK = mkName; currentView = 'mk'; const items = allMK.filter(m => m.mk_name === mkName); let html = `

${mkName}

`; if (currentUser && currentUser.role === 'admin') { html += ``; } html += `
`; if (items.length === 0) { html += '

Belum ada materi.

'; } else { for (let item of items) { html += `
${item.title}
${currentUser && currentUser.role === 'admin' ? `
` : ''}
${item.content.replace(/\n/g, '
')}
`; } } document.getElementById('viewContent').innerHTML = html; } async function showSkripsiList() { currentView = 'skripsiList'; let html = `

Koleksi Skripsi

`; html += `
`; if (allSkripsi.length === 0) { html += '

Belum ada skripsi.

'; } else { for (let s of allSkripsi) { html += `
${s.judul}

Penulis: ${s.penulis}

${s.abstrak.substring(0, 200)}...
`; } } document.getElementById('viewContent').innerHTML = html; } async function showPasalDetail(id) { const law = allPasal.find(p => p.id == id); if (!law) return; const isBookmarked = bookmarks.includes(id); const note = notes[id] || ''; const modalBody = document.getElementById('pasalModalBody'); modalBody.innerHTML = `
${law.category} ${law.number}

${law.title}

${law.content.replace(/\n/g, '
')}

${note ? `
Catatan: ${note}
` : ''} `; new bootstrap.Modal(document.getElementById('pasalModal')).show(); } async function showSkripsiDetail(id) { const s = allSkripsi.find(s => s.id == id); if (!s) return; const isiFormatted = s.isi.split('\n').filter(p => p.trim()).map(p => `

${p}

`).join(''); const modalBody = document.getElementById('pasalModalBody'); modalBody.innerHTML = `

${s.judul}

${s.penulis}

Abstrak

${s.abstrak}

${isiFormatted}
`; new bootstrap.Modal(document.getElementById('pasalModal')).show(); } async function showBookmarks() { const bookmarked = allPasal.filter(p => bookmarks.includes(p.id)); let html = `

Bookmark Saya

`; if (bookmarked.length === 0) { html += '

Belum ada bookmark.

'; } else { html += '
'; for (let law of bookmarked) { html += `
${law.number} ${law.title}

${law.content.substring(0, 100)}...

`; } html += '
'; } document.getElementById('viewContent').innerHTML = html; currentView = 'bookmarks'; } async function showNotes() { const noted = allPasal.filter(p => notes[p.id]); let html = `

Catatan Saya

`; if (noted.length === 0) { html += '

Belum ada catatan.

'; } else { for (let law of noted) { html += `
${law.number} ${law.title}
📝 ${notes[law.id]}
`; } } document.getElementById('viewContent').innerHTML = html; currentView = 'notes'; } // ==================== ADMIN DASHBOARD ==================== async function showAdminDashboard() { currentView = 'admin'; const stats = await fetchData('stats'); const users = await fetchData('get_users'); let html = `

Admin Dashboard

Total Pasal

${stats.total_pasal}

Materi Kuliah

${stats.total_mk}

Skripsi

${stats.total_skripsi}

Pengguna

${stats.total_users}

Daftar Pengguna
${users.map(u => ` `).join('')}
IDUsernameEmailRoleTanggal DaftarAksi
${u.id} ${u.username} ${u.email} ${u.role} ${new Date(u.created_at).toLocaleDateString()}
`; document.getElementById('viewContent').innerHTML = html; } async function deleteUser(userId) { if (confirm('Hapus pengguna ini? Semua data terkait akan dihapus.')) { await apiCall('delete_user', { id: userId }); showAdminDashboard(); } } // ==================== BOOKMARK & NOTES ==================== async function toggleBookmark(pasalId) { const res = await apiCall('toggle_bookmark', { pasal_id: pasalId }); if (res.bookmarked) { bookmarks.push(pasalId); } else { bookmarks = bookmarks.filter(id => id != pasalId); } refreshCurrentView(); } async function editNote(pasalId) { const existing = notes[pasalId] || ''; const newNote = prompt('Tulis catatan untuk pasal ini:', existing); if (newNote !== null) { if (newNote.trim() === '') { delete notes[pasalId]; } else { notes[pasalId] = newNote.trim(); } await apiCall('save_note', { pasal_id: pasalId, note: notes[pasalId] || '' }); refreshCurrentView(); } } // ==================== CRUD OPERATIONS ==================== function showAddPasalForm(category) { document.getElementById('pasalId').value = ''; document.getElementById('pasalCategory').value = category; document.getElementById('pasalNumber').value = ''; document.getElementById('pasalTitle').value = ''; document.getElementById('pasalContent').value = ''; document.getElementById('pasalFormTitle').innerText = 'Tambah Pasal'; new bootstrap.Modal(document.getElementById('pasalFormModal')).show(); } async function editPasal(id) { const law = allPasal.find(p => p.id == id); if (law) { document.getElementById('pasalId').value = law.id; document.getElementById('pasalCategory').value = law.category; document.getElementById('pasalNumber').value = law.number; document.getElementById('pasalTitle').value = law.title; document.getElementById('pasalContent').value = law.content; document.getElementById('pasalFormTitle').innerText = 'Edit Pasal'; new bootstrap.Modal(document.getElementById('pasalFormModal')).show(); } } async function deletePasal(id) { if (confirm('Hapus pasal ini?')) { await apiCall('delete_pasal', { id }); await refreshAllData(); refreshCurrentView(); } } document.getElementById('savePasalBtn').onclick = async () => { const id = document.getElementById('pasalId').value; const category = document.getElementById('pasalCategory').value; const number = document.getElementById('pasalNumber').value; const title = document.getElementById('pasalTitle').value; const content = document.getElementById('pasalContent').value; const api = id ? 'edit_pasal' : 'add_pasal'; await apiCall(api, { id, category, number, title, content }); bootstrap.Modal.getInstance(document.getElementById('pasalFormModal')).hide(); await refreshAllData(); refreshCurrentView(); }; function showAddMKForm(mkName) { document.getElementById('mkId').value = ''; document.getElementById('mkName').value = mkName; document.getElementById('mkTitle').value = ''; document.getElementById('mkContent').value = ''; document.getElementById('mkFormTitle').innerText = 'Tambah Materi'; new bootstrap.Modal(document.getElementById('mkFormModal')).show(); } async function editMK(id) { const item = allMK.find(m => m.id == id); if (item) { document.getElementById('mkId').value = item.id; document.getElementById('mkName').value = item.mk_name; document.getElementById('mkTitle').value = item.title; document.getElementById('mkContent').value = item.content; document.getElementById('mkFormTitle').innerText = 'Edit Materi'; new bootstrap.Modal(document.getElementById('mkFormModal')).show(); } } async function deleteMK(id) { if (confirm('Hapus materi ini?')) { await apiCall('delete_mk', { id }); await refreshAllData(); refreshCurrentView(); } } document.getElementById('saveMKBtn').onclick = async () => { const id = document.getElementById('mkId').value; const mk_name = document.getElementById('mkName').value; const title = document.getElementById('mkTitle').value; const content = document.getElementById('mkContent').value; const api = id ? 'edit_mk' : 'add_mk'; await apiCall(api, { id, mk_name, title, content }); bootstrap.Modal.getInstance(document.getElementById('mkFormModal')).hide(); await refreshAllData(); refreshCurrentView(); }; function showAddSkripsiForm() { document.getElementById('skripsiId').value = ''; document.getElementById('skripsiJudul').value = ''; document.getElementById('skripsiPenulis').value = ''; document.getElementById('skripsiAbstrak').value = ''; document.getElementById('skripsiIsi').value = ''; document.getElementById('skripsiFormTitle').innerText = 'Tulis Skripsi Baru'; new bootstrap.Modal(document.getElementById('skripsiFormModal')).show(); } async function editSkripsi(id) { const s = allSkripsi.find(s => s.id == id); if (s) { document.getElementById('skripsiId').value = s.id; document.getElementById('skripsiJudul').value = s.judul; document.getElementById('skripsiPenulis').value = s.penulis; document.getElementById('skripsiAbstrak').value = s.abstrak; document.getElementById('skripsiIsi').value = s.isi; document.getElementById('skripsiFormTitle').innerText = 'Edit Skripsi'; new bootstrap.Modal(document.getElementById('skripsiFormModal')).show(); } } async function deleteSkripsi(id) { if (confirm('Hapus skripsi ini?')) { await apiCall('delete_skripsi', { id }); await refreshAllData(); refreshCurrentView(); } } document.getElementById('saveSkripsiBtn').onclick = async () => { const id = document.getElementById('skripsiId').value; const judul = document.getElementById('skripsiJudul').value; const penulis = document.getElementById('skripsiPenulis').value; const abstrak = document.getElementById('skripsiAbstrak').value; const isi = document.getElementById('skripsiIsi').value; const api = id ? 'edit_skripsi' : 'add_skripsi'; await apiCall(api, { id, judul, penulis, abstrak, isi }); bootstrap.Modal.getInstance(document.getElementById('skripsiFormModal')).hide(); await refreshAllData(); refreshCurrentView(); }; // ==================== PENCARIAN GLOBAL ==================== async function searchGlobal() { const query = document.getElementById('globalSearch').value.toLowerCase().trim(); if (!query) { showDashboard(); return; } const pasalResults = allPasal.filter(p => p.content.toLowerCase().includes(query) || p.title.toLowerCase().includes(query) || p.number.toLowerCase().includes(query) || p.category.toLowerCase().includes(query) ); const mkResults = allMK.filter(m => m.content.toLowerCase().includes(query) || m.title.toLowerCase().includes(query) || m.mk_name.toLowerCase().includes(query) ); const skripsiResults = allSkripsi.filter(s => s.judul.toLowerCase().includes(query) || s.abstrak.toLowerCase().includes(query) || s.penulis.toLowerCase().includes(query) ); let html = `

Hasil pencarian: "${query}"

`; if (pasalResults.length > 0) { html += `
Pasal (${pasalResults.length})
`; for (let law of pasalResults) { html += `
${law.number} ${law.title}

${law.content.substring(0, 100)}...

`; } html += '
'; } if (mkResults.length > 0) { html += `
Materi Kuliah (${mkResults.length})
`; for (let m of mkResults) { html += `
${m.title}

${m.content.substring(0, 150)}...

`; } } if (skripsiResults.length > 0) { html += `
Skripsi (${skripsiResults.length})
`; for (let s of skripsiResults) { html += `
${s.judul}

Penulis: ${s.penulis}

`; } } if (pasalResults.length === 0 && mkResults.length === 0 && skripsiResults.length === 0) { html += '

Tidak ditemukan hasil.

'; } document.getElementById('viewContent').innerHTML = html; currentView = 'search'; } // ==================== REFRESH VIEW ==================== async function refreshCurrentView() { await refreshAllData(); if (currentView === 'dashboard') showDashboard(); else if (currentView === 'bookmarks') showBookmarks(); else if (currentView === 'notes') showNotes(); else if (currentView === 'category' && currentCategory) showCategory(currentCategory); else if (currentView === 'mk' && currentMK) showMK(currentMK); else if (currentView === 'skripsiList') showSkripsiList(); else if (currentView === 'admin') showAdminDashboard(); else showDashboard(); } // ==================== EVENT LISTENERS ==================== document.getElementById('dashboardLink').onclick = (e) => { e.preventDefault(); showDashboard(); }; document.getElementById('dashboardNav').onclick = (e) => { e.preventDefault(); showDashboard(); }; document.getElementById('skripsiNav').onclick = (e) => { e.preventDefault(); showSkripsiList(); }; document.getElementById('bookmarksNav').onclick = (e) => { e.preventDefault(); showBookmarks(); }; document.getElementById('notesNav').onclick = (e) => { e.preventDefault(); showNotes(); }; if (document.getElementById('adminDashboardNav')) document.getElementById('adminDashboardNav').onclick = (e) => { e.preventDefault(); showAdminDashboard(); }; if (document.getElementById('manageUsersNav')) document.getElementById('manageUsersNav').onclick = (e) => { e.preventDefault(); showAdminDashboard(); }; document.querySelectorAll('[data-category]').forEach(el => { el.onclick = (e) => { e.preventDefault(); showCategory(el.dataset.category); }; }); document.querySelectorAll('[data-mk]').forEach(el => { el.onclick = (e) => { e.preventDefault(); showMK(el.dataset.mk); }; }); // Inisialisasi checkSession();