import 'package:auto_route/auto_route.dart';
import 'package:cpt_payment/modules/payment_info/payment_info_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/load_state_layout.dart';
import 'package:widgets/widget_export.dart';
import '../../payment_confirm/payment_confirm_page.dart';
import 'payment_list_view_model.dart';
import 'item_payment_list.dart';

@RoutePage()
class PaymentListScreen extends HookConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final viewModel = ref.read(paymentListViewModelProvider.notifier);
    final state = ref.watch(paymentListViewModelProvider);

    useEffect(() {
      // 组件挂载时执行 - 执行接口请求
      Future.microtask(() => viewModel.fetchList());
      return () {
        // 组件卸载时执行
      };
    }, []);

    return SizedBox(
      width: double.infinity,
      height: double.infinity,
      child: EasyRefresh(
        controller: viewModel.refreshController,
        onRefresh: viewModel.onRefresh,
        onLoad: viewModel.loadMore,
        child: LoadStateLayout(
          state: state.loadingState,
          errorMessage: state.errorMessage,
          errorRetry: () {
            viewModel.retryRequest();
          },
          successSliverWidget: [
            SliverList(
                delegate: SliverChildBuilderDelegate(
                      (context, index) {
                    return PaymentListItem(index: index, item: state.datas[index]).onTap(() {
                      //去信息页面
                      //根据类型跳转到不同的页面
                      viewModel.gotoPaymentPage(state.datas[index]);

                    });
                  },
                  childCount: state.datas.length,
                ))
          ],
        ),
      ).marginOnly(top: 5, bottom: 5),
    );
  }
}
