// import 'package:cheminova/controller/shiptoandbillto_service.dart';
// import 'package:cheminova/models/shiping_billing_address_model.dart';
// import 'package:get/get.dart';
// import 'package:shared_preferences/shared_preferences.dart';
//
// class AddressController extends GetxController {
//   final ShiptoandbilltoService _productService = ShiptoandbilltoService();
//
//   var addressList = <UserShippingAddress>[].obs; // Observable list of addresses
//   var selectedShippingAddressId = ''.obs;
//   var selectedBillingAddressId = ''.obs;
//
//   @override
//   void onInit() {
//     super.onInit();
//     _loadAddresses();
//   }
//
//   void _loadAddresses() async {
//     try {
//       SharedPreferences prefs = await SharedPreferences.getInstance();
//       String? token = prefs.getString('token');
//       final response = await _productService.fetchshiptobillAddress(token!);
//       addressList.assignAll(response as Iterable<UserShippingAddress>);
//
//       // Load previously selected addresses
//       selectedShippingAddressId.value = prefs.getString('selectedShippingAddress') ?? '';
//       selectedBillingAddressId.value = prefs.getString('selectedBillingAddress') ?? '';
//
//       // Set the default selection to the first address if available
//       if (addressList.isNotEmpty) {
//         if (selectedShippingAddressId.value.isEmpty) {
//           selectedShippingAddressId.value = addressList.first.id;
//         }
//         if (selectedBillingAddressId.value.isEmpty) {
//           selectedBillingAddressId.value = addressList.first.id;
//         }
//       }
//     } catch (e) {
//       print("Exception: $e");
//     }
//   }
//
//   void _saveSelectedAddress() async {
//     SharedPreferences prefs = await SharedPreferences.getInstance();
//     await prefs.setString('selectedShippingAddress', selectedShippingAddressId.value);
//     await prefs.setString('selectedBillingAddress', selectedBillingAddressId.value);
//   }
//
//   void onShippingAddressChanged(String? value) {
//     if (value != null) {
//       selectedShippingAddressId.value = value;
//       _saveSelectedAddress();
//     }
//   }
//
//   void onBillingAddressChanged(String? value) {
//     if (value != null) {
//       selectedBillingAddressId.value = value;
//       _saveSelectedAddress();
//     }
//   }
// }


import 'package:cheminova/controller/shiptoandbillto_service.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../models/shiping_billing_address_model.dart';

class AddressController extends GetxController {
  RxList<UserShippingAddress> addressList = <UserShippingAddress>[].obs;
  RxString selectedShippingAddressId = ''.obs;
  RxString selectedBillingAddressId = ''.obs;
  Rx<UserShippingAddress?> selectedShippingAddress = Rx<UserShippingAddress?>(null);
  Rx<UserShippingAddress?> selectedBillingAddress = Rx<UserShippingAddress?>(null);

  void fetchAddresses() async {
    // Fetch the addresses from the API
          SharedPreferences prefs = await SharedPreferences.getInstance();
      String? token = prefs.getString('token');
    var response = await ShiptoandbilltoService().fetchshiptobillAddress(token!);
    if (response!= null) {
      addressList.value = response;
    }
  }

  void onShippingAddressChanged(String? value) {
    selectedShippingAddressId.value = value ?? '';
    selectedShippingAddress.value = addressList.firstWhere((address) => address.id == value);
  }

  void onBillingAddressChanged(String? value) {
    selectedBillingAddressId.value = value ?? '';
    selectedBillingAddress.value = addressList.firstWhere((address) => address.id == value,);
  }
}
