import { apiList, apiCreate, toast } from './api.js'; export function initCreate(){ const btn=document.getElementById('btn-new'), dlg=document.getElementById('createDialog'), form=document.getElementById('createForm'), fields=document.getElementById('createFields'), hint=document.getElementById('createHint'); if(!btn||!dlg||!form||!fields) return; const curTab=()=>{ const a=document.querySelector('nav [data-tab].bg-sky-50')||document.querySelector('nav [data-tab]'); return a?a.getAttribute('data-tab'):'templates'; }; const normalizeApiName=(v='')=>{ return String(v) .trim() .toLowerCase() .replace(/\s+/g,'-') .replace(/[^a-z0-9_-]+/g,'-') .replace(/-+/g,'-') .replace(/^-|-$/g,''); }; btn.onclick = async ()=>{ fields.innerHTML=''; const tab=curTab(); const name=document.createElement('input'); name.type='text'; name.required=true; name.placeholder='Name*'; name.className='w-full border rounded-lg px-3 py-2'; name.id='f-name'; fields.appendChild(name); let apiName = null; let apiTouched = false; const addEditorSelect = () => { const editorSelect = document.createElement('select'); editorSelect.id = 'f-editor-type'; editorSelect.className = 'w-full border rounded-lg px-3 py-2'; editorSelect.innerHTML = ` `; const defaultEditor = (window.__editorDefault || 'grapesjs').toLowerCase(); editorSelect.value = defaultEditor === 'craftjs' ? 'craftjs' : 'grapesjs'; fields.appendChild(editorSelect); }; if(tab==='templates'){ apiName=document.createElement('input'); apiName.type='text'; apiName.required=true; apiName.placeholder='API Name* (ohne Leerzeichen)'; apiName.className='w-full border rounded-lg px-3 py-2'; apiName.id='f-api-name'; fields.appendChild(apiName); addEditorSelect(); apiName.addEventListener('input', ()=>{ apiTouched = true; const next = normalizeApiName(apiName.value); if (next !== apiName.value) apiName.value = next; }); name.addEventListener('input', ()=>{ if (!apiTouched) apiName.value = normalizeApiName(name.value); }); } async function addSel(id,label,res){ const sel=document.createElement('select'); sel.id=id; sel.className='w-full border rounded-lg px-3 py-2'; sel.innerHTML=``; const data=await apiList(res); (data||[]).forEach(t=>{ const o=document.createElement('option'); o.value=t.id; o.textContent=`#${t.id} ยท ${t.name||''}`; sel.appendChild(o); }); fields.appendChild(sel); } if(tab==='sections') await addSel('f-template','Template','templates'); if(tab==='blocks') await addSel('f-section','Section','sections'); if(tab==='snippets') await addSel('f-block','Block','blocks'); if (tab !== 'templates') addEditorSelect(); hint.textContent=`Neues ${tab} anlegen`; dlg.showModal(); form.onsubmit=async(e)=>{ e.preventDefault(); const payload={ name:(document.getElementById('f-name')?.value||'').trim() }; if(!payload.name) return; if(tab==='templates') { payload.api_name=(document.getElementById('f-api-name')?.value||'').trim(); if(!payload.api_name) return; } payload.editor_type=(document.getElementById('f-editor-type')?.value||'grapesjs'); if(tab==='snippets') payload.content=''; else payload.html=''; if(tab==='sections') payload.template_id=document.getElementById('f-template')?.value||null; if(tab==='blocks') payload.section_id =document.getElementById('f-section')?.value ||null; if(tab==='snippets') payload.block_id =document.getElementById('f-block')?.value ||null; const r=await apiCreate(tab,payload); if(r&&r.id){ dlg.close(); toast('Erstellt',true); window.loadList && window.loadList(tab); } else { toast('Erstellen fehlgeschlagen',false,{duration:3000}); console.error('Create failed',r); } }; }; const cancel=document.getElementById('createCancel'); cancel && (cancel.onclick=()=>dlg.close()); }