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/service_requests_provider.dart';
import '../../../../../controllers/providers/settings/setting_provider.dart';
import '../../../../../controllers/providers/user_provider.dart';
import '../../../../../models/service_report.dart';
import '../../../../../models/service_request/service_request.dart';
import '../../../../../models/subtitle.dart';
import '../../../../widgets/loaders/app_loading.dart';
import '../../../../widgets/loaders/failed_loading.dart';
import 'edit_service_report.dart';

class FutureServiceReport extends StatefulWidget {
  final ServiceRequest request;

  const FutureServiceReport({Key? key, required this.request}) : super(key: key);

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

class _FutureServiceReportState extends State<FutureServiceReport> {
  late UserProvider _userProvider;
  late SettingProvider _settingProvider;
  @override
  Widget build(BuildContext context) {
    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    Subtitle _subtitle = AppLocalization.of(context)!.subtitle!;
    return Scaffold(
      body: FutureBuilder<ServiceReport>(
        future: ServiceRequestsProvider().getSingleServiceReport(reportId: widget.request.reportID ?? "", user: UserApiClient().user!, host: _settingProvider.host ?? "", subtitle: _subtitle),
        builder: (BuildContext context, AsyncSnapshot<ServiceReport> snapshot) {
          if (snapshot.hasError)
            return FailedLoading(
              message: snapshot.error.toString(),
              onReload: () {
                setState(() {});
              },
            );
          if (snapshot.hasData) {
            return EditServiceReport(
              report: snapshot.data!,
              request: widget.request,
            );
          }
          return Center(child: ALoading());
        },
      ),
    );
  }
}
