import 'package:flutter/material.dart'; import '../services/supabase_service.dart'; class LoginScreen extends StatefulWidget { const LoginScreen({super.key}); @override State createState() => _LoginScreenState(); } class _LoginScreenState extends State { final _emailController = TextEditingController(); final _passwordController = TextEditingController(); bool _isLoading = false; bool _obscurePassword = true; Future _handleLogin() async { if (_emailController.text.trim().isEmpty || _passwordController.text.trim().isEmpty) { _showError('Email dan password tidak boleh kosong!'); return; } setState(() => _isLoading = true); try { await SupabaseService().signIn( _emailController.text.trim(), _passwordController.text.trim(), ); if (mounted) { Navigator.pushReplacementNamed(context, '/home'); } } catch (e) { if (mounted) { final errorStr = e.toString().toLowerCase(); String pesanError = 'Login gagal, coba lagi.'; if (errorStr.contains('invalid_credentials') || errorStr.contains('invalid login credentials') || errorStr.contains('email or password')) { pesanError = 'Email atau password salah!'; } else if (errorStr.contains('not confirmed')) { pesanError = 'Email belum dikonfirmasi, cek inbox kamu.'; } else if (errorStr.contains('network') || errorStr.contains('socket') || errorStr.contains('connection')) { pesanError = 'Tidak ada koneksi internet.'; } else if (errorStr.contains('too many') || errorStr.contains('rate limit')) { pesanError = 'Terlalu banyak percobaan, tunggu sebentar.'; } else if (errorStr.contains('user not found')) { pesanError = 'Email atau password salah!'; } _showError(pesanError); } } finally { if (mounted) setState(() => _isLoading = false); } } void _showError(String pesan) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Row( children: [ const Icon(Icons.error_outline, color: Colors.white), const SizedBox(width: 10), Expanded( child: Text( pesan, style: const TextStyle( color: Colors.white, fontWeight: FontWeight.bold, ), ), ), ], ), backgroundColor: Colors.red.shade700, behavior: SnackBarBehavior.floating, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), margin: const EdgeInsets.all(16), duration: const Duration(seconds: 3), ), ); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.black, body: SingleChildScrollView( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 30.0, vertical: 80.0), child: Column( children: [ const CircleAvatar( radius: 60, backgroundColor: Color(0xFF1A1A1A), child: Icon(Icons.coffee, size: 60, color: Colors.orange), ), const SizedBox(height: 30), const Text( 'Selamat Datang', style: TextStyle( color: Colors.green, fontSize: 32, fontWeight: FontWeight.bold), ), const Text( 'Sistem Kendali Pengering Kopi', style: TextStyle(color: Colors.grey, fontSize: 16), ), const SizedBox(height: 50), Container( padding: const EdgeInsets.all(20), decoration: BoxDecoration( color: const Color(0xFF1A1A1A), borderRadius: BorderRadius.circular(25), border: Border.all(color: Colors.grey.withOpacity(0.3)), ), child: Column( children: [ TextField( controller: _emailController, keyboardType: TextInputType.emailAddress, style: const TextStyle(color: Colors.white), decoration: const InputDecoration( prefixIcon: Icon(Icons.email, color: Colors.green), hintText: 'Email', hintStyle: TextStyle(color: Colors.grey), border: InputBorder.none, ), ), const Divider(color: Colors.grey), TextField( controller: _passwordController, obscureText: _obscurePassword, style: const TextStyle(color: Colors.white), decoration: InputDecoration( prefixIcon: const Icon(Icons.lock, color: Colors.green), suffixIcon: IconButton( icon: Icon( _obscurePassword ? Icons.visibility : Icons.visibility_off, color: Colors.grey, ), onPressed: () => setState( () => _obscurePassword = !_obscurePassword), ), hintText: 'Password', hintStyle: const TextStyle(color: Colors.grey), border: InputBorder.none, ), ), const SizedBox(height: 30), SizedBox( width: double.infinity, height: 55, child: ElevatedButton( onPressed: _isLoading ? null : _handleLogin, style: ElevatedButton.styleFrom( backgroundColor: Colors.green, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(15), ), ), child: _isLoading ? const CircularProgressIndicator( color: Colors.white) : const Text( 'Masuk ke Akun', style: TextStyle( fontSize: 18, fontWeight: FontWeight.bold, color: Colors.white), ), ), ), ], ), ), // Lupa Password dan Daftar — rapat tanpa jarak besar, warna sama hijau const SizedBox(height: 10), TextButton( onPressed: () => Navigator.pushNamed(context, '/forgot_password'), child: const Text( 'Lupa Password?', style: TextStyle(color: Colors.green), ), ), TextButton( onPressed: () => Navigator.pushNamed(context, '/register'), child: const Text( 'Belum punya akun? Daftar Sekarang', style: TextStyle(color: Colors.green), ), ), ], ), ), ), ); } }