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/service_requests_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/service_report.dart';
import 'package:test_sa/models/service_request/service_request.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/views/widgets/loaders/app_loading.dart';
import 'package:test_sa/views/widgets/loaders/failed_loading.dart';

import 'edit_service_report.dart';
class FutureServiceReport extends StatefulWidget {
  final ServiceRequest request;

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

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

class _FutureServiceReportState extends State<FutureServiceReport> {

  UserProvider _userProvider;
  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: _userProvider.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());
        },
      ),
    );
  }
}
