import 'dart:convert';
import 'package:cheminova/controller/place_order_service.dart';
import 'package:cheminova/models/product_model.dart';
import 'package:dio/dio.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../models/oder_place_model.dart';


class OrderPlacedController extends GetxController {
  var placedOrder1 = PlacedOrderModel(
    paymentMode: 'cheque',
    shipTo: '456, Park Street, Kolkata, West Bengal - 700016',
    billTo: '456, Park Street, Kolkata, West Bengal - 700016',
    subtotal: 300,
    gstTotal: 100,
    grandTotal: 400,
    orderItems: [
    ],
  ).obs;

  var isLoading = false.obs;

  final OrderPlacedService _orderPlacedService = OrderPlacedService();

  Future<void> placeOrder() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? token = prefs.getString('token');
    isLoading.value = true;
    //try {
      // Construct order details
      PlacedOrderModel orderDetails = placedOrder1.value;
      print("Order Details: $orderDetails");

      // Call the service to place the order
     await _orderPlacedService.placeOrder(orderDetails, token!);

    }
    // catch (e) {
    //
    // } finally {
    //   isLoading.value = false;
    // }
  }








// import 'package:cheminova/controller/place_order_service.dart';
// import 'package:cheminova/models/category_model.dart';
// import 'package:get/get.dart';
// import 'package:shared_preferences/shared_preferences.dart';
// import '../models/oder_place_model.dart';
// import 'cart_controller.dart';
//
//
// class OrderPlacedController extends GetxController {
//   final OrderPlacedService _orderPlacedService = OrderPlacedService();
//   final CartController _cartController = Get.find();
//   Rx<PlacedOrderModel?> placedOrder1 = Rx<PlacedOrderModel?>(null);
//   RxBool isLoading = false.obs;
//
//   // Method to place an order with provided details
//   Future<void> placeOrder() async {
//     SharedPreferences prefs = await SharedPreferences.getInstance();
//     String? token = prefs.getString('token');
//
//     // Prepare order details
//     final order = PlacedOrderModel(
//       paymentMode: 'cheque',
//       shipTo: '456, Park Street, Kolkata, West Bengal - 700016',
//       billTo: '456, Park Street, Kolkata, West Bengal - 700016',
//       subtotal: 4000,
//       gstTotal: 18,
//       grandTotal: 4720,
//       orderItems: [
//         OrderItem(
//           id: "66cc7869f02b935094127a27",
//           sku: "BJD",
//           name: "Zycor 60m.l",
//           price: 4000,
//           gst: 18,
//           hsnCode: 200,
//           description: "",
//           productStatus: "Active",
//           addedBy: "ChemiNova",
//           image: [],
//           createdAt: DateTime.parse("2024-08-26T12:43:21.103Z"),
//           updatedAt: DateTime.parse("2024-08-26T12:43:21.103Z"),
//           count: 1, category:Category(id: "66cc7868f02b935094127a21", categoryName: "Insectiside") ,
//           brand: Brand(id: "66cc77fbf02b9350941279f5", brandName: "Old"), v: 0,
//         ),
//       ],
//     );
//
//     try {
//       isLoading.value = true;
//       PlacedOrderModel? result = await _orderPlacedService.placeOrder(order, token);
//       if (result != null) {
//         placedOrder1.value = result;
//         Get.snackbar('Success', 'Order placed successfully.');
//       // _cartController.clearCart(); // Clear cart items after order
//       } else {
//         Get.snackbar('Error', 'Failed to place order.');
//       }
//     } catch (e) {
//       Get.snackbar('Error', e.toString());
//     } finally {
//       isLoading.value = false;
//     }
//   }
//
//   String getShippingAddress() {
//     return placedOrder1.value?.shipTo ?? 'No shipping address';
//   }
//
//   String getBillingAddress() {
//     return placedOrder1.value?.billTo ?? 'No billing address';
//   }
//
//   String getPaymentMode() {
//     return placedOrder1.value?.paymentMode ?? 'No payment mode';
//   }
// }
