import 'dart:convert';

import 'package:cheminova/models/rd_order_item_model.dart';

//class PlacedOrdersResponse {
//   final List<PlacedOrder> placedOrders;
//   final int totalOrders;
//
//   PlacedOrdersResponse({required this.placedOrders, required this.totalOrders});
//
//   factory PlacedOrdersResponse.fromJson(Map<String, dynamic> json) {
//     return PlacedOrdersResponse(
//       placedOrders: (json['placedOrders'] != null
//           ? (json['placedOrders'] as List)
//           .map((i) => PlacedOrder.fromJson(i))
//           .toList()
//           : []), // Fallback to empty list if null
//       totalOrders: json['totalOrders'] ?? 0, // Fallback to 0 if null
//     );
//   }
//   Map<String, dynamic> toJson() {
//     return {
//       'plcaedOrders': placedOrders.map((order) => order.toJson()).toList(),
//       'totalOrders': totalOrders,
//     };
//   }
//
//   @override
//   String toString() {
//     return 'PlacedOrdersResponse(placedOrders: $placedOrders, totalOrders: $totalOrders)';
//   }
// }

// class PlacedOrdersResponse {
//   final String id;
//   final String paymentMode;
//   final String shipTo;
//   final String billTo;
//   final List<OrderItem2> orderItems;
//   final double subtotal;
//   final double gstTotal;
//   final double grandTotal;
//   final String status;
//   final List<dynamic> invoices;
//   final String addedBy;
//   final String pd;
//   final bool isCancelled;
//   final bool isDelivered;
//   final String deliveredDate;
//   final String statusUpdatedAt;
//   final String uniqueId;
//   final DateTime createdAt;
//   final DateTime updatedAt;
//
//   final int v;
//
//   PlacedOrdersResponse({
//     required this.id,
//     required this.paymentMode,
//     required this.shipTo,
//     required this.billTo,
//     required this.orderItems,
//     required this.subtotal,
//     required this.gstTotal,
//     required this.grandTotal,
//     required this.status,
//     required this.invoices,
//     required this.addedBy,
//     required this.pd,
//     required this.isCancelled,
//     required this.isDelivered,
//     required this.deliveredDate,
//     required this.statusUpdatedAt,
//     required this.uniqueId,
//     required this.createdAt,
//     required this.updatedAt,
//     required this.v,
//   });
//
//   factory PlacedOrdersResponse.fromJson(Map<String, dynamic> json) {
//     return PlacedOrdersResponse(
//       id: json['_id']??22343,
//       paymentMode: json['paymentMode']??"uuiu",
//       shipTo: json['shipTo']??"iiouui",
//       billTo: json['billTo']??"iiouio",
//       orderItems: (json['orderItem'] as List)
//           .map((i) => OrderItem2.fromJson(i))
//           .toList(),
//       subtotal: json['subtotal'].toDouble()??"78787.99",
//       gstTotal: json['gstTotal'].toDouble()??"",
//       grandTotal: json['grandTotal'].toDouble()??"",
//       status: json['status']??"",
//       invoices: json['invoices']??"",
//       addedBy: json['addedBy']??"",
//       pd: json['pd']??"",
//       isCancelled: json['iscancelled']??"",
//       isDelivered: json['isDelivered']??"",
//       deliveredDate: json['DeliveredDate']??"",
//       statusUpdatedAt: json['statusUpdatedAt']??"",
//       uniqueId: json['uniqueId']??"uyiy",
//       createdAt: DateTime.parse(json['createdAt']),
//       updatedAt: DateTime.parse(json['updatedAt']),
//       v: json['__v'],
//     );
//   }
//
//   Map<String, dynamic> toJson() {
//     return {
//       '_id': id,
//       'paymentMode': paymentMode,
//       'shipTo': shipTo,
//       'billTo': billTo,
//       'orderItem': orderItems.map((item) => item.toJson()).toList(),
//       'subtotal': subtotal,
//       'gstTotal': gstTotal,
//       'grandTotal': grandTotal,
//       'status': status,
//       'invoices': invoices,
//       'addedBy': addedBy,
//       'pd': pd,
//       'iscancelled': isCancelled,
//       'isDelivered': isDelivered,
//       'DeliveredDate': deliveredDate,
//       'statusUpdatedAt': statusUpdatedAt,
//       'uniqueId': uniqueId,
//       'createdAt': createdAt.toIso8601String(),
//       'updatedAt': updatedAt.toIso8601String(),
//       '__v': v,
//     };
//   }
//
//   @override
//   String toString() {
//     return 'PlacedOrder(id: $id, paymentMode: $paymentMode, shipTo: $shipTo, billTo: $billTo, orderItems: $orderItems, subtotal: $subtotal, gstTotal: $gstTotal, grandTotal: $grandTotal, status: $status, invoices: $invoices, addedBy: $addedBy, pd: $pd, isCancelled: $isCancelled, isDelivered: $isDelivered, deliveredDate: $deliveredDate, statusUpdatedAt: $statusUpdatedAt, uniqueId: $uniqueId, createdAt: $createdAt, updatedAt: $updatedAt, v: $v)';
//   }
// }
//
// class OrderItem2 {
//   final String productId;
//   final String sku;
//   final String name;
//   final String categoryName;
//   final String brandName;
//   final double price;
//   final double gst;
//   final String hsnCode;
//   final String description;
//   final List<dynamic> image;
//   final int quantity;
//   final int remainingQuantity;
//   final String id;
//
//   OrderItem2({
//     required this.productId,
//     required this.sku,
//     required this.name,
//     required this.categoryName,
//     required this.brandName,
//     required this.price,
//     required this.gst,
//     required this.hsnCode,
//     required this.description,
//     required this.image,
//     required this.quantity,
//     required this.remainingQuantity,
//     required this.id,
//   });
//
//   factory OrderItem2.fromJson(Map<String, dynamic> json) {
//     return OrderItem2(
//       productId: json['productId']??"",
//       sku: json['SKU']??"",
//       name: json['name']??"",
//       categoryName: json['categoryName']??"",
//       brandName: json['brandName']??"",
//       price: json['price'].toDouble()??"",
//       gst: json['GST'].toDouble()??"",
//       hsnCode: json['HSN_Code']??"",
//       description: json['description']??"",
//       image: json['image']??"",
//       quantity: json['quantity']??5,
//       remainingQuantity: json['remainingQuantity']??6,
//       id: json['_id']??555,
//     );
//   }
//
//   Map<String, dynamic> toJson() {
//     return {
//       'productId': productId,
//       'SKU': sku,
//       'name': name,
//       'categoryName': categoryName,
//       'brandName': brandName,
//       'price': price,
//       'GST': gst,
//       'HSN_Code': hsnCode,
//       'description': description,
//       'image': image,
//       'quantity': quantity,
//       'remainingQuantity': remainingQuantity,
//       '_id': id,
//     };
//   }
//
//   @override
//   String toString() {
//     return 'OrderItem(productId: $productId, sku: $sku, name: $name, categoryName: $categoryName, brandName: $brandName, price: $price, gst: $gst, hsnCode: $hsnCode, description: $description, image: $image, quantity: $quantity, remainingQuantity: $remainingQuantity, id: $id)';
//   }
// }


