import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import '../config.dart'; class ResetPasswordScreen extends StatefulWidget { final int userId; const ResetPasswordScreen({Key? key, required this.userId}) : super(key: key); @override State createState() => _ResetPasswordScreenState(); } class _ResetPasswordScreenState extends State { final TextEditingController _passwordController = TextEditingController(); final TextEditingController _confirmController = TextEditingController(); bool _isLoading = false; Future _resetPassword() async { final password = _passwordController.text.trim(); final confirmPassword = _confirmController.text.trim(); if (password.isEmpty || confirmPassword.isEmpty) { _showMessage('Semua kolom harus diisi.'); return; } if (password != confirmPassword) { _showMessage('Password tidak sama.'); return; } setState(() => _isLoading = true); try { final response = await http.post( Uri.parse('$baseUrl/forgot-password/reset'), body: { 'user_id': widget.userId.toString(), 'password': password, 'password_confirmation': confirmPassword, }, ); final data = jsonDecode(response.body); if (response.statusCode == 200) { _showMessage(data['message'] ?? 'Password berhasil diubah'); Navigator.popUntil(context, (route) => route.isFirst); // Kembali ke login } else { _showMessage(data['message'] ?? 'Gagal reset password.'); } } catch (e) { _showMessage('Gagal menghubungi server.'); } finally { setState(() => _isLoading = false); } } void _showMessage(String message) { showDialog( context: context, builder: (_) => AlertDialog( title: const Text('Informasi'), content: Text(message), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: const Text('OK'), ), ], ), ); } @override void dispose() { _passwordController.dispose(); _confirmController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Reset Password Baru')), body: Padding( padding: const EdgeInsets.all(20.0), child: Column( children: [ TextField( controller: _passwordController, obscureText: true, decoration: const InputDecoration( labelText: 'Password Baru', border: OutlineInputBorder(), ), ), const SizedBox(height: 16), TextField( controller: _confirmController, obscureText: true, decoration: const InputDecoration( labelText: 'Konfirmasi Password', border: OutlineInputBorder(), ), ), const SizedBox(height: 24), _isLoading ? const CircularProgressIndicator() : ElevatedButton( onPressed: _resetPassword, child: const Text('Reset Password'), ), ], ), ), ); } }