import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_fault_description_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/fault_description.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
import 'package:test_sa/views/widgets/status/report/fault_desc_menu.dart';

import '../../app_text_form_field.dart';

class ServiceReportFaultDescription extends StatelessWidget {
  final String requestId;
  final Function(FaultDescription) onSelect;
  final FaultDescription initialValue;
  final bool enabled;

  const ServiceReportFaultDescription({Key key, this.requestId, this.onSelect, this.initialValue, this.enabled = true}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    SettingProvider settingProvider = Provider.of<SettingProvider>(context);
    UserProvider userProvider = Provider.of<UserProvider>(context);
    ServiceRequestFaultDescriptionProvider menuProvider = Provider.of<ServiceRequestFaultDescriptionProvider>(context);
    return LoadingManager(
      isLoading: menuProvider.isLoading,
      isFailedLoading: menuProvider.items == null,
      stateCode: menuProvider.stateCode,
      onRefresh: () async {
        menuProvider.reset();
        await menuProvider.getCallRequestForWorkOrder(user: userProvider.user, host: settingProvider.host, requestId: requestId);
      },
      child: ((menuProvider.items?.isEmpty ?? true) || (!enabled))
          ? ATextFormField(
              initialValue: (!enabled) ? initialValue?.defectName ?? "" : "",
              enable: false,
            )
          : FaultDescriptionMenu(
              initialStatus: initialValue,
              statuses: menuProvider.items,
              onSelect: onSelect,
            ),
    );
  }
}
