import 'package:auto_route/auto_route.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 '../../detail/form_detail_page.dart';
import 'form_submit_view_model.dart';
import 'item_form_submit.dart';

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

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

    return Container(
      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 FormSubmitItem(index: index, item: state.datas[index]).onTap(() {
                  //去详情页面
                  //去详情页面
                  FormDetailPage.startInstance(
                    context: context,
                    estateFormId: state.datas[index].estateOnlineFormId ?? "",
                    taskId: state.datas[index].id ?? "",
                    type: state.datas[index].estateOnlineForm?.type?.id ?? "",
                  );
                });
              },
              childCount: state.datas.length,
            ))
          ],
        ),
      ).marginOnly(top: 5, bottom: 5),
    );
  }
}
