import 'dart:developer';

import 'package:hub/components/molecular_components/throw_exception/throw_exception_widget.dart';
import 'package:hub/custom_code/actions/convert_image_file_to_base64.dart';
import 'package:hub/flutter_flow/flutter_flow_theme.dart';

import '/backend/api_requests/api_calls.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/flutter_flow/form_field_controller.dart';
import 'package:flutter/material.dart';

import 'regisiter_vistor_template_component_widget.dart';

class RegisiterVistorTemplateComponentModel
    extends FlutterFlowModel<RegisiterVistorTemplateComponentWidget> {
  ///  State fields for stateful widgets in this page.

  final unfocusNode = FocusNode();
  bool isDataUploading = false;
  FFUploadedFile uploadedLocalFile =
      FFUploadedFile(bytes: Uint8List.fromList([]));

  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode1;
  TextEditingController? textController1;
  String? Function(BuildContext, String?)? textController1Validator;
  String? _textController1Validator(BuildContext context, String? val) {
    if (val == null || val.isEmpty) {
      return FFLocalizations.of(context).getVariableText(
        enText: 'This field is required',
        ptText: 'Este campo é obrigatório',
      );
    }

    return null;
  }

  Future<bool?> getVisitanteByDocument(
      String document, BuildContext context) async {
    final response = await PhpGroup.getVisitorByDocCall.call(
      devUUID: FFAppState().devUUID,
      userUUID: FFAppState().userUUID,
      cliID: FFAppState().cliUUID,
      atividade: 'getVisitante',
      documento: document,
    );
    log('${response.jsonBody}');
    if (response.jsonBody['visitante']['VTE_ID'] != '0' &&
        response.jsonBody['error'] != 'false') {
      return true;
    }
    return null;
  }

  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode2;
  TextEditingController? textController2;
  String? Function(BuildContext, String?)? textController2Validator;
  String? _textController2Validator(BuildContext context, String? val) {
    if (val == null || val.isEmpty) {
      return FFLocalizations.of(context).getVariableText(
        enText: 'This field is required',
        ptText: 'Este campo é obrigatório',
      );
    }

    return null;
  }

  // State field(s) for DropDown widget.
  String? dropDownValue;
  FormFieldController<String>? dropDownValueController;
  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode3;
  TextEditingController? textController3;
  String? Function(BuildContext, String?)? textController3Validator;
  String? _textController3Validator(BuildContext context, String? val) {
    if (val == null || val.isEmpty) {
      return FFLocalizations.of(context).getVariableText(
        enText: 'This field is required',
        ptText: 'Este campo é obrigatório',
      );
    }
    return null;
  }

  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode4;
  TextEditingController? textController4;
  String? Function(BuildContext, String?)? textController4Validator;
  String? _textController4Validator(BuildContext context, String? val) {
    if (val == null || val.isEmpty) {
      return FFLocalizations.of(context).getVariableText(
        enText: 'This field is required',
        ptText: 'Este campo é obrigatório',
      );
    }
    if (!val.contains('@') || !val.contains('.')) {
      return FFLocalizations.of(context).getVariableText(
        enText: 'Invalid email',
        ptText: 'Email inválido',
      );
    }
    return null;
  }

  // Stores action output result for [Custom Action - convertImageFileToBase64] action in Button widget.
  String? imgBase64;
  // Stores action output result for [Backend Call - API (postScheduleVisitor)] action in Button widget.
  ApiCallResponse? scheduleVisitor;

  @override
  void initState(BuildContext context) {
    textFieldFocusNode1 = FocusNode();
    textController1 = TextEditingController();
    textController1Validator = _textController1Validator;

    textFieldFocusNode2 = FocusNode();
    textController2 = TextEditingController();
    textController2Validator = _textController2Validator;

    dropDownValue = '';
    // dropDownValueController = FormFieldController<String>(initialValue: dropDownValue);

    textFieldFocusNode3 = FocusNode();
    textController3 = TextEditingController();
    textController3Validator = _textController3Validator;

    textFieldFocusNode4 = FocusNode();
    textController4 = TextEditingController();
    textController4Validator = _textController4Validator;
  }

  @override
  void dispose() {
    unfocusNode.dispose();
    textFieldFocusNode1?.dispose();
    textController1?.dispose();

    textFieldFocusNode2?.dispose();
    textController2?.dispose();

    textFieldFocusNode3?.dispose();
    textController3?.dispose();

    textFieldFocusNode4?.dispose();
    textController4?.dispose();
  }
}
