import 'package:domain/repository/facility_repository.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import 'facility_detail_state.dart';

part 'facility_detail_view_model.g.dart';

@riverpod
class FacilityDetailViewModel extends _$FacilityDetailViewModel with DioCancelableMixin{
  late final FacilityRepository _facilityRepository;
  @override
  FacilityDetailState build() {
    _facilityRepository = ref.read(facilityRepositoryProvider);
    final state =  FacilityDetailState();
    registerCancellation();
    return state;
  }

  /// 获取详情
  Future fetchFacilityDetail(String? bookingId) async {
    final result = await _facilityRepository.fetchFacilityBookingDetail(id: bookingId, cancelToken: cancelToken);

    // 处理数据
    if (result.isSuccess) {
      state = state.copyWith(detail: result.data);
    } else {
      ToastEngine.show(result.errorMsg ?? "UnKnow Error");
    }
  }

}
