import 'package:mc_common_app/extensions/string_extensions.dart'; import 'package:mc_common_app/models/services/service_model.dart'; import 'package:mc_common_app/models/general/widgets_models.dart'; class CustomTimeDateSlotModel { TimeSlotModel? date; List? availableSlots; CustomTimeDateSlotModel({this.date, this.availableSlots}); } class ServiceAppointmentScheduleModel { List? serviceSlotList; List? servicesListInAppointment; int? selectedDateIndex; List? customTimeDateSlotList; CustomTimeDateSlotModel? selectedCustomTimeDateSlotModel; double? amountToPay; double? amountTotal; double? amountRem; int? appointmentType; ServiceAppointmentScheduleModel({ this.serviceSlotList, this.servicesListInAppointment, this.selectedDateIndex, this.customTimeDateSlotList, this.selectedCustomTimeDateSlotModel, this.amountToPay, this.amountTotal, this.amountRem, this.appointmentType, }); List getFormattedDateTimeSlotPackage() { List customTimeDateSlotList = []; for (var element in serviceSlotList!) { if (!isAlreadyThere(element.slotDate!, customTimeDateSlotList)) { customTimeDateSlotList.add(CustomTimeDateSlotModel( date: TimeSlotModel(slotId: element.id!, isSelected: false, date: element.slotDate!, allowAppointment: (element.allowAppointment ?? 0) > (element.bookAppointment ?? 0)), availableSlots: getAvailableSlotsByDate(element.slotDate!), )); } } return customTimeDateSlotList; } List? getAvailableSlotsByDate(String date) { List timeslots = []; for (var element in serviceSlotList!) { if (element.slotDate == date) { timeslots.add(TimeSlotModel(slotId: element.id!, slot: element.startTime!, date: element.slotDate!, isSelected: false, allowAppointment: element.bookAppointment! < element.allowAppointment!)); } } return timeslots; } List getFormattedSlotTimes() { var seenSlots = {}; var slotTimeData = serviceSlotList! .where((slot) => seenSlots.add(TimeSlotModel( slot: slot.startTime!, slotId: slot.id!, isSelected: false, date: slot.slotDate!.toFormattedDateWithoutTime(), ))) .toList(); List slotTime = []; for (var element in slotTimeData) { slotTime.add(TimeSlotModel(isSelected: false, slotId: element.id!, slot: element.startTime ?? "", date: element.slotDate!.toFormattedDateWithoutTime() ?? "")); } return slotTime; } bool isAlreadyThere(String dateToFind, List slots) { int index = slots.indexWhere((element) { return element.date!.date == dateToFind; }); if (index == -1) { return false; } else { return true; } } ServiceAppointmentScheduleModel.fromJson(Map json, {bool isForAppointment = false}) { if (json['serviceSlotList'] != null) { serviceSlotList = []; json['serviceSlotList'].forEach((v) { serviceSlotList!.add(ServiceSlotList.fromJson(v)); }); } if (json['serviceList'] != null) { servicesListInAppointment = []; json['serviceList'].forEach((v) { servicesListInAppointment!.add(ServiceModel.fromJson(v, isForAppointment: isForAppointment)); }); } customTimeDateSlotList = getFormattedDateTimeSlotPackage(); amountToPay = json['amountToPay']; selectedDateIndex = null; amountTotal = json['amountTotal']; amountRem = json['amountRem']; appointmentType = json['appointmentType']; } } class ServiceSlotList { int? id; int? branchAppointmentScheduleID; String? branchAppointmentSchedule; int? serviceProviderID; String? slotDate; String? startTime; String? endTime; int? bookAppointment; int? allowAppointment; int? slotDurationMinute; int? appointmentType; bool? isActive; int? createdBy; String? createdOn; int? modifiedBy; String? modifiedOn; ServiceSlotList( {this.id, this.branchAppointmentScheduleID, this.branchAppointmentSchedule, this.serviceProviderID, this.slotDate, this.startTime, this.endTime, this.bookAppointment, this.allowAppointment, this.slotDurationMinute, this.appointmentType, this.isActive, this.createdBy, this.createdOn, this.modifiedBy, this.modifiedOn}); ServiceSlotList.fromJson(Map json) { id = json['id']; branchAppointmentScheduleID = json['branchAppointmentScheduleID']; branchAppointmentSchedule = json['branchAppointmentSchedule']; serviceProviderID = json['serviceProviderID']; slotDate = (json['slotDate'] as String).toFormattedDateWithoutTime(); startTime = json['startTime']; endTime = json['endTime']; bookAppointment = json['bookAppointment']; allowAppointment = json['allowAppointment']; slotDurationMinute = json['slotDurationMinute']; appointmentType = json['appointmentType']; isActive = json['isActive']; createdBy = json['createdBy']; createdOn = json['createdOn']; modifiedBy = json['modifiedBy']; modifiedOn = json['modifiedOn']; } @override String toString() { return 'ServiceSlotList{id: $id, branchAppointmentScheduleID: $branchAppointmentScheduleID, branchAppointmentSchedule: $branchAppointmentSchedule, serviceProviderID: $serviceProviderID, slotDate: $slotDate, startTime: $startTime, endTime: $endTime, bookAppointment: $bookAppointment, allowAppointment: $allowAppointment, slotDurationMinute: $slotDurationMinute, appointmentType: $appointmentType, isActive: $isActive, createdBy: $createdBy, createdOn: $createdOn, modifiedBy: $modifiedBy, modifiedOn: $modifiedOn}'; } }