import '/flutterlib/flutter_icon_button.dart';
import '/flutterlib/flutter_theme.dart';
import '/flutterlib/flutter_util.dart';
import '/flutterlib/custom_functions.dart' as functions;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';
import 'date_model.dart';
export 'date_model.dart';

class DateWidget extends StatefulWidget {
  const DateWidget({
    Key? key,
    this.companyMembers,
  }) : super(key: key);

  final dynamic companyMembers;

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

class _DateWidgetState extends State<DateWidget> {
  late DateModel _model;

  @override
  void setState(VoidCallback callback) {
    super.setState(callback);
    _model.onUpdate();
  }

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

    _model.dateController ??= TextEditingController();
    _model.dateFocusNode ??= FocusNode();
  }

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

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    context.watch<FFAppState>();

    return Row(
      mainAxisSize: MainAxisSize.max,
      children: [
        Expanded(
          child: Padding(
            padding: EdgeInsetsDirectional.fromSTEB(8.0, 0.0, 8.0, 0.0),
            child: Container(
              width: 326.0,
              child: TextFormField(
                controller: _model.dateController,
                focusNode: _model.dateFocusNode,
                textCapitalization: TextCapitalization.none,
                readOnly: true,
                obscureText: false,
                decoration: InputDecoration(
                  hintText: FFLocalizations.of(context).getText(
                    'q97i0ohq' /* YYYY/MM/DD */,
                  ),
                  hintStyle: FlutterTheme.of(context).labelMedium,
                  enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: Color(0xFF6D7581),
                      width: 2.0,
                    ),
                    borderRadius: BorderRadius.circular(23.8),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: FlutterTheme.of(context).primary,
                      width: 2.0,
                    ),
                    borderRadius: BorderRadius.circular(23.8),
                  ),
                  errorBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: FlutterTheme.of(context).error,
                      width: 2.0,
                    ),
                    borderRadius: BorderRadius.circular(23.8),
                  ),
                  focusedErrorBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: FlutterTheme.of(context).error,
                      width: 2.0,
                    ),
                    borderRadius: BorderRadius.circular(23.8),
                  ),
                  filled: true,
                  fillColor: Colors.white,
                ),
                style: FlutterTheme.of(context).bodyMedium.override(
                      fontFamily: 'Readex Pro',
                      fontSize: 16.0,
                    ),
                keyboardType: TextInputType.datetime,
                validator: _model.dateControllerValidator.asValidator(context),
              ),
            ),
          ),
        ),
        Padding(
          padding: EdgeInsetsDirectional.fromSTEB(0.0, 0.0, 8.0, 0.0),
          child: FlutterIconButton(
            borderColor: FlutterTheme.of(context).primaryText,
            borderRadius: 20.0,
            borderWidth: 1.0,
            buttonSize: 40.0,
            fillColor: FlutterTheme.of(context).secondaryBackground,
            icon: Icon(
              Icons.calendar_month,
              color: FlutterTheme.of(context).primaryText,
              size: 24.0,
            ),
            onPressed: () async {
              final _datePickedDate = await showDatePicker(
                context: context,
                initialDate: getCurrentTimestamp,
                firstDate: DateTime(1900),
                lastDate: getCurrentTimestamp,
              );

              if (_datePickedDate != null) {
                safeSetState(() {
                  _model.datePicked = DateTime(
                    _datePickedDate.year,
                    _datePickedDate.month,
                    _datePickedDate.day,
                  );
                });
              }
              setState(() {
                _model.dateController?.text =
                    functions.formatDate(_model.datePicked!.toString());
              });
            },
          ),
        ),
      ],
    );
  }
}
