import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flux/core/blocs/session/session_bloc.dart'; import 'package:flux/core/theme/theme.dart'; import 'package:flux/core/widgets/flux_text_field.dart'; import 'package:flux/features/staff/blocs/staff_cubit.dart'; // Tuo percorso import 'package:flux/features/staff/models/staff_member_model.dart'; import 'package:flux/features/store/bloc/store_bloc.dart'; class StaffScreen extends StatefulWidget { const StaffScreen({super.key}); @override State createState() => _StaffScreenState(); } class _StaffScreenState extends State { String? _selectedStoreId; bool _showAllCompanyStaff = true; // Partiamo con la vista globale @override void initState() { super.initState(); // Carichiamo subito tutto context.read().loadAllStaff(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: context.background, appBar: AppBar( title: const Text("Anagrafica Personale"), actions: [ // Toggle per vista Azienda / Negozio Padding( padding: const EdgeInsets.only(right: 16), child: FilterChip( label: const Text("Tutta l'Azienda"), selected: _showAllCompanyStaff, onSelected: (val) => setState(() => _showAllCompanyStaff = val), selectedColor: context.accent.withValues(alpha: 0.2), ), ), ], ), body: Column( children: [ // --- BARRA FILTRO NEGOZIO (Visibile solo se non 'Tutta l'Azienda') --- AnimatedContainer( duration: const Duration(milliseconds: 300), height: _showAllCompanyStaff ? 0 : 80, child: _showAllCompanyStaff ? const SizedBox() : _buildStoreSelector(), ), // --- LISTA PERSONALE --- Expanded( child: BlocBuilder( builder: (context, state) { final list = _showAllCompanyStaff ? state.allStaff : (state.staffByStore[_selectedStoreId] ?? []); if (state.isLoading && list.isEmpty) { return const Center(child: CircularProgressIndicator()); } if (list.isEmpty) { return _buildEmptyState(); } return ListView.separated( padding: const EdgeInsets.all(16), itemCount: list.length, separatorBuilder: (_, _) => const SizedBox(height: 12), itemBuilder: (context, index) { final member = list[index]; return _buildStaffCard(member); }, ); }, ), ), ], ), floatingActionButton: FloatingActionButton.extended( onPressed: () => _openStaffForm(context), label: const Text("Aggiungi"), icon: const Icon(Icons.person_add_alt_1), ), ); } Widget _buildStoreSelector() { return BlocBuilder( // Assumendo tu abbia uno StoreCubit builder: (context, state) { return Padding( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), child: DropdownButtonFormField( initialValue: _selectedStoreId, decoration: const InputDecoration(labelText: "Filtra per Negozio"), items: state.stores .map((s) => DropdownMenuItem(value: s.id, child: Text(s.nome))) .toList(), onChanged: (id) { setState(() => _selectedStoreId = id); if (id != null) context.read().loadStaffForStore(id); }, ), ); }, ); } Widget _buildStaffCard(StaffMemberModel member) { return Card( elevation: 0, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16), side: BorderSide(color: context.accent.withValues(alpha: 0.1)), ), child: ListTile( contentPadding: const EdgeInsets.all(12), leading: CircleAvatar( backgroundColor: context.accent.withValues(alpha: 0.1), child: Text(member.name[0], style: TextStyle(color: context.accent)), ), title: Text( member.name, style: const TextStyle(fontWeight: FontWeight.bold), ), subtitle: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ if (member.email.isNotEmpty) Text(member.email), Text(member.phone.isNotEmpty ? member.phone : "Nessun telefono"), ], ), trailing: const Icon(Icons.edit_note), onTap: () => _openStaffForm(context, member: member), ), ); } void _openStaffForm(BuildContext context, {StaffMemberModel? member}) { final nameController = TextEditingController(text: member?.name); final emailController = TextEditingController(text: member?.email); final phoneController = TextEditingController(text: member?.phone); // Lista temporanea per le chip (se รจ un edit, andrebbe popolata con le assegnazioni attuali) List tempSelectedStores = []; showModalBottomSheet( context: context, isScrollControlled: true, backgroundColor: Colors.transparent, builder: (context) => StatefulBuilder( // Necessario per le chip dentro il BottomSheet builder: (context, setModalState) => Container( decoration: BoxDecoration( color: Theme.of(context).scaffoldBackgroundColor, borderRadius: const BorderRadius.vertical(top: Radius.circular(24)), ), padding: EdgeInsets.only( top: 24, left: 24, right: 24, bottom: MediaQuery.of(context).viewInsets.bottom + 24, ), child: SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( member == null ? "Nuovo Collaboratore" : "Modifica Collaboratore", style: const TextStyle( fontSize: 20, fontWeight: FontWeight.bold, ), ), const SizedBox(height: 24), FluxTextField( controller: nameController, label: "Nome e Cognome", icon: Icons.person, ), const SizedBox(height: 16), FluxTextField( controller: emailController, label: "Email", icon: Icons.email, ), const SizedBox(height: 16), FluxTextField( controller: phoneController, label: "Telefono", icon: Icons.phone, ), const SizedBox(height: 24), const Text( "Assegna ai Negozi", style: TextStyle(fontWeight: FontWeight.bold), ), const SizedBox(height: 12), // --- SELETTORE NEGOZI (CHIPS) --- BlocBuilder( builder: (context, state) { return Wrap( spacing: 8, runSpacing: 8, children: state.stores.map((store) { final isSelected = tempSelectedStores.contains( store.id, ); return FilterChip( label: Text(store.nome), selected: isSelected, onSelected: (selected) { setModalState(() { selected ? tempSelectedStores.add(store.id!) : tempSelectedStores.remove(store.id); }); }, selectedColor: context.accent.withValues(alpha: 0.2), checkmarkColor: context.accent, ); }).toList(), ); }, ), const SizedBox(height: 32), SizedBox( width: double.infinity, height: 50, child: ElevatedButton( onPressed: () { final newMember = StaffMemberModel( id: member?.id, name: nameController.text, email: emailController.text, phone: phoneController.text, companyId: context .read() .state .company! .id, ); context.read().saveStaffWithStores( member: newMember, selectedStoreIds: tempSelectedStores, ); Navigator.pop(context); }, child: const Text("SALVA COLLABORATORE"), ), ), ], ), ), ), ), ); } Widget _buildEmptyState() { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(Icons.people_outline, size: 64, color: context.secondaryText), const SizedBox(height: 16), Text( "Nessun membro trovato", style: TextStyle(color: context.secondaryText), ), ], ), ); } }