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:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/managers/interfaces/entities/patient_Info.dart';
import 'package:flyinsono/managers/interfaces/report.dart';
import 'package:flyinsono/pages/remedical/consultation/controller.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/patient_refer_history.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/patient_refer_mobile_widget.dart';
import 'package:get/get.dart';

/// 手机端转诊

class PatientReferMobile extends FisView<ConsultationController>
    implements FPage {
  final PatientInfo patientInfo;
  final VoidCallback? editRefer;
  @override
  final String pageName;
  PatientReferMobile({
    required this.patientInfo,
    this.editRefer,
    this.pageName = 'PatientReferMobile',
  });
  @override
  FWidget build(BuildContext context) {
    return FScaffold(
      resizeToAvoidBottomInset: false,
      body: FSafeArea(
        child: FColumn(
          mainAxisSize: MainAxisSize.max,
          children: [
            _MobileLayoutTab(this),
            FObx(() {
              if (controller.state.referTab == 0) {
                return FExpanded(
                  child: ReferralMobileApplication(
                    patientInfo,
                  ),
                );
              } else {
                return FExpanded(
                  child: _ReferralHistory(
                    editRefer: editRefer,
                    patientCode: patientInfo.patientCode,
                  ),
                );
              }
            })
          ],
        ),
      ),
    );
  }
}

class _MobileLayoutTab extends FisView<ConsultationController>
    implements FWidget {
  _MobileLayoutTab(this.businessParent);
  bool get isRefer => controller.state.referTab == 1;

  ///父级节点
  final FPage businessParent;
  void changeTab(int index) {
    controller.inspectionRecordsController.cleanState();
    controller.state.referTab = index;
  }

  @override
  FWidget build(BuildContext context) {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        FInkWell(
          onTap: () {
            router.back();
            changeTab(0);
          },
          child: FContainer(
            padding: EdgeInsets.all(10),
            child: FIcon(
              Icons.chevron_left_rounded,
            ),
          ),
        ),
        FExpanded(
          child: FRow(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              FTextButton(
                name: "referralRequest",
                businessParent: this.businessParent,
                onPressed: () {
                  changeTab(0);
                },
                child: FObx(
                  () => FText(
                    i18nBook.remedical.referralRequest.t,
                    style: TextStyle(
                      fontSize: 16,
                      color: isRefer
                          ? FTheme.ins.colorScheme.text
                          : FTheme.ins.colorScheme.primary,
                    ),
                  ),
                ),
              ),
              FTextButton(
                name: "referralHistory",
                onPressed: () {
                  if (controller.state.referralRecordDetail.length == 0) {
                    PromptBox.toast(
                      i18nBook.homePage.notYet.t +
                          i18nBook.remedical.referralHistory.t,
                    );
                    return;
                  }
                  changeTab(1);
                },
                businessParent: this.businessParent,
                child: FObx(
                  () => FText(
                    i18nBook.remedical.referralHistory.t,
                    style: TextStyle(
                      fontSize: 16,
                      color: isRefer
                          ? FTheme.ins.colorScheme.primary
                          : FTheme.ins.colorScheme.text,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
        FContainer(
          width: 35,
        ),
      ],
    );
  }
}

class _ReferralHistory extends FStatelessWidget {
  final controller = Get.find<ConsultationController>();
  final reportManager = Get.find<IReportManager>();
  final VoidCallback? editRefer;
  final String? patientCode;
  final ScrollController _scrollController = ScrollController();
  _ReferralHistory({
    this.editRefer,
    this.patientCode,
  });
  @override
  FWidget build(BuildContext context) {
    return FObx(() {
      return FAlwaysScrollListView(
        scrollController: _scrollController,
        child: QuickFWidget(ScrollConfiguration(
          behavior: ScrollConfiguration.of(context).copyWith(scrollbars: false),
          child: FListView(
            controller: _scrollController,
            children: controller.state.referralRecordDetail
                .map(
                  (e) => ReferHistoryCard(
                    time: reportManager.getTime(e.createTime.toString()),
                    referralOutOrganization: e.referralOutOrganizationName,
                    subjectMatter: e.subjectMatter,
                    creator: e.creatorName,
                    creatorCode: e.creator,
                    referralInOrganization: e.referralInOrganizationName,
                    referralInUser: e.referralInUserName,
                    referralStatus: e.referralStatus,
                    referralRecordCode: e.code,
                    editRefer: editRefer,
                    referralInOrganizationCode: e.referralInOrganizationCode,
                    referralInUserCode: e.referralInUserCode,
                    patientCode: patientCode,
                    rejectReason: e.rejectReason,
                  ),
                )
                .toList(),
          ),
        )),
      );
    });
  }
}
