218 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			218 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'package:flutter/foundation.dart';
 | |
| import 'package:em2rp/models/equipment_model.dart';
 | |
| import 'package:em2rp/services/equipment_service.dart';
 | |
| 
 | |
| class EquipmentProvider extends ChangeNotifier {
 | |
|   final EquipmentService _service = EquipmentService();
 | |
| 
 | |
|   List<EquipmentModel> _equipment = [];
 | |
|   List<String> _models = [];
 | |
|   List<String> _brands = [];
 | |
| 
 | |
|   EquipmentCategory? _selectedCategory;
 | |
|   EquipmentStatus? _selectedStatus;
 | |
|   String? _selectedModel;
 | |
|   String _searchQuery = '';
 | |
| 
 | |
|   // Getters
 | |
|   List<EquipmentModel> get equipment => _equipment;
 | |
|   List<String> get models => _models;
 | |
|   List<String> get brands => _brands;
 | |
|   EquipmentCategory? get selectedCategory => _selectedCategory;
 | |
|   EquipmentStatus? get selectedStatus => _selectedStatus;
 | |
|   String? get selectedModel => _selectedModel;
 | |
|   String get searchQuery => _searchQuery;
 | |
| 
 | |
|   /// Stream des équipements avec filtres appliqués
 | |
|   Stream<List<EquipmentModel>> get equipmentStream {
 | |
|     return _service.getEquipment(
 | |
|       category: _selectedCategory,
 | |
|       status: _selectedStatus,
 | |
|       model: _selectedModel,
 | |
|       searchQuery: _searchQuery,
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   /// Charger tous les modèles uniques
 | |
|   Future<void> loadModels() async {
 | |
|     try {
 | |
|       _models = await _service.getAllModels();
 | |
|       notifyListeners();
 | |
|     } catch (e) {
 | |
|       print('Error loading models: $e');
 | |
|       rethrow;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /// Charger toutes les marques uniques
 | |
|   Future<void> loadBrands() async {
 | |
|     try {
 | |
|       _brands = await _service.getAllBrands();
 | |
|       notifyListeners();
 | |
|     } catch (e) {
 | |
|       print('Error loading brands: $e');
 | |
|       rethrow;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /// Charger les modèles filtrés par marque
 | |
|   Future<List<String>> loadModelsByBrand(String brand) async {
 | |
|     try {
 | |
|       return await _service.getModelsByBrand(brand);
 | |
|     } catch (e) {
 | |
|       print('Error loading models by brand: $e');
 | |
|       rethrow;
 | |
|     }
 | |
|   }
 | |
| 
 | |
| 
 | |
|   /// Ajouter un équipement
 | |
|   Future<void> addEquipment(EquipmentModel equipment) async {
 | |
|     try {
 | |
|       await _service.createEquipment(equipment);
 | |
| 
 | |
|       // Recharger les modèles si un nouveau modèle a été ajouté
 | |
|       if (equipment.model != null && !_models.contains(equipment.model)) {
 | |
|         await loadModels();
 | |
|       }
 | |
|     } catch (e) {
 | |
|       print('Error adding equipment: $e');
 | |
|       rethrow;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /// Mettre à jour un équipement
 | |
|   Future<void> updateEquipment(String id, Map<String, dynamic> data) async {
 | |
|     try {
 | |
|       await _service.updateEquipment(id, data);
 | |
| 
 | |
|       // Recharger les modèles si le modèle a changé
 | |
|       if (data.containsKey('model')) {
 | |
|         await loadModels();
 | |
|       }
 | |
|     } catch (e) {
 | |
|       print('Error updating equipment: $e');
 | |
|       rethrow;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /// Supprimer un équipement
 | |
|   Future<void> deleteEquipment(String id) async {
 | |
|     try {
 | |
|       await _service.deleteEquipment(id);
 | |
|     } catch (e) {
 | |
|       print('Error deleting equipment: $e');
 | |
|       rethrow;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /// Récupérer un équipement par ID
 | |
|   Future<EquipmentModel?> getEquipmentById(String id) async {
 | |
|     try {
 | |
|       return await _service.getEquipmentById(id);
 | |
|     } catch (e) {
 | |
|       print('Error getting equipment: $e');
 | |
|       rethrow;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /// Trouver des alternatives disponibles
 | |
|   Future<List<EquipmentModel>> findAlternatives(
 | |
|     String model,
 | |
|     DateTime startDate,
 | |
|     DateTime endDate,
 | |
|   ) async {
 | |
|     try {
 | |
|       return await _service.findAlternatives(model, startDate, endDate);
 | |
|     } catch (e) {
 | |
|       print('Error finding alternatives: $e');
 | |
|       rethrow;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /// Vérifier la disponibilité d'un équipement
 | |
|   Future<List<String>> checkAvailability(
 | |
|     String equipmentId,
 | |
|     DateTime startDate,
 | |
|     DateTime endDate,
 | |
|   ) async {
 | |
|     try {
 | |
|       return await _service.checkAvailability(equipmentId, startDate, endDate);
 | |
|     } catch (e) {
 | |
|       print('Error checking availability: $e');
 | |
|       rethrow;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /// Mettre à jour le stock d'un consommable
 | |
|   Future<void> updateStock(String id, int quantityChange) async {
 | |
|     try {
 | |
|       await _service.updateStock(id, quantityChange);
 | |
|     } catch (e) {
 | |
|       print('Error updating stock: $e');
 | |
|       rethrow;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /// Vérifier les stocks critiques
 | |
|   Future<void> checkCriticalStock() async {
 | |
|     try {
 | |
|       await _service.checkCriticalStock();
 | |
|     } catch (e) {
 | |
|       print('Error checking critical stock: $e');
 | |
|       rethrow;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /// Générer les données du QR code
 | |
|   String generateQRCodeData(String equipmentId) {
 | |
|     return _service.generateQRCodeData(equipmentId);
 | |
|   }
 | |
| 
 | |
|   /// Vérifier si un ID est unique
 | |
|   Future<bool> isIdUnique(String id) async {
 | |
|     try {
 | |
|       return await _service.isIdUnique(id);
 | |
|     } catch (e) {
 | |
|       print('Error checking ID uniqueness: $e');
 | |
|       rethrow;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   // === FILTRES ===
 | |
| 
 | |
|   /// Définir la catégorie sélectionnée
 | |
|   void setSelectedCategory(EquipmentCategory? category) {
 | |
|     _selectedCategory = category;
 | |
|     notifyListeners();
 | |
|   }
 | |
| 
 | |
|   /// Définir le statut sélectionné
 | |
|   void setSelectedStatus(EquipmentStatus? status) {
 | |
|     _selectedStatus = status;
 | |
|     notifyListeners();
 | |
|   }
 | |
| 
 | |
|   /// Définir le modèle sélectionné
 | |
|   void setSelectedModel(String? model) {
 | |
|     _selectedModel = model;
 | |
|     notifyListeners();
 | |
|   }
 | |
| 
 | |
|   /// Définir la recherche
 | |
|   void setSearchQuery(String query) {
 | |
|     _searchQuery = query;
 | |
|     notifyListeners();
 | |
|   }
 | |
| 
 | |
|   /// Réinitialiser tous les filtres
 | |
|   void resetFilters() {
 | |
|     _selectedCategory = null;
 | |
|     _selectedStatus = null;
 | |
|     _selectedModel = null;
 | |
|     _searchQuery = '';
 | |
|     notifyListeners();
 | |
|   }
 | |
| }
 | |
| 
 | 
