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

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

class ValueAdjustmentsAdminPage extends StatefulWidget {
  const ValueAdjustmentsAdminPage();

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

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

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

                  // Loop through the list of City objects and insert them into the local database.
                  for (ValueAdjustments valueAdjustments in valueAdj) {
                    await SQLServices.instance
                        .createValueAdjustments(valueAdjustments);
                  }
                } catch (e) {
                  // Handle any errors that might occur during the API call or database insertion.
                  print("Error: $e");
                }
              },
              child: const Text('SYNC'),
            ),
          ],
        ),
        body: BlocConsumer<ValueAdjustmentsBloc, ValueAdjustmentsState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            if (state is ValueAdjustmentsLoaded) {
              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('Base Market Value'),
                                ),
                                const DataColumn(
                                  label: Text('Adjustment Factor'),
                                ),
                                const DataColumn(
                                  label: Text('Adjustment'),
                                ),
                                const DataColumn(
                                  label: Text('Value Adjustment'),
                                ),
                                const DataColumn(
                                  label: Text('Market Value'),
                                ),
                              ],
                              rows: state.valueAdjustments.map((dataRow) {
                                return DataRow(
                                  cells: [
                                    DataCell(Text(dataRow.id.toString() ??
                                        'N/A')), // Use a default value if cityCode is null
                                    DataCell(Text(dataRow.baseMarketval ??
                                        'N/A')), // Use a default value if cityDescription is null
                                    DataCell(Text(
                                        dataRow.adjustmentFactors ?? 'N/A')),
                                    DataCell(Text(dataRow.adjustment ?? 'N/A')),
                                    DataCell(
                                        Text(dataRow.valueAdjustment ?? 'N/A')),
                                    DataCell(
                                        Text(dataRow.marketValue ?? 'N/A')),
                                  ],
                                );
                              }).toList(),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                )
              ]);
            }
            return Container();
          },
        ));
  }
}
