import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:generp/Models/crmModels/crmDashboardResponse.dart';
import 'package:provider/provider.dart';

import '../../Models/crmModels/AccessiblePagesResponse.dart';
import '../../Models/crmModels/crmPendingTasksResponse.dart';
import '../../Models/crmModels/crmUniversalSearchResponse.dart';
import '../../services/api_calling.dart';
import '../HomeScreenNotifier.dart';

class Crmdashboardprovider extends ChangeNotifier{
  TextEditingController searchController = TextEditingController();
  List<PagesAccessible> _accessPages = [];
  List<PagesAccessible> get accessPages => _accessPages;
  List<PendingTasks> _pendingTasks = [];
  List<PendingTasks> get pendingTasksLists => _pendingTasks;
  List<Accounts> _accountsList = [];
  List<Leads> _leadsList = [];
  List<Enquires> _enquiresList = [];

  List<Accounts> get accountsList => _accountsList;
  List<Leads> get leadsList => _leadsList;
  List<Enquires> get enquiresList => _enquiresList;

  Hotleads _hotleads = Hotleads();
  Hotleads get hotleads => _hotleads;
  Hotleads _coldleads = Hotleads();
  Hotleads get coldleads => _coldleads;
  Hotleads _warmleads = Hotleads();
  Hotleads get warmleads => _warmleads;
  Hotleads _totalleads = Hotleads();
  Hotleads get totalleads => _totalleads;
  Hotleads _orderlost = Hotleads();
  Hotleads get orderlost => _orderlost;
  Hotleads _norequirement = Hotleads();
  Hotleads get norequirement => _norequirement;
  Hotleads _openEnquiries = Hotleads();
  Hotleads get openEnquiries => _openEnquiries;

  List<Hotleads> _allLeads = [];
  List<Hotleads> get allLeads => _allLeads;

  Future<void>accessPagesAPIFunction(context) async {
    try{
      var HomeProv = Provider.of<HomescreenNotifier>(context,listen: false);
      final data = await ApiCalling.crmAccessiblePagesAPI(HomeProv.empId, HomeProv.session);
      if(data!=null){
        if(data.error=="0"){
          _accessPages = data.pagesAccessible!;
          _accessPages.add(PagesAccessible(mode: "",id: 0,pageName: "Nearby Leads"));
          notifyListeners();
        }
      }

    }catch (e,s){

    }
  }

  Future<void> crmDashboardAPIFunction(context, mode,
  from,to,teamemployee) async {
    try{
      var HomeProv = Provider.of<HomescreenNotifier>(context,listen: false);
      final data = await ApiCalling.crmDashboardAPI(HomeProv.empId, HomeProv.session, mode,
          from,to,teamemployee);
      if(data!=null){
        if(data.error=="0"){
          _hotleads = data.hotleads!;
          _coldleads = data.coldleads!;
          _warmleads = data.warmleads!;
          _totalleads = data.totalleads!;
          _orderlost = data.orderlost!;
          _norequirement = data.norequirement!;
          _openEnquiries = data.openEnquiries!;
          _allLeads = [
            _hotleads,
            _coldleads,
            _warmleads,
            _totalleads,
            _orderlost,
            _norequirement,
            _openEnquiries,
          ];
          notifyListeners();
        }
      }

    }catch (e,s){

    }
  }


 Future<void> crmPendingTasksAPIFunction(context) async {
    try{
      var HomeProv = Provider.of<HomescreenNotifier>(context,listen: false);
      final data = await ApiCalling.crmPendingTasksAPI(HomeProv.empId, HomeProv.session);
      if(data!=null){
        if(data.error=="0"){
          _pendingTasks = data.pendingTasks!;
          notifyListeners();
        }
      }

    }catch (e,s){

    }
  }

 Future<void> crmUniversalSearchFunction(context) async {
    try{
      var HomeProv = Provider.of<HomescreenNotifier>(context,listen: false);
      final data = await ApiCalling.crmUniversalSearchAPI(HomeProv.empId, HomeProv.session,searchController.text);
      if(data!=null){
        if(data.error=="0"){
        _accountsList = data.accounts??[];
         _leadsList = data.leads??[];
         _enquiresList = data.enquires??[];
          notifyListeners();
        }
      }

    }catch (e,s){

    }
  }

}