diff --git a/lib/features/master_data/store/bloc/store_cubit.dart b/lib/features/master_data/store/bloc/store_cubit.dart index 158f16c..b6bfc30 100644 --- a/lib/features/master_data/store/bloc/store_cubit.dart +++ b/lib/features/master_data/store/bloc/store_cubit.dart @@ -1,6 +1,8 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:equatable/equatable.dart'; import 'package:flux/core/blocs/session/session_bloc.dart'; +import 'package:flux/features/master_data/staff/data/staff_repository.dart'; +import 'package:flux/features/master_data/staff/models/staff_member_model.dart'; import 'package:flux/features/master_data/store/data/store_repository.dart'; import 'package:flux/features/master_data/store/models/store_model.dart'; import 'package:get_it/get_it.dart'; @@ -9,6 +11,7 @@ part 'store_state.dart'; class StoreCubit extends Cubit { final StoreRepository _repository = GetIt.I(); + final StaffRepository _staffRepository = GetIt.I(); final SessionBloc _sessionBloc; StoreCubit(this._sessionBloc) : super(const StoreState(stores: [])); @@ -31,10 +34,17 @@ class StoreCubit extends Cubit { final stores = await _repository.getStoresByCompany( _sessionBloc.state.company!.id, ); + final Map> staffByStore = {}; + for (StoreModel store in stores) { + staffByStore[store.id!] = await _staffRepository.getStaffMembersInStore( + store.id!, + ); + } emit( state.copyWith( status: StoreStatus.success, - stores: stores, // Assicurati di avere 'stores' nello StoreState + stores: stores, + staffByStore: staffByStore, ), ); } catch (e) { @@ -43,4 +53,45 @@ class StoreCubit extends Cubit { ); } } + + Future assignStaffToStore(String storeId, String staffId) async { + try { + await _staffRepository.assignToStore(staffId, storeId); + // Dopo l'assegnazione, potresti voler ricaricare lo staff per quel negozio + final updatedStaff = await _staffRepository.getStaffMembersInStore( + storeId, + ); + final newMap = Map>.from( + state.staffByStore, + ); + newMap[storeId] = updatedStaff; + emit(state.copyWith(status: StoreStatus.success, staffByStore: newMap)); + } catch (e) { + emit( + state.copyWith(status: StoreStatus.failure, errorMessage: e.toString()), + ); + } + } + + // Rimuove un dipendente da un negozio + Future removeStaffFromStore(String staffId, String storeId) async { + try { + await _staffRepository.removeFromStore(staffId, storeId); + final updatedStaff = await _staffRepository.getStaffMembersInStore( + storeId, + ); + final newMap = Map>.from( + state.staffByStore, + ); + newMap[storeId] = updatedStaff; + emit(state.copyWith(staffByStore: newMap)); + } catch (e) { + emit( + state.copyWith( + status: StoreStatus.failure, + errorMessage: "Errore nella rimozione: $e", + ), + ); + } + } } diff --git a/lib/features/master_data/store/bloc/store_state.dart b/lib/features/master_data/store/bloc/store_state.dart index 012129c..772e8c6 100644 --- a/lib/features/master_data/store/bloc/store_state.dart +++ b/lib/features/master_data/store/bloc/store_state.dart @@ -7,12 +7,14 @@ class StoreState extends Equatable { final StoreModel? store; final String? errorMessage; final List stores; + final Map> staffByStore; const StoreState({ this.status = StoreStatus.initial, this.store, this.errorMessage, required this.stores, + this.staffByStore = const {}, }); StoreState copyWith({ @@ -20,15 +22,23 @@ class StoreState extends Equatable { StoreModel? store, String? errorMessage, List? stores, + Map>? staffByStore, }) { return StoreState( status: status ?? this.status, store: store ?? this.store, errorMessage: errorMessage ?? this.errorMessage, stores: stores ?? this.stores, + staffByStore: staffByStore ?? this.staffByStore, ); } @override - List get props => [status, store, errorMessage, stores]; + List get props => [ + status, + store, + errorMessage, + stores, + staffByStore, + ]; } diff --git a/lib/features/master_data/store/ui/stores_screen.dart b/lib/features/master_data/store/ui/stores_screen.dart index a5a833c..5b23524 100644 --- a/lib/features/master_data/store/ui/stores_screen.dart +++ b/lib/features/master_data/store/ui/stores_screen.dart @@ -90,10 +90,10 @@ class _StoresScreenState extends State { mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ // Mostra quanti dipendenti ci sono (usando lo StaffCubit) - BlocBuilder( - builder: (context, staffState) { + BlocBuilder( + builder: (context, storeState) { final staffCount = - staffState.storesByStaff[store.id]?.length ?? 0; + storeState.staffByStore[store.id]?.length ?? 0; return ActionChip( avatar: const Icon(Icons.people, size: 16), label: Text("$staffCount Dipendenti"), @@ -119,42 +119,52 @@ class _StoresScreenState extends State { context: context, isScrollControlled: true, builder: (context) => BlocBuilder( - builder: (context, state) { - return Container( - padding: const EdgeInsets.all(24), - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - Text("Personale di ${store.nome}", style: context.titleLarge), - const SizedBox(height: 16), - // Lista di TUTTO lo staff dell'azienda con checkbox - ...state.allStaff.map((person) { - final bool isAssigned = - state.storesByStaff[store.id!]?.any( - (s) => s.id == person.id, - ) ?? - false; + // 1. Prendi TUTTI i dipendenti + builder: (context, staffState) { + return BlocBuilder( + // 2. Prendi le ASSEGNAZIONI + builder: (context, storeState) { + final assignedToThisStore = + storeState.staffByStore[store.id!] ?? []; - return CheckboxListTile( - title: Text(person.name), - value: isAssigned, - onChanged: (selected) { - if (selected == true) { - context.read().assignMemberToStore( - person.id!, - store.id!, - ); - } else { - context.read().removeMemberFromStore( - person.id!, - store.id!, - ); - } - }, - ); - }), - ], - ), + return Container( + padding: const EdgeInsets.all(24), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Text( + "Personale di ${store.nome}", + style: context.titleLarge, + ), + const SizedBox(height: 16), + ...staffState.allStaff.map((person) { + // La spunta deve dipendere dallo StoreCubit! + final bool isAssigned = assignedToThisStore.any( + (s) => s.id == person.id, + ); + + return CheckboxListTile( + title: Text(person.name), + value: isAssigned, + onChanged: (selected) { + if (selected == true) { + context.read().assignStaffToStore( + store.id!, + person.id!, + ); + } else { + context.read().removeStaffFromStore( + person.id!, + store.id!, + ); + } + }, + ); + }), + ], + ), + ); + }, ); }, ),