import 'dart:convert';

import 'package:connectivity_plus/connectivity_plus.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:http/http.dart' as http;

import '../../modules/tenant/category.dart';

class CategoryController extends GetxController {
  var categories = <Category>[].obs;


  var isLoading = false.obs;
  var selectedIndex = 0.obs;

  @override
  void onInit() {
    fetchCategories();
    super.onInit();
  }

  void fetchCategories() async {
    try {
      isLoading(true);

      final url = Uri.parse(
          'https://fiesta.nearle.app/live/api/v1/mob/utils/getappcategories');

      final response = await http.get(url);

      if (response.statusCode == 200) {
        final data = jsonDecode(response.body);

        categories.value = (data['details'] as List)
            .map((e) => Category.fromJson(e))
            .toList();

        print(response.body);

      }
    } catch (e) {
      print("Error: $e");
    } finally {
      isLoading(false);
    }
  }

  void selectCategory(int index) {
    selectedIndex.value = index;
  }
}