"
>${ad?.js_script || ''}
Optional: Insert third-party ad network JS code.
`;
modal.classList.remove('hidden');
modal.classList.add('flex');
document.getElementById('adForm').addEventListener('submit', async (e) => {
e.preventDefault();
const fd = new FormData(e.target);
const payload = {
name: fd.get('name'),
ad_type: fd.get('ad_type'),
media_type: fd.get('media_type'),
media_url: fd.get('media_url'),
click_url: fd.get('click_url') || null,
countdown_seconds: parseInt(fd.get('countdown_seconds')),
width: parseInt(fd.get('width') || 0),
height: parseInt(fd.get('height') || 0),
js_script: fd.get('js_script') || null
};
try {
if (isEdit) {
await api('/api/ads/' + ad.id, {method: 'PUT', body: payload});
notify('Ad updated successfully!');
} else {
await api('/api/ads', {method: 'POST', body: payload});
notify('Ad created successfully!');
}
closeModal();
await loadAds();
await loadStats();
} catch(err) { notify(err.message || 'Failed to save ad', 'error'); }
});
}
async function editAd(id) {
try {
const ad = await api('/api/ads/' + id);
openAdModal(ad);
} catch(e) { notify('Failed to load ad', 'error'); }
}
async function deleteAd(id) {
if (!confirm('Are you sure you want to delete this ad?')) return;
try {
await api('/api/ads/' + id, {method: 'DELETE'});
notify('Ad deleted');
await loadAds();
await loadStats();
} catch(e) { notify('Failed to delete ad', 'error'); }
}
function closeModal() {
document.getElementById('modal').classList.add('hidden');
document.getElementById('modal').classList.remove('flex');
}
// โโ Init โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if (token) {
initDashboard().catch(() => renderLogin());
} else {
renderLogin();
}