block. Include: @context, @type, headline, datePublished (use today\'s date or eventDate), author (Organization, name "Zindiya"), publisher (Organization "Zindiya", logo as a sub-object with @type ImageObject), about (the event), locationCreated (Moseley, Birmingham). Do NOT include a mainEntityOfPage URL.',
'- The HTML must be valid and ready to paste straight into Shopify\'s blog article body.',
'',
'CRITICAL',
'- Return ONLY the JSON object. No ```json fences. No preamble. No closing remarks.',
'- Do not fabricate facts. If a field was not provided, simply omit that detail rather than making something up.'
].join('\n');
}
function showError(id, msg){
var e = el(id);
if (msg){ e.textContent = msg; e.classList.remove('zbt-hidden'); }
else { e.classList.add('zbt-hidden'); }
}
function copyToClipboard(text){
return navigator.clipboard.writeText(text).catch(function(){
var ta = document.createElement('textarea');
ta.value = text; document.body.appendChild(ta); ta.select();
try { document.execCommand('copy'); } catch(e){}
document.body.removeChild(ta);
});
}
function flashCopy(btn){
var orig = btn.textContent;
btn.textContent = 'Copied ✓';
setTimeout(function(){ btn.textContent = orig; }, 1500);
}
el('zbt-build').addEventListener('click', function(){
showError('zbt-error', null);
var f = getForm();
if (!f.client || !f.eventType || !f.dishes){
showError('zbt-error', 'At minimum: client, event type, and food served.');
return;
}
var prompt = buildPrompt(f);
el('zbt-prompt-output').value = prompt;
el('zbt-prompt-panel').style.display = 'block';
el('zbt-response-panel').style.display = 'block';
copyToClipboard(prompt).then(function(){
flashCopy(el('zbt-build'));
});
el('zbt-prompt-panel').scrollIntoView({ behavior: 'smooth', block: 'start' });
});
el('zbt-copy-prompt').addEventListener('click', function(){
copyToClipboard(el('zbt-prompt-output').value);
flashCopy(this);
});
el('zbt-parse').addEventListener('click', function(){
showError('zbt-parse-error', null);
var raw = el('zbt-response-input').value.trim();
if (!raw){ showError('zbt-parse-error', 'Paste the JSON response first.'); return; }
var clean = raw.replace(/^```json\s*|\s*```$/g, '').trim();
var firstBrace = clean.indexOf('{');
var lastBrace = clean.lastIndexOf('}');
if (firstBrace > 0 || lastBrace > -1) clean = clean.slice(firstBrace, lastBrace + 1);
var parsed;
try { parsed = JSON.parse(clean); }
catch(e){ showError('zbt-parse-error', 'Could not parse JSON: ' + e.message); return; }
var required = ['blogTitle','blogMetaDescription','blogSlug','blogTags','blogHtml','linkedinPost','instagramCaption','emailSnippet'];
var missing = required.filter(function(k){ return !(k in parsed); });
if (missing.length){ showError('zbt-parse-error', 'JSON is missing: ' + missing.join(', ')); return; }
window.__zbtParsed = parsed;
renderOutput(parsed);
save();
el('zbt-output-panel').scrollIntoView({ behavior: 'smooth', block: 'start' });
});
function renderOutput(p){
el('zbt-output-panel').style.display = 'block';
document.querySelectorAll('[data-output]').forEach(function(t){
t.value = p[t.getAttribute('data-output')] || '';
});
document.querySelectorAll('[data-meta]').forEach(function(d){
d.textContent = p[d.getAttribute('data-meta')] || '';
});
var tagBox = el('zbt-tags');
tagBox.innerHTML = '';
(p.blogTags || []).forEach(function(t){
var s = document.createElement('span');
s.className = 'zbt-tag'; s.textContent = t;
tagBox.appendChild(s);
});
var prev = el('zbt-preview-area');
prev.innerHTML = '
' + (p.blogTitle || '') + '
' + (p.blogHtml || '');
}
document.querySelectorAll('.zbt-tab').forEach(function(tab){
tab.addEventListener('click', function(){
var t = this.getAttribute('data-tab');
document.querySelectorAll('.zbt-tab').forEach(function(x){ x.classList.remove('active'); });
this.classList.add('active');
document.querySelectorAll('.zbt-pane').forEach(function(p){ p.classList.add('zbt-hidden'); });
el('zbt-pane-' + t).classList.remove('zbt-hidden');
});
});
document.querySelectorAll('[data-copy]').forEach(function(btn){
btn.addEventListener('click', function(){
var key = this.getAttribute('data-copy');
var p = window.__zbtParsed || {};
if (p[key]){ copyToClipboard(p[key]); flashCopy(this); }
});
});
el('zbt-new').addEventListener('click', function(){
if (!confirm('Clear current draft?')) return;
setForm({});
el('zbt-prompt-panel').style.display = 'none';
el('zbt-response-panel').style.display = 'none';
el('zbt-output-panel').style.display = 'none';
el('zbt-response-input').value = '';
window.__zbtParsed = null;
save();
});
FIELDS.forEach(function(k){
var e = el('zbt-'+k);
if (e) e.addEventListener('input', save);
if (e && e.tagName === 'SELECT') e.addEventListener('change', save);
});
load();
})();