import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../../../api/login_api_client.dart';
import '../../../../../controllers/localization/localization.dart';
import '../../../../../controllers/providers/api/regular_visits_provider.dart';
import '../../../../../controllers/providers/settings/setting_provider.dart';
import '../../../../../models/pantry/pentry.dart';
import '../../../../../models/subtitle.dart';
import '../../../../../models/visits/visit.dart';
import '../../../../widgets/loaders/app_loading.dart';
import '../../../../widgets/loaders/failed_loading.dart';
import 'edit_pentry.dart';

class FutureEditPentry extends StatefulWidget {
  final Visit visit;

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

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

class _FutureEditPentryState extends State<FutureEditPentry> {
  late SettingProvider _settingProvider;
  @override
  Widget build(BuildContext 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: UserApiClient().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());
        },
      ),
    );
  }
}
