import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../../api/login_api_client.dart';
import '../../../../controllers/providers/api/status_drop_down/report/service_report_types_provider.dart';
import '../../../../controllers/providers/settings/setting_provider.dart';
import '../../../../controllers/providers/user_provider.dart';
import '../../../../models/lookup.dart';
import '../../loaders/loading_manager.dart';
import '../single_status_menu.dart';

class ServiceReportTypeMenu extends StatelessWidget {
  final Function(Lookup?) onSelect;
  final Lookup? initialValue;

  const ServiceReportTypeMenu({
    Key? key,
    required this.onSelect,
    this.initialValue,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    SettingProvider settingProvider = Provider.of<SettingProvider>(context);
    UserProvider userProvider = Provider.of<UserProvider>(context);
    ServiceReportTypesProvider menuProvider = Provider.of<ServiceReportTypesProvider>(context);
    return LoadingManager(
        isLoading: menuProvider.isLoading,
        isFailedLoading: menuProvider.types == null,
        stateCode: menuProvider.stateCode,
        onRefresh: () async {
          menuProvider.reset();
          await menuProvider.getTypes(
            user: UserApiClient().user,
            host: settingProvider.host,
          );
          onSelect(initialValue ?? menuProvider.types?.last);
        },
        child: SingleStatusMenu(
          initialStatus: initialValue ?? menuProvider.types?.last,
          statuses: menuProvider.types,
          onSelect: onSelect,
        ));
  }
}
