import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/trees_improvements/trees_improvements_bloc.dart';
import 'package:unit2/sevices/offline/offline_passo/admin/api_services/trees_improvements_api_services.dart';
import 'package:unit2/theme-data.dart/colors.dart';

import '../../../../model/passo/trees_improvements.dart';
import '../../../../sevices/offline/offline_passo/admin/sql_services/sql_services.dart';

class TreesImprovementsAdminPage extends StatefulWidget {
  const TreesImprovementsAdminPage();

  @override
  _TreesImprovementsAdminPage createState() => _TreesImprovementsAdminPage();
}

class _TreesImprovementsAdminPage extends State<TreesImprovementsAdminPage> {
  final items = [];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: primary,
          title: const Text("Trees Improvement"),
          centerTitle: true,
          actions: [
            TextButton(
              style: TextButton.styleFrom(
                textStyle: const TextStyle(fontSize: 15),
              ),
              onPressed: () async {
                try {
                  final result =
                      await TreesImprovementsAdminApiServices.instance.fetch();

                  // Assuming result is a List of JSON objects, convert them to City objects.
                  final treesImpr = result
                      .map((json) => TreesImprovements.fromJson(json))
                      .toList();

                  // Loop through the list of City objects and insert them into the local database.
                  for (TreesImprovements treesImprovements in treesImpr) {
                    await SQLServices.instance
                        .createTreesImprovements(treesImprovements);
                  }
                } catch (e) {
                  // Handle any errors that might occur during the API call or database insertion.
                  print("Error: $e");
                }
              },
              child: const Text('SYNC'),
            ),
          ],
        ),
        body: BlocConsumer<TreesImprovementsBloc, TreesImprovementsState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            if (state is TreesImprovementsLoaded) {
              return Column(children: [
                Expanded(
                  child: SingleChildScrollView(
                    child: Padding(
                      padding: EdgeInsets.all(15.0),
                      child: Column(
                        children: [
                          SingleChildScrollView(
                            scrollDirection: Axis.horizontal,
                            child: DataTable(
                              // ignore: prefer_const_literals_to_create_immutables
                              columns: [
                                const DataColumn(
                                  label: Text('ID'),
                                ),
                                const DataColumn(
                                  label: Text('Code'),
                                ),
                                const DataColumn(
                                  label: Text('Memoranda'),
                                ),
                              ],
                              rows: state.treesImprovements.map((dataRow) {
                                return DataRow(
                                  cells: [
                                    DataCell(Text(dataRow.id.toString() ??
                                        'N/A')), // Use a default value if cityCode is null
                                    DataCell(Text(dataRow.improvement ??
                                        'N/A')), // Use a default value if cityDescription is null
                                    DataCell(
                                        Text(dataRow.pricePerTree ?? 'N/A')),
                                  ],
                                );
                              }).toList(),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                )
              ]);
            }
            return Container();
          },
        ));
  }
}
