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/constants.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: [ // ── Où se déroule le parcours ? ── Padding( padding: const EdgeInsets.fromLTRB(16, 16, 16, 0), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text("Où se déroule le parcours ?", style: TextStyle(fontWeight: FontWeight.w600)), RadioGroup( groupValue: parcoursDTO.showMap ?? true, onChanged: (val) { setState(() => parcoursDTO.showMap = val); widget.onChanged(parcoursDTO); }, child: Column( children: [ RadioListTile( dense: true, contentPadding: EdgeInsets.zero, activeColor: kPrimaryColor, value: false, title: Text("En salle", style: TextStyle(fontSize: 14)), subtitle: Text( "Une suite d'étapes dans un espace restreint, sans carte ni géolocalisation.", style: TextStyle(fontSize: 12, color: Colors.grey[600]), ), ), RadioListTile( dense: true, contentPadding: EdgeInsets.zero, activeColor: kPrimaryColor, value: true, title: Text("Sur le terrain", style: TextStyle(fontSize: 14)), subtitle: Text( "Le visiteur se déplace : carte et position sur chaque étape.", style: TextStyle(fontSize: 12, color: Colors.grey[600]), ), ), ], ), ), ], ), ), if (parcoursDTO.showMap == true) 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: availableMaps.isEmpty ? null : (val) { setState( () => parcoursDTO.baseSectionMapId = val); widget.onChanged(parcoursDTO); }, ), if (availableMaps.isEmpty) Text( "Aucune section Carte dans cette configuration — créez-en une pour pouvoir la réutiliser ici.", style: TextStyle( fontSize: 12, color: Colors.grey[600]), ), ], ), ), ), ], ), ), // ── Liste des parcours ── if (parcoursDTO.id != null) SizedBox( height: 500, child: ParcoursConfig( initialValue: parcoursDTO.guidedPaths ?? [], parentId: parcoursDTO.id!, isEvent: false, isParcours: true, isGeolocated: parcoursDTO.showMap ?? 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]), ), ), ], ); } }