import 'package:flutter/material.dart';
import 'package:satu/core/models/flow/analytics/analytics_bean.dart';
import 'package:satu/core/services/api_service.dart';
import 'package:satu/core/utils/locator.dart';
import 'package:satu/shared/ui_helpers.dart';
import 'package:satu/widgets/bar/products_app_bar.dart';
import 'package:satu/widgets/row/info_row.dart';

class AnalyticsView extends StatefulWidget {
  const AnalyticsView({Key? key}) : super(key: key);

  @override
  State<AnalyticsView> createState() => _AnalyticsViewState();
}

class _AnalyticsViewState extends State<AnalyticsView> {
  final ApiService _apiService = locator<ApiService>();

  AnalyticsBean? _analyticsBean;

  @override
  void initState() {
    super.initState();
    initQuery();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const ProductsAppBar(
        title: 'Аналитика',
        drawerShow: true,
      ),
      body: Column(
        children: [
          verticalSpaceLarge,
          InfoRow(
            'Денег в кассе',
            value: _analyticsBean?.money?.toString() ?? '0',
          ),
          InfoRow(
            'Сегодня',
            value: _analyticsBean?.stats?.today?.summ?.toString() ?? '0',
          ),
          InfoRow(
            'Вчера',
            value: _analyticsBean?.stats?.yesterday?.summ?.toString() ?? '0',
          ),
          InfoRow(
            'Неделя',
            value: _analyticsBean?.stats?.week?.summ?.toString() ?? '0',
          ),
          InfoRow(
            'Месяц',
            value: _analyticsBean?.stats?.month?.summ?.toString() ?? '0',
          ),
        ],
      ),
    );
  }

  Future<void> initQuery() async {
    final AnalyticsBean? analyticsBean = await _apiService.getAnalytics();
    setState(() {
      _analyticsBean = analyticsBean;
    });
  }
}
