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;

  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 = [];

  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 isLoading(bool value){
    _isLoading = value;
    notifyListeners();
  }

  Future<void> crmLeadDetailsAPI(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??"-",
          ];
          _isLoading = false;
          notifyListeners();
        }else{
          _isLoading = false;
          notifyListeners();
        }
      }else{
        _isLoading = false;
        notifyListeners();
      }
    } catch (e, s) {
      _isLoading = false;
      notifyListeners();
    }
  }
  void resetAll(){

  }
}
