import 'package:cs_resources/generated/assets.dart';
import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:router/ext/auto_router_extensions.dart';
import 'package:shared/utils/color_utils.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:widgets/load_state_layout.dart';
import 'package:widgets/my_button.dart';
import 'package:widgets/my_load_image.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/my_appbar.dart';
import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:widgets/utils/dark_theme_util.dart';
import 'package:widgets/widget_export.dart';

import '../../../components/garage_card.dart';
import '../../../components/newfeed_card_header.dart';
import '../../../router/page/community_page_router.dart';
import '../components/my_posts_sale_rent_item.dart';
import 'my_posts_forrent_vm.dart';

@RoutePage()
class MyPostsForRentPage extends HookConsumerWidget {
  const MyPostsForRentPage({Key? key}) : super(key: key);

  //启动当前页面
  static void startInstance({BuildContext? context}) {
    if (context != null) {
      context.router.push(const MyPostsForRentPageRoute());
    } else {
      appRouter.push(const MyPostsForRentPageRoute());
    }
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final vm = ref.read(myPostsForRentVmProvider.notifier);
    final state = ref.watch(myPostsForRentVmProvider);
    useEffect(() {
      // 组件挂载时执行 - 执行接口请求
      Future.microtask(() => vm.initPageData());
      return () {
        // 组件卸载时执行
        Log.d("garage_forrent_page 组件卸载时执行");
      };
    }, []);

    return Scaffold(
      // appBar: MyAppBar.appBar(
      //   context,
      //   "MyPostsForRent",
      //   backgroundColor: context.appColors.whiteBG,
      // ),
      backgroundColor: DarkThemeUtil.multiColors(context, ColorUtils.string2Color("#F2F3F6"), darkColor: Colors.black),
      body: SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: EasyRefresh(
            controller: vm.refreshController,
            // 上拉加载
            onLoad: () async{
              Log.d("----onLoad");
              vm.loadMore();
            },
            // 下拉刷新
            onRefresh: () async{
              Log.d("----onRefresh");
              vm.onRefresh();
            },
            child: LoadStateLayout(
              state: state.loadingState,
              errorMessage: state.errorMessage,
              errorRetry: () {
                vm.retryRequest();
              },
              successSliverWidget:[
                SliverGrid(
                  gridDelegate:  const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2, // 每行显示两个项目
                    mainAxisSpacing: 15,
                    crossAxisSpacing: 15,
                    childAspectRatio: 0.85, // 宽高比
                  ),
                  delegate: SliverChildBuilderDelegate(
                        (context, index) {
                          return MyPostsSaleRentItem(item: state.list[index],garageSaleType: 'forRent').onTap((){
                            vm.handlerGotoDetail(state.list[index]['id']);
                          });
                    },
                    childCount: state.list.length,
                  ),
                ),
              ],
            ),
          ).marginOnly(left: 15,right: 15,top: 15,bottom: 15)
      ),
    );
  }
}