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

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

class GetDispatchService{

  Future<List<GetDispatchModel>?> getRDDispatchedProduct(String token) async {
    try {
      String url = ApiUrls.getRdDispatchedOrdergUrl; // Base URL to fetch product manuals

      final response = await commonApiService<List<GetDispatchModel>>(
        method: "GET",
        url: url,
        additionalHeaders: {  // Pass the token here
          'Authorization': 'Bearer $token',
        },
        fromJson: (json) {
          if (json['invoices'] != null) {
            // If the productManuals key is present, map the response to a list of ProductManualModel objects
            final List<GetDispatchModel> productManuals = (json['invoices'] as List)
                .map((manualJson) => GetDispatchModel.fromJson(manualJson))
                .toList();
            return productManuals; // Return the list of product manuals
          } else {
            return [];
          }
        },
      );
      return response;
    } catch (e) {

      print("fkfgghgh ,${e.toString()}");
      //print(e.toString());
      return null;
    }
  }


  Future<void> RDProcessingToDispatchOrder(String token, String orderId, String courierName,String couriertrackingId) async {
    try {
      // Correct API URL with orderId passed in the URL
      final String url = 'https://api.cnapp.co.in/api/pd-invoice/dispatched/$orderId';

      // Make the PUT request
      final response = await Dio().put(
        url, // Use the correct URL here
        data: {
          "courierName": courierName,
          "couriertrackingId":couriertrackingId,
          // Send the cancellation reason as JSON
        },
        options: Options(
          headers: {
            'Authorization': 'Bearer $token', // Pass the token in the Authorization header
            'Content-Type': 'application/json', // Set content-type to application/json
          },
        ),
      );

      // Check the response status
      if (response.statusCode == 200) {
        print("Order Dispatched successfully");
      } else {
        throw Exception('Failed to Dispatched the order');
      }
    } catch (e) {
      // Handle error
      print('Error Dispatched the order: $e');
    }
  }


  Future<void> RDDispatchToDeliveredOrder(String token, String orderId,) async {
    try {
      // Correct API URL with orderId passed in the URL
      final String url = 'https://api.cnapp.co.in/api/pd-invoice/delivered/$orderId';

      // Make the PUT request
      final response = await Dio().put(
        url, // Use the correct URL here

        options: Options(
          headers: {
            'Authorization': 'Bearer $token', // Pass the token in the Authorization header
            'Content-Type': 'application/json', // Set content-type to application/json
          },
        ),
      );

      // Check the response status
      if (response.statusCode == 200) {
        print("Order cancelled successfully");
      } else {
        throw Exception('Failed to cancel the order');
      }
    } catch (e) {
      // Handle error
      print('Error cancelling the order: $e');
    }
  }

}