import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import 'package:rounded_loading_button_plus/rounded_loading_button.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../../Data/Repository/Staffs/Addstaff/Addstaffrepository.dart';
import '../../../../Data/Repository/Tenantlocation/Getlocationrepository.dart';
import '../../../../Helper/toast.dart';
import '../../../../Model/Request/Staffs/Addstaffs/Addstaffsrequest.dart';
import '../../../../Model/Response/Staffs/Addstaffs/Addstaffresponse.dart';
import '../../../../Model/Response/Tenantlocation/Getlocationbyidresponse.dart';
import '../../../../View/More/Staffs/Staffview.dart';

class AddStaffController extends GetxController{

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

  int?tenantId;
  int?appLocationId;
  int?partnerId;


  RxInt locationId = 0.obs;


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


  List<TenantLocationDetails>? tenantLocation;

  final RoundedLoadingButtonController btnController = RoundedLoadingButtonController();




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

  GetLocationsRepository getLocationsRepository= GetLocationsRepository();
  AddStaffRepository addStaffRepository = AddStaffRepository();


  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;
      addStaff();
    }
  }




  addStaff()async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    tenantId = prefs.getInt('tenantId');
    appLocationId = prefs.getInt('appLocationId');
    partnerId = prefs.getInt('partnerId');

    addStaffsResult(
      CreateStaffRequest(
          userid:0,
          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:2,
          applocationid:appLocationId,
          partnerid:partnerId,
          tenantid:tenantId,
          locationid:locationId.value
      )
    );

  }

  addStaffsResult(CreateStaffRequest data)async{
    CreateStaffResponse? result  = await addStaffRepository.addStaff(data);
    if(result?.status == true){
      Get.to(()=>StaffsView());
      nameController.clear();
      lastNameController.clear();
      phoneNoController.clear();
      emailController.clear();
      locationId.value = 0;
    }

  }


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