import 'package:cheminova/controller/get_dispatch_service.dart';
import 'package:cheminova/models/get_dispatch_model.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

class GetDispatchController extends GetxController{
  var isLoading = true.obs; // Tracks the loading state
  var productProcessingRDList = <GetDispatchModel>[].obs; // Li
  // Fetch the products from the API
  Future<void> getRDDispatchInvoiceProduct() async {
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String? token = prefs.getString('token');
      isLoading(true); // Start loading
      final  response = await GetDispatchService().getRDDispatchedProduct(token!); // Fetch products from API
      if (response != null) {
        productProcessingRDList.assignAll(response); // Assign products to the observable list
      }
    } finally {
      isLoading(false); // End loading
    }
  }


  Future<void> RDProcessingToDispatchProduct(String orderId, String courierName,String couriertrackingId) async {
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String? token = prefs.getString('token'); // Get the token

      if (token == null || token.isEmpty) {
        throw Exception("Token is missing. Please login again.");
      }

      isLoading(true); // Show loading indicator

      // Call the service function and pass the token, orderId, and reason
      await GetDispatchService().RDProcessingToDispatchOrder(token, orderId, courierName,couriertrackingId);

      // Optionally refresh the data or show success message
      print("Order Dispatched  process complete.");
    } catch (e) {
      print("Error: $e");
    } finally {
      isLoading(false); // Hide loading indicator
    }
  }





  Future<void> RDDispatchToDeliveredProduct(String orderId) async {
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String? token = prefs.getString('token'); // Get the token

      if (token == null || token.isEmpty) {
        throw Exception("Token is missing. Please login again.");
      }

      isLoading(true); // Show loading indicator

      // Call the service function and pass the token, orderId, and reason
      await GetDispatchService().RDDispatchToDeliveredOrder(token, orderId,);

      // Optionally refresh the data or show success message
      print("Order Delivered  process complete.");
    } catch (e) {
      print("Error: $e");
    } finally {
      isLoading(false); // Hide loading indicator
    }
  }

}