import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import '../../../services/notification_admin_service.dart'; class NotificationAdminScreen extends StatefulWidget { const NotificationAdminScreen({super.key}); @override State createState() => _NotificationAdminScreenState(); } class _NotificationAdminScreenState extends State { List _notifications = []; bool _isLoading = true; @override void initState() { super.initState(); _loadNotifications(); } Future _loadNotifications() async { try { final data = await NotificationAdminService.getNotifications(); debugPrint("NOTIF COUNT: ${data.length}"); if (mounted) { setState(() { _notifications = data; _isLoading = false; }); } } catch (e) { debugPrint("ERROR NOTIF: $e"); if (mounted) { setState(() => _isLoading = false); } } } Future _markAsRead(int id) async { await NotificationAdminService.markAsRead(id); await _loadNotifications(); } Future _markAllAsRead() async { await NotificationAdminService.markAllRead(); await _loadNotifications(); } Future _deleteNotification(int id) async { await NotificationAdminService.deleteNotification(id); await _loadNotifications(); } Future _deleteAllNotifications() async { final confirmed = await _showDeleteAllDialog(); if (confirmed == true) { await NotificationAdminService.deleteAllNotifications(); await _loadNotifications(); } } Future _showDeleteAllDialog() { return showDialog( context: context, builder: (ctx) => AlertDialog( title: const Text('Hapus Semua Notifikasi'), content: const Text( 'Apakah kamu yakin ingin menghapus semua notifikasi? Tindakan ini tidak dapat dibatalkan.'), actions: [ TextButton( onPressed: () => Navigator.pop(ctx, false), child: const Text('Batal'), ), TextButton( onPressed: () => Navigator.pop(ctx, true), style: TextButton.styleFrom(foregroundColor: Colors.red), child: const Text('Hapus Semua'), ), ], ), ); } String _formatDate(String iso) { try { final dt = DateTime.parse(iso).toLocal(); return DateFormat('dd MMM yyyy, HH:mm', 'id_ID').format(dt); } catch (_) { return iso; } } @override Widget build(BuildContext context) { final unread = _notifications.where((e) => !e.isRead).length; return Scaffold( backgroundColor: const Color(0xFFF8F9FD), appBar: AppBar( backgroundColor: const Color(0xFF1A39B1), foregroundColor: Colors.white, title: const Text( 'Notifikasi Admin', style: TextStyle(fontWeight: FontWeight.bold), ), actions: [ if (unread > 0) TextButton( onPressed: _markAllAsRead, child: const Text( 'Tandai Semua', style: TextStyle(color: Colors.white70), ), ), if (_notifications.isNotEmpty) IconButton( onPressed: _deleteAllNotifications, icon: const Icon(Icons.delete_sweep_outlined), tooltip: 'Hapus Semua', ), ], ), body: _isLoading ? const Center(child: CircularProgressIndicator()) : _notifications.isEmpty ? const Center(child: Text("Belum ada notifikasi")) : ListView.separated( padding: const EdgeInsets.all(16), itemCount: _notifications.length, separatorBuilder: (_, __) => const SizedBox(height: 10), itemBuilder: (context, index) { final notif = _notifications[index]; return _buildItem(notif); }, ), ); } Widget _buildItem(NotificationAdmin notif) { return Dismissible( key: Key('notif_${notif.id}'), direction: DismissDirection.endToStart, background: Container( alignment: Alignment.centerRight, padding: const EdgeInsets.symmetric(horizontal: 20), decoration: BoxDecoration( color: Colors.red.shade400, borderRadius: BorderRadius.circular(16), ), child: const Icon(Icons.delete_outline, color: Colors.white, size: 28), ), confirmDismiss: (_) async { return await showDialog( context: context, builder: (ctx) => AlertDialog( title: const Text('Hapus Notifikasi'), content: const Text('Hapus notifikasi ini?'), actions: [ TextButton( onPressed: () => Navigator.pop(ctx, false), child: const Text('Batal'), ), TextButton( onPressed: () => Navigator.pop(ctx, true), style: TextButton.styleFrom(foregroundColor: Colors.red), child: const Text('Hapus'), ), ], ), ); }, onDismissed: (_) => _deleteNotification(notif.id), child: GestureDetector( onTap: () => _markAsRead(notif.id), child: Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: notif.isRead ? Colors.white : const Color(0xFFEFF6FF), borderRadius: BorderRadius.circular(16), border: Border.all( color: notif.isRead ? Colors.grey.shade200 : Colors.blue.withOpacity(0.3), ), ), child: Row( children: [ Icon( Icons.notifications_active, color: notif.isRead ? Colors.grey : Colors.blue, ), const SizedBox(width: 12), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( notif.title, style: TextStyle( fontWeight: notif.isRead ? FontWeight.normal : FontWeight.bold, ), ), const SizedBox(height: 4), Text( notif.message, style: TextStyle(color: Colors.grey.shade600), ), const SizedBox(height: 6), Text( _formatDate(notif.createdAt), style: TextStyle( fontSize: 11, color: Colors.grey.shade400), ), ], ), ), if (!notif.isRead) Container( width: 8, height: 8, decoration: const BoxDecoration( color: Colors.blue, shape: BoxShape.circle, ), ), ], ), ), ), ); } }