import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/land_classification/land_classification_bloc.dart';

import 'package:unit2/model/passo/land_classification.dart';
import 'package:unit2/sevices/offline/offline_passo/admin/api_services/land_classification_api_services.dart';

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

class LandClassificationAdminPage extends StatefulWidget {
  const LandClassificationAdminPage();

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

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

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

                // Loop through the list of City objects and insert them into the local database.
                for (LandClassification landClassification in landClass) {
                  await SQLServices.instance
                      .createLandClassification(landClassification);
                }
              } catch (e) {
                // Handle any errors that might occur during the API call or database insertion.
                print("Error: $e");
              }
            },
            child: const Text('SYNC'),
          ),
        ],
      ),
      body: BlocConsumer<LandClassificationBloc, LandClassificationState>(
        listener: (context, state) {
          // TODO: implement listener
        },
        builder: (context, state) {
          if (state is LandClassificationLoaded) {
            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.landClassification.map((dataRow) {
                              return DataRow(
                                cells: [
                                  DataCell(Text(dataRow.id.toString() ??
                                      'N/A')), // Use a default value if cityCode is null
                                  DataCell(Text(dataRow.classificationCode ??
                                      'N/A')), // Use a default value if cityDescription is null
                                  DataCell(Text(dataRow.description ?? 'N/A')),
                                ],
                              );
                            }).toList(),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              )
            ]);
          }
          return Container();
        },
      ),
    );
  }
}
