import 'package:bahasajepang/service/API_config.dart'; import 'package:bahasajepang/theme.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; class ResetPasswordPage extends StatefulWidget { final String email; final String token; const ResetPasswordPage({ Key? key, required this.email, required this.token, }) : super(key: key); @override State createState() => _ResetPasswordPageState(); } class _ResetPasswordPageState extends State { final TextEditingController _passwordController = TextEditingController(); final TextEditingController _confirmController = TextEditingController(); bool _obscurePassword = true; bool _obscureConfirm = true; bool _isLoading = false; Future _resetPassword() async { if (_passwordController.text != _confirmController.text) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Password tidak cocok')), ); return; } if (_passwordController.text.length < 8) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Password minimal 8 karakter')), ); return; } setState(() { _isLoading = true; }); try { final url = Uri.parse('${ApiConfig.baseUrl}/reset-password'); final response = await http.post( url, headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', 'ngrok-skip-browser-warning': 'true' }, body: json.encode({ 'email': widget.email, 'token': widget.token, 'password': _passwordController.text, 'password_confirmation': _confirmController.text, }), ); final responseData = json.decode(response.body); if (response.statusCode == 200) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(responseData['message'] ?? 'Password berhasil direset')), ); Navigator.popUntil(context, (route) => route.isFirst); } else { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(responseData['message'] ?? 'Gagal reset password')), ); } } catch (e) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Gagal terhubung ke server: $e')), ); } finally { setState(() { _isLoading = false; }); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Reset Password'), backgroundColor: bgColor3, ), backgroundColor: bgColor1, body: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ const Text('Masukkan password baru Anda'), const SizedBox(height: 20), TextField( cursorColor: bgColor3, controller: _passwordController, obscureText: _obscurePassword, decoration: InputDecoration( labelText: 'Password Baru', border: const OutlineInputBorder(), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: bgColor2, width: 2.0), ), suffixIcon: IconButton( icon: Icon( _obscurePassword ? Icons.visibility : Icons.visibility_off, color: bgColor3, ), onPressed: () { setState(() { _obscurePassword = !_obscurePassword; }); }, ), ), ), const SizedBox(height: 20), TextField( cursorColor: bgColor3, controller: _confirmController, obscureText: _obscureConfirm, decoration: InputDecoration( labelText: 'Konfirmasi Password', border: const OutlineInputBorder(), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: bgColor2, width: 2.0), ), suffixIcon: IconButton( icon: Icon( _obscureConfirm ? Icons.visibility : Icons.visibility_off, color: bgColor3, ), onPressed: () { setState(() { _obscureConfirm = !_obscureConfirm; }); }, ), ), ), const SizedBox(height: 20), SizedBox( width: double.infinity, child: ElevatedButton( style: ElevatedButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 16), backgroundColor: bgColor2, ), onPressed: _isLoading ? null : _resetPassword, child: _isLoading ? CircularProgressIndicator(color: bgColor2) : const Text( 'Simpan Password Baru', style: TextStyle(fontSize: 16, color: Colors.black), ), ), ), ], ), ), ); } @override void dispose() { _passwordController.dispose(); _confirmController.dispose(); super.dispose(); } }