import 'dart:convert';
import 'package:cheminova/controller/rd_get_order_service.dart';
import 'package:cheminova/controller/rd_get_single_service.dart';
import 'package:cheminova/models/single_get_order_model.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/rd_get_order_model.dart';
import '../utils/show_snackbar.dart';

class RdSingleOrderController extends GetxController {
  var isLoading = true.obs; // Tracks the loading state
  var productRDOrderSingleList = <SingleGetOrderModel>[].obs; // List of products
  //
  Future<void> fetchAllOrdersAndSingleOrder() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? token = prefs.getString('token');

    final List<PlacedOrdersResponse>? allOrders = await GetProductRDService().getRDProducts(token!);

    if (allOrders == null || allOrders.isEmpty) {
      print("No orders found.");
      showSnackbar("No orders found.");
      return;
    }

    // Display the number of fetched orders
    print("Fetched ${allOrders.length} orders.");

    // Get the ID of the first order and convert it to a String
    final String firstOrderId = allOrders[0].id.toString(); // Convert to String
    print("First order ID: $firstOrderId"); // Debugging: Log the order ID

    // Fetch the single order using the first order ID
    final SingleGetOrderModel? result = await GetSingleProductService().getSingleOrder(token!, firstOrderId);

    // Check if the result is not null
    if (result != null) {
      // Here, you should add the single order to your observable list
      productRDOrderSingleList.clear(); // Clear the previous data if necessary
      productRDOrderSingleList.add(result); // Add the single order to the list
    } else {
      // If the result is null, you can clear the list
      productRDOrderSingleList.clear();
    }

    // For debugging, log the fetched orders count
    print("Fetched orders count: ${productRDOrderSingleList.length}");
  }



  //
  // Future<void> fetchAllOrdersAndSingleOrder() async {
  //   try {
  //     // Fetch all orders from your API
  //       SharedPreferences prefs = await SharedPreferences.getInstance();
  //       String? token = prefs.getString('token');
  //     final response = await GetProductRDService().getRDProducts(token!); // Adjust as per your API
  //
  //     if (response != null && response.isNotEmpty) {
  //       productRDOrderSingleList.value = jsonDecode(response.toString()); // Assuming response is a List of orders
  //     } else {
  //       productRDOrderSingleList;
  //     }
  //   } catch (e) {
  //     print('Error fetching orders: $e');
  //     productRDOrderSingleList; // Ensure the list is empty on error
  //   }
  // }


}

  // Future<void> fetchAllOrdersAndSingleOrder(String id) async {
  //   SharedPreferences prefs = await SharedPreferences.getInstance();
  //   String? token = prefs.getString('token');
  //
  //   final List<PlacedOrdersResponse>? allOrders = await GetProductRDService().getRDProducts(token!);
  //
  //   if (allOrders == null || allOrders.isEmpty) {
  //     print("No orders found.");
  //     showSnackbar("No orders found.");
  //     return;
  //   }
  //
  //   // Display the number of fetched orders
  //   print("Fetched ${allOrders.length} orders.");
  //
  //   // Fetch the single order using the provided selectedOrderId
  //   String orderIdToFetch = allOrders[0].id.toString(); // Use provided ID or first order ID
  //   print("Fetching order ID: $orderIdToFetch"); // Debugging: Log the order ID
  //
  //   final result = (await GetSingleProductService().getSingleOrder(token, orderIdToFetch)) ;
  //
  //   // Check if the result is not null
  //   if (result != null) {
  //     // Here, you should add the single order to your observable list
  //     productRDOrderSingleList.clear(); // Clear the previous data if necessary
  //     productRDOrderSingleList.add(result); // Add the single order to the list
  //   } else {
  //     // If the result is null, you can clear the list
  //     productRDOrderSingleList.clear();
  //   }
  //
  //   // For debugging, log the fetched orders count
  //   print("Fetched orders count: ${productRDOrderSingleList.length}");
  // }








