import 'package:domain/repository/payment_repository.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import 'payment_success_state.dart';

part 'payment_success_view_model.g.dart';

@riverpod
class PaymentSuccessViewModel extends _$PaymentSuccessViewModel {
  late final PaymentRepository _paymentRepository;

  @override
  PaymentSuccessState build() {
    _paymentRepository = ref.read(paymentRepositoryProvider);
    return PaymentSuccessState();
  }

  //获取表单的详情数据
  void fetchFormDetail(String? orderId) async {
    final result = await _paymentRepository.fetchPaymentDetail(orderId: orderId ?? "");

    //根据后端返回的数据赋值状态
    if (result.isSuccess) {
      state = state.copyWith(detail: result.data);
    } else {
      ToastEngine.show(result.errorMsg ?? "Unknown Error");
    }
  }

}
