diff --git a/static/sport2.html b/static/sport2.html index 35a7013..607e86a 100644 --- a/static/sport2.html +++ b/static/sport2.html @@ -357,34 +357,78 @@ async function apiPost(path, body){ } } +// Cache the latest preview so /apply can pass back the same sources +window._enrichPreviews = window._enrichPreviews || {}; + async function enrichEntity(kind, id){ const targetId = 'enrich-out-'+kind+'-'+id; const target = document.getElementById(targetId); if(target) target.innerHTML = '
⏳ Obogaćivanje u tijeku — pretraživanje izvora…
'; const r = await apiPost('/v2/enrich/'+kind+'/'+id); if(!r){ if(target) target.innerHTML = '
Greška pri obogaćivanju
'; return; } + window._enrichPreviews[kind+':'+id] = r; const cov = r.coverage||0; const covCls = cov>=70?'high':(cov>=40?'mid':'low'); + const proposed = r.proposed || {}; + const current = r.current || {}; + const propKeys = Object.keys(proposed); + const lastEnr = r.last_enriched_at + ? `✓ Obogaćeno ${esc(String(r.last_enriched_at).slice(0,10))}` + : ''; + + let diffHtml = ''; + if(propKeys.length){ + const rows = propKeys.map(k => { + const cv = current[k]; const pv = proposed[k]; + const cvHtml = cv + ? ''+esc(String(cv).slice(0,200))+'' + : 'prazno'; + const pvHtml = ''+esc(String(pv).slice(0,400))+''; + return ` + + ${cvHtml} + ${pvHtml} + `; + }).join(''); + diffHtml = ` +
+
📋 Predložene izmjene (uncheck za preskočiti)
+ + + ${rows} +
PoljeTrenutnoPredloženo
+
+ + + +
+
`; + } else { + diffHtml = '
Nema novih predloženih dopuna iz vanjskih izvora.
'; + } + + const sourcesHtml = (r.sources||[]).map(s => ` +
+
${esc(s.source||'')}
+ ${s.title ? '
'+esc(s.title)+'
' : ''} + ${s.extract ? '
'+esc(String(s.extract).slice(0,300))+'…
' : ''} + ${s.url ? '
↗ '+esc(String(s.url).slice(0,90))+'
' : ''} +
`).join(''); + const html = ` -
+
🟢 OBOGAĆENO Coverage ${cov}% ${r.filled_fields}/${r.total_fields} polja popunjeno + ${lastEnr}
- ${r.live_snippet && r.live_snippet.title ? ` -
-
📡 Live snippet
-
${esc(r.live_snippet.title)}
- ${r.live_snippet.description ? '
'+esc(r.live_snippet.description)+'
' : ''} -
↗ ${esc(r.live_snippet.url.slice(0,80))}
-
- ` : ''} - ${r.missing_fields && r.missing_fields.length ? ` -
-
Nedostaje:
-
${r.missing_fields.map(f=>''+esc(f)+'').join('')}
-
- ` : ''} + ${diffHtml} + ${sourcesHtml ? `
+
🔗 Izvori
+ ${sourcesHtml} +
` : ''}
🔍 Istraži dalje:
@@ -395,6 +439,54 @@ async function enrichEntity(kind, id){ if(target) target.innerHTML = html; } +function enrichSelectAll(kind, id, on){ + const tbody = document.getElementById('enrich-diff-'+kind+'-'+id); + if(!tbody) return; + tbody.querySelectorAll('input[type=checkbox]').forEach(cb => { cb.checked = !!on; }); +} + +async function enrichApply(kind, id){ + const target = document.getElementById('enrich-out-'+kind+'-'+id); + const tbody = document.getElementById('enrich-diff-'+kind+'-'+id); + const preview = (window._enrichPreviews||{})[kind+':'+id]; + if(!preview){ alert('Prvo pokreni "▶ Pokreni"'); return; } + const proposed = preview.proposed || {}; + const fields = {}; + if(tbody){ + tbody.querySelectorAll('input[type=checkbox]:checked').forEach(cb => { + const f = cb.getAttribute('data-field'); + if(f && proposed[f] !== undefined) fields[f] = proposed[f]; + }); + } else { + Object.assign(fields, proposed); + } + if(!Object.keys(fields).length){ alert('Označi barem jedno polje za primjenu.'); return; } + if(target) target.innerHTML = '
⏳ Spremam u bazu…
'; + try{ + const r = await fetch(API+'/v2/enrich/'+kind+'/'+id+'/apply', { + method:'POST', + headers:{'Content-Type':'application/json'}, + body: JSON.stringify({fields, sources: preview.sources || []}), + }); + const data = await r.json(); + if(!r.ok){ throw new Error(data.detail || ('HTTP '+r.status)); } + // Refresh the entire detail panel so the new values render + if(kind === 'klub' && typeof openKlub === 'function') await openKlub(id); + else if(kind === 'savez' && typeof openSavez === 'function') await openSavez(id); + else if(kind === 'sportas' && typeof openSportas === 'function') await openSportas(id); + setTimeout(() => enrichEntity(kind, id), 350); + const cnt = Object.keys(data.applied||{}).length; + const t = document.createElement('div'); + t.style.cssText = 'position:fixed;bottom:20px;right:20px;background:var(--ok,#1ec773);color:#0b1a16;padding:10px 16px;border-radius:6px;font-weight:700;z-index:9999;box-shadow:0 4px 16px rgba(0,0,0,.4)'; + t.textContent = '✓ Spremljeno '+cnt+' polja u bazu'; + document.body.appendChild(t); + setTimeout(()=>t.remove(), 3500); + }catch(e){ + console.error(e); + if(target) target.innerHTML = '
Greška pri spremanju: '+esc(e.message||String(e))+'
'; + } +} + function enrichBlock(kind, id){ return `