2026-05-04 15:36:42 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2026-05-07 16:28:01 +02:00
|
|
|
import 'package:flux/core/widgets/shared_forms/model_section.dart';
|
2026-05-15 10:12:05 +02:00
|
|
|
import 'package:flux/features/master_data/providers/blocs/provider_list_cubit.dart';
|
2026-06-02 13:12:21 +02:00
|
|
|
import 'package:flux/features/master_data/providers/models/provider_model_extensions.dart';
|
2026-05-08 12:28:14 +02:00
|
|
|
import 'package:flux/features/operations/blocs/operation_form_cubit.dart';
|
2026-05-04 15:36:42 +02:00
|
|
|
import 'package:flux/features/operations/models/operation_model.dart';
|
|
|
|
|
|
2026-05-15 10:12:05 +02:00
|
|
|
class OperationDetailsSection extends StatelessWidget {
|
2026-05-04 15:36:42 +02:00
|
|
|
final OperationModel? currentOp;
|
|
|
|
|
final String currentType;
|
|
|
|
|
final TextEditingController freeTextSubtypeController;
|
|
|
|
|
final TextEditingController freeTextDescriptionController;
|
|
|
|
|
final Widget durationQuickPicks;
|
|
|
|
|
|
2026-05-15 10:12:05 +02:00
|
|
|
const OperationDetailsSection({
|
2026-05-04 15:36:42 +02:00
|
|
|
super.key,
|
|
|
|
|
required this.currentOp,
|
|
|
|
|
required this.currentType,
|
|
|
|
|
required this.freeTextSubtypeController,
|
|
|
|
|
required this.freeTextDescriptionController,
|
|
|
|
|
required this.durationQuickPicks,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
void _showProviderModal(BuildContext context, String operationType) {
|
2026-05-09 09:50:20 +02:00
|
|
|
final OperationFormCubit cubit = context.read<OperationFormCubit>();
|
2026-05-04 15:36:42 +02:00
|
|
|
showModalBottomSheet(
|
|
|
|
|
context: context,
|
|
|
|
|
isScrollControlled: true,
|
|
|
|
|
shape: const RoundedRectangleBorder(
|
|
|
|
|
borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
|
|
|
|
|
),
|
|
|
|
|
builder: (modalContext) {
|
|
|
|
|
return DraggableScrollableSheet(
|
|
|
|
|
initialChildSize: 0.5,
|
|
|
|
|
minChildSize: 0.4,
|
|
|
|
|
maxChildSize: 0.8,
|
|
|
|
|
expand: false,
|
|
|
|
|
builder: (_, scrollController) {
|
|
|
|
|
return Column(
|
|
|
|
|
children: [
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
|
child: Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
|
|
|
|
'Seleziona Gestore',
|
|
|
|
|
style: Theme.of(context).textTheme.titleLarge,
|
|
|
|
|
),
|
|
|
|
|
IconButton(
|
|
|
|
|
icon: const Icon(Icons.close),
|
|
|
|
|
onPressed: () => Navigator.pop(modalContext),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const Divider(),
|
|
|
|
|
Expanded(
|
2026-05-15 10:12:05 +02:00
|
|
|
child: BlocBuilder<ProviderListCubit, ProviderListState>(
|
2026-05-04 15:36:42 +02:00
|
|
|
builder: (context, state) {
|
2026-05-15 10:12:05 +02:00
|
|
|
if (state.status == ProviderListStatus.loading) {
|
2026-05-04 15:36:42 +02:00
|
|
|
return const Center(child: CircularProgressIndicator());
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 13:12:21 +02:00
|
|
|
// 🥷 IL TOCCO DEL NINJA: Filtriamo usando direttamente l'Extension sul Modello!
|
2026-05-15 10:12:05 +02:00
|
|
|
final filteredProviders = state.providers.where((p) {
|
2026-06-02 13:12:21 +02:00
|
|
|
return p.supportsOperation(operationType) && p.isActive;
|
2026-05-15 10:12:05 +02:00
|
|
|
}).toList();
|
2026-05-04 15:36:42 +02:00
|
|
|
|
|
|
|
|
if (filteredProviders.isEmpty) {
|
|
|
|
|
return const Center(
|
|
|
|
|
child: Text(
|
|
|
|
|
'Nessun gestore compatibile con questo servizio.',
|
|
|
|
|
style: TextStyle(color: Colors.grey),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 09:50:20 +02:00
|
|
|
return BlocProvider.value(
|
|
|
|
|
value: cubit,
|
|
|
|
|
child: ListView.builder(
|
|
|
|
|
controller: scrollController,
|
|
|
|
|
itemCount: filteredProviders.length,
|
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
|
final provider = filteredProviders[index];
|
|
|
|
|
return ListTile(
|
|
|
|
|
leading: const Icon(Icons.business),
|
|
|
|
|
title: Text(
|
|
|
|
|
provider.name,
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
),
|
2026-05-04 15:36:42 +02:00
|
|
|
),
|
2026-05-15 10:12:05 +02:00
|
|
|
// Facoltativo: mostra i ruoli sotto al nome
|
|
|
|
|
subtitle: Text(
|
|
|
|
|
provider.roles
|
|
|
|
|
.map((r) => r.displayValue)
|
|
|
|
|
.join(', '),
|
|
|
|
|
style: const TextStyle(fontSize: 12),
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
),
|
2026-05-09 09:50:20 +02:00
|
|
|
onTap: () {
|
2026-06-03 19:16:15 +02:00
|
|
|
context
|
|
|
|
|
.read<OperationFormCubit>()
|
|
|
|
|
.updateProvider(provider);
|
2026-05-09 09:50:20 +02:00
|
|
|
Navigator.pop(modalContext);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
2026-05-04 15:36:42 +02:00
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
final theme = Theme.of(context);
|
|
|
|
|
|
|
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
// PROVIDER (Mostrato quasi sempre)
|
|
|
|
|
ListTile(
|
|
|
|
|
title: const Text('Seleziona Gestore'),
|
|
|
|
|
subtitle: Text(
|
2026-06-03 19:16:15 +02:00
|
|
|
(currentOp?.provider != null)
|
|
|
|
|
? currentOp!.provider!.name
|
2026-05-04 15:36:42 +02:00
|
|
|
: 'Nessun gestore selezionato',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color:
|
2026-06-04 13:42:29 +02:00
|
|
|
(currentOp?.provider == null ||
|
|
|
|
|
currentOp!.provider!.name.isEmpty)
|
2026-05-04 15:36:42 +02:00
|
|
|
? Colors.grey
|
|
|
|
|
: null,
|
|
|
|
|
fontWeight:
|
2026-06-04 13:42:29 +02:00
|
|
|
(currentOp?.provider == null ||
|
|
|
|
|
currentOp!.provider!.name.isEmpty)
|
2026-05-04 15:36:42 +02:00
|
|
|
? FontWeight.normal
|
|
|
|
|
: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
trailing: const Icon(Icons.arrow_drop_down),
|
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
|
side: BorderSide(color: theme.dividerColor),
|
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
|
),
|
|
|
|
|
onTap: () => _showProviderModal(context, currentType),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
|
|
|
|
|
// 1. SCENARIO ENERGY (Dropdown Fisso)
|
|
|
|
|
if (currentType == 'Energy') ...[
|
|
|
|
|
DropdownButtonFormField<String>(
|
|
|
|
|
initialValue:
|
2026-06-03 12:08:59 +02:00
|
|
|
(currentOp?.subType != null && currentOp!.subType!.isNotEmpty)
|
|
|
|
|
? currentOp!.subType
|
2026-05-04 15:36:42 +02:00
|
|
|
: null,
|
|
|
|
|
decoration: const InputDecoration(labelText: 'Dettaglio Fornitura'),
|
|
|
|
|
items: [
|
|
|
|
|
'Luce',
|
|
|
|
|
'Gas',
|
|
|
|
|
].map((s) => DropdownMenuItem(value: s, child: Text(s))).toList(),
|
|
|
|
|
onChanged: (val) {
|
|
|
|
|
if (val != null) {
|
2026-06-03 12:08:59 +02:00
|
|
|
context.read<OperationFormCubit>().updateFields(subType: val);
|
2026-05-04 15:36:42 +02:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
TextFormField(
|
|
|
|
|
controller: freeTextDescriptionController,
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
labelText: currentType == 'Energy'
|
|
|
|
|
? 'Offerta scelta'
|
|
|
|
|
: 'Nome del servizio/offerta',
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
// 2. SCENARIO FIN (Ricerca Modello/Prodotto)
|
|
|
|
|
if (currentType == 'Fin') ...[
|
2026-05-07 16:28:01 +02:00
|
|
|
SharedModelSection(
|
|
|
|
|
label: 'Seleziona Dispositivo/Prodotto',
|
|
|
|
|
modelId: currentOp?.modelId,
|
|
|
|
|
modelName: currentOp?.modelDisplayName,
|
|
|
|
|
onModelSelected: (id, name) {
|
2026-05-08 12:28:14 +02:00
|
|
|
context.read<OperationFormCubit>().updateFields(
|
2026-05-07 16:28:01 +02:00
|
|
|
modelId: id,
|
|
|
|
|
modelDisplayName: name,
|
|
|
|
|
);
|
|
|
|
|
},
|
2026-05-04 15:36:42 +02:00
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
// 3. SCENARIO ENTERTAINMENT O CUSTOM (Testo libero)
|
2026-05-19 16:00:40 +02:00
|
|
|
if (currentType == 'Entertainment' || currentType == 'Altro') ...[
|
2026-05-04 15:36:42 +02:00
|
|
|
TextFormField(
|
|
|
|
|
controller: freeTextSubtypeController,
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
labelText: currentType == 'Entertainment'
|
|
|
|
|
? 'Piattaforma (es. Netflix, DAZN, Spotify...)'
|
|
|
|
|
: 'Specifica il servizio (es. Monopattino)',
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
// SCADENZA (Reattivo per tipi complessi)
|
|
|
|
|
if ([
|
|
|
|
|
'Energy',
|
|
|
|
|
'Fin',
|
|
|
|
|
'Entertainment',
|
2026-05-19 16:00:40 +02:00
|
|
|
'Altro',
|
2026-05-04 15:36:42 +02:00
|
|
|
].contains(currentType)) ...[
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
durationQuickPicks, // Passiamo i chips dall'esterno
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
ListTile(
|
|
|
|
|
title: const Text('Data di Scadenza Effettiva'),
|
|
|
|
|
subtitle: Text(
|
|
|
|
|
currentOp?.expirationDate != null
|
|
|
|
|
? "${currentOp!.expirationDate!.day}/${currentOp!.expirationDate!.month}/${currentOp!.expirationDate!.year}"
|
|
|
|
|
: 'Nessuna scadenza impostata',
|
|
|
|
|
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
|
|
|
|
|
),
|
|
|
|
|
trailing: const Icon(Icons.calendar_month, color: Colors.blue),
|
|
|
|
|
tileColor: Colors.blue.withValues(alpha: 0.05),
|
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
|
borderRadius: BorderRadius.circular(12),
|
|
|
|
|
side: const BorderSide(color: Colors.blue, width: 0.5),
|
|
|
|
|
),
|
|
|
|
|
onTap: () async {
|
|
|
|
|
final date = await showDatePicker(
|
|
|
|
|
context: context,
|
|
|
|
|
initialDate:
|
|
|
|
|
currentOp?.expirationDate ??
|
|
|
|
|
DateTime.now().add(const Duration(days: 365)),
|
|
|
|
|
firstDate: DateTime.now(),
|
|
|
|
|
lastDate: DateTime.now().add(const Duration(days: 3650)),
|
|
|
|
|
);
|
|
|
|
|
if (date != null && context.mounted) {
|
2026-05-08 12:28:14 +02:00
|
|
|
context.read<OperationFormCubit>().updateFields(
|
2026-05-04 15:36:42 +02:00
|
|
|
expirationDate: date,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|