diff --git a/lib/Views/HandleProduct.dart b/lib/Views/HandleProduct.dart index 8867e41..8a8c58b 100644 --- a/lib/Views/HandleProduct.dart +++ b/lib/Views/HandleProduct.dart @@ -60,7 +60,141 @@ class _ProductManagementPageState extends State { String _importStatusText = ''; // Ajoutez ces méthodes à la classe _ProductManagementPageState - void _resetImportState() { + +void _resetImportState() { + setState(() { + _isImporting = false; + _importProgress = 0.0; + _importStatusText = ''; + }); +} + +void _showExcelCompatibilityError() { + Get.dialog( + AlertDialog( + title: const Text('Fichier Excel incompatible'), + content: const Text( + 'Ce fichier Excel contient des éléments qui ne sont pas compatibles avec notre système d\'importation.\n\n' + 'Solutions recommandées :\n' + '• Téléchargez notre modèle Excel et copiez-y vos données\n' + '• Ou exportez votre fichier en format simple: Classeur Excel .xlsx depuis Excel\n' + '• Ou créez un nouveau fichier Excel simple sans formatage complexe' + ), + actions: [ + TextButton( + onPressed: () => Get.back(), + child: const Text('Annuler'), + ), + TextButton( + onPressed: () { + Get.back(); + _downloadExcelTemplate(); + }, + child: const Text('Télécharger modèle'), + style: TextButton.styleFrom( + backgroundColor: Colors.green, + foregroundColor: Colors.white, + ), + ), + ], + ), + ); +} + +Future _downloadExcelTemplate() async { + try { + // Créez un nouveau fichier Excel + final excel = Excel.createExcel(); + + // Supprimez la feuille par défaut si elle existe + excel.delete('Sheet1'); + + // Créez une nouvelle feuille nommée "Produits" + final sheet = excel['Produits']; + + // Ajoutez les en-têtes + final headers = ['Nom', 'Prix', 'Catégorie', 'Description', 'Stock']; + for (int i = 0; i < headers.length; i++) { + final cell = sheet.cell(CellIndex.indexByColumnRow(columnIndex: i, rowIndex: 0)); + cell.value = headers[i]; + cell.cellStyle = CellStyle( + bold: true, + backgroundColorHex: '#E8F4FD', + ); + } + + // Ajoutez des exemples de données + final examples = [ + ['Croissant', '1.50', 'Sucré', 'Délicieux croissant beurré', '20'], + ['Sandwich jambon', '4.00', 'Salé', 'Sandwich fait maison', '15'], + ['Jus d\'orange', '2.50', 'Jus', 'Jus d\'orange frais', '30'], + ['Gâteau chocolat', '18.00', 'Gateaux', 'Gâteau au chocolat portion 8 personnes', '5'], + ]; + + for (int row = 0; row < examples.length; row++) { + for (int col = 0; col < examples[row].length; col++) { + final cell = sheet.cell(CellIndex.indexByColumnRow(columnIndex: col, rowIndex: row + 1)); + cell.value = examples[row][col]; + } + } + + // Définissez la largeur des colonnes + sheet.setColWidth(0, 20); + sheet.setColWidth(1, 10); + sheet.setColWidth(2, 15); + sheet.setColWidth(3, 30); + sheet.setColWidth(4, 10); + + // Sauvegardez le fichier Excel + final bytes = excel.save(); + + if (bytes == null) { + Get.snackbar('Erreur', 'Impossible de créer le fichier modèle'); + return; + } + + // Demandez à l'utilisateur où sauvegarder le fichier + final String? outputFile = await FilePicker.platform.saveFile( + fileName: 'modele_import_produits.xlsx', + allowedExtensions: ['xlsx'], + type: FileType.custom, + ); + + if (outputFile != null) { + try { + // Écrivez les données dans le fichier + await File(outputFile).writeAsBytes(bytes); + Get.snackbar( + 'Succès', + 'Modèle téléchargé avec succès\n$outputFile', + duration: const Duration(seconds: 4), + backgroundColor: Colors.green, + colorText: Colors.white, + ); + } catch (e) { + Get.snackbar('Erreur', 'Impossible d\'écrire le fichier: $e'); + } + } + } catch (e) { + Get.snackbar('Erreur', 'Erreur lors de la création du modèle: $e'); + debugPrint('Erreur création modèle Excel: $e'); + } +} + + +Future _importFromExcel() async { + try { + final result = await FilePicker.platform.pickFiles( + type: FileType.custom, + allowedExtensions: ['xlsx', 'xls','csv'], + allowMultiple: false, + ); + + if (result == null || result.files.isEmpty) { + Get.snackbar('Annulé', 'Aucun fichier sélectionné'); + return; + } + setState(() { _isImporting = false; _importProgress = 0.0;