/* ============================================================ BAN YARO — Züchter-Profil-Editor Selbstverwaltung des öffentlichen Züchter-Profils. ============================================================ */ window.Page_breeder_editor = (() => { let _container = null; let _data = null; // { profile, litters, storage_mb, storage_limit_mb } async function init(container) { _container = container; _container.innerHTML = `
${UI.skeleton(5)}
`; await _load(); } function refresh() { _load(); } function onDogChange() {} async function _load() { try { _data = await API.get('/breeder/my-editor'); _render(); } catch (e) { _container.innerHTML = `
${e.message}
`; } } function _render() { const { profile: p, litters, storage_mb, storage_limit_mb } = _data; _container.innerHTML = `

Mein Züchter-Profil

Gestalte deine öffentliche Profilseite — Fotos, Videos und Infos zu deinen Würfen.

Logo / Titelbild
${p.logo_url ? `` : ``}
Quadratisch · max. 5 MB · HEIC wird unterstützt
Profil-Texte
Profil-Fotos & Videos
JPG, PNG, HEIC, MP4, MOV · max. 200 MB pro Datei
${_storageBar(storage_mb, storage_limit_mb)}
${_renderPhotoGrid(p.photos || [])}
${litters.length ? `
Aktuelle Würfe — Fotos & Videos
${litters.map(l => _renderLitterCard(l)).join('')}
` : ''}
`; _bindEvents(); } function _renderPhotoGrid(photos) { return photos.map((ph, i) => { const isVid = ph.media_type === 'video' || (ph.url || '').endsWith('.mp4'); return `
${isVid ? `
▶ Video
` : ``} ${ph.is_primary ? `
LOGO
` : ''} ${!ph.is_primary ? `` : ''}
`; }).join(''); } function _renderLitterCard(l) { const label = l.geburtsdatum ? `Wurf vom ${new Date(l.geburtsdatum).toLocaleDateString('de-DE')}` : `Wurf #${l.id}`; const info = [ l.welpen_gesamt ? `${l.welpen_gesamt} Welpen` : null, `${l.foto_count} Medien`, ].filter(Boolean).join(' · '); return `
${UI.escape(label)}
${info}
`; } function _storageBar(usedMb, limitMb) { const pct = Math.min(100, Math.round((usedMb / limitMb) * 100)); const color = pct > 85 ? '#dc2626' : pct > 60 ? '#f59e0b' : '#22c55e'; return `
${usedMb.toFixed(1)} / ${limitMb} MB
`; } function _bindEvents() { const el = _container; // Logo hochladen el.querySelector('#be-logo-input')?.addEventListener('change', async e => { const file = e.target.files[0]; if (!file) return; const fd = new FormData(); fd.append('file', file); fd.append('entity_type', 'breeder'); fd.append('entity_id', String(_data.profile.id)); fd.append('is_primary', '1'); fd.append('visibility', 'public'); try { await API.breederPhotos.upload(fd); UI.toast.success('Logo gespeichert.'); await _load(); } catch (err) { UI.toast.error(err.message); } }); // Profil-Texte speichern el.querySelector('#be-text-form')?.addEventListener('submit', async e => { e.preventDefault(); const btn = e.target.querySelector('[type="submit"]'); const fd = UI.formData(e.target); await UI.asyncButton(btn, async () => { await API.put('/breeder/profile', fd); _data.profile = { ..._data.profile, ...fd }; UI.toast.success('Profil gespeichert.'); }); }); // Profil-Foto/-Video hochladen el.querySelector('#be-profile-photo-input')?.addEventListener('change', async e => { const file = e.target.files[0]; if (!file) return; const isVideo = file.type.startsWith('video/'); if (isVideo) UI.toast.info('Video wird komprimiert – das kann 1–2 Minuten dauern …', 120_000); const fd = new FormData(); fd.append('file', file); fd.append('entity_type', 'breeder'); fd.append('entity_id', String(_data.profile.id)); fd.append('visibility', 'public'); try { await API.breederPhotos.upload(fd); UI.toast.success(isVideo ? 'Video hinzugefügt.' : 'Foto hinzugefügt.'); await _load(); } catch (err) { UI.toast.error(err.message); } }); // Foto löschen el.querySelectorAll('.be-photo-del').forEach(btn => { btn.addEventListener('click', async () => { if (!confirm('Löschen?')) return; try { await API.breederPhotos.remove(parseInt(btn.dataset.id)); await _load(); } catch (err) { UI.toast.error(err.message); } }); }); // Als Logo setzen el.querySelectorAll('.be-photo-primary').forEach(btn => { btn.addEventListener('click', async () => { try { await API.patch(`/breeder/photos/${btn.dataset.id}/primary`, {}); await _load(); } catch (err) { UI.toast.error(err.message); } }); }); // Wurf-Upload el.querySelectorAll('.be-litter-input').forEach(input => { input.addEventListener('change', async e => { const file = e.target.files[0]; if (!file) return; const isVideo = file.type.startsWith('video/'); const litterId = input.dataset.litterId; const label = input.dataset.label; if (isVideo) UI.toast.info('Video wird komprimiert – das kann 1–2 Minuten dauern …', 120_000); const fd = new FormData(); fd.append('file', file); fd.append('entity_type', 'litter'); fd.append('entity_id', litterId); fd.append('visibility', 'public'); try { await API.breederPhotos.upload(fd); UI.toast.success(`${isVideo ? 'Video' : 'Foto'} zu „${label}" hinzugefügt.`); // Foto-Count aktualisieren const litter = _data.litters.find(l => String(l.id) === String(litterId)); if (litter) litter.foto_count++; _render(); } catch (err) { UI.toast.error(err.message); } }); }); } return { init, refresh, onDogChange }; })();