import 'package:flutter/material.dart'; import 'package:insparkspokalen_ui/models/informationModel.dart'; import 'package:insparkspokalen_ui/services/backend/informationService.dart'; class AdminInformationPage extends StatefulWidget { const AdminInformationPage({super.key}); @override State createState() => _AdminInformationPageState(); } class _AdminInformationPageState extends State { List informationList = []; @override void initState() { super.initState(); _loadInformation(); } Future _loadInformation() async { try { final info = await InformationService().fetchInformation(); info.sort((a, b) => a.createdAt.compareTo(b.createdAt)); setState(() { informationList = info; }); } catch (e) { print("Fel vid inläsning av information: $e"); } } void _deleteInformation(InformationModel info) async { try { await InformationService().deleteInformation(info); setState(() => informationList.remove(info)); } catch (e) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text("Kunde inte ta bort information")), ); } } void _showAddDialog() { final titleController = TextEditingController(); final contentController = TextEditingController(); showDialog( context: context, builder: (context) => AlertDialog( title: const Text("Lägg till information"), content: Column( mainAxisSize: MainAxisSize.min, children: [ TextField( controller: titleController, decoration: const InputDecoration(labelText: "Titel"), ), TextField( controller: contentController, decoration: const InputDecoration(labelText: "Innehåll"), ), ], ), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(), child: const Text("Avbryt"), ), ElevatedButton( onPressed: () async { final newInfo = InformationModel( title: titleController.text.trim(), content: contentController.text.trim(), author: 'Admin', createdAt: DateTime.now(), ); try { await InformationService().createInformation(newInfo); Navigator.of(context).pop(); _loadInformation(); } catch (e) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text("Kunde inte skapa information"), ), ); } }, child: const Text("Lägg till"), ), ], ), ); } void _confirmDelete(BuildContext context, InformationModel info) { showDialog( context: context, builder: (context) => AlertDialog( title: const Text("Ta bort inlägg"), content: const Text("Är du säker på att du vill ta bort detta inlägg?"), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(), child: const Text("Avbryt"), ), TextButton( onPressed: () { Navigator.of(context).pop(); _deleteInformation(info); }, child: const Text( "Ta bort", style: TextStyle(color: Colors.red), ), ), ], ), ); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: const Color(0xFF2E2E2E), appBar: AppBar( title: const Text("Admin - Information"), backgroundColor: const Color(0xFF2E2E2E), foregroundColor: Color(0xFFDAA520), ), floatingActionButton: FloatingActionButton( onPressed: _showAddDialog, backgroundColor: Colors.white, foregroundColor: Colors.black, child: const Icon(Icons.add), ), body: informationList.isEmpty ? const Center(child: CircularProgressIndicator(color: Color(0xFFDAA520))) : ListView.builder( itemCount: informationList.length, itemBuilder: (context, index) { final info = informationList[index]; return Card( color: Colors.white, margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), child: ListTile( title: Text( info.title, style: const TextStyle(fontWeight: FontWeight.bold), ), subtitle: Text(info.content), trailing: IconButton( icon: const Icon(Icons.delete, color: Colors.red), onPressed: () => _confirmDelete(context, info), ), ), ); }, ), ); } }