import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:fis_ui/index.dart';
import 'package:flyinsono/managers/interfaces/entities/image_report_list_params.dart';
import 'package:flyinsono/pages/remedical/consultation/view.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/widgets/consultation_record_filter.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/widgets/history_mode_header.dart';
import 'package:flyinsono/pages/remedical/image_report_inner_view/controller.dart';
import 'package:flyinsono/pages/user/group_chat/model.dart';
import 'package:flyinsono/pages/user/group_chat/view.dart';
import 'package:get/get.dart';
import 'index.dart';
import 'package:flyinsono/pages/remedical/image_report_inner_view/view.dart';
import 'widgets/consultation_record_filter_drawer.dart';
import 'widgets/mobile/consultation_record_view_mobile.dart';
import 'widgets/widgets.dart';
import 'package:fis_ui/base_define/page.dart';

class ConsultationRecordViewPage
    extends FisView<ConsultationRecordViewController> implements FPage {
  @override
  String get pageName => "ConsultationRecordViewPage";

  ConsultationRecordViewPage({Key? key}) : super(key: key);

  // 主视图
  FWidget _buildView() {
    var body = FRow(
      children: [
        FExpanded(
          child: FInkWell(
            // 将highlightColor和splashColor设置为透明色
            highlightColor: Colors.transparent,
            splashColor: Colors.transparent,
            hoverColor: Colors.transparent,
            focusColor: Colors.transparent,
            mouseCursor: SystemMouseCursors.basic,
            onTap: () {
              controller.closePatientInfoDialog();
            },
            child: FContainer(
              child: FColumn(
                children: [
                  FisBuilder<ConsultationRecordViewController>(
                    id: 'record_data_table_header',
                    builder: (context) {
                      if (controller.isShowHistory) {
                        return HistoryModeHeader();
                      } else {
                        return ConsultationRecordFilter();
                      }
                    },
                  ),
                  FExpanded(
                    child: FContainer(
                      width: double.infinity,
                      child: RecordDataTable(),
                    ),
                  ),
                  OperationButtonsRow(),
                ],
              ),
            ),
          ),
        ),
        FContainer(
          width: 320,
          decoration: BoxDecoration(
            border: Border(
              left: BorderSide(
                color: Colors.grey[300]!,
                width: 1,
              ),
            ),
          ),
          child: FFutureBuilder(
            future: Future.delayed(Duration(
                milliseconds: Get.isRegistered<ImagereportinnerviewController>()
                    ? 800
                    : 0)),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                return ImageReportInnerView(
                  args: ImageReportListParams(
                    recordCode: controller.desktopListSelectedRecordCode,
                  ),
                );
              } else {
                return FContainer();
              }
            },
          ),
        )
      ],
    );
    return FFutureBuilder<bool>(
        future: _checkConsultationRecordViewController(),
        builder: (c, snapshot) {
          if (snapshot.connectionState == ConnectionState.done &&
              snapshot.hasData &&
              snapshot.data == true) {
            return FScaffold(
              key: controller.scaffoldKey,
              body: FisBuilder<ConsultationRecordViewController>(
                id: 'record_body',
                builder: (_) {
                  return FStack(
                    children: [
                      body,
                      if (controller.isDrawerOpen) ...[
                        FPositioned(
                          right: 0,
                          top: 0,
                          bottom: 0,
                          child: FContainer(
                            width: 800,
                            decoration: BoxDecoration(
                              boxShadow: [
                                BoxShadow(
                                  color: Colors.grey.withOpacity(0.5),
                                  spreadRadius: 2,
                                  blurRadius: 5,
                                  offset: Offset(0, 3), // 阴影偏移量
                                ),
                              ],
                            ),
                            child: _buildDrawerContent(),
                          ),
                        ),
                      ],
                    ],
                  );
                },
              ),
              drawerEdgeDragWidth: 500,
              drawerDragStartBehavior: DragStartBehavior.down,
              endDrawer: QuickFWidget(
                Drawer(
                  width: 500,
                  child: RecordFilterDrawer(),
                ),
              ),
            );
          } else {
            return FSizedBox();
          }
        });
  }

  @override
  FWidget build(BuildContext context) {
    if (kIsMobile) {
      return ConsultationRecordMobileView();
    }
    return _buildView();
  }

  FWidget _buildDrawerContent() {
    return ConsultationPage();
  }

  static Future<bool>? _checkConsultationRecordViewController() async {
    if (Get.isRegistered<ConsultationRecordViewController>()) {
      await Future.delayed(Duration(milliseconds: 200));
    }
    return true;
  }
}
