import 'package:flutter/material.dart';
import 'package:test_sa/controllers/providers/api/service_requests_provider.dart';
import 'package:test_sa/models/service_request/search_work_order.dart';
import 'package:test_sa/models/service_request/service_report.dart';
import 'package:test_sa/models/service_request/service_request.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 UpdateServiceReport extends StatefulWidget {
  final ServiceRequest request;
  final SearchWorkOrder workOrder;

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

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

class _UpdateServiceReportState extends State<UpdateServiceReport> {
  @override
  Widget build(BuildContext context) {
    ServiceRequestsProvider().getSingleServiceReport(context, reportId: widget.workOrder.id!.toInt());
    return Scaffold(
      body: FutureBuilder<ServiceReport>(
        future: ServiceRequestsProvider().getSingleServiceReport(context, reportId: widget.workOrder.id!.toInt()),
        builder: (BuildContext context, AsyncSnapshot<ServiceReport> snapshot) {
          if (snapshot.hasError) {
            return FailedLoading(
              message: snapshot.error.toString(),
              onReload: () {
                setState(() {});
              },
            );
          }
          if (snapshot.connectionState == ConnectionState.done) {
            return EditServiceReport(report: snapshot.data as ServiceReport, request: widget.request);
          }
          return const Center(child: ALoading());
        },
      ),
    );
  }
}
