import 'package:easy_debounce/easy_debounce.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hub/components/atomic_components/shared_components_atoms/media_upload_button.dart';
import 'package:hub/features/backend/index.dart';
import 'package:hub/shared/utils/dialog_util.dart';
import 'package:hub/shared/utils/image_util.dart';
import 'package:hub/shared/utils/limited_text_size.dart';
import 'package:hub/shared/utils/validator_util.dart';
import 'package:rxdart/rxdart.dart';

import '/flutter_flow/flutter_flow_drop_down.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/flutter_flow/flutter_flow_widgets.dart';
import '/flutter_flow/form_field_controller.dart';
import '/flutter_flow/upload_data.dart';
import 'regisiter_vistor_template_component_model.dart';

export 'regisiter_vistor_template_component_model.dart';

class RegisiterVistorTemplateComponentWidget extends StatefulWidget {
  final String source;
  final String? doc;

  const RegisiterVistorTemplateComponentWidget(
      {super.key, required this.source, this.doc});

  @override
  State<RegisiterVistorTemplateComponentWidget> createState() =>
      _RegisiterVistorTemplateComponentWidgetState();
}

class _RegisiterVistorTemplateComponentWidgetState
    extends State<RegisiterVistorTemplateComponentWidget> {
  late RegisiterVistorTemplateComponentModel _model;
  bool _isLoading = false;
  final scaffoldKey = GlobalKey<ScaffoldState>();

  bool _isVisitorRegistered = false;
  BehaviorSubject<bool> visitorAlreadyRegistered = BehaviorSubject<bool>();

  final _formKey = GlobalKey<FormState>();

  void _resetForm() {
    _formKey.currentState?.reset();
  }

  @override
  void initState() {
    super.initState();

    visitorAlreadyRegistered = BehaviorSubject<bool>.seeded(false);

    _model =
        createModel(context, () => RegisiterVistorTemplateComponentModel());

    _model.textController1 ??= TextEditingController();
    _model.textFieldFocusNode1 ??= FocusNode();

    _model.textController2 ??= TextEditingController();
    _model.textFieldFocusNode2 ??= FocusNode();
    _model.textController2?.addListener(_onTextChanged);

    _model.textController3 ??= TextEditingController();
    _model.textFieldFocusNode3 ??= FocusNode();

    _model.textController4 ??= TextEditingController();
    _model.textFieldFocusNode4 ??= FocusNode();

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _model.textController2.text = widget.doc ?? '';
    });
  }

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

    super.dispose();
    _model.textController2?.removeListener(_onTextChanged);
  }

  void _onTextChanged() {
    setState(() {});
  }

  bool _isFormValid(BuildContext context) {
    if (_model.uploadedLocalFile.bytes?.isEmpty ?? true) {
      return false;
    }

    if (_model.textController1.text.isEmpty ||
        _model.textController1.text == '') {
      return false;
    }

    if (_model.dropDownValue == null ||
        _model.dropDownValue!.isEmpty ||
        _model.dropDownValue == '') {
      return false;
    }

    if (_model.textController2.text.isEmpty ||
        _model.textController2.text == '') {
      return false;
    }

    if (_isVisitorRegistered) {
      return false;
    }

    if (_model.textController4.text.isNotEmpty &&
        _model.textController4.text != '' &&
        ValidatorUtil.isValidEmail(_model.textController4.text) == false) {
      return false;
    }

    return true;
  }

  @override
  Widget build(BuildContext context) {
    double limitedInputFontSize = LimitedFontSizeUtil.getInputFontSize(context);
    double limitedHeaderFontSize =
        LimitedFontSizeUtil.getHeaderFontSize(context);
    double limitedSubHeaderFontSize =
        LimitedFontSizeUtil.getSubHeaderFontSize(context);

    return Align(
      alignment: const AlignmentDirectional(0.0, 1.0),
      child: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: BoxDecoration(
          color: FlutterFlowTheme.of(context).primaryBackground,
          borderRadius: const BorderRadius.only(
            bottomLeft: Radius.circular(0.0),
            bottomRight: Radius.circular(0.0),
            topLeft: Radius.circular(25.0),
            topRight: Radius.circular(25.0),
          ),
        ),
        child: SingleChildScrollView(
          child: Form(
            key: _formKey,
            autovalidateMode: AutovalidateMode.onUserInteraction,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Align(
                  alignment: const AlignmentDirectional(-1.0, 0.0),
                  child: Padding(
                    padding: const EdgeInsetsDirectional.fromSTEB(
                        24.0, 0.0, 24.0, 15.0),
                    child: Text(
                      FFLocalizations.of(context).getText(
                        'zazj5d8b' /* Preencha o formulário com os d... */,
                      ),
                      overflow: TextOverflow.clip,
                      textAlign: TextAlign.start,
                      style: FlutterFlowTheme.of(context).bodyMedium.override(
                            fontFamily:
                                FlutterFlowTheme.of(context).bodyMediumFamily,
                            letterSpacing: 0.0,
                            useGoogleFonts: GoogleFonts.asMap().containsKey(
                                FlutterFlowTheme.of(context).bodyMediumFamily),
                            fontSize: limitedHeaderFontSize,
                          ),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsetsDirectional.fromSTEB(
                      24.0, 0.0, 24.0, 0.0),
                  child: Column(children: [
                    TextFormField(
                      controller: _model.textController2,
                      focusNode: _model.textFieldFocusNode2,
                      cursorColor: FlutterFlowTheme.of(context).primary,
                      autovalidateMode: AutovalidateMode.onUserInteraction,
                      autofocus: true,
                      textCapitalization: TextCapitalization.none,
                      autofillHints: const [AutofillHints.password],
                      keyboardType: TextInputType.number,
                      textInputAction: TextInputAction.next,
                      obscureText: false,
                      maxLength: 20,
                      decoration: InputDecoration(
                        isDense: true,
                        labelText: FFLocalizations.of(context).getText(
                          'rl8tvwnr' /* Documento */,
                        ),
                        labelStyle: FlutterFlowTheme.of(context)
                            .labelMedium
                            .override(
                              fontFamily: FlutterFlowTheme.of(context)
                                  .labelMediumFamily,
                              color: FlutterFlowTheme.of(context).primaryText,
                              letterSpacing: 0.0,
                              useGoogleFonts: GoogleFonts.asMap().containsKey(
                                  FlutterFlowTheme.of(context)
                                      .labelMediumFamily),
                              fontSize: limitedInputFontSize,
                            ),
                        hintStyle:
                            FlutterFlowTheme.of(context).labelMedium.override(
                                  fontFamily: FlutterFlowTheme.of(context)
                                      .labelMediumFamily,
                                  letterSpacing: 0.0,
                                  useGoogleFonts: GoogleFonts.asMap()
                                      .containsKey(FlutterFlowTheme.of(context)
                                          .labelMediumFamily),
                                  fontSize: limitedInputFontSize,
                                ),
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                            color: FlutterFlowTheme.of(context).customColor6,
                            width: 0.5,
                          ),
                          borderRadius: BorderRadius.circular(10.0),
                        ),
                        focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                            color: FlutterFlowTheme.of(context).primary,
                            width: 0.5,
                          ),
                          borderRadius: BorderRadius.circular(10.0),
                        ),
                        errorBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                            color: FlutterFlowTheme.of(context).error,
                            width: 0.5,
                          ),
                          borderRadius: BorderRadius.circular(10.0),
                        ),
                        errorStyle: TextStyle(
                          color: FlutterFlowTheme.of(context).error,
                          fontSize: limitedInputFontSize,
                        ),
                        focusedErrorBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                            color: FlutterFlowTheme.of(context).error,
                            width: 0.5,
                          ),
                          borderRadius: BorderRadius.circular(10.0),
                        ),
                        suffixIcon: Icon(
                          Icons.document_scanner,
                          color: FlutterFlowTheme.of(context).accent1,
                        ),
                      ),
                      style: FlutterFlowTheme.of(context).bodyMedium.override(
                            fontFamily:
                                FlutterFlowTheme.of(context).bodyMediumFamily,
                            color: FlutterFlowTheme.of(context).primaryText,
                            letterSpacing: 0.0,
                            useGoogleFonts: GoogleFonts.asMap().containsKey(
                                FlutterFlowTheme.of(context).bodyMediumFamily),
                            fontSize: limitedInputFontSize,
                          ),
                      onChanged: (value) {
                        _model.debounce(() async {
                          var data = await _model.getVisitanteByDocument(
                              value, context);
                          setState(() {
                            _isVisitorRegistered = data;
                          });
                        }, const Duration(milliseconds: 500));
                      },
                      validator:
                          _model.textController2Validator.asValidator(context),
                      inputFormatters: [
                        FilteringTextInputFormatter.allow(RegExp('[0-9]')),
                        LengthLimitingTextInputFormatter(20)
                      ],
                    ),
                    _model.textController2.text.isEmpty
                        ? const SizedBox()
                        : _isVisitorRegistered == true
                            ? Row(
                                mainAxisSize: MainAxisSize.max,
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: [
                                  Padding(
                                    padding: const EdgeInsetsDirectional.only(
                                        top: 5, start: 12),
                                    child: Text(
                                        FFLocalizations.of(context)
                                            .getVariableText(
                                          enText: 'Visitor already registered',
                                          ptText: 'Visitante já cadastrado',
                                        ),
                                        style: FlutterFlowTheme.of(context)
                                            .labelSmall
                                            .override(
                                              fontFamily: 'Nunito',
                                              color: Theme.of(context)
                                                          .brightness ==
                                                      Brightness.dark
                                                  ? Color.alphaBlend(
                                                      Colors.white
                                                          .withOpacity(0.7),
                                                      Colors.red)
                                                  : Color.alphaBlend(
                                                      Colors.black
                                                          .withOpacity(0.25),
                                                      Colors.red),
                                              fontSize: limitedInputFontSize,
                                              letterSpacing: 0.0,
                                            )),
                                  ),
                                ],
                              )
                            : const SizedBox()
                  ]),
                ),
                Padding(
                  padding: const EdgeInsetsDirectional.fromSTEB(
                      24.0, 0.0, 24.0, 0.0),
                  child: TextFormField(
                    controller: _model.textController1,
                    autovalidateMode: AutovalidateMode.onUserInteraction,
                    cursorColor: FlutterFlowTheme.of(context).primary,
                    focusNode: _model.textFieldFocusNode1,
                    onChanged: (_) => EasyDebounce.debounce(
                      '_model.textFieldFocusNode1',
                      const Duration(milliseconds: 500),
                      () => setState(() {}),
                    ),
                    autofocus: true,
                    textInputAction: TextInputAction.next,
                    obscureText: false,
                    decoration: InputDecoration(
                      isDense: true,
                      labelText: FFLocalizations.of(context).getText(
                        'v7g73yik' /* Nome */,
                      ),
                      labelStyle: FlutterFlowTheme.of(context)
                          .labelMedium
                          .override(
                            fontFamily:
                                FlutterFlowTheme.of(context).labelMediumFamily,
                            color: FlutterFlowTheme.of(context).primaryText,
                            letterSpacing: 0.0,
                            useGoogleFonts: GoogleFonts.asMap().containsKey(
                                FlutterFlowTheme.of(context).labelMediumFamily),
                            fontSize: limitedInputFontSize,
                          ),
                      hintStyle: FlutterFlowTheme.of(context)
                          .labelMedium
                          .override(
                            fontFamily:
                                FlutterFlowTheme.of(context).labelMediumFamily,
                            color: FlutterFlowTheme.of(context).primaryText,
                            letterSpacing: 0.0,
                            useGoogleFonts: GoogleFonts.asMap().containsKey(
                                FlutterFlowTheme.of(context).labelMediumFamily),
                            fontSize: limitedInputFontSize,
                          ),
                      enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: FlutterFlowTheme.of(context).customColor6,
                          width: 0.5,
                        ),
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: FlutterFlowTheme.of(context).primary,
                          width: 0.5,
                        ),
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      errorBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: FlutterFlowTheme.of(context).error,
                          width: 0.5,
                        ),
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      errorStyle: TextStyle(
                        color: FlutterFlowTheme.of(context).error,
                        fontSize: limitedInputFontSize,
                      ),
                      focusedErrorBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: FlutterFlowTheme.of(context).error,
                          width: 0.5,
                        ),
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      suffixIcon: Icon(
                        Icons.person,
                        color: FlutterFlowTheme.of(context).accent1,
                      ),
                    ),
                    style: FlutterFlowTheme.of(context).bodyMedium.override(
                          fontFamily:
                              FlutterFlowTheme.of(context).bodyMediumFamily,
                          color: FlutterFlowTheme.of(context).primaryText,
                          letterSpacing: 0.0,
                          useGoogleFonts: GoogleFonts.asMap().containsKey(
                              FlutterFlowTheme.of(context).bodyMediumFamily),
                          fontSize: limitedInputFontSize,
                        ),
                    maxLines: null,
                    maxLength: 80,
                    keyboardType: TextInputType.name,
                    inputFormatters: [LengthLimitingTextInputFormatter(80)],
                    validator:
                        _model.textController1Validator.asValidator(context),
                  ),
                ),
                Padding(
                  padding: const EdgeInsetsDirectional.fromSTEB(
                      24.0, 0.0, 24.0, 10.0),
                  child: Column(
                    children: [
                      FlutterFlowDropDown<String>(
                        controller: _model.dropDownValueController ??=
                            FormFieldController<String>(null),
                        options: [
                          FFLocalizations.of(context).getText(
                            'n8vddmcq' /* Visitante  */,
                          ),
                          FFLocalizations.of(context).getText(
                            '9luaa09e' /* Prestador de Serviço */,
                          )
                        ],
                        onChanged: (val) =>
                            setState(() => _model.dropDownValue = val),
                        width: MediaQuery.sizeOf(context).width * 0.9,
                        // height: 44.0,
                        textStyle: FlutterFlowTheme.of(context)
                            .bodyMedium
                            .override(
                              fontFamily:
                                  FlutterFlowTheme.of(context).bodyMediumFamily,
                              color: FlutterFlowTheme.of(context).primaryText,
                              letterSpacing: 0.0,
                              useGoogleFonts: GoogleFonts.asMap().containsKey(
                                  FlutterFlowTheme.of(context)
                                      .bodyMediumFamily),
                              fontSize: limitedInputFontSize,
                            ),
                        hintText: FFLocalizations.of(context).getVariableText(
                          enText: 'Visitor Type',
                          ptText: 'Tipo de Visitante',
                        ),
                        icon: Icon(
                          Icons.keyboard_arrow_down_rounded,
                          color: FlutterFlowTheme.of(context).primaryText,
                          size: 24.0,
                        ),
                        elevation: 2.0,
                        borderColor: FlutterFlowTheme.of(context).customColor6,
                        borderWidth: 0.5,
                        borderRadius: 8.0,
                        margin: const EdgeInsetsDirectional.fromSTEB(
                            16.0, 0.0, 16.0, 0.0),
                        hidesUnderline: true,
                        isOverButton: true,
                        isSearchable: false,
                        isMultiSelect: false,
                      ),
                      if (_model.dropDownValue == null ||
                          _model.dropDownValue == '')
                        Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          mainAxisSize: MainAxisSize.max,
                          children: [
                            Padding(
                              padding: const EdgeInsetsDirectional.only(
                                  top: 5, start: 15),
                              child: Text(
                                  FFLocalizations.of(context).getVariableText(
                                    enText: 'Select the type of visitor',
                                    ptText: 'Selecione o tipo de visitante',
                                  ),
                                  style: FlutterFlowTheme.of(context)
                                      .bodySmall
                                      .override(
                                        fontFamily: FlutterFlowTheme.of(context)
                                            .bodySmallFamily,
                                        color: FlutterFlowTheme.of(context)
                                            .customColor6,
                                        letterSpacing: 0.0,
                                        useGoogleFonts: GoogleFonts.asMap()
                                            .containsKey(
                                                FlutterFlowTheme.of(context)
                                                    .bodySmallFamily),
                                        fontSize: limitedInputFontSize,
                                      )),
                            ),
                          ],
                        ),
                    ],
                  ),
                ),
                Padding(
                  padding: const EdgeInsetsDirectional.fromSTEB(
                      24.0, 0.0, 24.0, 0.0),
                  child: Builder(
                    builder: (context) {
                      if ((_model.uploadedLocalFile.bytes?.isNotEmpty ??
                          false)) {
                        return InkWell(
                          splashColor: Colors.transparent,
                          focusColor: Colors.transparent,
                          hoverColor: Colors.transparent,
                          highlightColor: Colors.transparent,
                          onTap: () async {
                            setState(() {
                              _model.isDataUploading = false;
                              _model.uploadedLocalFile =
                                  FFUploadedFile(bytes: Uint8List.fromList([]));
                            });
                          },
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(8.0),
                            child: Image.memory(
                              _model.uploadedLocalFile.bytes ??
                                  Uint8List.fromList([]),
                              width: 300.0,
                              height: 200.0,
                              fit: BoxFit.cover,
                            ),
                          ),
                        );
                      } else {
                        return Stack(children: [
                          MediaUploadButtonUtil(
                            onUploadComplete: (uploadedFile) {
                              setState(() {
                                _model.uploadedLocalFile = uploadedFile;
                              });
                            },
                            isUploading: _model.isDataUploading,
                            labelText: FFLocalizations.of(context).getText(
                              'p4ftwxcy' /* Selecione uma foto */,
                            ),
                            uploadedFiles: _model.uploadedLocalFile,
                          ),
                        ]);
                      }
                    },
                  ),
                ),
                Align(
                  alignment: const AlignmentDirectional(-1.0, 0.0),
                  child: Padding(
                    padding: const EdgeInsetsDirectional.fromSTEB(
                        20.0, 10.0, 0.0, 15.0),
                    child: Text(
                      FFLocalizations.of(context).getText(
                        'bqpucwh0' /* Contatos */,
                      ),
                      textAlign: TextAlign.start,
                      style: FlutterFlowTheme.of(context).bodyMedium.override(
                            fontFamily:
                                FlutterFlowTheme.of(context).bodyMediumFamily,
                            letterSpacing: 0.0,
                            useGoogleFonts: GoogleFonts.asMap().containsKey(
                                FlutterFlowTheme.of(context).bodyMediumFamily),
                            fontSize: limitedSubHeaderFontSize,
                          ),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsetsDirectional.fromSTEB(
                      24.0, 0.0, 24.0, 0.0),
                  child: TextFormField(
                    controller: _model.textController3,
                    focusNode: _model.textFieldFocusNode3,
                    cursorColor: FlutterFlowTheme.of(context).primary,
                    autovalidateMode: AutovalidateMode.onUserInteraction,
                    autofocus: false,
                    textInputAction: TextInputAction.next,

                    maxLength: 25,
                    keyboardType: TextInputType.phone,
                    inputFormatters: [
                      FilteringTextInputFormatter.allow(
                          RegExp('[0-9, +, -, (, )]')),
                      LengthLimitingTextInputFormatter(25)
                    ],
                    obscureText: false,

                    decoration: InputDecoration(
                      isDense: true,
                      labelText: FFLocalizations.of(context).getText(
                        'h84ls2r6' /* Telefone */,
                      ),
                      labelStyle: FlutterFlowTheme.of(context)
                          .labelMedium
                          .override(
                            fontFamily:
                                FlutterFlowTheme.of(context).labelMediumFamily,
                            color: FlutterFlowTheme.of(context).primaryText,
                            letterSpacing: 0.0,
                            useGoogleFonts: GoogleFonts.asMap().containsKey(
                                FlutterFlowTheme.of(context).labelMediumFamily),
                            fontSize: limitedInputFontSize,
                          ),
                      hintStyle: FlutterFlowTheme.of(context)
                          .labelMedium
                          .override(
                            fontFamily:
                                FlutterFlowTheme.of(context).labelMediumFamily,
                            color: FlutterFlowTheme.of(context).primaryText,
                            letterSpacing: 0.0,
                            useGoogleFonts: GoogleFonts.asMap().containsKey(
                                FlutterFlowTheme.of(context).labelMediumFamily),
                            fontSize: limitedInputFontSize,
                          ),
                      enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: FlutterFlowTheme.of(context).customColor6,
                          width: 0.5,
                        ),
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: FlutterFlowTheme.of(context).primary,
                          width: 0.5,
                        ),
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      errorBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: FlutterFlowTheme.of(context).error,
                          width: 0.5,
                        ),
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      focusedErrorBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: FlutterFlowTheme.of(context).error,
                          width: 0.5,
                        ),
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      suffixIcon: Icon(
                        Icons.phone,
                        color: FlutterFlowTheme.of(context).accent1,
                      ),
                    ),
                    style: FlutterFlowTheme.of(context).bodyMedium.override(
                          fontFamily:
                              FlutterFlowTheme.of(context).bodyMediumFamily,
                          color: FlutterFlowTheme.of(context).primaryText,
                          letterSpacing: 0.0,
                          useGoogleFonts: GoogleFonts.asMap().containsKey(
                              FlutterFlowTheme.of(context).bodyMediumFamily),
                          fontSize: limitedInputFontSize,
                        ),
                    // validator:
                    //     _model.textController3Validator.asValidator(context),
                  ),
                ),
                Padding(
                  padding: const EdgeInsetsDirectional.fromSTEB(
                      24.0, 0.0, 24.0, 0.0),
                  child: TextFormField(
                    controller: _model.textController4,
                    cursorColor: FlutterFlowTheme.of(context).primary,
                    focusNode: _model.textFieldFocusNode4,
                    autovalidateMode: AutovalidateMode.onUserInteraction,
                    autofocus: true,
                    onChanged: (_) => EasyDebounce.debounce(
                      '_model.textFieldFocusNode4',
                      const Duration(milliseconds: 500),
                      () => setState(() {}),
                    ),
                    textInputAction: TextInputAction.done,
                    obscureText: false,
                    maxLength: 80,
                    decoration: InputDecoration(
                      isDense: true,
                      labelText: FFLocalizations.of(context).getText(
                        'fqp7qmka' /* Email */,
                      ),
                      labelStyle: FlutterFlowTheme.of(context)
                          .labelMedium
                          .override(
                            fontFamily:
                                FlutterFlowTheme.of(context).labelMediumFamily,
                            color: FlutterFlowTheme.of(context).primaryText,
                            letterSpacing: 0.0,
                            useGoogleFonts: GoogleFonts.asMap().containsKey(
                                FlutterFlowTheme.of(context).labelMediumFamily),
                            fontSize: limitedInputFontSize,
                          ),
                      hintStyle: FlutterFlowTheme.of(context)
                          .labelMedium
                          .override(
                            fontFamily:
                                FlutterFlowTheme.of(context).labelMediumFamily,
                            color: FlutterFlowTheme.of(context).primaryText,
                            letterSpacing: 0.0,
                            useGoogleFonts: GoogleFonts.asMap().containsKey(
                                FlutterFlowTheme.of(context).labelMediumFamily),
                            fontSize: limitedInputFontSize,
                          ),
                      enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: FlutterFlowTheme.of(context).customColor6,
                          width: 0.5,
                        ),
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: FlutterFlowTheme.of(context).primary,
                          width: 0.5,
                        ),
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      errorBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: FlutterFlowTheme.of(context).error,
                          width: 0.5,
                        ),
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      focusedErrorBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: FlutterFlowTheme.of(context).error,
                          width: 0.5,
                        ),
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      suffixIcon: Icon(
                        Icons.email,
                        color: FlutterFlowTheme.of(context).accent1,
                      ),
                    ),
                    style: FlutterFlowTheme.of(context).bodyMedium.override(
                          fontFamily:
                              FlutterFlowTheme.of(context).bodyMediumFamily,
                          color: FlutterFlowTheme.of(context).primaryText,
                          letterSpacing: 0.0,
                          useGoogleFonts: GoogleFonts.asMap().containsKey(
                              FlutterFlowTheme.of(context).bodyMediumFamily),
                          fontSize: limitedInputFontSize,
                        ),
                    keyboardType: TextInputType.emailAddress,
                    inputFormatters: [LengthLimitingTextInputFormatter(80)],
                    validator:
                        _model.textController4Validator.asValidator(context),
                  ),
                ),
                Padding(
                  padding:
                      const EdgeInsetsDirectional.fromSTEB(0.0, 65.0, 0.0, 0.0),
                  child: FFButtonWidget(
                    onPressed: _isFormValid(context)
                        ? () async {
                            _model.imgBase64 =
                                await ImageUtils.convertImageFileToBase64(
                              _model.uploadedLocalFile,
                            );
                            _model.scheduleVisitor =
                                await PhpGroup.postScheduleVisitorCall
                                    .call(
                              documento: _model.textController2.text,
                              nome: _model.textController1.text,
                              tipo: _model.dropDownValue ==
                                      FFLocalizations.of(context).getText(
                                        'n8vddmcq' /* Visitante  */,
                                      )
                                  ? 'V'
                                  : 'P',
                              foto: 'base64;jpeg,${_model.imgBase64}',
                            )
                                    .onError((e, s) async {
                              return await DialogUtil.errorDefault(context);
                            });
                            if (PhpGroup.postScheduleVisitorCall.error(
                                  (_model.scheduleVisitor?.jsonBody ?? ''),
                                ) ==
                                false) {
                              setState(() {
                                _model.textController1?.clear();
                                _model.textController2?.clear();
                                _model.textController3?.clear();
                                _model.textController4?.clear();
                                _model.dropDownValueController?.reset();
                                _model.uploadedLocalFile = FFUploadedFile(
                                    bytes: Uint8List.fromList([]));
                                ScaffoldMessenger.of(context).showSnackBar(
                                  SnackBar(
                                    content: Text(
                                        FFLocalizations.of(context).getVariableText(
                                            ptText:
                                                'Visitante cadastrado com sucesso.',
                                            enText:
                                                'Visitor successfully registered.'),
                                        style: TextStyle(
                                          color:
                                              FlutterFlowTheme.of(context).info,
                                          fontSize: limitedInputFontSize,
                                        )),
                                    backgroundColor:
                                        FlutterFlowTheme.of(context).primary,
                                    duration: const Duration(seconds: 3),
                                    width: MediaQuery.of(context).size.width,
                                    behavior: SnackBarBehavior.floating,
                                    shape: const RoundedRectangleBorder(
                                      borderRadius: BorderRadius.only(
                                        topLeft: Radius.circular(15),
                                        topRight: Radius.circular(15),
                                      ),
                                    ),
                                  ),
                                );
                                _formKey.currentState?.reset();
                                if (widget.source ==
                                    'VisitorNotFoundComponent') {
                                  Navigator.pop(context, true);
                                }
                              });
                            } else {
                              return await DialogUtil.error(
                                  context,
                                  PhpGroup.postScheduleVisitorCall.errorMsg(
                                              _model
                                                  .scheduleVisitor?.jsonBody) ==
                                          null
                                      ? FFLocalizations.of(context)
                                          .getVariableText(
                                          ptText:
                                              'Erro ao se conectar com o servidor',
                                          enText: 'Error connecting to server',
                                        )
                                      : PhpGroup.postScheduleVisitorCall
                                          .errorMsg(
                                              _model.scheduleVisitor?.jsonBody)
                                          .toString());
                            }
                          }
                        : null,
                    text: FFLocalizations.of(context).getText(
                      'okbw0aiu' /* Cadastrar */,
                    ),
                    options: FFButtonOptions(
                      height: 30.0 * MediaQuery.textScalerOf(context).scale(1),
                      disabledColor: FlutterFlowTheme.of(context).customColor5,
                      padding: const EdgeInsetsDirectional.fromSTEB(
                          80.0, 0.0, 80.0, 0.0),
                      iconPadding: const EdgeInsetsDirectional.fromSTEB(
                          0.0, 0.0, 0.0, 0.0),
                      color: FlutterFlowTheme.of(context).primary,
                      textStyle: FlutterFlowTheme.of(context)
                          .titleSmall
                          .override(
                            fontFamily:
                                FlutterFlowTheme.of(context).titleSmallFamily,
                            color: FlutterFlowTheme.of(context).info,
                            letterSpacing: 0.0,
                            useGoogleFonts: GoogleFonts.asMap().containsKey(
                                FlutterFlowTheme.of(context).titleSmallFamily),
                            fontSize: limitedInputFontSize,
                          ),
                      borderSide: const BorderSide(
                        color: Colors.transparent,
                        width: 30.0,
                      ),
                      borderRadius: const BorderRadius.only(
                        bottomLeft: Radius.circular(15.0),
                        bottomRight: Radius.circular(15.0),
                        topLeft: Radius.circular(15.0),
                        topRight: Radius.circular(15.0),
                      ),
                    ),
                  ),
                ),
              ]
                  .divide(const SizedBox(height: 10.0))
                  .addToStart(const SizedBox(height: 30.0))
                  .addToEnd(const SizedBox(height: 30.0)),
            ),
          ),
        ),
      ),
    );
  }
}
