📢 แจ้งเตือน

แจ้งเตือนการกำจัดสิ่งปฏิกูล

⚠️ ต้อง Login ก่อนทำการแจ้งเตือน
กำลังประมวลผล...
const form = document.getElementById('notifyForm'); const alertDiv = document.getElementById('alert'); const loadingDiv = document.getElementById('loading'); const userIdField = document.getElementById('userId'); // ตรวจสอบ Token จาก localStorage window.addEventListener('load', () => { const token = localStorage.getItem('token'); const userId = localStorage.getItem('user_id'); if (!token || !userId) { showAlert('ต้อง Login ก่อน', 'error'); setTimeout(() => { window.location.href = 'index.html'; }, 2000); return; } userIdField.value = userId; }); form.addEventListener('submit', async (e) => { e.preventDefault(); const token = localStorage.getItem('token'); const userId = localStorage.getItem('user_id'); if (!token || !userId) { showAlert('Token ไม่ถูกต้อง', 'error'); return; } loadingDiv.classList.add('show'); try { const formData = new FormData(form); const data = Object.fromEntries(formData); const response = await fetch('api/notifications/create.php', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token }, body: JSON.stringify(data) }); const result = await response.json(); if (result.success) { showAlert('ส่งแจ้งเตือนสำเร็จ!', 'success'); form.reset(); userIdField.value = userId; setTimeout(() => { window.location.href = 'dashboard.html'; }, 2000); } else { showAlert(result.error || 'ส่งแจ้งเตือนไม่สำเร็จ', 'error'); } } catch (error) { showAlert('เกิดข้อผิดพลาด: ' + error.message, 'error'); } finally { loadingDiv.classList.remove('show'); } }); function showAlert(message, type) { alertDiv.textContent = message; alertDiv.className = 'alert alert-' + type; setTimeout(() => { alertDiv.className = 'alert'; }, 5000); }