import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/managers/interfaces/entities/patient_Info.dart';
import 'package:flyinsono/pages/remedical/consultation/controller.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/report/build_record_view.dart';

/// 关联报告对话框界面
class InspectionReportDiaLog extends FisView<ConsultationController> {
  /// 检查记录编号
  final String recordCode;

  /// 报告列表
  final List<ReportDTO>? reportLists;

  ///病人信息
  final PatientInfo? patientInfo;
  InspectionReportDiaLog({
    Key? key,
    required this.recordCode,
    this.reportLists,
    this.patientInfo,
  }) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        i18nBook.remedical.reportList.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      children: [
        FContainer(
          height: 500,
          child: FColumn(
            children: [
              FExpanded(
                child: FContainer(
                  padding: EdgeInsets.only(bottom: 20),
                  child: _InspectionReport(reportLists),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}

class _InspectionReport extends FisView<ConsultationController>
    implements FWidget {
  /// 报告列表
  final List<ReportDTO>? reportLists;
  _InspectionReport(this.reportLists);
  @override
  FWidget build(BuildContext context) {
    return FContainer(
      height: 400,
      width: 700,
      color: Colors.white,
      child: FColumn(
        children: [
          reportLists!.length == 0
              ? _mockEmptyReportView()
              : _buildReportsContentView(reportLists!, context),
        ],
      ),
    );
  }

  ///缺省显示
  FWidget _mockEmptyReportView() {
    return FContainer(
      margin: EdgeInsets.only(top: 80),
      child: FColumn(
        children: [
          FCenter(
            child: FImage.asset(
              "assets/images/no_data.png",
              width: 300,
              height: 300,
              fit: BoxFit.cover,
            ),
          ),
          FText(i18nBook.common.noneDataTip.t),
        ],
      ),
    );
  }

  ///报告列表内容视图显示
  FWidget _buildReportsContentView(
      List<ReportDTO> reportList, BuildContext context) {
    return FExpanded(
      child: FAlwaysScrollListView(
        scrollController: controller.reportController,
        child: QuickFWidget(ScrollConfiguration(
          behavior: ScrollConfiguration.of(context).copyWith(scrollbars: false),
          child: FListView.builder(
            shrinkWrap: true,
            itemCount: reportList.length,
            controller: controller.reportController,
            itemBuilder: (context, index) {
              final report = reportList[index];
              var patientName = report.patientName ?? '';
              final state = controller.state;
              if (patientName.isEmpty) {
                patientName = state.patientInfo.name ?? '';
              }
              String reportUserName = report.reportUserName ?? '';
              if (reportUserName.isEmpty) {
                final labels = report.diagnosisLabels ?? [];
                reportUserName = labels.isNotEmpty
                    ? report.diagnosisLabels![0]
                    : i18nBook.homePage.notYet.t;
              }
              return BuildReportRecord(
                currReport: report,
                patientName: patientName,
                isInspectionReport: true,
                canEditReport: report.canEditReport,
                onEditReport: () {
                  controller.inspectionReportController.openReportEdit(
                    report.recordCode ?? '',
                    report.patientCode ?? '',
                    reportCode: report.reportCode ?? '',
                    patientTab: controller.state.patientTab,
                    referralRecordCode: report.referralRecordCode ?? '',
                  );
                },
                onEditReportTag: () {
                  controller.state.recordCode = report.recordCode ?? '';
                  controller.state.reportCode = report.reportCode ?? '';
                  controller.state.selectedTags = report.reportLabels ?? [];
                  controller.openAddTagDialog();
                },
                onImageTap: () {
                  controller.inspectionRecordsController
                      .getCheckScannedImage(report.recordCode ?? '');
                },
              );
            },
          ),
        )),
      ),
    );
  }
}
