import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:rounded_loading_button_plus/rounded_loading_button.dart';
import '../../../../Data/Repository/Staffs/Updatestaff/Updatestaffrepository.dart';
import '../../../../Data/Repository/Tenantlocation/Getlocationrepository.dart';
import '../../../../Helper/toast.dart';
import '../../../../Model/Request/Staffs/UpdateStaff/Updatestaffrequest.dart';
import '../../../../Model/Response/Staffs/Getstaffsresponse.dart';
import '../../../../Model/Response/Staffs/Updatestaff/Updatestaffresponse.dart';
import '../../../../Model/Response/Tenantlocation/Getlocationbyidresponse.dart';
import '../../../../View/More/Staffs/Staffview.dart';

class UpdateStaffController extends GetxController{

  String? locationName;
  String? locationAddress;
  String? locationSuburb;
  String? locationCity;
  String? locationState;
  String? locationPostcode;

  int?tenantId;
  int?appLocationId;
  int?partnerId;
  int?userId;
  int?roleId;


  RxInt locationId = 0.obs;


  RxBool isLocation = false.obs;
  RxBool progress = false.obs;

  StaffDetails? staffData;

  List<TenantLocationDetails>? tenantLocation;

  TextEditingController nameController = TextEditingController();
  TextEditingController lastNameController = TextEditingController();
  TextEditingController phoneNoController = TextEditingController();
  TextEditingController emailController = TextEditingController();

  GetLocationsRepository getLocationsRepository= GetLocationsRepository();

  UpdateStaffRepository updateStaffRepository = UpdateStaffRepository();

  final RoundedLoadingButtonController btnController = RoundedLoadingButtonController();



  setValues(){
    nameController.text = staffData?.firstname??'';
    lastNameController.text = staffData?.lastname??'';
    phoneNoController.text = staffData?.contactno??'';
    emailController.text = staffData?.email??'';
    locationId.value = staffData?.locationid??0;
    tenantId = staffData?.tenantid??0;
    appLocationId = staffData?.applocationid??0;
    partnerId = staffData?.partnerid??0;
    userId = staffData?.userid??0;
    roleId = staffData?.roleid??0;
    update();
  }

  getLocation()async {
    GetLocationByTenantId? result = await getLocationsRepository.getTenantLocationById();
    tenantLocation = result?.details??[];
    update();
    print('resultgetLocation${result?.toJson()}');
  }


  describeValidation({bool isUpdate = false}) {
    if (nameController.text.isEmpty) {
      btnController.reset();
      Toast.showToast("Please Enter first name");
      // AwesomeHelper.createAwesome(title: "title", message: "message");

    } else if (lastNameController.text.isEmpty) {
      btnController.reset();
      Toast.showToast("Please Enter last name");
    } else if (phoneNoController.text.isEmpty) {
      btnController.reset();
      Toast.showToast("Please Enter contact no");
    } else if (emailController.text.isEmpty) {
      btnController.reset();
      // Toast.showToast("Please enter email");
      Toast.showToast("Please Enter email");
    } else if (RegExp(
        r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
        .hasMatch(emailController.text) ==
        false) {
      btnController.reset();
      // Toast.showToast("Enter valid email");
      Toast.showToast("Please enter vaid email address");
    } else if (locationId.value ==0) {
      btnController.reset();
      Toast.showToast("Please select location}");
    } else {

      progress.value = true;
      updateStaff();
    }
  }

  updateStaff()async {
    updateStaffResult(
        UpdateStaffRequest(
            userid:userId,
            authname:"${emailController.text}",
            configid: 1,
            contactno:"${phoneNoController.text}",
            firstname:"${nameController.text}",
            lastrname:"${lastNameController.text}",
            email:"${emailController.text}",
            address:"$locationAddress",
            suburb:"$locationSuburb",
            city:"$locationCity",
            state:"$locationState",
            postcode:"$locationPostcode",
            roleid:roleId,
            applocationid:appLocationId,
            partnerid:partnerId,
            tenantid:tenantId,
            locationid:locationId.value
        )
    );

  }

  updateStaffResult(UpdateStaffRequest data)async{
    UpdateStaffResponse? result = await updateStaffRepository.updateStaff(data);
    if(result?.status == true){
      Get.to(()=>StaffsView());
    }
  }


  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
  }
}