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.
338 lines
12 KiB
338 lines
12 KiB
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:youmazgestion/Services/stock_managementDatabase.dart';
|
|
import 'package:youmazgestion/Views/Dashboard.dart';
|
|
import 'package:youmazgestion/Views/mobilepage.dart';
|
|
import 'package:youmazgestion/Views/particles.dart' show ParticleBackground;
|
|
import 'package:youmazgestion/accueil.dart';
|
|
import '../Models/users.dart';
|
|
import '../controller/userController.dart';
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
const LoginPage({super.key});
|
|
|
|
@override
|
|
_LoginPageState createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
late TextEditingController _usernameController;
|
|
late TextEditingController _passwordController;
|
|
final UserController userController = Get.put(UserController());
|
|
bool _isErrorVisible = false;
|
|
bool _isLoading = false;
|
|
String _errorMessage = 'Nom d\'utilisateur ou mot de passe invalide';
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_usernameController = TextEditingController();
|
|
_passwordController = TextEditingController();
|
|
checkUserCount();
|
|
}
|
|
|
|
void checkUserCount() async {
|
|
try {
|
|
final userCount = await AppDatabase.instance.getUserCount();
|
|
print('Nombre d\'utilisateurs trouvés: $userCount');
|
|
} catch (error) {
|
|
print('Erreur lors de la vérification du nombre d\'utilisateurs: $error');
|
|
setState(() {
|
|
_errorMessage = 'Erreur de connexion à la base de données';
|
|
_isErrorVisible = true;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_usernameController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> saveUserData(Users user, String role, int userId) async {
|
|
try {
|
|
userController.setUserWithCredentials(user, role, userId);
|
|
|
|
if (user.pointDeVenteId != null) {
|
|
await userController.loadPointDeVenteDesignation();
|
|
}
|
|
|
|
print('Utilisateur sauvegardé avec point de vente: ${userController.pointDeVenteDesignation}');
|
|
} catch (error) {
|
|
print('Erreur lors de la sauvegarde: $error');
|
|
throw Exception('Erreur lors de la sauvegarde des données utilisateur');
|
|
}
|
|
}
|
|
|
|
void _login() async {
|
|
if (_isLoading) return;
|
|
|
|
final String username = _usernameController.text.trim();
|
|
final String password = _passwordController.text.trim();
|
|
|
|
if (username.isEmpty || password.isEmpty) {
|
|
setState(() {
|
|
_errorMessage =
|
|
'Veuillez saisir le nom d\'utilisateur et le mot de passe';
|
|
_isErrorVisible = true;
|
|
});
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_isLoading = true;
|
|
_isErrorVisible = false;
|
|
});
|
|
|
|
try {
|
|
print('Tentative de connexion pour: $username');
|
|
final dbInstance = AppDatabase.instance;
|
|
|
|
try {
|
|
final userCount = await dbInstance.getUserCount();
|
|
print('Base de données accessible, $userCount utilisateurs trouvés');
|
|
} catch (dbError) {
|
|
throw Exception('Impossible d\'accéder à la base de données: $dbError');
|
|
}
|
|
|
|
bool isValidUser = await dbInstance.verifyUser(username, password);
|
|
|
|
if (isValidUser) {
|
|
Users user = await dbInstance.getUser(username);
|
|
print('Utilisateur récupéré: ${user.username}');
|
|
|
|
Map<String, dynamic>? userCredentials =
|
|
await dbInstance.getUserCredentials(username, password);
|
|
|
|
if (userCredentials != null) {
|
|
print('Connexion réussie pour: ${user.username}');
|
|
print('Rôle: ${userCredentials['role']}');
|
|
print('ID: ${userCredentials['id']}');
|
|
|
|
await saveUserData(
|
|
user,
|
|
userCredentials['role'] as String,
|
|
userCredentials['id'] as int,
|
|
);
|
|
|
|
// MODIFICATION PRINCIPALE ICI
|
|
if (mounted) {
|
|
if (userCredentials['role'] == 'commercial') {
|
|
// Redirection vers MainLayout pour les commerciaux
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const MainLayout()),
|
|
);
|
|
} else {
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => DashboardPage()),
|
|
);
|
|
}
|
|
}
|
|
} else {
|
|
throw Exception('Erreur lors de la récupération des credentials');
|
|
}
|
|
} else {
|
|
setState(() {
|
|
_errorMessage = 'Nom d\'utilisateur ou mot de passe invalide';
|
|
_isErrorVisible = true;
|
|
});
|
|
}
|
|
} catch (error) {
|
|
setState(() {
|
|
_errorMessage = 'Erreur de connexion: ${error.toString()}';
|
|
_isErrorVisible = true;
|
|
});
|
|
} finally {
|
|
if (mounted) setState(() => _isLoading = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final Color primaryBlue = const Color(0xFF0033A1);
|
|
final Color accentRed = const Color(0xFFD70000);
|
|
final Color secondaryBlue = const Color(0xFF1976D2);
|
|
final Color primaryColor = primaryBlue;
|
|
final Color accentColor = secondaryBlue;
|
|
final Color cardColor = Colors.white;
|
|
|
|
return Scaffold(
|
|
backgroundColor: primaryColor,
|
|
body: ParticleBackground(
|
|
child: Center(
|
|
child: SingleChildScrollView(
|
|
child: Container(
|
|
width: MediaQuery.of(context).size.width < 500
|
|
? double.infinity
|
|
: 400,
|
|
padding:
|
|
const EdgeInsets.symmetric(horizontal: 24.0, vertical: 32.0),
|
|
decoration: BoxDecoration(
|
|
color: cardColor.withOpacity(0.98),
|
|
borderRadius: BorderRadius.circular(30.0),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: primaryColor.withOpacity(0.2),
|
|
blurRadius: 16,
|
|
spreadRadius: 4,
|
|
offset: const Offset(0, 8),
|
|
),
|
|
],
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Center(
|
|
child: Column(
|
|
children: [
|
|
CircleAvatar(
|
|
radius: 38,
|
|
backgroundColor: accentColor.withOpacity(0.15),
|
|
child: Icon(
|
|
Icons.lock_outline,
|
|
color: accentColor,
|
|
size: 50,
|
|
),
|
|
),
|
|
const SizedBox(height: 14),
|
|
Text(
|
|
'GUYCOM',
|
|
style: TextStyle(
|
|
color: primaryColor,
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 28,
|
|
),
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
'Connectez-vous à votre compte',
|
|
style: TextStyle(
|
|
color: primaryColor.withOpacity(.8),
|
|
fontSize: 16,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 24),
|
|
TextField(
|
|
controller: _usernameController,
|
|
enabled: !_isLoading,
|
|
decoration: InputDecoration(
|
|
labelText: 'Nom d\'utilisateur',
|
|
labelStyle: TextStyle(
|
|
color: primaryColor.withOpacity(0.7),
|
|
),
|
|
prefixIcon: Icon(Icons.person, color: accentColor),
|
|
filled: true,
|
|
fillColor: accentColor.withOpacity(0.045),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(30.0),
|
|
borderSide: BorderSide(color: accentColor, width: 2),
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(30.0),
|
|
borderSide: BorderSide(color: accentColor, width: 2),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 18.0),
|
|
TextField(
|
|
controller: _passwordController,
|
|
enabled: !_isLoading,
|
|
obscureText: true,
|
|
decoration: InputDecoration(
|
|
labelText: 'Mot de passe',
|
|
labelStyle: TextStyle(
|
|
color: primaryColor.withOpacity(0.7),
|
|
),
|
|
prefixIcon: Icon(Icons.lock, color: accentColor),
|
|
filled: true,
|
|
fillColor: accentColor.withOpacity(0.045),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(30.0),
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(30.0),
|
|
borderSide: BorderSide(color: accentColor, width: 2),
|
|
),
|
|
),
|
|
onSubmitted: (_) => _login(),
|
|
),
|
|
if (_isErrorVisible) ...[
|
|
const SizedBox(height: 12.0),
|
|
Text(
|
|
_errorMessage,
|
|
style: const TextStyle(
|
|
color: Colors.redAccent,
|
|
fontSize: 15,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
],
|
|
const SizedBox(height: 26.0),
|
|
ElevatedButton(
|
|
onPressed: _isLoading ? null : _login,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: accentColor,
|
|
disabledBackgroundColor: accentColor.withOpacity(0.3),
|
|
foregroundColor: Colors.white,
|
|
elevation: 7.0,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(30.0),
|
|
),
|
|
minimumSize: const Size(double.infinity, 52),
|
|
),
|
|
child: _isLoading
|
|
? const SizedBox(
|
|
height: 24,
|
|
width: 24,
|
|
child: CircularProgressIndicator(
|
|
color: Colors.white,
|
|
strokeWidth: 2.5,
|
|
),
|
|
)
|
|
: const Text(
|
|
'Se connecter',
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
letterSpacing: .4,
|
|
),
|
|
),
|
|
),
|
|
// Option debug, à enlever en prod
|
|
if (_isErrorVisible) ...[
|
|
TextButton(
|
|
onPressed: () async {
|
|
try {
|
|
final count =
|
|
await AppDatabase.instance.getUserCount();
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('$count utilisateurs trouvés')),
|
|
);
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Erreur: $e')),
|
|
);
|
|
}
|
|
},
|
|
child: const Text('Debug: Vérifier BDD'),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|