import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_measure/utils/prompt_box.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
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/report/report_edit/widgets/add_report_tag.dart';
import 'package:get/get.dart';

import '../controller/real_time_consultation_controller.dart';

class RealtimeConsultationReport extends FisView<RealTimeConsultationController>
    implements FPage {
  @override
  final String pageName = "RealtimeConsultationReport";
  final reportManager = Get.find<IReportManager>();
  RealtimeConsultationReport({
    Key? key,
  }) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return FObx(() {
      var reportList = controller.state.reportList;
      if (reportList.isEmpty) {
        return _mockEmptyReportView();
      }
      return FColumn(
        children: [
          FExpanded(
            child: FListView.builder(
              shrinkWrap: true,
              itemCount: reportList.length,
              itemBuilder: (context, index) {
                final currReport = reportList[index];
                var patientName = currReport.patientName ?? '';
                final state = controller.state;
                if (patientName.isEmpty) {
                  patientName =
                      state.consultationsRecordInfos.patientName ?? '';
                }
                var recordCode = currReport.recordCode ?? '';
                var reportCode = currReport.reportCode ?? '';
                return BuildReportRecord(
                  patientName: patientName,
                  currReport: currReport,
                  onEditReport: () {
                    router.back();
                    reportManager.openReportEdit(
                      currReport.patientCode ?? '',
                      consultationCode: recordCode,
                      reportCode: currReport.reportCode ?? '',
                    );
                  },
                  onEditReportTag: () async {
                    List<String> selectedTags = currReport.reportLabels ?? [];
                    var allReportTags = await reportManager.getReportTagDatas(
                      recordCode,
                      ReportTypeEnum.LiveConsultation,
                    );

                    router.dialog(
                      () => AddReportTag(
                        allReportTags,
                        selectedTags,
                        (values) async {
                          final result = await reportManager.submitReportTags(
                              reportCode, values);
                          if (result) {
                            PromptBox.toast(i18nBook.remedical.submitSuccess.t);
                            controller.reportController.getReportInfoAsync();
                            router.back();
                          }
                        },
                      ),
                    );
                  },
                  onExamTap: null,
                  onImageTap: null,
                  canEditReport: controller.state.isEditReport,
                );
              },
            ),
          ),
        ],
      );
    });
  }

  ///缺省显示
  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),
        ],
      ),
    );
  }
}
