import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hub/features/backend/index.dart';
import 'package:hub/flutter_flow/flutter_flow_icon_button.dart';
import 'package:hub/flutter_flow/flutter_flow_theme.dart';
import 'package:hub/flutter_flow/flutter_flow_util.dart';
import 'package:hub/flutter_flow/nav/nav.dart';
import 'package:hub/pages/people_on_the_property_page/people_on_the_property_page_model.dart';

import '../../shared/utils/log_util.dart';

class PeopleOnThePropertyPage extends StatefulWidget {
  const PeopleOnThePropertyPage({super.key});

  @override
  State<PeopleOnThePropertyPage> createState() =>
      _PeopleOnThePropertyPageState();
}

class _PeopleOnThePropertyPageState extends State<PeopleOnThePropertyPage> {
  late PeopleOnThePropertyPageModel _model;

  final scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  void initState() {
    super.initState();
    _model = createModel(context, () => PeopleOnThePropertyPageModel());
  }

  @override
  void dispose() {
    _model.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldKey,
      backgroundColor: FlutterFlowTheme.of(context).primaryBackground,
      appBar: AppBar(
        backgroundColor: FlutterFlowTheme.of(context).primaryBackground,
        automaticallyImplyLeading: false,
        forceMaterialTransparency: true,
        leading: FlutterFlowIconButton(
          key: ValueKey<String>('BackNavigationAppBar'),
          borderColor: Colors.transparent,
          borderRadius: 30.0,
          borderWidth: 1.0,
          buttonSize: 60.0,
          icon: Icon(
            Icons.keyboard_arrow_left,
            color: FlutterFlowTheme.of(context).primaryText,
            size: 30.0,
          ),
          onPressed: () async {
            context.pop();
          },
        ),
        title: Text(
          FFLocalizations.of(context).getText(
            'nsu13r5d' /* Pessoas na Propriedade */,
          ),
          style: FlutterFlowTheme.of(context).headlineMedium.override(
                fontFamily: FlutterFlowTheme.of(context).headlineMediumFamily,
                color: FlutterFlowTheme.of(context).primaryText,
                fontSize: 16.0,
                fontWeight: FontWeight.bold,
                letterSpacing: 0.0,
                useGoogleFonts: GoogleFonts.asMap().containsKey(
                    FlutterFlowTheme.of(context).headlineMediumFamily),
              ),
        ),
        actions: const [],
        centerTitle: true,
        elevation: 0.0,
      ),
      body: SafeArea(
        top: true,
        child: FutureBuilder<ApiCallResponse>(
          future: PhpGroup.getPessoasLocalCall.call(),
          builder: (context, snapshot) {
            // Customize what your widget looks like when it's loading.
            if (!snapshot.hasData) {
              return Center(
                child: SizedBox(
                  width: 50.0,
                  height: 50.0,
                  child: SpinKitCircle(
                    color: FlutterFlowTheme.of(context).primary,
                    size: 50.0,
                  ),
                ),
              );
            }

            if (snapshot.hasError || snapshot.data?.exception != null) {
              if (snapshot.error != null && snapshot.stackTrace != null) {
                LogUtil.requestAPIFailed(
                    'getPessoasLocal.php',
                    "",
                    'Busca Pessoas no Local',
                    snapshot.error,
                    snapshot.stackTrace!);
              }

              return Center(
                child: SizedBox(
                  width: double.infinity,
                  height: 100,
                  child: Text(
                    FFLocalizations.of(context).getVariableText(
                        ptText: "Pessoas não encontradas",
                        enText: "Persons not found"),
                    textAlign: TextAlign.center,
                  ),
                ),
              );
            }

            final columnGetPessoasLocalResponse = snapshot.data!;
            final getPoepleProperty = PhpGroup.getPessoasLocalCall
                    .pessoas(
                      columnGetPessoasLocalResponse.jsonBody,
                    )
                    ?.toList() ??
                [];

            return ListView.builder(
              physics: const AlwaysScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: getPoepleProperty.length,
              itemBuilder: (context, index) {
                final getPoeplePropertyItem = getPoepleProperty[index];
                return Row(
                  mainAxisSize: MainAxisSize.max,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Padding(
                      padding: const EdgeInsets.all(4.0),
                      child: Container(
                        width: 50.0,
                        height: 50.0,
                        clipBehavior: Clip.antiAlias,
                        decoration: const BoxDecoration(
                          shape: BoxShape.circle,
                        ),
                        child: CachedNetworkImage(
                          fadeInDuration: const Duration(milliseconds: 500),
                          fadeOutDuration: const Duration(milliseconds: 500),
                          imageUrl:
                              'https://storage.googleapis.com/flutterflow-io-6f20.appspot.com/projects/flutter-freaccess-hub-0xgz9q/assets/7ftdetkzc3s0/360_F_64676383_LdbmhiNM6Ypzb3FM4PPuFP9rHe7ri8Ju.jpg',
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                    Text(
                      getJsonField(
                        getPoeplePropertyItem,
                        r'''$.USU_NOME''',
                      ).toString(),
                      style: FlutterFlowTheme.of(context).bodyMedium.override(
                            fontFamily:
                                FlutterFlowTheme.of(context).bodyMediumFamily,
                            fontSize: 14.0,
                            letterSpacing: 0.0,
                            useGoogleFonts: GoogleFonts.asMap().containsKey(
                                FlutterFlowTheme.of(context).bodyMediumFamily),
                          ),
                    ),
                  ]
                      .divide(const SizedBox(width: 20.0))
                      .addToStart(const SizedBox(width: 40.0)),
                );
              },
            );
          },
        ),
      ),
    );
  }
}
