import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/land_subclassification/land_subclassification_bloc.dart';
import 'package:unit2/sevices/offline/offline_passo/admin/api_services/land_sub_classification_api_services.dart';
import 'package:unit2/theme-data.dart/colors.dart';

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

class LandSubClassificationAdminPage extends StatefulWidget {
  const LandSubClassificationAdminPage();

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

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

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

                // Loop through the list of City objects and insert them into the local database.
                for (LandSubClassification landSubClassification
                    in landSubClass) {
                  await SQLServices.instance
                      .createLandSubClassification(landSubClassification);
                }
              } catch (e) {
                // Handle any errors that might occur during the API call or database insertion.
                print("Error: $e");
              }
            },
            child: const Text('SYNC'),
          ),
        ],
      ),
      body: BlocConsumer<LandSubclassificationBloc, LandSubclassificationState>(
        listener: (context, state) {
          // TODO: implement listener
        },
        builder: (context, state) {
          if (state is LandSubClassificationLoaded) {
            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('Municipality'),
                              ),
                              const DataColumn(
                                label: Text('Sub-Class Description'),
                              ),
                              const DataColumn(
                                label: Text('Base Unit Market Value'),
                              ),
                            ],
                            rows: state.landSubClassification.map((dataRow) {
                              return DataRow(
                                cells: [
                                  DataCell(
                                      Text(dataRow.id.toString() ?? 'N/A')),
                                  DataCell(Text(dataRow.cityCode.toString() ??
                                      'N/A')), // Use a default value if cityCode is null
                                  DataCell(Text(dataRow.subclassDescription ??
                                      'N/A')), // Use a default value if cityDescription is null
                                  DataCell(
                                      Text(dataRow.baseUnitMarketval ?? 'N/A')),
                                ],
                              );
                            }).toList(),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              )
            ]);
          }
          return Container();
        },
      ),
    );
  }
}
