import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:provider/provider.dart';

import '../../Models/crmModels/LeadDetailsResponse.dart';
import '../../services/api_calling.dart';
import '../HomeScreenNotifier.dart';

class crmLeadDetailsProvider extends ChangeNotifier {

  bool _isLoading = false;

  TextEditingController nameController = TextEditingController();
  TextEditingController designationController = TextEditingController();
  TextEditingController mobileNumberController = TextEditingController();
  TextEditingController alternativeMobileController = TextEditingController();
  TextEditingController telephoneController = TextEditingController();
  TextEditingController emailController = TextEditingController();

  TextEditingController editNameController = TextEditingController();
  TextEditingController editDesignationController = TextEditingController();
  TextEditingController editMobileNumberController = TextEditingController();
  TextEditingController editAlternativeMobileController = TextEditingController();
  TextEditingController editTelephoneController = TextEditingController();
  TextEditingController editEmailController = TextEditingController();

  String? _nameError;
  String? _designationError;
  String? _mobileNumError;
  String? _altMobError;
  String? _teleError;
  String? _emailError;

  String? _editNameError;
  String? _editDesignationError;
  String? _editMobileNumError;
  String? _editAltMobError;
  String? _editTeleError;
  String? _editEmailError;

  LeadDetails _leadDetails = LeadDetails();
  AccountDetails _accountDetails = AccountDetails();
  List<LeadProducts> _leadProducts = [];
  List<ContactDetails> _contactDetails = [];
  List<FollowupDetails> _followupDetails = [];
  List<AppointmentDetails> _appointmentDetails = [];
  List<QuotationsDetails> _quotationsDetails = [];
  List<String> _headings = [];
  List<String> _subHeadings = [];

  String? get nameError => _nameError;
  String? get designationError => _designationError;
  String? get mobileNumError => _mobileNumError;
  String? get altMobError => _altMobError;
  String? get teleError => _teleError;
  String? get emailError => _emailError;

  String? get editNameError => _editNameError;
  String? get editDesignationError => _editDesignationError;
  String? get editMobileNumError => _editMobileNumError;
  String? get editAltMobError => _editAltMobError;
  String? get editTeleError => _editTeleError;
  String? get editEmailError => _editEmailError;

  bool get isLoading => _isLoading;

  LeadDetails get leadDetails => _leadDetails;

  AccountDetails get accountDetails => _accountDetails;

  List<LeadProducts> get leadProducts => _leadProducts;

  List<ContactDetails> get contactDetails => _contactDetails;

  List<FollowupDetails> get followupDetails => _followupDetails;

  List<AppointmentDetails> get appointmentDetails => _appointmentDetails;

  List<QuotationsDetails> get quotationsDetails => _quotationsDetails;

  List<String> get Headings => _headings;

  List<String> get subHeadings => _subHeadings;

  set nameError(String? value){
    _nameError = value;
    notifyListeners();
  }
  set designationError(String? value){
    _designationError = value;
    notifyListeners();
  }
  set mobileNumError(String? value){
    _mobileNumError = value;
    notifyListeners();
  }
  set altMobError(String? value){
    _altMobError = value;
    notifyListeners();
  }
  set teleError(String? value){
    _teleError = value;
    notifyListeners();
  }
  set emailError(String? value){
    _emailError = value;
    notifyListeners();
  }


  set editNameError(String? value){
    _editNameError = value;
    notifyListeners();
  }
  set editDesignationError(String? value){
    _editDesignationError = value;
    notifyListeners();
  }
  set editMobileNumError(String? value){
    _editMobileNumError = value;
    notifyListeners();
  }
  set editAltMobError(String? value){
    _editAltMobError = value;
    notifyListeners();
  }
  set editTeleError(String? value){
    _editTeleError = value;
    notifyListeners();
  }
  set editEmailError(String? value){
    _editEmailError = value;
    notifyListeners();
  }

  set isLoading(bool value){
    _isLoading = value;
    notifyListeners();
  }

