import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_last_calls_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/service_request/service_report.dart';
import 'package:test_sa/new_views/common_widgets/single_item_drop_down_menu.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';

import '../../../../providers/loading_list_notifier.dart';

class ServiceReportLastCallsMenu extends StatelessWidget {
  final Function(Lookup?) onSelect; // Accepts nullable Lookup
  final ServiceReport report;
  final String? title; // Nullable title
  final String? parentId; // Nullable parentId

  const ServiceReportLastCallsMenu({
    Key? key,
    required this.onSelect,
    required this.report,
    this.parentId,
    this.title,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final menuProvider = Provider.of<ServiceReportLastCallsProvider>(context);
    return LoadingManager(
      isLoading: menuProvider.isLoading,
      isFailedLoading: menuProvider.calls == null,
      stateCode: menuProvider.stateCode,
      onRefresh: () async {
        await menuProvider.getCalls(typeName: report.assetType?.name, id: report.id!.toInt(), parentId: parentId);
      },
      child: SingleItemDropDownMenu<Lookup, NullableLoadingProvider>(
        context: context,
        title: title ?? context.translation.callLastSituation,
        // Default title if null
        staticData: menuProvider.calls ?? [],
        // Empty list if calls is null
        initialValue: report.calllastSituation,
        onSelect: onSelect,
      ),
    );
  }
}
