

import 'package:cheminova/models/single_get_order_model.dart';
import 'package:dio/dio.dart';

import '../utils/common_api_service.dart';
import '../utils/show_snackbar.dart';


class GetSingleProductService {
  Future<SingleGetOrderModel?> getSingleOrder(String token,String orderId) async {
    try {
      // Ensure the base URL and orderId are concatenated properly
      String url = "/api/pd-get-single-place-order/$orderId";

      final response = await commonApiService<SingleGetOrderModel>(
        method: "GET",
        url: url,
        additionalHeaders: {
          'Authorization': 'Bearer $token',  // Correctly pass the token as 'Bearer <token>'
        },
        fromJson: (json) {
          // Check if the JSON response contains the 'singleOrder' key
          if (json['singleOrder'] != null) {
            // Convert the JSON response to a SingleGetOrderModel
            return SingleGetOrderModel.fromJson(json['singleOrder'] as Map<String, dynamic>);
          } else {
            // Handle the case when the order is not found
            throw Exception("Order not found in response.");
          }
        },
      );

      return response;  // Return the fetched order if successful
    } catch (e) {
      // Show a snackbar with the error message
      showSnackbar(e.toString());
      return null;  // Return null to indicate an error occurred
    }
  }

  //
  // Future<SingleGetOrderModel?> fetchSingleOrder(String token,
  //     String orderId) async {
  //   final String url = 'https://api.cnapp.co.in/api/pd-get-single-place-order/$orderId';
  //
  //   try {
  //     Response response = await Dio().get(
  //       url,
  //       options: Options(
  //         headers: {
  //           'Authorization': 'Bearer $token',
  //           'Content-Type': 'application/json',
  //         },
  //       ),
  //     );
  //
  //     if (response.statusCode == 200 && response.data != null) {
  //       // Check if 'singleOrder' is present in the response
  //       final singleOrderData = response.data['singleOrder'];
  //
  //       if (singleOrderData != null) {
  //         return SingleGetOrderModel.fromJson(singleOrderData);
  //       } else {
  //         print('No single order data found in response.');
  //         return null;
  //       }
  //     } else {
  //       print('Failed to load order data: ${response.statusCode}');
  //       return null;
  //     }
  //   } catch (e) {
  //     print('Error fetching order data: $e');
  //     return null;
  //   }
  // }
}