class PlacedOrdersResponse {
  final String id;
  final String paymentMode;
  final String shipTo;
  final String billTo;
  final List<RDOrderItem> orderItem;
  final double subtotal;
  final double gstTotal;
  final double grandTotal;
  final String status;
  final bool isCancelled;
  final bool isDelivered;
  final String deliveredDate;
  final String statusUpdatedAt;
  final String uniqueId;
  final String createdAt;
  final String updatedAt;
  final int version;

  PlacedOrdersResponse({
    required this.id,
    required this.paymentMode,
    required this.shipTo,
    required this.billTo,
    required this.orderItem,
    required this.subtotal,
    required this.gstTotal,
    required this.grandTotal,
    required this.status,
    required this.isCancelled,
    required this.isDelivered,
    required this.deliveredDate,
    required this.statusUpdatedAt,
    required this.uniqueId,
    required this.createdAt,
    required this.updatedAt,
    required this.version,
  });

  factory PlacedOrdersResponse.fromJson(Map<String, dynamic> json) {
    return PlacedOrdersResponse(
      id: json['_id'] ?? '',  // Handle null values or incorrect types
      paymentMode: json['paymentMode'] ?? '',
      shipTo: json['shipTo'] ?? '',
      billTo: json['billTo'] ?? '',
      orderItem: (json['orderItem'] as List)
          .map((item) => RDOrderItem.fromJson(item))
          .toList(),
      subtotal: (json['subtotal'] as num).toDouble(),  // Ensure it is double
      gstTotal: (json['gstTotal'] as num).toDouble(),  // Ensure it is double
      grandTotal: (json['grandTotal'] as num).toDouble(),  // Ensure it is double
      status: json['status'] ?? '',
      isCancelled: json['iscancelled'] ?? false,
      isDelivered: json['isDelivered'] ?? false,
      deliveredDate: json['DeliveredDate'] ?? '',
      statusUpdatedAt: json['statusUpdatedAt'] ?? '',
      uniqueId: json['uniqueId']?.toString() ?? '',  // Ensure this is a String
      createdAt: json['createdAt'] ?? '',
      updatedAt: json['updatedAt'] ?? '',
      version: json['__v'] ?? 0,
    );
  }
  // Method to convert instance to JSON
  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'paymentMode': paymentMode,
      'shipTo': shipTo,
      'billTo': billTo,
      'orderItem': orderItem.map((item) => item.toJson()).toList(),
      'subtotal': subtotal,
      'gstTotal': gstTotal,
      'grandTotal': grandTotal,
      'status': status,
      'iscancelled': isCancelled,
      'isDelivered': isDelivered,
      'DeliveredDate': deliveredDate,
      'statusUpdatedAt': statusUpdatedAt,
      'uniqueId': uniqueId,
      'createdAt': createdAt,
      'updatedAt': updatedAt,
      '__v': version,
    };
  }
}

