saveInformation method

dynamic saveInformation()

Implementation

saveInformation() async {
  String phoneNumber = countryCode.value + phoneNumberController.text;
  print(
      "${phoneNumber.toString()} current no ${FirebaseAuth.instance.currentUser?.phoneNumber}");
  if (phoneNumber != FirebaseAuth.instance.currentUser?.phoneNumber) {
    bool response = await updatePhoneNumber(phoneNumber);
    if (response == false) {
      return;
    }
  }
  if (firstNameController.text.trim().length < 2) {
    CustomFlashWidget.showFlashMessage(
      type: FlashType.error,
      title: "Invalid first name",
      message: "Please enter a valid first name",
    );
    // DefaultSnackbar.show(
    //     "Invalid first name", "Please enter a valid first name");
    return;
  } else if (lastNameController.text.trim().length < 2) {
    CustomFlashWidget.showFlashMessage(
      type: FlashType.error,
      title: "Invalid last name",
      message: "Please enter a valid last name",
    );
    // DefaultSnackbar.show(
    //     "Invalid last name", "Please enter a valid last name");
    return;
  } else if (!RegExp(r'^[0-9+]+$')
      .hasMatch(countryCode.value + phoneNumberController.text.trim())) {
    CustomFlashWidget.showFlashMessage(
      type: FlashType.error,
      title: "Invalid phone number",
      message: "Please enter a valid phone number",
    );
    return;
  }
  // else if (passportController.text.trim().isEmpty||passportIssuingCountry.value == "") {
  //   AddPassportDetailsDialog().show();
  //   return;
  // }

  // String dobText = dobController.text.trim();
  //
  // if (dobText.isEmpty) {
  //   CustomFlashWidget.showFlashMessage(
  //     type: FlashType.error,
  //     title: "Invalid DOB",
  //     message: "Please enter your valid DOB",
  //   );
  //   return;
  // }
  //
  // DateTime? dob = DateTime.tryParse(dobText);
  // if (dob == null) {
  //   CustomFlashWidget.showFlashMessage(
  //     type: FlashType.error,
  //     title: "Invalid DOB",
  //     message: "Please enter a valid date format (YYYY-MM-DD).",
  //   );
  //   return;
  // }
  //
  // DateTime today = DateTime.now();
  // int age = today.year - dob.year;
  // if (today.month < dob.month || (today.month == dob.month && today.day < dob.day)) {
  //   age--;
  // }
  //
  // if (age < 18) {
  //   CustomFlashWidget.showFlashMessage(
  //     type: FlashType.error,
  //     title: "Invalid DOB",
  //     message: "You must be at least 18 years old.",
  //   );
  //   return;
  // }
  //
  //
  //   else if (passportExpiryController.text.trim().isEmpty) {
  //     CustomFlashWidget.showFlashMessage(
  //       type: FlashType.error,
  //       title: "Invalid passport expiry date",
  //       message: "Please enter your valid passport expiry date",
  //     );
  //     return;
  //   } else {
  //     DateTime? expiryDate =
  //         DateTime.tryParse(passportExpiryController.text.trim());
  //
  //     if (expiryDate == null) {
  //       CustomFlashWidget.showFlashMessage(
  //         type: FlashType.error,
  //         title: "Invalid passport expiry date",
  //         message: "Please enter a valid date format (YYYY-MM-DD).",
  //       );
  //       return;
  //     }
  //
  //     DateTime currentDate = DateTime.now();
  //     DateTime oneMonthLater = currentDate.add(const Duration(days: 30));
  //     if (expiryDate.isBefore(oneMonthLater)) {
  //         openDialog(const PassportExpiredDialog());
  //       return;
  //     }
  //   }
  //
  // else if (cityController.text.trim().isEmpty) {
  //   DialogAlert.show(
  //     dialogType: DialogType.failure,
  //     title: "Invalid city",
  //     message: "Please enter a valid city name",
  //   );
  //   // DefaultSnackbar.show("Invalid city", "Please enter a valid city name");
  //   return;
  // } else if (addressLine1Controlller.text.trim().isEmpty) {
  //   DialogAlert.show(
  //     dialogType: DialogType.failure,
  //     title: "Invalid address line 1",
  //     message: "Please enter a valid address line 1",
  //   );
  //   // DefaultSnackbar.show(
  //   //     "Invalid address line 1", "Please enter a valid address line 1");
  //   return;
  // } else if (addressLine2Controlller.text.trim().isEmpty) {
  //   DialogAlert.show(
  //     dialogType: DialogType.failure,
  //     title: "Invalid address line 2",
  //     message: "Please enter a valid address line 2",
  //   );
  //   // DefaultSnackbar.show(
  //   //     "Invalid address line 2", "Please enter a valid address line 2");
  //   return;
  // } else if (country.value == "") {
  //   DialogAlert.show(
  //     dialogType: DialogType.failure,
  //     title: "Invalid country",
  //     message: "Please select your country name",
  //   );
  //   // DefaultSnackbar.show(
  //   //     "Invalid country", "Please select your country name");
  //   return;
  // } else if (residentCountry.value == "") {
  //   DialogAlert.show(
  //     dialogType: DialogType.failure,
  //     title: "Invalid resident country",
  //     message: "Please select resident country",
  //   );
  //   // DefaultSnackbar.show(
  //   //     "Invalid resident country", "Please select resident country");
  //   return;
  // } else if (selectedGender.value == "") {
  //   DialogAlert.show(
  //     dialogType: DialogType.failure,
  //     title: "Invalid gender",
  //     message: "Please select your gender",
  //   );
  //   // DefaultSnackbar.show("Invalid gender", "Please select your gender");
  //   return;
  // } else if (passportController.text.trim().isEmpty) {
  //   DialogAlert.show(
  //     dialogType: DialogType.failure,
  //     title: "Invalid passport number",
  //     message: "Please enter your valid passport number",
  //   );
  //   // DefaultSnackbar.show(
  //   //     "Invalid passport number", "Please enter your valid passport number");
  //   return;
  // } else if (passportIssuingCountry.value == "") {
  //    DialogAlert.show(
  //     dialogType: DialogType.failure,
  //     title: "Invalid passport issuing country",
  //     message: "Please select your valid passport issuing country",
  //   );
  //   // DefaultSnackbar.show("Invalid passport issuing country",
  //   //     "Please select your valid passport issuing country");
  //   return;
  // }  else if (nationality.value == "") {
  //   DialogAlert.show(
  //     dialogType: DialogType.failure,
  //     title: "Invalid nationlity",
  //     message: "Please select your valid nationality",
  //   );
  //   // DefaultSnackbar.show(
  //   //     "Invalid nationlity", "Please select your valid nationality");
  //   return;
  // }

  try {
    print("value-residentCountry: ${residentCountry.value}");
    // final fullName = fullNameController.text.trim();
    final firstName = firstNameController.text.trim();
    final middleName = middleNameController.text.trim();
    final lastName = lastNameController.text.trim();
    final city = cityController.text.trim();
    final addressLane1 = addressLine1Controlller.text.trim();
    final addressLane2 = addressLine2Controlller.text.trim();
    final countryy = country.value;
    final residentCountryy = residentCountry.value;
    // final genderr = selectedGender.value;
    // final passportIssuingCountryy = passportIssuingCountry.value;
    // final passportNumber = passportController.text.trim();
    // final passportExpiry = passportExpiryController.text.trim().split('T')[0];
    // final dob = dobController.text.trim().split('T')[0];
    // final nationalityy = nationality.value;
    // print(
    //     "DOB-Error: ${dobController.text.trim().split('T')[0]} || FULL: ${dobController.text.trim()}");

    var data = {
      "firstName": firstName,
      "middleName": middleName,
      "lastName": lastName,
      "phone": phoneNumberController.text.trim(),
      "city": city,
      "countryCode": countryCode.value,
      "country": Constants.ALL_COUNTRIES_ALPHA_2[countryy],
      "addressLine1": addressLane1,
      "addressLine2": addressLane2,
      "residentCountry": Constants.ALL_COUNTRIES_ALPHA_2[residentCountryy],
      // "passportInfo": {
      //   "gender": genderr.toLowerCase(),
      //   "passportNo": passportNumber,
      //   "passportIssuingCountry":
      //       Constants.ALL_COUNTRIES_ALPHA_2[passportIssuingCountryy],
      //   "passportExpiry": passportExpiry,
      //   "dob": dob,
      //   "nationality": nationalityy,
      // }
    };
    // data: {
    //   'firstName': firstName,
    //   'lastName': lastName,
    //   'phone': phoneNumberController.text.trim(),
    //   'countryCode': countryCode.value,
    // },
    final response =
        await Requests.getDio().put('/users/update-user', data: data);
    log(data.toString());

    if (response.statusCode == 200) {
      passengerController.getPassengers();
      await authenticationController.getUserProfile();

      CustomFlashWidget.showFlashMessage(
          title: "Success",
          message: "Updated user information successfully",
          type: FlashType.success);
    } else if (response.statusCode == 400) {
      log('Error updating profilvb e info: ${response.data}');

      CustomFlashWidget.showFlashMessage(
        type: FlashType.error,
        title: "Error",
        message: response.data['message'] ??
            "Please ensure that you have entered all the mandatory fields",
      );
    } else {
      log('Error updating profilvb e info: ${response.data}');

      CustomFlashWidget.showFlashMessage(
        type: FlashType.error,
        title: "Error",
        message: response.data['message'] ??
            "Error while updating user information",
      );
    }
  } catch (_) {
    log('Exception updating profile info: ${_.toString()}');

    CustomFlashWidget.showFlashMessage(
      type: FlashType.error,
      title: "Error",
      message: "Error while updating user information",
    );
  }
}