You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
283 lines
9.0 KiB
283 lines
9.0 KiB
import 'package:flutter/material.dart';
|
|
import 'package:youmazgestion/Models/users.dart';
|
|
import 'package:youmazgestion/Models/role.dart';
|
|
import 'package:youmazgestion/Services/stock_managementDatabase.dart';
|
|
//import '../Services/app_database.dart';
|
|
|
|
class EditUserPage extends StatefulWidget {
|
|
final Users user;
|
|
|
|
const EditUserPage({super.key, required this.user});
|
|
|
|
@override
|
|
_EditUserPageState createState() => _EditUserPageState();
|
|
}
|
|
|
|
class _EditUserPageState extends State<EditUserPage> {
|
|
late TextEditingController _nameController;
|
|
late TextEditingController _lastNameController;
|
|
late TextEditingController _emailController;
|
|
late TextEditingController _usernameController;
|
|
late TextEditingController _passwordController;
|
|
|
|
List<Role> _roles = [];
|
|
Role? _selectedRole;
|
|
bool _isLoading = false;
|
|
bool _isLoadingRoles = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_nameController = TextEditingController(text: widget.user.name);
|
|
_lastNameController = TextEditingController(text: widget.user.lastName);
|
|
_emailController = TextEditingController(text: widget.user.email);
|
|
_usernameController = TextEditingController(text: widget.user.username);
|
|
_passwordController = TextEditingController();
|
|
|
|
_loadRoles();
|
|
}
|
|
|
|
Future<void> _loadRoles() async {
|
|
try {
|
|
final roles = await AppDatabase.instance.getRoles();
|
|
final currentRole = roles.firstWhere(
|
|
(r) => r.id == widget.user.roleId,
|
|
orElse: () => Role(id: widget.user.roleId, designation: widget.user.roleName ?? 'Inconnu'),
|
|
);
|
|
|
|
setState(() {
|
|
_roles = roles;
|
|
_selectedRole = currentRole;
|
|
_isLoadingRoles = false;
|
|
});
|
|
} catch (e) {
|
|
print('Erreur lors du chargement des rôles: $e');
|
|
setState(() {
|
|
_isLoadingRoles = false;
|
|
});
|
|
_showErrorDialog('Erreur', 'Impossible de charger les rôles.');
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_nameController.dispose();
|
|
_lastNameController.dispose();
|
|
_emailController.dispose();
|
|
_usernameController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
bool _validateFields() {
|
|
if (_nameController.text.trim().isEmpty ||
|
|
_lastNameController.text.trim().isEmpty ||
|
|
_emailController.text.trim().isEmpty ||
|
|
_usernameController.text.trim().isEmpty ||
|
|
_selectedRole == null) {
|
|
_showErrorDialog('Champs manquants', 'Veuillez remplir tous les champs requis.');
|
|
return false;
|
|
}
|
|
|
|
if (!RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$')
|
|
.hasMatch(_emailController.text.trim())) {
|
|
_showErrorDialog('Email invalide', 'Veuillez saisir un email valide.');
|
|
return false;
|
|
}
|
|
|
|
if (_passwordController.text.isNotEmpty &&
|
|
_passwordController.text.length < 6) {
|
|
_showErrorDialog('Mot de passe trop court', 'Minimum 6 caractères.');
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
Future<void> _updateUser() async {
|
|
if (!_validateFields() || _isLoading) return;
|
|
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
|
|
try {
|
|
final updatedUser = Users(
|
|
id: widget.user.id,
|
|
name: _nameController.text.trim(),
|
|
lastName: _lastNameController.text.trim(),
|
|
email: _emailController.text.trim(),
|
|
username: _usernameController.text.trim(),
|
|
password: _passwordController.text.isNotEmpty
|
|
? _passwordController.text
|
|
: widget.user.password,
|
|
roleId: _selectedRole!.id!,
|
|
roleName: _selectedRole!.designation,
|
|
);
|
|
|
|
await AppDatabase.instance.updateUser(updatedUser);
|
|
if (mounted) _showSuccessDialog();
|
|
} catch (e) {
|
|
print('Erreur de mise à jour: $e');
|
|
if (mounted) {
|
|
_showErrorDialog('Échec', 'Une erreur est survenue lors de la mise à jour.');
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
void _showSuccessDialog() {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Mise à jour réussie'),
|
|
content: const Text('Les informations de l\'utilisateur ont été mises à jour.'),
|
|
actions: [
|
|
ElevatedButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text('OK'),
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showErrorDialog(String title, String message) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: Text(title),
|
|
content: Text(message),
|
|
actions: [
|
|
ElevatedButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text('OK'),
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Modifier Utilisateur', style: TextStyle(color: Colors.white)),
|
|
backgroundColor: const Color.fromARGB(255, 4, 54, 95),
|
|
iconTheme: const IconThemeData(color: Colors.white),
|
|
centerTitle: true,
|
|
),
|
|
body: _isLoadingRoles
|
|
? const Center(child: CircularProgressIndicator())
|
|
: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Card(
|
|
elevation: 4,
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
children: [
|
|
const Icon(Icons.edit, size: 64, color: Colors.blue),
|
|
const SizedBox(height: 16),
|
|
_buildTextField(_nameController, 'Prénom', Icons.person),
|
|
const SizedBox(height: 12),
|
|
_buildTextField(_lastNameController, 'Nom', Icons.person_outline),
|
|
const SizedBox(height: 12),
|
|
_buildTextField(_emailController, 'Email', Icons.email, keyboardType: TextInputType.emailAddress),
|
|
const SizedBox(height: 12),
|
|
_buildTextField(_usernameController, 'Nom d\'utilisateur', Icons.account_circle),
|
|
const SizedBox(height: 12),
|
|
_buildTextField(
|
|
_passwordController,
|
|
'Mot de passe (laisser vide si inchangé)',
|
|
Icons.lock,
|
|
obscureText: true,
|
|
),
|
|
const SizedBox(height: 12),
|
|
_buildDropdown(),
|
|
const SizedBox(height: 20),
|
|
SizedBox(
|
|
width: double.infinity,
|
|
height: 48,
|
|
child: ElevatedButton(
|
|
onPressed: _isLoading ? null : _updateUser,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: const Color(0xFF0015B7),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
),
|
|
child: _isLoading
|
|
? const CircularProgressIndicator(color: Colors.white)
|
|
: const Text('Mettre à jour', style: TextStyle(color: Colors.white, fontSize: 16)),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildTextField(
|
|
TextEditingController controller,
|
|
String label,
|
|
IconData icon, {
|
|
TextInputType keyboardType = TextInputType.text,
|
|
bool obscureText = false,
|
|
}) {
|
|
return TextField(
|
|
controller: controller,
|
|
decoration: InputDecoration(
|
|
labelText: label,
|
|
prefixIcon: Icon(icon),
|
|
border: OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
|
|
),
|
|
keyboardType: keyboardType,
|
|
obscureText: obscureText,
|
|
);
|
|
}
|
|
|
|
Widget _buildDropdown() {
|
|
return Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: Colors.grey),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: DropdownButtonHideUnderline(
|
|
child: DropdownButton<Role>(
|
|
value: _selectedRole,
|
|
isExpanded: true,
|
|
hint: const Text('Sélectionner un rôle'),
|
|
onChanged: _isLoading
|
|
? null
|
|
: (Role? newValue) {
|
|
setState(() {
|
|
_selectedRole = newValue;
|
|
});
|
|
},
|
|
items: _roles.map((role) {
|
|
return DropdownMenuItem<Role>(
|
|
value: role,
|
|
child: Row(
|
|
children: [
|
|
const Icon(Icons.badge, size: 20),
|
|
const SizedBox(width: 8),
|
|
Text(role.designation),
|
|
],
|
|
),
|
|
);
|
|
}).toList(),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|