import 'dart:convert'; import 'package:flutter/cupertino.dart'; import 'package:flutter_overlay_window/flutter_overlay_window.dart'; import 'package:get/get.dart'; import 'package:sefer_driver/constant/links.dart'; import 'package:sefer_driver/main.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'dart:math' as math; import '../../../constant/box_name.dart'; import '../../../print.dart'; import '../../functions/audio_controller.dart'; import '../../functions/crud.dart'; import '../../functions/location_controller.dart'; import 'home_captain_controller.dart'; class OrderRequestController extends GetxController { double progress = 0; double progressSpeed = 0; int duration = 15; int durationSpeed = 20; int remainingTime = 0; int remainingTimeSpeed = 0; String countRefuse = '0'; bool applied = false; final locationController = Get.put(LocationController()); BitmapDescriptor startIcon = BitmapDescriptor.defaultMarker; BitmapDescriptor endIcon = BitmapDescriptor.defaultMarker; final arguments = Get.arguments; var myList; late int hours; late int minutes; @override Future onInit() async { // AudioController audioController = Get.put(AudioController()); print('onInit called'); await initilizeOrderPage(); bool isOverlayActive = await FlutterOverlayWindow.isActive(); if (isOverlayActive) { await FlutterOverlayWindow.closeOverlay(); } // audioController.playAudio(); // getRefusedOrderByCaptain(); addCustomStartIcon(); addCustomEndIcon(); // calculateConsumptionFuel(); update(); super.onInit(); } late LatLngBounds bounds; late List pointsDirection; late String body; late double latPassengerLocation; late double lngPassengerLocation; late double lngPassengerDestination; late double latPassengerDestination; Future initilizeOrderPage() async { final myListString = Get.arguments['myListString']; if (Get.arguments['DriverList'] == null || Get.arguments['DriverList'].isEmpty) { myList = jsonDecode(myListString); Log.print('myList: ${myList}'); } else { myList = Get.arguments['DriverList']; Log.print('myList1: ${myList}'); } body = Get.arguments['body']; Duration durationToAdd = Duration(seconds: (double.tryParse(myList[4]) ?? 0).toInt()); hours = durationToAdd.inHours; minutes = (durationToAdd.inMinutes % 60).round(); startTimerSpeed(myList[6].toString(), body.toString()); // Instead of splitting, directly use the values from the list // First coordinate pair is at index 0 and 1 latPassengerLocation = double.tryParse(myList[0]) ?? 0.0; lngPassengerLocation = double.tryParse(myList[1]) ?? 0.0; // Second coordinate pair is at index 2 and 3 latPassengerDestination = double.tryParse(myList[2]) ?? 0.0; lngPassengerDestination = double.tryParse(myList[3]) ?? 0.0; pointsDirection = [ LatLng(latPassengerLocation, lngPassengerLocation), LatLng(latPassengerDestination, lngPassengerDestination) ]; // Calculate bounds double minLatitude = math.min(pointsDirection[0].latitude, pointsDirection[1].latitude); double maxLatitude = math.max(pointsDirection[0].latitude, pointsDirection[1].latitude); double minLongitude = math.min(pointsDirection[0].longitude, pointsDirection[1].longitude); double maxLongitude = math.max(pointsDirection[0].longitude, pointsDirection[1].longitude); bounds = LatLngBounds( southwest: LatLng(minLatitude, minLongitude), northeast: LatLng(maxLatitude, maxLongitude), ); update(); } getRideDEtailsForBackgroundOrder(String rideId) async { await CRUD().get(link: AppLink.getRidesDetails, payload: { 'id': rideId, }); } void addCustomStartIcon() async { // Create the marker with the resized image ImageConfiguration config = ImageConfiguration( size: const Size(30, 30), devicePixelRatio: Get.pixelRatio); BitmapDescriptor.asset( config, 'assets/images/A.png', ).then((value) { startIcon = value; update(); }); } void addCustomEndIcon() { ImageConfiguration config = ImageConfiguration( size: const Size(30, 30), devicePixelRatio: Get.pixelRatio); BitmapDescriptor.asset( config, 'assets/images/b.png', ).then((value) { endIcon = value; update(); }); } void changeApplied() { applied = true; update(); } double mpg = 0; calculateConsumptionFuel() { mpg = Get.find().fuelPrice / 12; //todo in register car add mpg in box } bool _timerActive = false; Future startTimer(String driverID, String orderID) async { _timerActive = true; for (int i = 0; i <= duration && _timerActive; i++) { await Future.delayed(const Duration(seconds: 1)); progress = i / duration; remainingTime = duration - i; update(); } if (remainingTime == 0 && _timerActive) { if (applied == false) { endTimer(); refuseOrder(orderID); } } } void endTimer() { _timerActive = false; } void startTimerSpeed(String driverID, orderID) async { for (int i = 0; i <= durationSpeed; i++) { await Future.delayed(const Duration(seconds: 1)); progressSpeed = i / durationSpeed; remainingTimeSpeed = durationSpeed - i; update(); } if (remainingTimeSpeed == 0) { if (applied == false) { Get.back(); // refuseOrder(box.read(BoxName.driverID), orderID); } } } void refuseOrder( orderID, ) async { await CRUD().postFromDialogue(link: AppLink.addDriverOrder, payload: { //TODO need review 'driver_id': box.read(BoxName.driverID), // box.read(BoxName.driverID).toString(), 'order_id': orderID, 'status': 'Refused' }); await CRUD().post(link: AppLink.updateRides, payload: { 'id': orderID, // 'rideTimeStart': DateTime.now().toString(), 'status': 'Refused', 'driver_id': box.read(BoxName.driverID), }); if (AppLink.endPoint != AppLink.seferCairoServer) { CRUD().post(link: '${AppLink.endPoint}/rides/update.php', payload: { 'id': orderID, // 'rideTimeStart': DateTime.now().toString(), 'status': 'Refused', 'driver_id': box.read(BoxName.driverID), }); } // applied = true; // if (box.read(BoxName.gender).toString() != 'Female') { // sql.insertData({ // 'order_id': orderID, // 'created_at': DateTime.now().toString(), // 'driver_id': box.read(BoxName.driverID).toString(), // }, TableName.driverOrdersRefuse); // getRefusedOrderByCaptain(); // } update(); // Get.back(); // Get.offAll(HomeCaptain()); } addRideToNotificationDriverString( orderID, String startLocation, String endLocation, String date, String time, String price, String passengerId, String status, String carType, String passengerRate, String priceForPassenger, String distance, String duration, ) async { await CRUD().post(link: AppLink.addWaitingRide, payload: { 'id': orderID, 'start_location': startLocation, 'end_location': endLocation, 'date': date, 'time': time, 'price': price, 'passenger_id': passengerId, 'status': status, 'carType': carType, 'passengerRate': passengerRate, 'price_for_passenger': priceForPassenger, 'distance': distance, 'duration': duration, }); if (AppLink.endPoint != AppLink.seferCairoServer) { CRUD().post( link: '${AppLink.endPoint}/notificationCaptain/addWaitingRide.php', payload: { 'id': orderID, 'start_location': startLocation, 'end_location': endLocation, 'date': date, 'time': time, 'price': price, 'passenger_id': passengerId, 'status': status, 'carType': carType, 'passengerRate': passengerRate, 'price_for_passenger': priceForPassenger, 'distance': distance, 'duration': duration, }); } } }