/** * Map Refinement (Place Audit) logic */ const refinement = { state: { candidates: [] }, init: async () => { console.log('📍 Initializing Map Refinement Logic...'); await refinement.fetchCandidates(); }, fetchCandidates: async () => { const headers = auth.getAuthHeader(); try { const res = await fetch('/api/map-refinement/candidates?status=PENDING', { headers }); if (res.ok) { refinement.state.candidates = await res.json(); refinement.renderTable(); } } catch (error) { console.error('Failed to fetch candidates', error); } }, renderTable: () => { const tbody = document.getElementById('refinement-table-body'); if (!tbody) return; if (refinement.state.candidates.length === 0) { tbody.innerHTML = `

No pending location suggestions.

`; lucide.createIcons(); return; } tbody.innerHTML = refinement.state.candidates.map(c => `
${c.name_ar || c.name}
${c.country}
${c.category || 'General'} ${parseFloat(c.latitude).toFixed(5)}, ${parseFloat(c.longitude).toFixed(5)} ${c.submittedBy || 'System User'} `).join(''); lucide.createIcons(); }, approve: async (id) => { if (!confirm('Are you sure you want to approve this location and add it to the production map?')) return; const headers = auth.getAuthHeader(); try { const res = await fetch(`/api/map-refinement/candidates/${id}/approve`, { method: 'PATCH', headers }); if (res.ok) { await refinement.fetchCandidates(); } else { alert('Approval failed.'); } } catch (error) { console.error(error); } }, reject: async (id) => { const reason = prompt('Please enter a reason for rejection:'); if (reason === null) return; const headers = auth.getAuthHeader(); try { const res = await fetch(`/api/map-refinement/candidates/${id}/reject`, { method: 'PATCH', headers: { ...headers, 'Content-Type': 'application/json' }, body: JSON.stringify({ reason }) }); if (res.ok) { await refinement.fetchCandidates(); } else { alert('Rejection failed.'); } } catch (error) { console.error(error); } } };