  Future<void> crmLeadDetailsAPIFunction(context, leadID, mode) async {
    try {
      var HomeProv = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.crmLeadDetailsAPI(
        HomeProv.empId,
        HomeProv.session,
        leadID,
        mode,
      );
      _isLoading = true;
      notifyListeners();
      if (data != null) {
        if (data.error == "0") {
          _leadDetails = data.leadDetails!;
          _accountDetails = data.accountDetails!;
          _leadProducts = data.leadProducts!;
          _contactDetails = data.contactDetails!;
          _followupDetails = data.followupDetails!;
          _appointmentDetails = data.appointmentDetails!;
          _quotationsDetails = data.quotationsDetails!;
          _headings = [
            "Address",
            "Account Manager Name",
            "Account Created By",
            "State",
            "District",
            "Sub Locality",
            "Source",
            "Reference",
            "Team",
            "Segment",
            "Lead Status",
            "Lead Age",
          ];
          _subHeadings = [
            data.leadDetails!.address??"-",
            data.accountDetails!.accManager??"-",
            data.accountDetails!.owner??"-",
            data.accountDetails!.state??"-",
            data.accountDetails!.district??"-",
            data.accountDetails!.subLocality??"-",
            data.accountDetails!.source??"-",
            data.accountDetails!.reference??"-",
            data.accountDetails!.team??"-",
            data.accountDetails!.segment??"-",
            data.leadDetails!.status??"-",
            "${data.leadDetails!.lage??"-"} days",
          ];
          _isLoading = false;
          notifyListeners();
        }else{
          _isLoading = false;
          notifyListeners();
        }
      }else{
        _isLoading = false;
        notifyListeners();
      }
    } catch (e, s) {
      _isLoading = false;
      notifyListeners();
    }
  }

  Future<void> crmLeadDetailsAddContactAPIFunction(context, accID) async {
    try {
      var prov = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.crmLeadDetailsAddContactAPI(prov.empId, prov.session, accID,
          nameController.text, mobileNumberController.text, designationController.text, alternativeMobileController.text, telephoneController.text, emailController.text);
      _isLoading = true;
      notifyListeners();
      if (data != null) {
        if (data.error == "0") {
          Navigator.pop(context,true);
          resetAll();
          _isLoading = false;
          notifyListeners();
        }else{
          _isLoading = false;
          notifyListeners();
        }
      }else{
        _isLoading = false;
        notifyListeners();
      }
    } catch (e, s) {
      _isLoading = false;
      notifyListeners();
    }
  }

  Future<void> crmLeadDetailsEditContactAPIFunction(context,contactID) async {
    try {
      var prov = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.crmLeadDetailsEditContactAPI(prov.empId, prov.session, contactID,
          nameController.text, mobileNumberController.text, designationController.text, alternativeMobileController.text, telephoneController.text, emailController.text);
      _isLoading = true;
      notifyListeners();
      if (data != null) {
        if (data.error == "0") {
          _isLoading = false;
          Navigator.pop(context,true);
          resetAll();
          _isLoading = false;
          notifyListeners();
        }else{
          _isLoading = false;
          notifyListeners();
        }
      }else{
        _isLoading = false;
        notifyListeners();
      }
    } catch (e, s) {
      _isLoading = false;
      notifyListeners();
    }
  }



  void onChangeName(value){
    _nameError = "";
    notifyListeners();
  }
  void onChangeDesignation(value){
    _designationError = "";
    notifyListeners();
  }
  void onChangeMobile(value){
    _mobileNumError = "";
    notifyListeners();
  }
  void onChangeAltMobile(value){
    _altMobError = "";
    notifyListeners();
  }
  void onChangeTelephone(value){
    _teleError = "";
    notifyListeners();
  }
  void onChangeEmailId(value){
    _emailError = "";
    notifyListeners();
  }

  void onChangeEditName(value){
    _editNameError = "";
    notifyListeners();
  }
  void onChangeEditDesignation(value){
    _editDesignationError = "";
    notifyListeners();
  }
  void onChangeEditMobile(value){
    _editMobileNumError = "";
    notifyListeners();
  }
  void onChangeEditAltMobile(value){
    _editAltMobError = "";
    notifyListeners();
  }
  void onChangeEditTelephone(value){
    _editTeleError = "";
    notifyListeners();
  }
  void onChangeEditEmailId(value){
    _editEmailError = "";
    notifyListeners();
  }

  void resetAll(){
    nameController.clear();
    designationController.clear();
    mobileNumberController.clear();
    alternativeMobileController.clear();
    telephoneController.clear();
    emailController.clear();
    editNameController.clear();
    editDesignationController.clear();
    editMobileNumberController.clear();
    editAlternativeMobileController.clear();
    editTelephoneController.clear();
    editEmailController.clear();
    _nameError = "";
    _designationError = "";
    _mobileNumError = "";
    _altMobError = "";
    _teleError = "";
    _emailError = "";
    _editNameError = "";
    _editDesignationError = "";
    _editMobileNumError = "";
    _editAltMobError = "";
    _editTeleError = "";
    _editEmailError = "";
    notifyListeners();
  }
}
