import 'dart:async';

import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:vbvs_app/common/color/appColors.dart';
import 'package:vbvs_app/common/color/appConstants.dart';
import 'package:vbvs_app/common/color/appFontsize.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/component/NullDataComponentWidget.dart';
import 'package:vbvs_app/component/tool/ClickableContainer.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/mh_controller/experience_store_list_page.dart';
import 'package:vbvs_app/controller/weather/weather_controller.dart';
import 'package:vbvs_app/pages/mh_page/Empty.dart';
import 'package:vbvs_app/pages/mh_page/experience_store_widget.dart';
import 'package:vbvs_app/pages/mh_page/searchWidget.dart';

class ExperienceStorePage extends StatefulWidget {
  ExperienceStorePage({super.key});
  @override
  State<ExperienceStorePage> createState() => _ExperienceStorePageState();
}

class _ExperienceStorePageState extends State<ExperienceStorePage> {
  final scaffoldKey = GlobalKey<ScaffoldState>();
  BoxConstraints? bodysize;

  ScrollController scrollController = ScrollController();
  ExperienceStoreListController controller = Get.find();
  WeatherModelController weatherModelController = Get.find();
  int runTime = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Timer(Duration(milliseconds: 100), () async {
      controller.resetParm();
      await weatherModelController.determinePosition();
      controller.page = 0;
      controller.getData();
      scrollController.addListener(() {
        int ctime = DateTime.now().millisecondsSinceEpoch;
        if (ctime - runTime > 100 &&
            scrollController.position.pixels + 80 >
                scrollController.position.maxScrollExtent) {
          runTime = ctime;
          print(
              "bottom get more data ${scrollController.position.pixels} ${scrollController.position.maxScrollExtent}");
          controller.getData();
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, cc) {
      bodysize = cc;

      return GestureDetector(
          onTap: () => FocusScope.of(context).unfocus(),
          child: Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/images/new_background.png'), // 本地图片
                fit: BoxFit.fill, // 填满整个 Container
              ),
            ),
            child: Scaffold(
                // key: scaffoldKey,
                backgroundColor: Colors.transparent,
                appBar: AppBar(
                  backgroundColor: Colors.transparent,
                  automaticallyImplyLeading: false,
                  iconTheme: IconThemeData(color: Colors.white),
                  titleSpacing: 0,
                  title: SizedBox(
                    width: double.infinity,
                    height: 180.rpx,
                    child: Stack(
                      alignment: Alignment.center,
                      children: [
                        // 中间居中的标题
                        Text(
                          '预约体验',
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 30.rpx,
                          ),
                        ),
                        // 左侧图标
                        Positioned(
                          left: 0.rpx,
                          child: returnIconButtomNew,
                        ),
                        Positioned(
                            right: 30.rpx,
                            child: ClickableContainer(
                                backgroundColor: Colors.transparent,
                                highlightColor: Color(0xFF055466),
                                padding: EdgeInsets.only(left: 0),
                                onTap: () {
                                  TopSlideNotification.show(context,
                                      text: "待开发功能".tr);
                                  //  Get.toNamed("/myExperiencePage");
                                },
                                child: SvgPicture.asset(
                                  'assets/img/icon/history_store.svg',
                                  width: 35.rpx,
                                  height: 35.rpx,
                                  color: Colors.white,
                                )))
                      ],
                    ),
                  ),
                  centerTitle: false,
                ),
                body: SingleChildScrollView(
                  child: Container(
                    width: bodysize!.maxWidth,
                    height: bodysize!.maxHeight * 1,
                    child: Column(
                      mainAxisSize: MainAxisSize.max,
                      children: [
                        //todo

                        Padding(
                          padding: EdgeInsetsDirectional.fromSTEB(
                              0, 40.rpx, 0, 38.rpx),
                          child: SearchWidget(
                            keyword: controller.model.keyword,
                            color: controller.model.color,
                            hint: "请输入门店名称",
                            onChange: (d) {
                              controller.model.keyword = d;
                            },
                            findCallback: () {
                              // controller.page = 0;
                              // controller.getData();
                            },
                          ),
                        ),

                        Obx(() {
                          if (controller
                                  .model.experienceStoreModelList.length ==
                              0) {
                            return CircularProgressIndicator(
                              strokeWidth: 1,
                              valueColor:
                                  AlwaysStoppedAnimation<Color>(Colors.white),
                            );
                          } else {
                            return Container();
                          }
                        }),

                        Expanded(
                            child: Obx(() => ListView(
                                controller: scrollController,
                                shrinkWrap: true,
                                scrollDirection: Axis.vertical,
                                children: List.generate(
                                        controller
                                            .model
                                            .experienceStoreModelList
                                            .length, (index) {
                                  return ExperienceStoreWidget(
                                      index: index,
                                      data: controller.model
                                          .experienceStoreModelList[index]);
                                })
                                    .divide(const SizedBox(
                                      height: 25,
                                    ))
                                    .addToStart(SizedBox(
                                      height: AppConstants.list_start_height,
                                    ))
                                    .addToEnd(SizedBox(
                                      height: AppConstants.list_ano_end_height,
                                    ))))),
                      ],
                    ),
                  ),
                )),
          ));
    });
  }
}
