Files
flux/lib/features/tickets/blocs/ticket_form_cubit.dart

166 lines
5.4 KiB
Dart
Raw Normal View History

2026-05-06 12:20:26 +02:00
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_cubit.dart';
import 'package:flux/features/customers/models/customer_model.dart';
import 'package:flux/features/tickets/models/ticket_model.dart';
import 'package:flux/features/tickets/data/ticket_repository.dart';
import 'package:get_it/get_it.dart';
import 'ticket_form_state.dart';
class TicketFormCubit extends Cubit<TicketFormState> {
final TicketRepository _repository = GetIt.I.get<TicketRepository>();
final SessionCubit _sessionCubit = GetIt.I.get<SessionCubit>();
TicketFormCubit()
: super(
// Inizializziamo con un ticket vuoto di default
2026-05-06 12:40:27 +02:00
TicketFormState(ticket: TicketModel.empty()),
2026-05-06 12:20:26 +02:00
);
/// 1. INIZIALIZZAZIONE (Se stiamo modificando un ticket esistente)
void initForm(TicketModel? existingTicket) {
if (existingTicket != null) {
emit(
state.copyWith(ticket: existingTicket, status: TicketFormStatus.ready),
);
} else {
// È un nuovo ticket! Inseriamo i default base (Azienda, Negozio, Creatore)
final currentUser = _sessionCubit.state.currentStaffMember;
final currentStore = _sessionCubit.state.currentStore;
final companyId = _sessionCubit.state.company?.id ?? '';
2026-05-06 12:40:27 +02:00
final newTicket = TicketModel.empty().copyWith(
companyId: companyId,
storeId: currentStore?.id,
createdById: currentUser?.id,
createdByName: currentUser?.name,
// Impostiamo lo stato iniziale
2026-05-06 19:25:17 +02:00
ticketStatus: TicketStatus.open,
2026-05-06 12:40:27 +02:00
ticketType: TicketType.repair, // Default
);
2026-05-06 12:20:26 +02:00
emit(state.copyWith(ticket: newTicket, status: TicketFormStatus.ready));
}
}
/// 2. AGGIORNAMENTO CLIENTE (Usato dal nostro SharedCustomerSection!)
void updateCustomer(CustomerModel customer) {
emit(
state.copyWith(
ticket: state.ticket.copyWith(
customerId: customer.id,
customerName: customer.name,
alternativePhoneNumber: customer.phoneNumber, // Comodo come fallback!
),
),
);
}
/// 3. AGGIORNAMENTO MODELLO (Usato dal nostro SharedModelSection!)
void updateModel({required String modelId, required String modelName}) {
emit(
state.copyWith(
ticket: state.ticket.copyWith(
targetModelId: modelId,
targetModelName: modelName,
),
),
);
}
void updateCreator({required String staffId, required String staffName}) {
emit(
state.copyWith(
2026-05-06 12:40:27 +02:00
ticket: state.ticket.copyWith(
createdById: staffId,
createdByName: staffName,
),
2026-05-06 12:20:26 +02:00
),
);
}
/// 4. AGGIORNAMENTO GENERICO DEI CAMPI
void updateFields({
TicketType? ticketType,
TicketStatus? status,
String? request,
String? targetSn,
String? alternativePhoneNumber,
bool? hasCourtesyDevice,
String? includedAccessories,
String? publicNotes,
String? internalNotes,
double? customerPrice,
double? internalCost,
String? assignedToId,
String? assignedToName,
}) {
emit(
state.copyWith(
ticket: state.ticket.copyWith(
ticketType: ticketType ?? state.ticket.ticketType,
2026-05-06 19:25:17 +02:00
ticketStatus: status ?? state.ticket.ticketStatus,
2026-05-06 12:20:26 +02:00
request: request ?? state.ticket.request,
targetSn: targetSn ?? state.ticket.targetSn,
alternativePhoneNumber:
alternativePhoneNumber ?? state.ticket.alternativePhoneNumber,
hasCourtesyDevice:
hasCourtesyDevice ?? state.ticket.hasCourtesyDevice,
includedAccessories:
includedAccessories ?? state.ticket.includedAccessories,
publicNotes: publicNotes ?? state.ticket.publicNotes,
internalNotes: internalNotes ?? state.ticket.internalNotes,
customerPrice: customerPrice ?? state.ticket.customerPrice,
internalCost: internalCost ?? state.ticket.internalCost,
assignedToId: assignedToId ?? state.ticket.assignedToId,
assignedToName: assignedToName ?? state.ticket.assignedToName,
),
),
);
}
/// 5. SALVATAGGIO
Future<void> saveTicket({required bool keepAdding}) async {
emit(state.copyWith(status: TicketFormStatus.saving));
try {
final ticketToSave = state.ticket;
// Validazione base
if (ticketToSave.customerId == null || ticketToSave.customerId!.isEmpty) {
throw Exception("Seleziona un cliente prima di salvare.");
}
final savedTicket = await _repository.saveTicket(ticketToSave);
if (keepAdding) {
emit(
state.copyWith(
status: TicketFormStatus.successAndAddAnother,
// Svuotiamo il form per il prossimo, mantenendo Store e Creatore ATTUALI
ticket: TicketModel.empty().copyWith(
companyId: savedTicket.companyId,
storeId: savedTicket.storeId,
createdById: ticketToSave
.createdById, // Manteniamo quello selezionato nella tendina!
createdByName: ticketToSave.createdByName,
2026-05-06 19:25:17 +02:00
ticketStatus: TicketStatus.open,
2026-05-06 12:20:26 +02:00
ticketType: TicketType.repair,
),
),
);
} else {
emit(
state.copyWith(status: TicketFormStatus.success, ticket: savedTicket),
);
}
} catch (e) {
emit(
state.copyWith(
status: TicketFormStatus.failure,
errorMessage: e.toString(),
),
);
}
}
}