import 'dart:convert';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
// import 'package:piring/bloc/nav/bottom_nav.dart';
// import 'package:piring/model/piechart.dart';
import 'package:piring_baru/bloc/nav/bottom_nav.dart';
import 'package:piring_baru/model/piechart.dart';
import 'package:syncfusion_flutter_charts/charts.dart';

class TestingAnal extends StatefulWidget {
  const TestingAnal({super.key});

  @override
  State<TestingAnal> createState() => _TestingAnalState();
}

class _TestingAnalState extends State<TestingAnal> {
  List<dynamic> responseData = [];
  double totalKarbohidrat = 0.0;
  double totalLemak = 0.0;
  double totalBesi = 0.0;
  double totalVitamina = 0.0;
  double totalVitaminC = 0.0;

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

  Future<void> fetchDataFromAPI() async {
    final response = await http.get(Uri.parse(
        'https://isipiringku.esolusindo.com/api/Makanan/konsumsi?id_user=36&waktu=2023-10-11&keterangan=Sarapan'));

    if (response.statusCode == 200) {
      final data = json.decode(response.body);
      setState(() {
        responseData = data['response'];

        for (var item in responseData) {
          totalKarbohidrat += double.parse(item['karbohidrat']);
          totalLemak += double.parse(item['lemak']);
          totalBesi += double.parse(item['besi']);
          totalVitamina += double.parse(item['vitamina']);
          totalVitaminC += double.parse(item['vitaminc']);
        }
      });
    } else {
      throw Exception('Failed to load data from API');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavBar(selected: 1),
      body: Column(
        children: [
          Container(
            height: 300, // Sesuaikan tinggi chart sesuai kebutuhan Anda.
            child: SfCircularChart(
              series: <CircularSeries>[
                PieSeries<Data, String>(
                  dataSource: [
                    Data('Karbohidrat', totalKarbohidrat),
                    Data('Lemak', totalLemak),
                    Data('Besi', totalBesi),
                    Data('Vitamina', totalVitamina),
                    Data('Vitamin C', totalVitaminC),
                  ],
                  xValueMapper: (Data data, _) => data.label,
                  yValueMapper: (Data data, _) => data.value,
                  dataLabelSettings: DataLabelSettings(isVisible: true),
                )
              ],
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                'Total Karbohidrat: $totalKarbohidrat',
                textAlign: TextAlign.left,
              ),
              Text(
                'Total Lemak: $totalLemak',
                textAlign: TextAlign.left,
              ),
              Text('Total Besi: $totalBesi'),
              Text('Total Vitamina: $totalVitamina'),
              Text('Total Vitamin C: $totalVitaminC'),
            ],
          )
        ],
      ),
    );
  }
}
