// 之前的实验功能
import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/widgets/tree/flutter_treeview/lib/flutter_treeview.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/pages/remedical/vocabulary_entry/widgets/entries_item.dart';
import 'package:flyinsono/views/desktop/laboratory/controller.dart';
import 'package:flyinsono/views/desktop/laboratory/widgets/main_content.dart';
import 'package:flyinsono/views/desktop/laboratory/widgets/upload_cases_dialog.dart';
import 'package:get/get.dart' hide Node;

class LaboratoryReportPage extends FisView<LaboratoryController>
    implements FPage {
  const LaboratoryReportPage({Key? key}) : super(key: key);
  @override
  String get pageName => 'LaboratoryReportPage';

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      alignment: Alignment.center,
      child: _buildRealTimeConsultationMain(),
    );
  }

  FWidget _buildRealTimeConsultationMain() {
    return FContainer(
      color: Colors.grey.shade200,
      child: FRow(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          FContainer(
            decoration: const BoxDecoration(
              color: Colors.white,
            ),
            width: 400,
            child: FColumn(
              children: [
                FContainer(
                  margin: const EdgeInsets.only(
                    left: 20,
                    right: 20,
                  ),
                  child: FRow(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      _buildUploadButton(),
                    ],
                  ),
                ),
                const FSizedBox(height: 7),
                const FPadding(
                  padding: EdgeInsets.symmetric(
                    horizontal: 20,
                  ),
                  child: FDivider(height: 1, color: Colors.grey),
                ),
                const FSizedBox(height: 7),
                FObx(
                  () => controller.state.caseAnalysis.length == 0
                      ? NoDataWidget()
                      : FExpanded(
                          child: _CaseAnalysis(),
                        ),
                )
              ],
            ),
          ),
          FExpanded(
            child: FContainer(
              child: const LaboratoryMainContent(),
            ),
          ),
        ],
      ),
    );
  }

  FWidget _buildUploadButton() {
    return FContainer(
      margin: EdgeInsets.only(
        top: 15,
      ),
      width: 150,
      height: 40,
      child: FElevatedButton(
        child: FObx(
          () => FText(
            controller.state.selectedKey.isEmpty
                ? i18nBook.laboratory.upload.t
                : i18nBook.laboratory.uploadToFolder.t,
          ),
        ),
        onPressed: () {
          router.dialog(
            () => UploadCasesDialog(),
            dismissible: false,
          );
        },
        businessParent: this,
        name: 'upload',
        style: ElevatedButton.styleFrom(
          elevation: 0,
        ),
      ),
    );
  }
}

class _CaseAnalysis extends FStatefulWidget {
  @override
  FState<_CaseAnalysis> createState() => _CaseAnalysisState();
}

class _CaseAnalysisState extends FState<_CaseAnalysis> {
  final controller = Get.find<LaboratoryController>();

  @override
  FWidget build(BuildContext context) {
    return FObx(() {
      return FContainer(
        margin: EdgeInsets.only(
          top: 15,
          left: 10,
          right: 10,
        ),
        child: FTreeView(
          theme: FTreeViewTheme(
            colorScheme: ColorScheme.dark(
              primary: FTheme.ins.colorScheme.primary,
            ),
            iconTheme: IconThemeData(size: 15),
          ),
          onNodeTap: (code) {
            controller.onNodeTap(code);
          },
          onExpansionChanged: (String code, bool isDrop) {
            controller.onExpansionChanged(
              code,
              isDrop,
            );
            setState(() {});
          },
          controller: TreeViewController(
            children: controller.state.caseAnalysis,
            selectedKey: controller.state.selectedKey,
          ),
          nodeBuilder: (buildContext, node) {
            return CaseAnalysisTreeView(node: node);
          },
        ),
      );
    });
  }
}

class CaseAnalysisTreeView extends FStatefulWidget implements FPage {
  CaseAnalysisTreeView({
    Key? key,
    required this.node,
    this.pageName = 'CaseAnalysisTreeView',
  }) : super(key: key);
  @override
  final String pageName;
  final Node node;
  @override
  FState<CaseAnalysisTreeView> createState() => _CaseAnalysisTreeViewState();
}

class _CaseAnalysisTreeViewState extends FState<CaseAnalysisTreeView> {
  bool hovering = false;
  final controller = Get.find<LaboratoryController>();
  @override
  FWidget build(BuildContext context) {
    var label = widget.node.label;
    return FMouseRegion(
      cursor: SystemMouseCursors.click,
      child: FGestureDetector(
        name: label,
        businessParent: widget,
        child: FRow(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          mainAxisSize: MainAxisSize.max,
          children: [
            FExpanded(
              child: FListTile(
                leading: FIcon(
                  Icons.folder,
                  color: Colors.orange, // 设置您希望的图标颜色
                ),
                title: _SelectText(
                  label: label,
                  isSelected: controller.state.selectedKey != widget.node.key,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

/// 选中样式
class _SelectText extends FStatelessWidget {
  _SelectText({
    required this.isSelected,
    required this.label,
  });
  final bool isSelected;
  final String label;
  @override
  FWidget build(BuildContext context) {
    return FContainer(
      alignment: Alignment.centerLeft,
      child: FText(
        label,
        textAlign: TextAlign.center,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          color: isSelected ? Colors.black : Colors.white,
        ),
      ),
    );
  }
}
