import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/barangay_admin/barangay_admin_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/signatories/signatories_admin_bloc.dart';
import 'package:unit2/model/passo/barangay.dart';
import 'package:unit2/model/passo/signatories.dart';
import 'package:unit2/sevices/offline/offline_passo/admin/api_services/barangay_api_services.dart';
import 'package:unit2/sevices/offline/offline_passo/admin/api_services/signatories.dart';

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

import '../../../../utils/urls.dart';

class SignatoriesAdminPage extends StatefulWidget {
  const SignatoriesAdminPage();

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

class _SignatoriesAdminPage extends State<SignatoriesAdminPage> {
  final items = [];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: primary,
        title: const Text("Signatories"),
        centerTitle: true,
        actions: [
          TextButton(
            style: TextButton.styleFrom(
              textStyle: const TextStyle(fontSize: 15),
            ),
            onPressed: () async {
              try {
                Response response = await get(Uri.parse(
                    'http://${Url.instance.host()}/api/rptass_app/signatories/'));

                print(response.body);
                if (response.statusCode == 200) {
                  final List result = jsonDecode(response.body)['data'];

                  result.map(((e) => Signatories.fromJson(e))).toList();

                  final signatories =
                      result.map((json) => Signatories.fromJson(json)).toList();
                  // Loop through the list of City objects and insert them into the local database.
                  for (Signatories signatory in signatories) {
                    await SQLServices.instance.createSignatories(signatory);
                  }
                }
                context.read<SignatoriesAdminBloc>().add(LoadSignatories());
              } catch (e) {
                // Handle any errors that might occur during the API call or database insertion.
                print("Error: $e");
              }
            },
            child: const Text('SYNC'),
          ),
        ],
      ),
      body: BlocConsumer<SignatoriesAdminBloc, SignatoriesAdminState>(
        listener: (context, state) {
          // TODO: implement listener
        },
        builder: (context, state) {
          if (state is SignatoriesLoaded) {
            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('First Name'),
                              ),
                              const DataColumn(
                                label: Text('Middle Name'),
                              ),
                              const DataColumn(
                                label: Text('Last Name'),
                              ),
                              const DataColumn(
                                label: Text('Designation'),
                              ),
                              // const DataColumn(
                              //   label: Text('Status'),
                              // ),
                            ],
                            rows: state.signatories.map((dataRow) {
                              return DataRow(
                                cells: [
                                  DataCell(Text(dataRow.signatoryId
                                      .toString())), // Use a default value if cityCode is null
                                  DataCell(Text(dataRow.firstname ??
                                      'N/A')), // Use a default value if cityDescription is null
                                  DataCell(Text(dataRow.middlename ?? 'N/A')),
                                  DataCell(Text(dataRow.lastname ?? 'N/A')),
                                  DataCell(Text(dataRow.designation ?? 'N/A')),
                                  // DataCell(Text(dataRow.status ?? 'N/A')),
                                ],
                              );
                            }).toList(),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              )
            ]);
          }
          return Container();
        },
      ),
    );
  }
}
