import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/type_of_road/type_of_road_bloc.dart';
import 'package:unit2/sevices/offline/offline_passo/admin/api_services/type_of_road_api_services.dart';

import '../../../../model/passo/type_of_road.dart';
import '../../../../sevices/offline/offline_passo/admin/sql_services/sql_services.dart';
import '../../../../theme-data.dart/colors.dart';

class TypesOfRoadAdminPage extends StatefulWidget {
  const TypesOfRoadAdminPage();

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

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

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

                  // Loop through the list of City objects and insert them into the local database.
                  for (TypeOfRoad typesOfRoad in typesOfRoad) {
                    await SQLServices.instance.createTypeOfRoad(typesOfRoad);
                  }
                } catch (e) {
                  // Handle any errors that might occur during the API call or database insertion.
                  print("Error: $e");
                }
              },
              child: const Text('SYNC'),
            ),
          ],
        ),
        body: BlocConsumer<TypeOfRoadBloc, TypeOfRoadState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            if (state is TypeOfRoadLoaded) {
              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('Road Type'),
                                ),
                                const DataColumn(
                                  label: Text('Deduction'),
                                ),
                              ],
                              rows: state.typeOfRoad.map((dataRow) {
                                return DataRow(
                                  cells: [
                                    DataCell(Text(dataRow.id.toString() ??
                                        'N/A')), // Use a default value if cityCode is null
                                    DataCell(Text(dataRow.roadType ??
                                        'N/A')), // Use a default value if cityDescription is null
                                    DataCell(Text(dataRow.deduction ?? 'N/A')),
                                  ],
                                );
                              }).toList(),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                )
              ]);
            }
            return Container();
          },
        ));
  }
}
