import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/type_of_location/type_of_location_bloc.dart';
import 'package:unit2/sevices/offline/offline_passo/admin/api_services/type_of_location.dart';

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

class TypesOfLocationAdminPage extends StatefulWidget {
  const TypesOfLocationAdminPage();

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

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

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

                  // Loop through the list of City objects and insert them into the local database.
                  for (TypeOfLocation typesOfLocation in typesOfLoc) {
                    await SQLServices.instance
                        .createTypeOfLocation(typesOfLocation);
                  }
                } catch (e) {
                  // Handle any errors that might occur during the API call or database insertion.
                  print("Error: $e");
                }
              },
              child: const Text('SYNC'),
            ),
          ],
        ),
        body: BlocConsumer<TypeOfLocationBloc, TypeOfLocationState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            if (state is TypeOfLocationLoaded) {
              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('Distance in KM'),
                                ),
                                const DataColumn(
                                  label: Text('Road Types'),
                                ),
                                const DataColumn(
                                  label: Text(
                                      'Distance from Local Trading Center'),
                                ),
                              ],
                              rows: state.typeOfLocation.map((dataRow) {
                                return DataRow(
                                  cells: [
                                    DataCell(Text(dataRow.id.toString() ??
                                        'N/A')), // Use a default value if cityCode is null
                                    DataCell(Text(dataRow.distanceKm ??
                                        'N/A')), // Use a default value if cityDescription is null
                                    DataCell(
                                        Text(dataRow.allRoadTypes ?? 'N/A')),
                                    DataCell(Text(
                                        dataRow.localTradingCenter ?? 'N/A')),
                                  ],
                                );
                              }).toList(),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                )
              ]);
            }
            return Container();
          },
        ));
  }
}
