import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/controllers/providers/api/regular_visits_provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/models/pantry/pentry.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/models/visits/visit.dart';
import 'package:test_sa/views/pages/user/visits/pantry/edit_pentry.dart';
import 'package:test_sa/views/widgets/loaders/app_loading.dart';
import 'package:test_sa/views/widgets/loaders/failed_loading.dart';

class FutureEditPentry extends StatefulWidget {
  final Visit visit;

  const FutureEditPentry({Key key, this.visit}) : super(key: key);

  @override
  State<FutureEditPentry> createState() => _FutureEditPentryState();
}

class _FutureEditPentryState extends State<FutureEditPentry> {
  UserProvider _userProvider;
  SettingProvider _settingProvider;
  @override
  Widget build(BuildContext context) {
    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    //String requestId = ModalRoute.of(context).settings.arguments;
    Subtitle subtitle = AppLocalization.of(context).subtitle;
    return Scaffold(
      body: FutureBuilder<Pentry>(
        future: RegularVisitsProvider().getPently(user: _userProvider.user, host: _settingProvider.host, id: widget.visit.id),
        builder: (BuildContext context, AsyncSnapshot<Pentry> snapshot) {
          if (snapshot.hasError) {
            return FailedLoading(
              message: subtitle.failedToCompleteRequest,
              onReload: () {
                setState(() {});
              },
            );
          }
          if (snapshot.hasData) {
            return EditPentry(
              pentry: snapshot.data,
              visit: widget.visit,
            );
          }
          return const Center(child: ALoading());
        },
      ),
    );
  }
}
