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/pages/remedical/consultation/controller.dart';

import 'build_record_view.dart';

/// 报告列表
class ReportList extends FisView<ConsultationController> implements FWidget {
  ReportList({Key? key}) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    controller.findPatientReportsAsync();
    return context.fResponsiveVoid(
      mobile: () => _MobileLayout(),
      desktop: () => _DesktopLayout(),
    );
  }
}

class _DesktopLayout extends FisView<ConsultationController>
    implements FWidget {
  @override
  FWidget build(BuildContext context) {
    return FObx(
      () {
        List<ReportDTO> reportList = controller.state.reportList;
        controller.state.accessingReportCode = "";
        return FContainer(
          color: Colors.white,
          child: FColumn(
            children: [
              reportList.length == 0
                  ? _mockEmptyReportView()
                  : _buildReportsContentView(reportList)
            ],
          ),
        );
      },
    );
  }

  ///缺省显示
  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) {
    return FExpanded(
      child: FListView.builder(
        shrinkWrap: true,
        itemCount: reportList.length,
        controller: controller.reportController,
        itemBuilder: (context, index) {
          final currReport = reportList[index];
          var patientName = currReport.patientName ?? '';
          final state = controller.state;
          if (patientName.isEmpty) {
            patientName = state.patientInfo.name ?? '';
          }

          return BuildReportRecord(
            patientName: patientName,
            currReport: currReport,
            canEditReport: currReport.canEditReport,
            onEditReport: () {
              controller.inspectionReportController.openReportEdit(
                currReport.recordCode ?? '',
                currReport.patientCode ?? '',
                reportCode: currReport.reportCode ?? '',
                patientTab: controller.state.patientTab,
                referralRecordCode: currReport.referralRecordCode ?? '',
              );
            },
            onEditReportTag: () {
              controller.state.recordCode = currReport.recordCode ?? '';
              controller.state.reportCode = currReport.reportCode ?? '';
              controller.state.selectedTags = currReport.reportLabels ?? [];
              controller.openAddTagDialog();
            },
            onExamTap: () {
              controller.inspectionRecordsController
                  .openCheckDialog(currReport.recordCode);
            },
            onImageTap: () {
              controller.inspectionRecordsController
                  .getCheckScannedImage(currReport.recordCode ?? '');
            },
          );
        },
      ),
    );
  }
}

class _MobileLayout extends FisView<ConsultationController> implements FWidget {
  @override
  FWidget build(BuildContext context) {
    throw UnimplementedError();
  }
}
