import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:generp/Models/ordersModels/paymentListByModeFilterResponse.dart';
import 'package:generp/Models/ordersModels/paymentListByModeResponse.dart';
import 'package:provider/provider.dart';

import '../../Models/ordersModels/paymentDetailsByModeFilterResponse.dart';
import '../../Utils/commonServices.dart';
import '../../services/api_calling.dart';
import '../HomeScreenNotifier.dart';

class Paymentsprovider extends ChangeNotifier {
  TextEditingController approvalRejectionController = TextEditingController();

  String? approvalRejectionError;

  List<Employees> _employeesList = [];
  List<OrderList> _paymentsList = [];
  Employees? _selectedEmployee;
  String? _selectedEmpID;
  String? _selectedEmpName;
  bool _isLoading = true;
  PaymentDetails _paymentsDetails = PaymentDetails();
  List<PaidList> _paidLists = [];
  List<String> _headings = [];
  List<String> _subHeadings = [];

  PaymentDetails get paymentDetails => _paymentsDetails;
  List<PaidList> get paidLists => _paidLists;
  List<String> get Headings => _headings;

  List<String> get subHeadings => _subHeadings;

  List<Employees> get employeesList => _employeesList;

  List<OrderList> get paymentsList => _paymentsList;

  Employees? get selectedEmployee => _selectedEmployee;

  String? get selectedEmpID => _selectedEmpID;

  String? get selectedEmpName => _selectedEmpName;

  bool get isLoading => _isLoading;

  set selectedEmployee(Employees? value) {
    _selectedEmployee = value;
    _selectedEmpID = value?.id!;
    _selectedEmpName = value!.name!;
    notifyListeners();
  }

  set selectedEmpID(String? value) {
    _selectedEmpID = value;
    notifyListeners();
  }

  set selectedEmpName(String? value) {
    _selectedEmpName = value;
    notifyListeners();
  }

  Future<void> paymentsListsByModeFilterAPIFunction(context, mode) async {
    try {
      var provider = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.paymentListByModeFilterAPI(
        provider.empId,
        provider.session,
        mode,
      );
      if (data != null) {
        if (data.error == "0") {
          _employeesList = data.employees!;
          notifyListeners();
        } else {}
      }
    } catch (e, s) {}
  }

  Future<void> paymentsListsByModeAPIFunction(
    context,
    mode,
    teamEmployee,
  ) async {
    try {
      var provider = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.paymentListsByModeAPI(
        provider.empId,
        provider.session,
        mode,
        teamEmployee,
      );
      if (data != null) {
        _paymentsList.clear();
        _isLoading = true;
        notifyListeners();
        if (data.error == "0") {
          _paymentsList = data.orderList!;
          print(data.orderList!.length);
          _isLoading = false;
          if (_selectedEmployee != null &&
              !_employeesList.contains(_selectedEmployee)) {
            _selectedEmployee = null;
            _selectedEmpID = "";
            _selectedEmpName = "";
          }
          notifyListeners();
        } else {
          _isLoading = false;
        }
        notifyListeners();
      }
    } catch (e, s) {}
  }
  
  Future<void> paymentsDetailsByModeAPIFunction(context, paymentId, mode) async {
    try {
      var provider = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.paymentsDetailsByModeAPI(
        provider.empId,
        provider.session,
        paymentId,
      );
      if (data != null) {
        _isLoading = true;
        notifyListeners();
        if (data.error == "0") {
          _paymentsDetails = data.paymentDetails!;
          _paidLists = data.paidList!;
          _headings = ["Account Name",
            "Entered By",
            "Payment Type",
            "Payment Reference Number",
            "Payment Date",
            "Adjusted Amount",
            "Amount",
            "Description",
            "Approved Employee",
            "Approved Remarks",
            "Attachment",
            "Created Datetime",
            "Description",
            "Status",];
          _subHeadings = [
            _paymentsDetails.accountName??"-",
            _paymentsDetails.enteredEmpName??"-",
            _paymentsDetails.paymentType??"-",
            _paymentsDetails.refNo??"-",
            _paymentsDetails.paymentDate??"-",
            _paymentsDetails.adjustedAmount??"-",
            _paymentsDetails.amount??"-",
            _paymentsDetails.description??"-",
            _paymentsDetails.approvalEmployee??"-",
            _paymentsDetails.approvalEmployee??"-",
            _paymentsDetails.attachmentViewFileName??"-",
            _paymentsDetails.datetime??"-",
            _paymentsDetails.status??"-",
          ];

          _isLoading = false;

        } else {
          _isLoading = false;
        }
        notifyListeners();
      }
    } catch (e, s) {}
  }


  Future<void> paymentApprovalRejectionAPIFunction(context,paymentId,type) async {
   try{
     if(!validateApprovalRejectionForm(context)){
       return;
     }
     var provider = Provider.of<HomescreenNotifier>(context, listen: false);
     final data = await ApiCalling.paymentOrderApproveRejectAPI(provider.empId, provider.session, paymentId, type, approvalRejectionController.text);
     if(data!=null){
       if(data.error=="0"){
         toast(context, "${data.message}");
         resetForm();
         Navigator.pop(context, true);
         notifyListeners();
       }
     }

   }catch(e,s){

   }
  }

  Future<void> paymentDeletionAPIFunction(context,paymentId) async {
   try{

     var provider = Provider.of<HomescreenNotifier>(context, listen: false);
     final data = await ApiCalling.paymentOrderDeleteAPI(provider.empId, provider.session, paymentId);
     if(data!=null){
       if(data.error=="0"){
         toast(context, "${data.message}");
         resetForm();
         Navigator.pop(context, true);
         Navigator.pop(context, true);
         notifyListeners();
       }
     }

   }catch(e,s){

   }
  }

  bool validateApprovalRejectionForm(BuildContext context){
    bool isValid = true;
    approvalRejectionError = null;
    if(approvalRejectionController.text.trim().isEmpty){
      approvalRejectionError = "Please Enter Remarks";
      isValid = false;
    }
    notifyListeners();
    return isValid;
  }



  void onChangeApprovalRejection(value){
    approvalRejectionError = "";
    notifyListeners();
  }



  void resetForm(){
    approvalRejectionController.clear();
    approvalRejectionError = "";

  }

  void resetAll() {
    approvalRejectionController.clear();
    approvalRejectionError = "";
    _selectedEmployee = null;
    _selectedEmpID = "";
    _selectedEmpName = "";

    notifyListeners();
  }
}
