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_request/service_request.dart';
import '../../../../models/subtitle.dart';
import '../../../widgets/loaders/app_loading.dart';
import '../../../widgets/loaders/failed_loading.dart';
import 'request_details.dart';

class FutureRequestServiceDetails extends StatefulWidget {
  static const String id = "/service-request-details";

  const FutureRequestServiceDetails({super.key});

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

class _FutureRequestServiceDetailsState extends State<FutureRequestServiceDetails> {
  late UserProvider _userProvider;
  late SettingProvider _settingProvider;
  @override
  Widget build(BuildContext context) {
    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    String? requestId = ModalRoute.of(context)?.settings.arguments as String?;
    Subtitle _subtitle = AppLocalization.of(context)!.subtitle!;
    return Scaffold(
      body: FutureBuilder<ServiceRequest>(
        future: ServiceRequestsProvider().getSingleServiceRequest(requestId: requestId ?? '', user: UserApiClient().user!, host: _settingProvider.host ?? "", subtitle: _subtitle),
        builder: (BuildContext context, AsyncSnapshot<ServiceRequest> snapshot) {
          if (snapshot.hasError) {
            return FailedLoading(
              message: snapshot.error.toString(),
              onReload: () {
                setState(() {});
              },
            );
          }
          if (snapshot.hasData) {
            return RequestDetailsPage(
              serviceRequest: snapshot.data!,
            );
          }
          return const Center(child: ALoading());
        },
      ),
    );
  }
}
