| 1 | import 'package:built_value/json_object.dart'; |
| 2 | import 'package:flutter/material.dart'; |
| 3 | import 'package:gap/gap.dart'; |
| 4 | import 'package:openapi/openapi.dart'; |
| 5 | import 'package:aero/services/booking_service.dart'; |
| 6 | import 'package:aero/widgets/input.dart'; |
| 7 | |
| 8 | class BookingDetailsSheet extends StatefulWidget |
| 9 | final String flightId; |
| 10 | final FlightBookingEntity? existingBooking; |
| 11 | final Function()? onSaved |
| 12 | final Function()? onDeleted |
| 13 | |
| 14 | const BookingDetailsSheet({ |
| 15 | super.key, |
| 16 | required this.flightId, |
| 17 | this.existingBooking, |
| 18 | this.onSaved, |
| 19 | this.onDeleted, |
| 20 | }); |
| 21 | |
| 22 | @override |
| 23 | State<BookingDetailsSheet> createState |
| 24 | } |
| 25 | |
| 26 | class _BookingDetailsSheetState extends State |
| 27 | final _formKey = GlobalKey< |
| 28 | final _bookingCodeController = TextEditingController() |
| 29 | final _seatNumberController = TextEditingController() |
| 30 | final _notesController = TextEditingController() |
| 31 | |
| 32 | final BookingService _bookingService = |
| 33 | |
| 34 | String? _selectedSeatTypeName; |
| 35 | String? _selectedSeatingClassName; |
| 36 | String? _selectedReasonName; |
| 37 | |
| 38 | bool _isLoading = false; |
| 39 | String? _errorMessage; |
| 40 | |
| 41 | @override |
| 42 | void initState() { |
| 43 | super.initState(); |
| 44 | if (widget.existingBooking != null) { |
| 45 | _loadExistingBooking(); |
| 46 | } else { |
| 47 | // Set default reason to personal for new bookings |
| 48 | _selectedReasonName = 'personal'; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void _loadExistingBooking() { |
| 53 | final booking = widget.existingBooking! |
| 54 | _bookingCodeController.text = |
| 55 | _extractStringFromJsonObject(booking.bookingCode) ?? '' |
| 56 | _seatNumberController.text = |
| 57 | _extractStringFromJsonObject(booking.seatNumber) ?? '' |
| 58 | _notesController.text = _extractStringFromJsonObject(booking. |
| 59 | |
| 60 | // Store enum names as strings to convert later |
| 61 | if (booking.seatType != null) { |
| 62 | _selectedSeatTypeName = booking.seatType!.name; |
| 63 | } |
| 64 | if (booking.seatingClass != null) { |
| 65 | _selectedSeatingClassName = booking.seatingClass!.name; |
| 66 | } |
| 67 | if (booking.reason != null) { |
| 68 | _selectedReasonName = booking.reason!.name; |
| 69 | } else { |
| 70 | _selectedReasonName = 'personal'; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | String? _extractStringFromJsonObject(JsonObject |
| 75 | if (jsonObject == null) return |
| 76 | final value = jsonObject.value; |
| 77 | if (value is String) return |
| 78 | return value.toString(); |
| 79 | } |
| 80 | |
| 81 | String _formatEnumName(String |
| 82 | return enumName |
| 83 | .split('_') |
| 84 | .map((word) => '${ |
| 85 | .join(' '); |
| 86 | } |
| 87 | |
| 88 | Future<void> _saveBooking |
| 89 | if (!_formKey.currentState!.validate |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | setState(() { |
| 94 | _isLoading = true; |
| 95 | _errorMessage = null; |
| 96 | }); |
| 97 | |
| 98 | try { |
| 99 | if (widget.existingBooking != null) { |
| 100 | // Update existing booking |
| 101 | final builder = UpdateFlightBookingDTOBuilder() |
| 102 | |
| 103 | // Set string fields - use empty string to clear (backend converts '' to null) |
| 104 | builder.bookingCode = JsonObject(_bookingCodeController. |
| 105 | builder.seatNumber = JsonObject(_seatNumberController. |
| 106 | |
| 107 | // Convert enum names to UpdateFlightBookingDTO enums |
| 108 | if (_selectedSeatTypeName != null) { |
| 109 | try { |
| 110 | builder.seatType = UpdateFlightBookingDTOSeatTypeEnum.valueOf |
| 111 | _selectedSeatTypeName!); |
| 112 | } catch (e) { |
| 113 | builder.seatType = null; |
| 114 | } |
| 115 | } else { |
| 116 | builder.seatType = null; |
| 117 | } |
| 118 | |
| 119 | if (_selectedSeatingClassName != null) { |
| 120 | try { |
| 121 | builder.seatingClass = |
| 122 | UpdateFlightBookingDTOSeatingClassEnum.valueOf( |
| 123 | _selectedSeatingClassName!); |
| 124 | } catch (e) { |
| 125 | builder.seatingClass = null; |
| 126 | } |
| 127 | } else { |
| 128 | builder.seatingClass = null; |
| 129 | } |
| 130 | |
| 131 | if (_selectedReasonName != null) { |
| 132 | try { |
| 133 | builder.reason = |
| 134 | UpdateFlightBookingDTOReasonEnum.valueOf(_selectedReasonName!) |
| 135 | } catch (e) { |
| 136 | builder.reason = null; |
| 137 | } |
| 138 | } else { |
| 139 | builder.reason = null; |
| 140 | } |
| 141 | |
| 142 | builder.notes = JsonObject(_notesController. |
| 143 | |
| 144 | final updateDto = builder.build |
| 145 | |
| 146 | await _bookingService.updateFlightBooking( |
| 147 | widget.existingBooking!.id, |
| 148 | updateDto, |
| 149 | ); |
| 150 | } else { |
| 151 | // Create new booking |
| 152 | CreateFlightBookingDTOSeatTypeEnum? seatTypeEnum; |
| 153 | CreateFlightBookingDTOSeatingClassEnum? seatingClassEnum; |
| 154 | CreateFlightBookingDTOReasonEnum reasonEnum = |
| 155 | CreateFlightBookingDTOReasonEnum.personal; |
| 156 | |
| 157 | if (_selectedSeatTypeName != null) { |
| 158 | try { |
| 159 | seatTypeEnum = CreateFlightBookingDTOSeatTypeEnum.valueOf( |
| 160 | _selectedSeatTypeName!); |
| 161 | } catch (e) { |
| 162 | seatTypeEnum = null; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (_selectedSeatingClassName != null) { |
| 167 | try { |
| 168 | seatingClassEnum = CreateFlightBookingDTOSeatingClassEnum.valueOf( |
| 169 | _selectedSeatingClassName!); |
| 170 | } catch (e) { |
| 171 | seatingClassEnum = null; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if (_selectedReasonName != null) { |
| 176 | try { |
| 177 | reasonEnum = |
| 178 | CreateFlightBookingDTOReasonEnum.valueOf(_selectedReasonName!) |
| 179 | } catch (e) { |
| 180 | reasonEnum = CreateFlightBookingDTOReasonEnum.personal; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | final createDto = CreateFlightBookingDTO((b) |
| 185 | ..flightId = widget.flightId |
| 186 | ..bookingCode = _bookingCodeController.text.isEmpty |
| 187 | ? null |
| 188 | : _bookingCodeController.text |
| 189 | ..seatNumber = _seatNumberController.text.isEmpty |
| 190 | ? null |
| 191 | : _seatNumberController.text |
| 192 | ..seatType = seatTypeEnum |
| 193 | ..seatingClass = seatingClassEnum |
| 194 | ..reason = reasonEnum |
| 195 | ..notes = |
| 196 | _notesController.text.isEmpty ? null : |
| 197 | |
| 198 | await _bookingService.createFlightBooking(createDto); |
| 199 | } |
| 200 | |
| 201 | if (mounted) { |
| 202 | widget.onSaved?.call(); |
| 203 | Navigator.of(context).pop |
| 204 | } |
| 205 | } catch (e) { |
| 206 | setState(() { |
| 207 | _isLoading = false; |
| 208 | if (e is ApiException) { |
| 209 | _errorMessage = e.message; |
| 210 | } else { |
| 211 | _errorMessage = 'Failed to save booking: $e'; |
| 212 | } |
| 213 | }); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | Future<void> _deleteBooking |
| 218 | if (widget.existingBooking == null) return |
| 219 | |
| 220 | final confirmed = await showDialog |
| 221 | context: context, |
| 222 | builder: (context) => AlertDialog( |
| 223 | title: const Text( |
| 224 | content: const Text( |
| 225 | actions: [ |
| 226 | TextButton( |
| 227 | onPressed: () => Navigator.of |
| 228 | child: const Text( |
| 229 | ), |
| 230 | TextButton( |
| 231 | onPressed: () => Navigator.of |
| 232 | style: TextButton.styleFrom(foregroundColor |
| 233 | child: const Text( |
| 234 | ), |
| 235 | ], |
| 236 | ), |
| 237 | ); |
| 238 | |
| 239 | if (confirmed != true) return |
| 240 | |
| 241 | setState(() { |
| 242 | _isLoading = true; |
| 243 | _errorMessage = null; |
| 244 | }); |
| 245 | |
| 246 | try { |
| 247 | await _bookingService.deleteFlightBooking(widget.existingBooking |
| 248 | |
| 249 | if (mounted) { |
| 250 | widget.onDeleted?.call(); |
| 251 | Navigator.of(context).pop |
| 252 | } |
| 253 | } catch (e) { |
| 254 | setState(() { |
| 255 | _isLoading = false; |
| 256 | if (e is ApiException) { |
| 257 | _errorMessage = e.message; |
| 258 | } else { |
| 259 | _errorMessage = 'Failed to delete booking: $e'; |
| 260 | } |
| 261 | }); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @override |
| 266 | void dispose() { |
| 267 | _bookingCodeController.dispose(); |
| 268 | _seatNumberController.dispose(); |
| 269 | _notesController.dispose(); |
| 270 | super.dispose(); |
| 271 | } |
| 272 | |
| 273 | @override |
| 274 | Widget build(BuildContext |
| 275 | return Container( |
| 276 | height: MediaQuery.of(context) |
| 277 | decoration: const BoxDecoration( |
| 278 | color: Colors.white, |
| 279 | borderRadius: BorderRadius.vertical(top |
| 280 | ), |
| 281 | child: Column( |
| 282 | children: [ |
| 283 | // Handle bar |
| 284 | Container( |
| 285 | margin: const EdgeInsets. |
| 286 | width: 40, |
| 287 | height: 4, |
| 288 | decoration: BoxDecoration( |
| 289 | color: Colors.grey[300] |
| 290 | borderRadius: BorderRadius.circular( |
| 291 | ), |
| 292 | ), |
| 293 | |
| 294 | Padding( |
| 295 | padding: const EdgeInsets. |
| 296 | vertical: 2, |
| 297 | horizontal: 16, |
| 298 | ), |
| 299 | child: Row( |
| 300 | children: [ |
| 301 | Text( |
| 302 | widget.existingBooking != null |
| 303 | ? 'Edit Booking' |
| 304 | : 'Add Booking Details', |
| 305 | style: const TextStyle( |
| 306 | fontSize: 18, |
| 307 | fontWeight: FontWeight.bold, |
| 308 | color: Colors.black, |
| 309 | ), |
| 310 | ), |
| 311 | const Spacer(), |
| 312 | IconButton( |
| 313 | onPressed: () => Navigator.of |
| 314 | icon: const Icon( |
| 315 | color: Colors.black, |
| 316 | ), |
| 317 | ], |
| 318 | ), |
| 319 | ), |
| 320 | const Divider(), |
| 321 | |
| 322 | Expanded( |
| 323 | child: Form( |
| 324 | key: _formKey, |
| 325 | child: SingleChildScrollView( |
| 326 | padding: const EdgeInsets. |
| 327 | child: Column( |
| 328 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 329 | children: [ |
| 330 | if (_errorMessage != null) ...[ |
| 331 | Container( |
| 332 | padding: const EdgeInsets. |
| 333 | decoration: BoxDecoration( |
| 334 | color: Colors.red[50] |
| 335 | borderRadius: BorderRadius.circular( |
| 336 | border: Border.all(color |
| 337 | ), |
| 338 | child: Text( |
| 339 | _errorMessage!, |
| 340 | style: TextStyle( |
| 341 | color: Colors.red[700] |
| 342 | fontSize: 14, |
| 343 | ), |
| 344 | ), |
| 345 | ), |
| 346 | const Gap(16) |
| 347 | ], |
| 348 | // Booking Code |
| 349 | InputField( |
| 350 | hintText: 'Booking Code (e.g., ABC123)', |
| 351 | keyboardType: TextInputType.text, |
| 352 | controller: _bookingCodeController, |
| 353 | validator: (value) { |
| 354 | if (value != null && value. |
| 355 | return 'Booking code must be 50 characters or less'; |
| 356 | } |
| 357 | return null; |
| 358 | }, |
| 359 | fillColor: Colors.grey[50] |
| 360 | enabledBorder: OutlineInputBorder( |
| 361 | borderRadius: BorderRadius.circular( |
| 362 | borderSide: BorderSide(color: Colors |
| 363 | ), |
| 364 | ), |
| 365 | const Gap(16) |
| 366 | // Seat Number |
| 367 | InputField( |
| 368 | hintText: 'Seat Number (e.g., 12A, 1F)', |
| 369 | keyboardType: TextInputType.text, |
| 370 | controller: _seatNumberController, |
| 371 | validator: (value) { |
| 372 | if (value != null && value. |
| 373 | return 'Seat number must be 10 characters or less'; |
| 374 | } |
| 375 | return null; |
| 376 | }, |
| 377 | fillColor: Colors.grey[50] |
| 378 | enabledBorder: OutlineInputBorder( |
| 379 | borderRadius: BorderRadius.circular( |
| 380 | borderSide: BorderSide(color: Colors |
| 381 | ), |
| 382 | ), |
| 383 | const Gap(16) |
| 384 | // Seat Type |
| 385 | DropdownButtonFormField<String>( |
| 386 | value: _selectedSeatTypeName, |
| 387 | decoration: InputDecoration( |
| 388 | labelText: 'Seat Type', |
| 389 | fillColor: Colors.grey[50] |
| 390 | filled: true, |
| 391 | border: OutlineInputBorder( |
| 392 | borderRadius: BorderRadius.circular( |
| 393 | borderSide: BorderSide.none, |
| 394 | ), |
| 395 | enabledBorder: OutlineInputBorder( |
| 396 | borderRadius: BorderRadius.circular( |
| 397 | borderSide: BorderSide(color: Colors |
| 398 | ), |
| 399 | contentPadding: const EdgeInsets. |
| 400 | ), |
| 401 | items: CreateFlightBookingDTOSeatTypeEnum.values |
| 402 | .map((enumValue) { |
| 403 | return DropdownMenuItem( |
| 404 | value: enumValue.name, |
| 405 | child: Text(_formatEnumName(enumValue |
| 406 | ); |
| 407 | }).toList(), |
| 408 | onChanged: _isLoading |
| 409 | ? null |
| 410 | : (value) { |
| 411 | setState(() { |
| 412 | _selectedSeatTypeName = value; |
| 413 | }); |
| 414 | }, |
| 415 | ), |
| 416 | const Gap(16) |
| 417 | // Seating Class |
| 418 | DropdownButtonFormField<String>( |
| 419 | value: _selectedSeatingClassName, |
| 420 | decoration: InputDecoration( |
| 421 | labelText: 'Seating Class', |
| 422 | fillColor: Colors.grey[50] |
| 423 | filled: true, |
| 424 | border: OutlineInputBorder( |
| 425 | borderRadius: BorderRadius.circular( |
| 426 | borderSide: BorderSide.none, |
| 427 | ), |
| 428 | enabledBorder: OutlineInputBorder( |
| 429 | borderRadius: BorderRadius.circular( |
| 430 | borderSide: BorderSide(color: Colors |
| 431 | ), |
| 432 | contentPadding: const EdgeInsets. |
| 433 | ), |
| 434 | items: CreateFlightBookingDTOSeatingClassEnum.values |
| 435 | .map((enumValue) { |
| 436 | return DropdownMenuItem( |
| 437 | value: enumValue.name, |
| 438 | child: Text(_formatEnumName(enumValue |
| 439 | ); |
| 440 | }).toList(), |
| 441 | onChanged: _isLoading |
| 442 | ? null |
| 443 | : (value) { |
| 444 | setState(() { |
| 445 | _selectedSeatingClassName = value; |
| 446 | }); |
| 447 | }, |
| 448 | ), |
| 449 | const Gap(16) |
| 450 | // Reason |
| 451 | DropdownButtonFormField<String>( |
| 452 | value: _selectedReasonName, |
| 453 | decoration: InputDecoration( |
| 454 | labelText: 'Reason', |
| 455 | fillColor: Colors.grey[50] |
| 456 | filled: true, |
| 457 | border: OutlineInputBorder( |
| 458 | borderRadius: BorderRadius.circular( |
| 459 | borderSide: BorderSide.none, |
| 460 | ), |
| 461 | enabledBorder: OutlineInputBorder( |
| 462 | borderRadius: BorderRadius.circular( |
| 463 | borderSide: BorderSide(color: Colors |
| 464 | ), |
| 465 | contentPadding: const EdgeInsets. |
| 466 | ), |
| 467 | items: CreateFlightBookingDTOReasonEnum.values |
| 468 | .map((enumValue) { |
| 469 | return DropdownMenuItem( |
| 470 | value: enumValue.name, |
| 471 | child: Text(_formatEnumName(enumValue |
| 472 | ); |
| 473 | }).toList(), |
| 474 | onChanged: _isLoading |
| 475 | ? null |
| 476 | : (value) { |
| 477 | setState(() { |
| 478 | _selectedReasonName = value; |
| 479 | }); |
| 480 | }, |
| 481 | ), |
| 482 | const Gap(16) |
| 483 | // Notes |
| 484 | InputField( |
| 485 | hintText: 'Notes (optional)', |
| 486 | keyboardType: TextInputType.multiline, |
| 487 | controller: _notesController, |
| 488 | maxLines: 4, |
| 489 | minLines: 3, |
| 490 | validator: (value) { |
| 491 | if (value != null && value. |
| 492 | return 'Notes must be 500 characters or less'; |
| 493 | } |
| 494 | return null; |
| 495 | }, |
| 496 | fillColor: Colors.grey[50] |
| 497 | enabledBorder: OutlineInputBorder( |
| 498 | borderRadius: BorderRadius.circular( |
| 499 | borderSide: BorderSide(color: Colors |
| 500 | ), |
| 501 | ), |
| 502 | const Gap(24) |
| 503 | // Save Button |
| 504 | ElevatedButton( |
| 505 | onPressed: _isLoading ? null : _saveBooking, |
| 506 | style: ElevatedButton.styleFrom( |
| 507 | backgroundColor: Colors.black, |
| 508 | foregroundColor: Colors.white, |
| 509 | padding: const EdgeInsets. |
| 510 | shape: RoundedRectangleBorder( |
| 511 | borderRadius: BorderRadius.circular( |
| 512 | ), |
| 513 | ), |
| 514 | child: _isLoading |
| 515 | ? const SizedBox( |
| 516 | height: 20, |
| 517 | width: 20, |
| 518 | child: CircularProgressIndicator( |
| 519 | strokeWidth: 2, |
| 520 | valueColor: |
| 521 | AlwaysStoppedAnimation<Color>(Colors |
| 522 | ), |
| 523 | ) |
| 524 | : Text( |
| 525 | widget.existingBooking != null |
| 526 | ? 'Update Booking' |
| 527 | : 'Save Booking', |
| 528 | style: const TextStyle( |
| 529 | fontSize: 14, |
| 530 | fontWeight: FontWeight.w600, |
| 531 | ), |
| 532 | ), |
| 533 | ), |
| 534 | if (widget.existingBooking != null) ... |
| 535 | const Gap(12) |
| 536 | // Delete Button |
| 537 | OutlinedButton( |
| 538 | onPressed: _isLoading ? null : _deleteBooking, |
| 539 | style: OutlinedButton.styleFrom( |
| 540 | foregroundColor: Colors.red, |
| 541 | side: const BorderSide(color |
| 542 | padding: const EdgeInsets. |
| 543 | shape: RoundedRectangleBorder( |
| 544 | borderRadius: BorderRadius.circular( |
| 545 | ), |
| 546 | ), |
| 547 | child: _isLoading |
| 548 | ? const SizedBox( |
| 549 | height: 20, |
| 550 | width: 20, |
| 551 | child: CircularProgressIndicator( |
| 552 | strokeWidth: 2, |
| 553 | valueColor: |
| 554 | AlwaysStoppedAnimation<Color>(Colors |
| 555 | ), |
| 556 | ) |
| 557 | : const Text( |
| 558 | 'Delete Booking', |
| 559 | style: TextStyle( |
| 560 | fontSize: 14, |
| 561 | fontWeight: FontWeight.w600, |
| 562 | ), |
| 563 | ), |
| 564 | ), |
| 565 | ], |
| 566 | ], |
| 567 | ), |
| 568 | ), |
| 569 | ), |
| 570 | ), |
| 571 | ], |
| 572 | ), |
| 573 | ); |
| 574 | } |
| 575 | } |