2026-05-03 12:05:47 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
|
import 'package:flux/features/master_data/products/blocs/product_cubit.dart';
|
|
|
|
|
import 'package:flux/features/master_data/products/ui/quick_product_dialog.dart';
|
|
|
|
|
import 'package:flux/features/master_data/providers/blocs/provider_cubit.dart';
|
|
|
|
|
import 'package:flux/features/operations/blocs/operations_cubit.dart';
|
|
|
|
|
import 'package:flux/features/operations/models/operation_model.dart';
|
|
|
|
|
|
|
|
|
|
class DetailsSection extends StatelessWidget {
|
|
|
|
|
final OperationModel? currentOp;
|
|
|
|
|
final String currentType;
|
|
|
|
|
final TextEditingController freeTextSubtypeController;
|
2026-05-04 12:50:00 +02:00
|
|
|
final TextEditingController freeTextDescriptionController;
|
2026-05-03 12:05:47 +02:00
|
|
|
final Widget durationQuickPicks;
|
|
|
|
|
|
|
|
|
|
const DetailsSection({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.currentOp,
|
|
|
|
|
required this.currentType,
|
|
|
|
|
required this.freeTextSubtypeController,
|
2026-05-04 12:50:00 +02:00
|
|
|
required this.freeTextDescriptionController,
|
2026-05-03 12:05:47 +02:00
|
|
|
required this.durationQuickPicks,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
bool _doesProviderMatchOperationType(dynamic provider, String operationType) {
|
|
|
|
|
if (operationType == 'Custom') return true;
|
|
|
|
|
switch (operationType) {
|
|
|
|
|
case 'AL':
|
|
|
|
|
case 'MNP':
|
|
|
|
|
return provider.mobile == true;
|
|
|
|
|
case 'NIP':
|
|
|
|
|
return provider.landline == true;
|
|
|
|
|
case 'UNICA':
|
|
|
|
|
return provider.landline == true || provider.mobile == true;
|
|
|
|
|
case 'Energy':
|
|
|
|
|
return provider.energy == true;
|
|
|
|
|
case 'Fin':
|
|
|
|
|
return provider.financing == true;
|
|
|
|
|
case 'Entertainment':
|
|
|
|
|
return provider.entertainment == true;
|
|
|
|
|
case 'TELEPASS':
|
|
|
|
|
return provider.telepass == true;
|
|
|
|
|
default:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _showProviderModal(BuildContext context, String operationType) {
|
|
|
|
|
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(
|
|
|
|
|
child: BlocBuilder<ProvidersCubit, ProvidersState>(
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
if (state.isLoading) {
|
|
|
|
|
return const Center(child: CircularProgressIndicator());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final allProviders = state.activeProviders;
|
|
|
|
|
final filteredProviders = allProviders
|
|
|
|
|
.where(
|
|
|
|
|
(p) => _doesProviderMatchOperationType(
|
|
|
|
|
p,
|
|
|
|
|
operationType,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
if (filteredProviders.isEmpty) {
|
|
|
|
|
return const Center(
|
|
|
|
|
child: Text(
|
|
|
|
|
'Nessun gestore compatibile con questo servizio.',
|
|
|
|
|
style: TextStyle(color: Colors.grey),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 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,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
onTap: () {
|
|
|
|
|
context
|
|
|
|
|
.read<OperationsCubit>()
|
|
|
|
|
.updateOperationFields(
|
|
|
|
|
providerId: provider.id,
|
|
|
|
|
providerDisplayName: provider.name,
|
|
|
|
|
);
|
|
|
|
|
Navigator.pop(modalContext);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _showModelModal(BuildContext context) {
|
|
|
|
|
showModalBottomSheet(
|
|
|
|
|
context: context,
|
|
|
|
|
isScrollControlled: true,
|
|
|
|
|
shape: const RoundedRectangleBorder(
|
|
|
|
|
borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
|
|
|
|
|
),
|
|
|
|
|
builder: (modalContext) {
|
|
|
|
|
return DraggableScrollableSheet(
|
|
|
|
|
initialChildSize: 0.6,
|
|
|
|
|
minChildSize: 0.4,
|
|
|
|
|
maxChildSize: 0.9,
|
|
|
|
|
expand: false,
|
|
|
|
|
builder: (_, scrollController) {
|
|
|
|
|
return Column(
|
|
|
|
|
children: [
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
|
child: Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
|
|
|
|
'Seleziona Modello',
|
|
|
|
|
style: Theme.of(context).textTheme.titleLarge,
|
|
|
|
|
),
|
|
|
|
|
IconButton(
|
|
|
|
|
icon: const Icon(Icons.close),
|
|
|
|
|
onPressed: () => Navigator.pop(modalContext),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
|
|
|
|
child: TextField(
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
hintText: 'Cerca modello (es. iPhone 15...)',
|
|
|
|
|
prefixIcon: const Icon(Icons.search),
|
|
|
|
|
border: OutlineInputBorder(
|
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
onChanged: (query) =>
|
|
|
|
|
context.read<ProductsCubit>().searchModels(query),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
|
child: ElevatedButton.icon(
|
|
|
|
|
style: ElevatedButton.styleFrom(
|
|
|
|
|
minimumSize: const Size.fromHeight(48),
|
|
|
|
|
),
|
|
|
|
|
icon: const Icon(Icons.add),
|
|
|
|
|
label: const Text('Aggiungi Modello al Volo'),
|
|
|
|
|
onPressed: () async {
|
|
|
|
|
final operationsCubit = context.read<OperationsCubit>();
|
|
|
|
|
final existingBrands = context
|
|
|
|
|
.read<ProductsCubit>()
|
|
|
|
|
.state
|
|
|
|
|
.brands;
|
|
|
|
|
|
|
|
|
|
final newModel = await showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (dialogContext) {
|
|
|
|
|
return BlocProvider.value(
|
|
|
|
|
value: context.read<ProductsCubit>(),
|
|
|
|
|
child: QuickProductDialog(
|
|
|
|
|
existingBrands: existingBrands,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (newModel != null) {
|
|
|
|
|
operationsCubit.updateOperationFields(
|
|
|
|
|
modelId: newModel.id,
|
|
|
|
|
modelDisplayName: newModel.nameWithBrand,
|
|
|
|
|
);
|
|
|
|
|
if (context.mounted) Navigator.pop(modalContext);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const Divider(),
|
|
|
|
|
Expanded(
|
|
|
|
|
child: BlocBuilder<ProductsCubit, ProductState>(
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
return ListView.builder(
|
|
|
|
|
controller: scrollController,
|
|
|
|
|
itemCount: state.models.length,
|
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
|
final deviceModel = state.models[index];
|
|
|
|
|
return ListTile(
|
|
|
|
|
leading: const Icon(Icons.devices),
|
|
|
|
|
title: Text(
|
|
|
|
|
deviceModel.nameWithBrand,
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
onTap: () {
|
|
|
|
|
context
|
|
|
|
|
.read<OperationsCubit>()
|
|
|
|
|
.updateOperationFields(
|
|
|
|
|
modelId: deviceModel.id,
|
|
|
|
|
modelDisplayName: deviceModel.nameWithBrand,
|
|
|
|
|
);
|
|
|
|
|
Navigator.pop(modalContext);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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(
|
|
|
|
|
(currentOp?.providerDisplayName != null &&
|
|
|
|
|
currentOp!.providerDisplayName!.isNotEmpty)
|
|
|
|
|
? currentOp!.providerDisplayName!
|
|
|
|
|
: 'Nessun gestore selezionato',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color:
|
|
|
|
|
(currentOp?.providerId == null ||
|
|
|
|
|
currentOp!.providerId!.isEmpty)
|
|
|
|
|
? Colors.grey
|
|
|
|
|
: null,
|
|
|
|
|
fontWeight:
|
|
|
|
|
(currentOp?.providerId == null ||
|
|
|
|
|
currentOp!.providerId!.isEmpty)
|
|
|
|
|
? 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:
|
|
|
|
|
(currentOp?.subtype != null && currentOp!.subtype!.isNotEmpty)
|
|
|
|
|
? currentOp!.subtype
|
|
|
|
|
: null,
|
|
|
|
|
decoration: const InputDecoration(labelText: 'Dettaglio Fornitura'),
|
|
|
|
|
items: [
|
|
|
|
|
'Luce',
|
|
|
|
|
'Gas',
|
|
|
|
|
].map((s) => DropdownMenuItem(value: s, child: Text(s))).toList(),
|
|
|
|
|
onChanged: (val) {
|
|
|
|
|
if (val != null) {
|
|
|
|
|
context.read<OperationsCubit>().updateOperationFields(
|
|
|
|
|
subtype: val,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
2026-05-04 12:50:00 +02:00
|
|
|
TextFormField(
|
|
|
|
|
controller: freeTextDescriptionController,
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
labelText: currentType == 'Energy'
|
|
|
|
|
? 'Offerta scelta'
|
|
|
|
|
: 'Nome del servizio/offerta',
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
2026-05-03 12:05:47 +02:00
|
|
|
],
|
|
|
|
|
|
|
|
|
|
// 2. SCENARIO FIN (Ricerca Modello/Prodotto)
|
|
|
|
|
if (currentType == 'Fin') ...[
|
|
|
|
|
ListTile(
|
|
|
|
|
title: const Text('Seleziona Dispositivo/Prodotto'),
|
|
|
|
|
subtitle: Text(
|
|
|
|
|
(currentOp?.modelDisplayName != null &&
|
|
|
|
|
currentOp!.modelDisplayName!.isNotEmpty)
|
|
|
|
|
? currentOp!.modelDisplayName!
|
|
|
|
|
: 'Nessun modello selezionato',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color:
|
|
|
|
|
(currentOp?.modelId == null || currentOp!.modelId!.isEmpty)
|
|
|
|
|
? Colors.grey
|
|
|
|
|
: null,
|
|
|
|
|
fontWeight:
|
|
|
|
|
(currentOp?.modelId == null || currentOp!.modelId!.isEmpty)
|
|
|
|
|
? FontWeight.normal
|
|
|
|
|
: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
trailing: const Icon(Icons.arrow_drop_down),
|
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
|
side: BorderSide(color: theme.dividerColor),
|
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
|
),
|
|
|
|
|
onTap: () => _showModelModal(context),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
// 3. SCENARIO ENTERTAINMENT O CUSTOM (Testo libero)
|
|
|
|
|
if (currentType == 'Entertainment' || currentType == 'Custom') ...[
|
|
|
|
|
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',
|
|
|
|
|
'Custom',
|
|
|
|
|
].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) {
|
|
|
|
|
context.read<OperationsCubit>().updateOperationFields(
|
|
|
|
|
expirationDate: date,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|