import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:intl/intl.dart'; import '../../providers/auth_provider.dart'; import '../../providers/time_provider.dart'; import '../../services/time_service.dart'; import '../../models/time_registration.dart'; import '../history/history_screen.dart'; import '../reports/reports_screen.dart'; import '../profile/profile_screen.dart'; import '../../widgets/timer_widget.dart'; class HomeScreen extends ConsumerStatefulWidget { const HomeScreen({super.key}); @override ConsumerState createState() => _HomeScreenState(); } class _HomeScreenState extends ConsumerState { int _selectedIndex = 0; @override Widget build(BuildContext context) { final List screens = [ const HomeTab(), const HistoryScreen(), const ReportsScreen(), const ProfileScreen(), ]; return Scaffold( body: screens[_selectedIndex], bottomNavigationBar: NavigationBar( selectedIndex: _selectedIndex, onDestinationSelected: (index) { setState(() { _selectedIndex = index; }); }, destinations: const [ NavigationDestination( icon: Icon(Icons.home_outlined), selectedIcon: Icon(Icons.home), label: 'Hjem', ), NavigationDestination( icon: Icon(Icons.history_outlined), selectedIcon: Icon(Icons.history), label: 'Historikk', ), NavigationDestination( icon: Icon(Icons.bar_chart_outlined), selectedIcon: Icon(Icons.bar_chart), label: 'Rapporter', ), NavigationDestination( icon: Icon(Icons.person_outlined), selectedIcon: Icon(Icons.person), label: 'Profil', ), ], ), ); } } class HomeTab extends ConsumerWidget { const HomeTab({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final userData = ref.watch(userDataProvider); final todayRegistrations = ref.watch(todayRegistrationsProvider); return Scaffold( appBar: AppBar( title: const Text('TimeReg'), actions: [ IconButton( icon: const Icon(Icons.notifications_outlined), onPressed: () { // TODO: Implementer varsler }, ), ], ), body: SafeArea( child: SingleChildScrollView( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // Velkomst userData.when( data: (user) => user != null ? Text( 'Hei, ${user.displayName}!', style: Theme.of(context).textTheme.headlineSmall, ) : const SizedBox.shrink(), loading: () => const SizedBox.shrink(), error: (_, __) => const SizedBox.shrink(), ), const SizedBox(height: 8), Text( DateFormat('EEEE d. MMMM yyyy', 'nb_NO').format(DateTime.now()), style: Theme.of(context).textTheme.bodyMedium?.copyWith( color: Colors.grey, ), ), const SizedBox(height: 24), // Timer widget const TimerWidget(), const SizedBox(height: 24), // Dagens registreringer Card( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( 'I dag', style: Theme.of(context).textTheme.titleMedium?.copyWith( fontWeight: FontWeight.bold, ), ), todayRegistrations.when( data: (registrations) { final totalMinutes = registrations .where((r) => r.endTime != null) .fold(0, (sum, r) => sum + r.netWorkMinutes); final hours = totalMinutes ~/ 60; final minutes = totalMinutes % 60; return Text( '${hours}t ${minutes}m', style: Theme.of(context).textTheme.titleLarge?.copyWith( fontWeight: FontWeight.bold, color: Theme.of(context).colorScheme.primary, ), ); }, loading: () => const SizedBox( width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2), ), error: (_, __) => const Text('--'), ), ], ), const SizedBox(height: 16), todayRegistrations.when( data: (registrations) { if (registrations.isEmpty) { return const Text('Ingen registreringer i dag'); } return Column( children: registrations.map((reg) { return ListTile( contentPadding: EdgeInsets.zero, leading: Icon( _getIconForType(reg.type), color: Theme.of(context).colorScheme.primary, ), title: Text( '${DateFormat('HH:mm').format(reg.startTime)} - ${reg.endTime != null ? DateFormat('HH:mm').format(reg.endTime!) : 'Pågår'}', ), subtitle: reg.comment != null ? Text(reg.comment!) : null, trailing: reg.endTime != null ? Text( '${reg.netWorkMinutes ~/ 60}t ${reg.netWorkMinutes % 60}m', style: const TextStyle( fontWeight: FontWeight.bold, ), ) : const Icon(Icons.play_arrow, color: Colors.green), ); }).toList(), ); }, loading: () => const Center( child: CircularProgressIndicator(), ), error: (error, _) => Text('Feil: $error'), ), ], ), ), ), const SizedBox(height: 16), // Ukens oversikt Card( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'Denne uken', style: Theme.of(context).textTheme.titleMedium?.copyWith( fontWeight: FontWeight.bold, ), ), const SizedBox(height: 16), _WeekSummary(), ], ), ), ), ], ), ), ), floatingActionButton: FloatingActionButton.extended( onPressed: () { // TODO: Implementer manuell registrering }, icon: const Icon(Icons.add), label: const Text('Manuell registrering'), ), ); } IconData _getIconForType(RegistrationType type) { switch (type) { case RegistrationType.ordinary: return Icons.work_outline; case RegistrationType.overtime: return Icons.access_time_filled; case RegistrationType.oncall: return Icons.phone_in_talk; case RegistrationType.travel: return Icons.directions_car; } } } class _WeekSummary extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final now = DateTime.now(); final startOfWeek = now.subtract(Duration(days: now.weekday - 1)); final endOfWeek = startOfWeek.add(const Duration(days: 6, hours: 23, minutes: 59)); final dateRange = DateRange(start: startOfWeek, end: endOfWeek); final totalHours = ref.watch(totalHoursProvider(dateRange)); return totalHours.when( data: (hours) { final totalMinutes = hours['total'] ?? 0; final ordinaryMinutes = hours['ordinary'] ?? 0; final overtimeMinutes = hours['overtime'] ?? 0; return Column( children: [ _SummaryRow( label: 'Total arbeidstid', value: '${totalMinutes ~/ 60}t ${totalMinutes % 60}m', ), const SizedBox(height: 8), _SummaryRow( label: 'Ordinær tid', value: '${ordinaryMinutes ~/ 60}t ${ordinaryMinutes % 60}m', ), const SizedBox(height: 8), _SummaryRow( label: 'Overtid', value: '${overtimeMinutes ~/ 60}t ${overtimeMinutes % 60}m', ), ], ); }, loading: () => const Center(child: CircularProgressIndicator()), error: (error, _) => Text('Feil: $error'), ); } } class _SummaryRow extends StatelessWidget { final String label; final String value; const _SummaryRow({ required this.label, required this.value, }); @override Widget build(BuildContext context) { return Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text(label), Text( value, style: const TextStyle(fontWeight: FontWeight.bold), ), ], ); } }