597 lines
25 KiB
Dart
597 lines
25 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:manager_app/constants.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'showNewOrUpdateProgrammeBlock.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/message_notification.dart';
|
|
import 'package:manager_app/Screens/Configurations/Section/SubSection/Parcours/parcours_config.dart';
|
|
import 'package:manager_app/Components/dropDown_input_container.dart';
|
|
import 'showNewOrUpdateMapAnnotation.dart';
|
|
|
|
class EventConfig extends StatefulWidget {
|
|
final SectionEventDTO initialValue;
|
|
final ValueChanged<SectionEventDTO> onChanged;
|
|
|
|
const EventConfig({
|
|
Key? key,
|
|
required this.initialValue,
|
|
required this.onChanged,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
_EventConfigState createState() => _EventConfigState();
|
|
}
|
|
|
|
class _EventConfigState extends State<EventConfig> {
|
|
late SectionEventDTO eventDTO;
|
|
List<SectionDTO> availableMaps = [];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
eventDTO = widget.initialValue;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_loadProgrammeBlocks();
|
|
_loadGlobalAnnotations();
|
|
_loadAvailableMaps();
|
|
});
|
|
}
|
|
|
|
Future<void> _loadProgrammeBlocks() async {
|
|
if (eventDTO.id == null || !mounted) return;
|
|
final appContext = Provider.of<AppContext>(context, listen: false);
|
|
final api = (appContext.getContext() as ManagerAppContext).clientAPI!.sectionEventApi!;
|
|
try {
|
|
final blocks = await api.sectionEventGetAllProgrammeBlockFromSection(eventDTO.id!);
|
|
if (blocks == null || !mounted) return;
|
|
setState(() {
|
|
eventDTO.programme = blocks;
|
|
});
|
|
} catch (e) {
|
|
// Silently keep initial value on error
|
|
}
|
|
}
|
|
|
|
Future<void> _loadGlobalAnnotations() async {
|
|
if (eventDTO.id == null || !mounted) return;
|
|
final appContext = Provider.of<AppContext>(context, listen: false);
|
|
final api = (appContext.getContext() as ManagerAppContext).clientAPI!.sectionEventApi!;
|
|
try {
|
|
final annotations = await api.sectionEventGetGlobalMapAnnotations(eventDTO.id!);
|
|
if (annotations == null || !mounted) return;
|
|
setState(() {
|
|
eventDTO.globalMapAnnotations = annotations;
|
|
});
|
|
} catch (e) {
|
|
// Silently keep initial value on error
|
|
}
|
|
}
|
|
|
|
Future<void> _loadAvailableMaps() async {
|
|
if (eventDTO.configurationId == null || !mounted) return;
|
|
final appContext = Provider.of<AppContext>(context, listen: false);
|
|
final api = (appContext.getContext() as ManagerAppContext).clientAPI!.sectionApi!;
|
|
try {
|
|
final sections = await api.sectionGetFromConfiguration(eventDTO.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: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: ListTile(
|
|
title: Text("Date de début"),
|
|
subtitle: Text(eventDTO.startDate != null
|
|
? DateFormat('dd/MM/yyyy HH:mm')
|
|
.format(eventDTO.startDate!.toLocal())
|
|
: "Non définie"),
|
|
trailing: Icon(Icons.calendar_today),
|
|
onTap: () async {
|
|
DateTime initialDate = eventDTO.startDate?.toLocal() ?? DateTime.now();
|
|
if (initialDate.isBefore(DateTime(2000))) {
|
|
initialDate = DateTime.now();
|
|
}
|
|
DateTime? picked = await showDatePicker(
|
|
context: context,
|
|
initialDate: initialDate,
|
|
firstDate: DateTime(2000),
|
|
lastDate: DateTime(2100),
|
|
builder: (context, child) {
|
|
return Theme(
|
|
data: Theme.of(context).copyWith(
|
|
colorScheme: ColorScheme.light(
|
|
primary: kPrimaryColor,
|
|
onPrimary: kWhite,
|
|
onSurface: kSecond,
|
|
),
|
|
),
|
|
child: child!,
|
|
);
|
|
},
|
|
);
|
|
if (picked != null) {
|
|
TimeOfDay? time = await showTimePicker(
|
|
context: context,
|
|
initialTime: TimeOfDay.fromDateTime(
|
|
eventDTO.startDate?.toLocal() ?? DateTime.now()),
|
|
builder: (context, child) {
|
|
return Theme(
|
|
data: Theme.of(context).copyWith(
|
|
colorScheme: ColorScheme.light(
|
|
primary: kPrimaryColor,
|
|
onPrimary: kWhite,
|
|
onSurface: kSecond,
|
|
),
|
|
),
|
|
child: child!,
|
|
);
|
|
},
|
|
);
|
|
if (time != null) {
|
|
setState(() {
|
|
eventDTO.startDate = DateTime(picked.year,
|
|
picked.month, picked.day, time.hour, time.minute);
|
|
widget.onChanged(eventDTO);
|
|
});
|
|
}
|
|
}
|
|
},
|
|
),
|
|
),
|
|
Expanded(
|
|
child: ListTile(
|
|
title: Text("Date de fin"),
|
|
subtitle: Text(eventDTO.endDate != null
|
|
? DateFormat('dd/MM/yyyy HH:mm').format(eventDTO.endDate!.toLocal())
|
|
: "Non définie"),
|
|
trailing: Icon(Icons.calendar_today),
|
|
onTap: () async {
|
|
DateTime initialDate = eventDTO.endDate?.toLocal() ??
|
|
DateTime.now().add(Duration(days: 1));
|
|
if (initialDate.isBefore(DateTime(2000))) {
|
|
initialDate = DateTime.now().add(Duration(days: 1));
|
|
}
|
|
DateTime? picked = await showDatePicker(
|
|
context: context,
|
|
initialDate: initialDate,
|
|
firstDate: DateTime(2000),
|
|
lastDate: DateTime(2100),
|
|
builder: (context, child) {
|
|
return Theme(
|
|
data: Theme.of(context).copyWith(
|
|
colorScheme: ColorScheme.light(
|
|
primary: kPrimaryColor,
|
|
onPrimary: kWhite,
|
|
onSurface: kSecond,
|
|
),
|
|
),
|
|
child: child!,
|
|
);
|
|
},
|
|
);
|
|
if (picked != null) {
|
|
TimeOfDay? time = await showTimePicker(
|
|
context: context,
|
|
initialTime: TimeOfDay.fromDateTime(eventDTO.endDate?.toLocal() ??
|
|
DateTime.now().add(Duration(days: 1))),
|
|
builder: (context, child) {
|
|
return Theme(
|
|
data: Theme.of(context).copyWith(
|
|
colorScheme: ColorScheme.light(
|
|
primary: kPrimaryColor,
|
|
onPrimary: kWhite,
|
|
onSurface: kSecond,
|
|
),
|
|
),
|
|
child: child!,
|
|
);
|
|
},
|
|
);
|
|
if (time != null) {
|
|
setState(() {
|
|
eventDTO.endDate = DateTime(picked.year, picked.month,
|
|
picked.day, time.hour, time.minute);
|
|
widget.onChanged(eventDTO);
|
|
});
|
|
}
|
|
}
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Divider(),
|
|
// --- Carte de base ---
|
|
_buildBaseSectionMapSection(),
|
|
Divider(),
|
|
// --- Annotations globales ---
|
|
_buildGlobalAnnotationsSection(),
|
|
Divider(),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text("Programme",
|
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
|
ElevatedButton.icon(
|
|
icon: Icon(Icons.add),
|
|
label: Text("Ajouter un bloc"),
|
|
onPressed: () {
|
|
final appContext =
|
|
Provider.of<AppContext>(context, listen: false);
|
|
showNewOrUpdateProgrammeBlock(
|
|
context,
|
|
null,
|
|
(newBlock) async {
|
|
try {
|
|
final programmeBlockDTO = ProgrammeBlockDTO(
|
|
id: newBlock.id,
|
|
title: newBlock.title,
|
|
description: newBlock.description,
|
|
startTime: newBlock.startTime,
|
|
endTime: newBlock.endTime,
|
|
);
|
|
|
|
final createdBlockDTO =
|
|
await (appContext.getContext() as ManagerAppContext)
|
|
.clientAPI!
|
|
.sectionEventApi!
|
|
.sectionEventCreateProgrammeBlock(
|
|
eventDTO.id!, programmeBlockDTO);
|
|
|
|
if (createdBlockDTO != null) {
|
|
final createdBlock = ProgrammeBlock(
|
|
id: createdBlockDTO.id,
|
|
title: createdBlockDTO.title,
|
|
description: createdBlockDTO.description,
|
|
startTime: createdBlockDTO.startTime,
|
|
endTime: createdBlockDTO.endTime,
|
|
);
|
|
setState(() {
|
|
eventDTO.programme = [
|
|
...(eventDTO.programme ?? []),
|
|
createdBlock
|
|
];
|
|
widget.onChanged(eventDTO);
|
|
});
|
|
showNotification(
|
|
kSuccess,
|
|
kWhite,
|
|
'Bloc de programme créé avec succès',
|
|
context,
|
|
null);
|
|
}
|
|
} catch (e) {
|
|
showNotification(
|
|
kError,
|
|
kWhite,
|
|
'Erreur lors de la création du bloc',
|
|
context,
|
|
null);
|
|
}
|
|
},
|
|
);
|
|
},
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: kSuccess, foregroundColor: kWhite),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Container(
|
|
height: 600,
|
|
child: (eventDTO.programme == null || eventDTO.programme!.isEmpty)
|
|
? Center(
|
|
child: Text("Aucun bloc de programme défini",
|
|
style: TextStyle(fontStyle: FontStyle.italic)))
|
|
: ListView.builder(
|
|
itemCount: eventDTO.programme!.length,
|
|
itemBuilder: (context, index) {
|
|
final block = eventDTO.programme![index];
|
|
return Card(
|
|
margin: EdgeInsets.symmetric(horizontal: 16, vertical: 4),
|
|
child: ListTile(
|
|
title: block.title != null && block.title!.isNotEmpty
|
|
? HtmlWidget(
|
|
block.title!
|
|
.firstWhere(
|
|
(t) => t.language == 'FR',
|
|
orElse: () => block.title![0])
|
|
.value ??
|
|
"Bloc ${index + 1}",
|
|
)
|
|
: Text("Bloc ${index + 1}"),
|
|
subtitle: Text(
|
|
"${block.startTime != null ? DateFormat('HH:mm').format(block.startTime!.toLocal()) : '??'} - ${block.endTime != null ? DateFormat('HH:mm').format(block.endTime!.toLocal()) : '??'}"),
|
|
trailing: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
IconButton(
|
|
icon: Icon(Icons.edit, color: kPrimaryColor),
|
|
onPressed: () {
|
|
final appContext = Provider.of<AppContext>(
|
|
context,
|
|
listen: false);
|
|
showNewOrUpdateProgrammeBlock(
|
|
context,
|
|
block,
|
|
(updatedBlock) async {
|
|
try {
|
|
final programmeBlockDTO = ProgrammeBlockDTO(
|
|
id: updatedBlock.id,
|
|
title: updatedBlock.title,
|
|
description: updatedBlock.description,
|
|
startTime: updatedBlock.startTime,
|
|
endTime: updatedBlock.endTime,
|
|
);
|
|
|
|
final resultDTO =
|
|
await (appContext.getContext()
|
|
as ManagerAppContext)
|
|
.clientAPI!
|
|
.sectionEventApi!
|
|
.sectionEventUpdateProgrammeBlock(
|
|
programmeBlockDTO);
|
|
|
|
if (resultDTO != null) {
|
|
final result = ProgrammeBlock(
|
|
id: resultDTO.id,
|
|
title: resultDTO.title,
|
|
description: resultDTO.description,
|
|
startTime: resultDTO.startTime,
|
|
endTime: resultDTO.endTime,
|
|
);
|
|
setState(() {
|
|
eventDTO.programme![index] = result;
|
|
widget.onChanged(eventDTO);
|
|
});
|
|
showNotification(
|
|
kSuccess,
|
|
kWhite,
|
|
'Bloc mis à jour avec succès',
|
|
context,
|
|
null);
|
|
}
|
|
} catch (e) {
|
|
showNotification(
|
|
kError,
|
|
kWhite,
|
|
'Erreur lors de la mise à jour du bloc',
|
|
context,
|
|
null);
|
|
}
|
|
},
|
|
);
|
|
},
|
|
),
|
|
IconButton(
|
|
icon: Icon(Icons.delete, color: kError),
|
|
onPressed: () async {
|
|
final appContext = Provider.of<AppContext>(
|
|
context,
|
|
listen: false);
|
|
try {
|
|
if (block.id != null) {
|
|
await (appContext.getContext()
|
|
as ManagerAppContext)
|
|
.clientAPI!
|
|
.sectionEventApi!
|
|
.sectionEventDeleteProgrammeBlock(
|
|
block.id!);
|
|
}
|
|
|
|
setState(() {
|
|
eventDTO.programme!.removeAt(index);
|
|
widget.onChanged(eventDTO);
|
|
});
|
|
showNotification(
|
|
kSuccess,
|
|
kWhite,
|
|
'Bloc supprimé avec succès',
|
|
context,
|
|
null);
|
|
} catch (e) {
|
|
showNotification(
|
|
kError,
|
|
kWhite,
|
|
'Erreur lors de la suppression du bloc',
|
|
context,
|
|
null);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
Divider(),
|
|
// --- Parcours ---
|
|
if (eventDTO.id != null)
|
|
ParcoursConfig(
|
|
initialValue: const [],
|
|
parentId: eventDTO.id!,
|
|
isEvent: true,
|
|
onChanged: (paths) {
|
|
// parcours are managed independently, no DTO update needed here
|
|
},
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildBaseSectionMapSection() {
|
|
final mapItems = <String>['Aucune', ...availableMaps.map((m) => m.label ?? m.id ?? '')];
|
|
final currentValue = eventDTO.baseSectionMapId != null
|
|
? (availableMaps
|
|
.where((m) => m.id == eventDTO.baseSectionMapId)
|
|
.map((m) => m.label ?? m.id ?? '')
|
|
.firstOrNull ??
|
|
'Aucune')
|
|
: 'Aucune';
|
|
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text("Carte de base",
|
|
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
|
DropDownInputContainer(
|
|
label: "Carte :",
|
|
values: mapItems,
|
|
initialValue: currentValue,
|
|
onChange: (val) {
|
|
setState(() {
|
|
if (val == 'Aucune') {
|
|
eventDTO.baseSectionMapId = null;
|
|
} else {
|
|
final match = availableMaps.firstWhere(
|
|
(m) => (m.label ?? m.id ?? '') == val,
|
|
orElse: () => availableMaps.first);
|
|
eventDTO.baseSectionMapId = match.id;
|
|
}
|
|
widget.onChanged(eventDTO);
|
|
});
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildGlobalAnnotationsSection() {
|
|
final annotations = eventDTO.globalMapAnnotations ?? [];
|
|
final appContext = Provider.of<AppContext>(context, listen: false);
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
const Text("Annotations globales",
|
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
|
ElevatedButton.icon(
|
|
icon: const Icon(Icons.add),
|
|
label: const Text("Ajouter une annotation"),
|
|
onPressed: eventDTO.id == null
|
|
? null
|
|
: () {
|
|
showNewOrUpdateMapAnnotation(context, null,
|
|
(newAnnotation) async {
|
|
try {
|
|
final created = await (appContext.getContext()
|
|
as ManagerAppContext)
|
|
.clientAPI!
|
|
.sectionEventApi!
|
|
.sectionEventCreateGlobalMapAnnotation(
|
|
eventDTO.id!, newAnnotation);
|
|
if (created != null && mounted) {
|
|
setState(() {
|
|
eventDTO.globalMapAnnotations = [
|
|
...annotations,
|
|
created
|
|
];
|
|
});
|
|
showNotification(kSuccess, kWhite,
|
|
'Annotation créée avec succès', context, null);
|
|
}
|
|
} catch (e) {
|
|
showNotification(kError, kWhite,
|
|
'Erreur lors de la création de l\'annotation',
|
|
context, null);
|
|
}
|
|
});
|
|
},
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: kSuccess,
|
|
foregroundColor: kWhite,
|
|
padding:
|
|
const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(10)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 8),
|
|
if (annotations.isEmpty)
|
|
const Padding(
|
|
padding: EdgeInsets.only(top: 8),
|
|
child: Text("Aucune annotation globale",
|
|
style: TextStyle(fontStyle: FontStyle.italic)),
|
|
)
|
|
else
|
|
...annotations.asMap().entries.map((entry) {
|
|
final idx = entry.key;
|
|
final ann = entry.value;
|
|
final labelText = ann.label != null && ann.label!.isNotEmpty
|
|
? (ann.label!.firstWhere((t) => t.language == 'FR',
|
|
orElse: () => ann.label![0])
|
|
.value ?? 'Annotation ${idx + 1}')
|
|
: 'Annotation ${idx + 1}';
|
|
return Card(
|
|
margin:
|
|
const EdgeInsets.symmetric(horizontal: 0, vertical: 4),
|
|
child: ListTile(
|
|
leading: const Icon(Icons.place, color: kPrimaryColor),
|
|
title: Text(labelText),
|
|
trailing: IconButton(
|
|
icon: const Icon(Icons.delete, color: kError),
|
|
onPressed: () async {
|
|
try {
|
|
if (ann.id != null) {
|
|
await (appContext.getContext() as ManagerAppContext)
|
|
.clientAPI!
|
|
.sectionEventApi!
|
|
.sectionEventDeleteMapAnnotation(ann.id!);
|
|
}
|
|
if (mounted) {
|
|
setState(() {
|
|
final updated = List<MapAnnotationDTO>.from(
|
|
eventDTO.globalMapAnnotations ?? []);
|
|
updated.removeAt(idx);
|
|
eventDTO.globalMapAnnotations = updated;
|
|
});
|
|
showNotification(kSuccess, kWhite,
|
|
'Annotation supprimée', context, null);
|
|
}
|
|
} catch (e) {
|
|
showNotification(kError, kWhite,
|
|
'Erreur lors de la suppression', context, null);
|
|
}
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|