import 'package:flutter/material.dart'; import 'package:manager_api_new/api.dart'; import 'package:provider/provider.dart'; import 'package:manager_app/app_context.dart'; import 'package:manager_app/Models/managerContext.dart'; import 'package:manager_app/Components/check_input_container.dart'; import 'package:manager_app/Screens/Configurations/Section/SubSection/Parcours/parcours_config.dart'; class SectionParcoursConfig extends StatefulWidget { final ParcoursDTO initialValue; final ValueChanged onChanged; const SectionParcoursConfig({ Key? key, required this.initialValue, required this.onChanged, }) : super(key: key); @override _SectionParcoursConfigState createState() => _SectionParcoursConfigState(); } class _SectionParcoursConfigState extends State { late ParcoursDTO parcoursDTO; List availableMaps = []; @override void initState() { super.initState(); parcoursDTO = widget.initialValue; WidgetsBinding.instance.addPostFrameCallback((_) => _loadAvailableMaps()); } Future _loadAvailableMaps() async { if (parcoursDTO.configurationId == null || !mounted) return; final appContext = Provider.of(context, listen: false); final api = (appContext.getContext() as ManagerAppContext).clientAPI!.sectionApi!; try { final sections = await api.sectionGetFromConfiguration(parcoursDTO.configurationId!); if (sections == null || !mounted) return; setState(() { availableMaps = sections.where((s) => s.type == SectionType.Map).toList(); }); } catch (e) { // Silently keep empty on error } } @override Widget build(BuildContext context) { return Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ // ── Options carte ── Padding( padding: const EdgeInsets.fromLTRB(16, 16, 16, 0), child: Row( children: [ Expanded( child: CheckInputContainer( label: "Afficher la carte", isChecked: parcoursDTO.showMap ?? true, onChanged: (val) { setState(() => parcoursDTO.showMap = val); widget.onChanged(parcoursDTO); }, ), ), if (parcoursDTO.showMap == true && availableMaps.isNotEmpty) Expanded( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 8), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text("Carte de base (optionnel)", style: TextStyle(fontWeight: FontWeight.w500)), DropdownButton( isExpanded: true, value: parcoursDTO.baseSectionMapId, items: [ DropdownMenuItem( value: null, child: Text("Aucune")), ...availableMaps.map((m) => DropdownMenuItem( value: m.id, child: Text(m.label ?? m.id ?? ''))), ], onChanged: (val) { setState(() => parcoursDTO.baseSectionMapId = val); widget.onChanged(parcoursDTO); }, ), ], ), ), ), ], ), ), // ── Liste des parcours ── if (parcoursDTO.id != null) SizedBox( height: 500, child: ParcoursConfig( initialValue: parcoursDTO.guidedPaths ?? [], parentId: parcoursDTO.id!, isEvent: false, isParcours: true, onChanged: (paths) { setState(() => parcoursDTO.guidedPaths = paths); widget.onChanged(parcoursDTO); }, ), ) else Padding( padding: const EdgeInsets.all(16), child: Text( "Sauvegardez d'abord la section pour gérer les parcours.", style: TextStyle(fontStyle: FontStyle.italic, color: Colors.grey[600]), ), ), ], ); } }