import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/managers/interfaces/report.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/report/build_record_view.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/controller.dart';
import 'package:flyinsono/pages/remedical/image_report_inner_view/controller.dart';
import 'package:flyinsono/pages/remedical/report/report_edit/widgets/add_report_tag.dart';
import 'package:flyinsono/pages/remedical/vocabulary_entry/widgets/entries_item.dart';
import 'package:get/get.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_jsonrpc/rpc.dart';

class FReportCardsView extends FStatefulWidget implements FPage {
  FReportCardsView();

  @override
  FState<FStatefulWidget> createState() {
    return FReportCardsViewState();
  }

  @override
  String get pageName => 'FReportCardsView';
}

class FReportCardsViewState extends FState<FReportCardsView> {
  final controller = Get.find<ImagereportinnerviewController>();

  final _reportManager = Get.find<IReportManager>();
  @override
  FWidget build(BuildContext context) {
    if (controller.reports.isNotEmpty) {
      return FListView(
        shrinkWrap: true,
        children: controller.reports
            .map(
              (e) => _buildReportCard(e),
            )
            .toList(),
      );
    }
    return _noDataWidget();
  }

  FWidget _noDataWidget() {
    return NoDataWidget();
  }

  FWidget _buildReportCard(ReportDTO report) {
    var patientName = report.patientName ?? '';
    if (patientName.isEmpty) {
      patientName = controller.recordInfo?.patientName ?? '';
    }
    return FMouseRegion(
      cursor: SystemMouseCursors.click,
      child: FGestureDetector(
        businessParent: widget,
        name: 'report preview',
        onDoubleTap: () {
          // FIXME 没用到
          // var reportUrls = report.reportPreviewList
          //     ?.where((element) => element.fileType == UploadFileTypeEnum.JPG)
          //     .map((e) => e.fileToken ?? '')
          //     .toList();
          _reportManager.openReportPreviewPage(
            controller.recordCode,
            controller.isReferral ? controller.recordCode : "",
            reportCode: report.reportCode ?? '',
          );
        },
        child: BuildReportRecord(
          currReport: report,
          patientName: patientName,
          canEditReport: report.canEditReport,
          onEditReport: () {
            _reportManager.openReportEdit(
              controller.patientCode,
              recordCode: controller.recordCode,
              reportCode: report.reportCode!,
              referralRecordCode:
                  controller.isReferral ? controller.recordCode : "",
            );
          },
          onEditReportTag: () async {
            var allReportTags =
                await _reportManager.getReportTagDatas(controller.recordCode);
            await router.dialog(
              () => AddReportTag(
                allReportTags,
                report.reportLabels ?? [],
                (tags) {
                  router.back();
                  _reportManager
                      .submitReportTags(report.reportCode!, tags)
                      .then((value) {
                    controller.loadReports();
                  });
                },
              ),
            );
          },
          isSmailCard: true,
        ),
      ),
    );
  }
}
