import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:ride/constant/box_name.dart';
import 'package:ride/constant/links.dart';
import 'package:ride/constant/style.dart';
import 'package:ride/controller/functions/crud.dart';
import 'package:ride/controller/home/map_passenger_controller.dart';
import 'package:ride/main.dart';
import 'package:ride/views/home/Captin/home_captin.dart';
import 'package:ride/views/home/map_page.dart';
import 'package:ride/views/widgets/elevated_btn.dart';

import '../home/captin/home_captain_controller.dart';

class RateController extends GetxController {
  double selectedRateItemId = -1;
  TextEditingController comment = TextEditingController();
  String? rideId, passengerId, driverId;
  late GlobalKey<FormState> formKey;
  @override
  void onInit() {
    formKey = GlobalKey<FormState>();
    passengerId = Get.arguments['passengerId'];
    rideId = Get.arguments['rideId'];
    driverId = Get.arguments['driverId'];

    super.onInit();
  }

  void selectRateItem(double id) {
    selectedRateItemId = id;
    update();
  }

  void addRateToPassenger() async {
    HomeCaptainController homeCaptainController =
        Get.find<HomeCaptainController>();
    if (selectedRateItemId < 1) {
      Get.defaultDialog(
          title: 'You Should choose rate figure'.tr,
          titleStyle: AppStyle.title,
          middleText: '',
          confirm: MyElevatedButton(title: 'Ok', onPressed: () => Get.back()));
    } else {
      await CRUD().post(link: AppLink.addRateToPassenger, payload: {
        'passenger_id': passengerId,
        'driverID': box.read(BoxName.driverID).toString(),
        'rideId': rideId,
        'rating': selectedRateItemId.toString(),
        'comment': comment.text,
      });
      Get.offAll(const HomeCaptain());
      homeCaptainController.isActive = true;
      homeCaptainController.getPaymentToday();
    }
  }

  void addRateToDriver() async {
    if (selectedRateItemId < 1) {
      Get.defaultDialog(
          title: 'You Should choose rate figure'.tr,
          titleStyle: AppStyle.title,
          middleText: '',
          confirm: MyElevatedButton(title: 'Ok', onPressed: () => Get.back()));
    } else {
      await CRUD().post(link: AppLink.addRateToDriver, payload: {
        'passenger_id': box.read(BoxName.passengerID).toString(),
        'driver_id': driverId,
        'ride_id': rideId,
        'rating': selectedRateItemId.toString(),
        'comment': comment.text,
      }).then((value) {
        Get.find<MapPassengerController>().restCounter();
        Get.offAll(const MapPage());
      });
    }
  }
}
