import 'dart:convert'; import 'package:get/get.dart'; import 'package:siro_admin/constant/links.dart'; import 'package:siro_admin/controller/functions/crud.dart'; import '../../print.dart'; class LiveAnalyticsController extends GetxController { bool isLoading = true; String selectedDate = ''; Map allData = {}; Map get realtime => _section('realtime'); Map get gap => _section('gap'); Map get heatmap => _section('heatmap'); Map get pricing => _section('pricing'); Map get revenue => _section('revenue'); Map get growth => _section('growth'); Map get market => _section('market'); Map get complaints => _section('complaints'); Map get funnel => _section('funnel'); Map get hourly => _section('hourly'); Map get weekly => _section('weekly'); Map get zones => _section('zones'); Map get retention => _section('retention'); Map get competitor => _section('competitor'); List availableDates = []; Map _section(String key) { final d = allData[key]; if (d is Map) return d; return {}; } @override void onInit() { super.onInit(); final now = DateTime.now(); selectedDate = '${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')}'; fetchAll(); } Future fetchAll() async { isLoading = true; update(); try { var res = await CRUD().get( link: AppLink.analyticsDashboardData, payload: {'date': selectedDate, 'section': 'all'}, ); if (res != 'failure' && res != null) { var d = res is String ? jsonDecode(res) : res; if (d['status'] == 'success' && d['data'] != null) { allData = Map.from(d['data']); availableDates = List.from(d['available_dates'] ?? []); } } } catch (e) { Log.print('Error fetching live analytics: $e'); } isLoading = false; update(); } void changeDate(String date) { selectedDate = date; fetchAll(); } }