import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/controller.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/widgets/mobile/record_card.dart';
import 'package:get/get.dart';
import 'package:fis_ui/widgets/scroll/refreshIndicator.dart';
import 'mobile_title_view.dart';

class ConsultationRecordMobileView extends FStatefulWidget implements FPage {
  @override
  FState<FStatefulWidget> createState() {
    return ConsultationRecordMobileViewState();
  }

  @override
  String get pageName => "ConsultationRecordMobileView";
}

class ConsultationRecordMobileViewState
    extends FState<ConsultationRecordMobileView> {
  final controller = Get.find<ConsultationRecordViewController>();
  final ScrollController scrollController = ScrollController();
  int _pageIndex = 1;
  @override
  initState() {
    scrollController.addListener(_scrollListener);
    super.initState();
  }

  @override
  void dispose() {
    scrollController.removeListener(_scrollListener);
    super.dispose();
  }

  @override
  FWidget build(BuildContext context) {
    return FScaffold(
      appBar: FAppBar(
        toolbarHeight: 50,
        title: MobileTitleView(
          onPressed: (r) {
            controller.changeTypeFilter(r);
          },
          recordProcessState: controller.typeFilter,
        ),
        leading: FSizedBox(),
        backgroundColor: Colors.white,
        elevation: 0,
        leadingWidth: 0,
        centerTitle: true,
      ),
      backgroundColor: Colors.white,
      body: _buildBody(),
    );
  }

  void _scrollListener() {
    if (scrollController.position.pixels ==
        scrollController.position.maxScrollExtent) {
      _pageIndex++;
      controller.onChangePage(_pageIndex, 20, isLoadMore: true);
    }
  }

  FWidget _buildBody() {
    return FisBuilder(
      init: ConsultationRecordViewController(),
      id: 'record_data_table',
      builder: (c) {
        return Column(
          children: [
            /// Flutter SDK 3.7.0 存在 bug ，如果将 TextField 放在顶部，会导致[复制|剪切]工具弹窗错位
            /// https://github.com/flutter/flutter/issues/122532
            /// 所以下方必须嵌一行 Container ，等 SDK 修复后，可以移除
            Container(
              height: 1,
            ),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 10),
              height: 50,
              child: _buildSearchInput(),
            ),
            FExpanded(
              child: FRefreshIndicator(
                onRefresh: () async {
                  controller.searchFindRecordPages();
                },
                child: controller.consultationsRecordDataList.length <= 0
                    ? FCenter(child: FText(i18nBook.common.noData.t))
                    : FAlwaysScrollListView(
                        scrollController: scrollController,
                        child: QuickFWidget(ScrollConfiguration(
                          behavior: ScrollConfiguration.of(context)
                              .copyWith(scrollbars: false),
                          child: FListView.builder(
                            shrinkWrap: true,
                            padding: EdgeInsets.only(bottom: 5),
                            controller: scrollController,
                            itemCount:
                                controller.consultationsRecordDataList.length,
                            itemBuilder: (context, index) {
                              /// 是否是已加载的最后一个
                              final isLoadedLast = index ==
                                  controller
                                          .consultationsRecordDataList.length -
                                      1;
                              final isLast =
                                  index == controller.totalDataCount - 1;

                              final recordDataInfo =
                                  controller.consultationsRecordDataList[index];
                              final card = RecordCard(
                                recordDataInfo,
                                () async {
                                  controller.openRecordDetailPage(
                                    recordDataInfo.recordCode!,
                                    recordDataInfo.patientCode!,
                                    recordDataInfo.devicePatientID ?? '',
                                  );
                                },
                              );
                              if (!isLoadedLast) {
                                return card;
                              } else {
                                return FColumn(children: [
                                  card,
                                  FContainer(
                                    height: 50,
                                    child: FCenter(
                                      child: FText(
                                        isLast
                                            ? i18nBook.remedical.noMore.t
                                            : i18nBook.common.loading.t,
                                        style: TextStyle(
                                          fontSize: 14,
                                          color: Colors.grey,
                                        ),
                                      ),
                                    ),
                                  )
                                ]);
                              }
                            },
                          ),
                        )),
                      ),
              ),
            ),
          ],
        );
      },
    );
  }

  FWidget _buildSearchInput() {
    return FBorderInput(
      fillColor: Colors.white,
      hintSize: 14,
      contentSize: 14,
      maxLength: 50,
      borderColor: Color.fromARGB(255, 185, 184, 184),
      suffixIcon: FIconButton(
        name: "search",
        onPressed: () {
          controller.searchFindRecordPages();
        },
        icon: FIcon(
          Icons.search,
          size: 16,
        ),
        businessParent: this.widget,
      ),
      height: 40,
      width: double.infinity,
      hintText: i18nBook.common.search.t,
      onChanged: (value) {
        controller.keyWord = value;
      },
    );
  }
}
