297 lines
9.7 KiB
Dart
297 lines
9.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:flux/core/blocs/session/session_cubit.dart';
|
|
import 'package:flux/core/theme/theme.dart';
|
|
import 'package:flux/features/customers/blocs/customers_cubit.dart';
|
|
import 'package:flux/features/customers/models/customer_model.dart';
|
|
import 'package:flux/features/customers/ui/customer_form.dart';
|
|
import 'package:flux/temp/migration_tools.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
class CustomersContent extends StatefulWidget {
|
|
const CustomersContent({super.key});
|
|
|
|
@override
|
|
State<CustomersContent> createState() => _CustomersContentState();
|
|
}
|
|
|
|
class _CustomersContentState extends State<CustomersContent> {
|
|
final TextEditingController _searchController = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadInitialCustomers();
|
|
}
|
|
|
|
void _loadInitialCustomers() {
|
|
final companyId = context.read<SessionCubit>().state.company?.id;
|
|
if (companyId != null) {
|
|
context.read<CustomersCubit>().loadCustomers();
|
|
}
|
|
}
|
|
|
|
void _onSearch(String query) {
|
|
final companyId = context.read<SessionCubit>().state.company?.id;
|
|
if (companyId != null) {
|
|
context.read<CustomersCubit>().searchCustomers(query);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: context.background,
|
|
appBar: AppBar(
|
|
title: const Text(
|
|
'Anagrafica Clienti',
|
|
style: TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
elevation: 0,
|
|
backgroundColor: context.background,
|
|
actions: [
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 16),
|
|
child: ElevatedButton.icon(
|
|
onPressed: () => openCustomerForm(context: context),
|
|
icon: const Icon(Icons.person_add_alt_1_rounded, size: 20),
|
|
label: const Text('NUOVO'),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: context.accent,
|
|
foregroundColor: Colors.white,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
children: [
|
|
// BARRA DI RICERCA
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
|
|
child: TextField(
|
|
controller: _searchController,
|
|
onChanged: _onSearch,
|
|
decoration: InputDecoration(
|
|
hintText: 'Cerca cliente per nome o telefono...',
|
|
prefixIcon: const Icon(Icons.search),
|
|
filled: true,
|
|
fillColor: context.accent.withValues(alpha: 0.05),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(16),
|
|
borderSide: BorderSide.none,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
//TODO cancella quando import finito
|
|
ElevatedButton(
|
|
onPressed: () async {
|
|
try {
|
|
// 1. Mostra un loading (opzionale ma utile)
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Caricamento JSON in corso...')),
|
|
);
|
|
|
|
// 2. Legge tutto il file come stringa
|
|
final String jsonString = await rootBundle.loadString(
|
|
'assets/schedeRiparazione-1778021345.json',
|
|
);
|
|
|
|
// 3. Lancia lo script (sostituisci l'UUID con l'ID della tua azienda su Supabase)
|
|
await TicketMigrationScript().runMigration(jsonString);
|
|
|
|
// 4. Successo!
|
|
if (context.mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Migrazione Completata! Guarda i log.'),
|
|
),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
if (context.mounted) {
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text('Errore: $e')));
|
|
}
|
|
}
|
|
},
|
|
child: const Text('migra clienti'),
|
|
),
|
|
|
|
// LISTA CLIENTI
|
|
Expanded(
|
|
child: BlocBuilder<CustomersCubit, CustomersState>(
|
|
builder: (context, state) {
|
|
if (state.status == CustomersStatus.loading &&
|
|
state.customers.isEmpty) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
if (state.customers.isEmpty) {
|
|
return _buildEmptyState(context);
|
|
}
|
|
|
|
return ListView.separated(
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 24,
|
|
vertical: 8,
|
|
),
|
|
itemCount: state.customers.length,
|
|
separatorBuilder: (context, index) =>
|
|
const SizedBox(height: 10),
|
|
itemBuilder: (context, index) {
|
|
final customer = state.customers[index];
|
|
return _CustomerTile(
|
|
customer: customer,
|
|
onTap: () => context.push(
|
|
'/customer/${customer.id}',
|
|
extra: customer,
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildEmptyState(BuildContext context) {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
Icons.people_outline_rounded,
|
|
size: 64,
|
|
color: context.secondaryText.withValues(alpha: 0.5),
|
|
),
|
|
const SizedBox(height: 16),
|
|
Text(
|
|
'Nessun cliente in lista',
|
|
style: TextStyle(color: context.secondaryText),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _CustomerTile extends StatelessWidget {
|
|
final CustomerModel customer;
|
|
final VoidCallback onTap;
|
|
|
|
const _CustomerTile({required this.customer, required this.onTap});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
color: context.background,
|
|
borderRadius: BorderRadius.circular(16),
|
|
border: Border.all(color: context.accent.withValues(alpha: 0.1)),
|
|
),
|
|
child: ListTile(
|
|
onTap: onTap,
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
|
|
leading: CircleAvatar(
|
|
radius: 24,
|
|
backgroundColor: context.accent.withValues(alpha: 0.1),
|
|
child: Text(
|
|
customer.name.isNotEmpty ? customer.name[0].toUpperCase() : '?',
|
|
style: TextStyle(
|
|
color: context.accent,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
),
|
|
title: Text(
|
|
customer.name,
|
|
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
|
|
),
|
|
subtitle: Padding(
|
|
padding: const EdgeInsets.only(top: 4),
|
|
child: Row(
|
|
children: [
|
|
Icon(Icons.phone_android, size: 14, color: context.secondaryText),
|
|
const SizedBox(width: 4),
|
|
Text(
|
|
customer.phoneNumber,
|
|
style: TextStyle(color: context.secondaryText),
|
|
),
|
|
if (customer.email.isNotEmpty) ...[
|
|
Text(' - ', style: TextStyle(color: context.secondaryText)),
|
|
Icon(Icons.email, size: 14, color: context.secondaryText),
|
|
const SizedBox(width: 4),
|
|
Text(
|
|
customer.email,
|
|
style: TextStyle(color: context.secondaryText),
|
|
),
|
|
],
|
|
if (customer.attachments.isNotEmpty) ...[
|
|
Text(' - ', style: TextStyle(color: context.secondaryText)),
|
|
Icon(Icons.attach_file, size: 14, color: context.accent),
|
|
Text(
|
|
'${customer.attachments.length} doc',
|
|
style: TextStyle(
|
|
color: context.accent,
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
trailing: IconButton(
|
|
onPressed: () =>
|
|
openCustomerForm(context: context, customer: customer),
|
|
icon: Icon(Icons.edit_note_rounded, color: context.accent),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Funzione unica per gestire Creazione e Modifica
|
|
void openCustomerForm({
|
|
CustomerModel? customer,
|
|
required BuildContext context,
|
|
}) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (dialogContext) => AlertDialog(
|
|
backgroundColor: context.background,
|
|
content: SizedBox(
|
|
width: 500, // Larghezza ottimale per desktop
|
|
child: CustomerForm(
|
|
customer: customer,
|
|
onSave: (customerFromForm) {
|
|
final session = context.read<SessionCubit>().state;
|
|
final companyId = session.company?.id;
|
|
|
|
if (companyId == null) return;
|
|
|
|
if (customer == null) {
|
|
// CASO NUOVO: Iniettiamo il companyId e inviamo l'evento create
|
|
context.read<CustomersCubit>().createCustomer(
|
|
customerFromForm.copyWith(companyId: companyId),
|
|
);
|
|
} else {
|
|
// CASO MODIFICA: L'ID e il companyId sono già nel modello
|
|
context.read<CustomersCubit>().updateCustomer(customerFromForm);
|
|
}
|
|
Navigator.pop(dialogContext);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|