import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../providers/auth_provider.dart'; import '../providers/penjadwalan_provider.dart'; import '../providers/kandang_provider.dart'; import '../providers/panen_provider.dart'; import 'dashboard_page.dart'; import 'kontrol_page.dart'; import 'riwayat_page.dart'; import 'panen_debug_screen.dart'; class HomePage extends StatefulWidget { const HomePage({super.key}); @override State createState() => _HomePageState(); } class _HomePageState extends State { int _selectedIndex = 0; String? _initializedForUserId; final List _pages = [ const DashboardPage(), const KontrolPage(), const RiwayatPage(), ]; @override void didChangeDependencies() { super.didChangeDependencies(); final authProvider = context.read(); final userId = authProvider.user?.uid; if (userId == null || userId == _initializedForUserId) return; _initializedForUserId = userId; // Re-init provider saat app restart/hot restart ketika user masih login. WidgetsBinding.instance.addPostFrameCallback((_) async { if (!mounted) return; try { await context.read().initializeWithUser(userId); if (!mounted) return; await context.read().initializeWithUser(userId); if (!mounted) return; final panenProvider = context.read(); await panenProvider.loadTodaySnapshots(); await panenProvider.restorePanenHistoryFromFirebase(); } catch (_) { if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Sinkronisasi data sedang bermasalah, coba lagi.'), ), ); } }); } @override Widget build(BuildContext context) { final authProvider = context.watch(); final user = authProvider.user; return Scaffold( appBar: AppBar( elevation: 0, backgroundColor: const Color(0xFFF4D03F), leading: Padding( padding: const EdgeInsets.only(left: 12.0), child: Image.asset('assets/icons/telurku.png'), ), title: Text( 'TelurKu', style: TextStyle( color: Colors.grey.shade800, fontWeight: FontWeight.bold, fontSize: 24, ), ), centerTitle: true, actions: [ Padding( padding: const EdgeInsets.only(right: 12.0), child: PopupMenuButton( onSelected: (value) async { if (value == 0) { // Navigate to Debug Screen Navigator.push( context, MaterialPageRoute( builder: (context) => const PanenDebugScreen(), ), ); } else if (value == 1) { showDialog( context: context, builder: (context) => AlertDialog( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16), ), title: Row( children: [ Icon( Icons.logout, color: Colors.red.shade600, size: 24, ), const SizedBox(width: 12), Text( 'Konfirmasi Logout', style: TextStyle( color: Colors.grey.shade800, fontWeight: FontWeight.bold, ), ), ], ), content: Text( 'Apakah Anda yakin ingin logout?', style: TextStyle(color: Colors.grey.shade700), ), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: Text( 'Batal', style: TextStyle(color: Colors.grey.shade600), ), ), ElevatedButton( onPressed: () async { Navigator.pop(context); // Clear semua user data final penjadwalanProvider = context.read(); final kandangProvider = context.read(); await penjadwalanProvider.clearData(); await kandangProvider.clearData(); // Logout final success = await authProvider.logout(); if (success && mounted) { Navigator.pushReplacementNamed( context, '/landing', ); } }, style: ElevatedButton.styleFrom( backgroundColor: Colors.red.shade600, foregroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), ), child: const Text('Logout'), ), ], ), ); } }, itemBuilder: (context) => [ PopupMenuItem( value: 0, child: Row( children: [ Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( gradient: LinearGradient( colors: [ Colors.orange.shade400, Colors.orange.shade600 ], ), borderRadius: BorderRadius.circular(8), ), child: const Icon( Icons.bug_report, color: Colors.white, size: 18, ), ), const SizedBox(width: 12), Text( 'Debug Panen', style: TextStyle( color: Colors.orange.shade600, fontWeight: FontWeight.bold, fontSize: 14, ), ), ], ), ), PopupMenuItem( value: 1, child: Row( children: [ Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( gradient: LinearGradient( colors: [Colors.red.shade400, Colors.red.shade600], ), borderRadius: BorderRadius.circular(8), ), child: const Icon( Icons.logout, color: Colors.white, size: 18, ), ), const SizedBox(width: 12), Text( 'Logout', style: TextStyle( color: Colors.red.shade600, fontWeight: FontWeight.bold, fontSize: 14, ), ), ], ), ), ], shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16), ), elevation: 10, offset: const Offset(0, 50), child: Container( decoration: BoxDecoration( gradient: LinearGradient( colors: [Colors.orange.shade500, Colors.orange.shade700], ), borderRadius: BorderRadius.circular(50), boxShadow: [ BoxShadow( color: Colors.orange.shade300.withOpacity(0.4), blurRadius: 10, offset: const Offset(0, 4), ), ], ), child: CircleAvatar( backgroundColor: Colors.transparent, radius: 20, child: Text( (user?.displayName?.isNotEmpty ?? false) ? user!.displayName![0].toUpperCase() : 'U', style: const TextStyle( color: Colors.white, fontWeight: FontWeight.bold, fontSize: 18, ), ), ), ), ), ), ], ), body: _pages[_selectedIndex], bottomNavigationBar: BottomNavigationBar( currentIndex: _selectedIndex, onTap: (index) { setState(() { _selectedIndex = index; }); }, items: [ BottomNavigationBarItem( icon: const Icon(Icons.dashboard_outlined), activeIcon: const Icon(Icons.dashboard), label: 'Dashboard', ), BottomNavigationBarItem( icon: const Icon(Icons.settings_outlined), activeIcon: const Icon(Icons.settings), label: 'Kontrol', ), BottomNavigationBarItem( icon: const Icon(Icons.history_outlined), activeIcon: const Icon(Icons.history), label: 'Riwayat', ), ], backgroundColor: Colors.white, selectedItemColor: Colors.orange.shade600, unselectedItemColor: Colors.grey.shade400, elevation: 8, ), ); } }