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:fis_jsonrpc/rpc.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/managers/interfaces/report.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/controller.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/widgets/mobile/record_patient_refer_mobile_history.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/widgets/mobile/record_patient_refer_mobile_widget.dart';
import 'package:get/get.dart';

/// 手机端转诊

class RecordPatientReferMobile extends FisView<ConsultationRecordViewController>
    implements FPage {
  final String patientName;
  final String? recordCode;
  final bool isShowApplay;
  final ReferralTypeEnum referralType;
  @override
  final String pageName;
  RecordPatientReferMobile({
    required this.patientName,
    required this.recordCode,
    required this.referralType,
    this.isShowApplay = true,
    this.pageName = 'RecordPatientReferMobile',
  }) {
    if (!isShowApplay) {
      controller.state.referTab = 3;
      controller
          .showReferralHistory(recordCode)
          .then((value) => controller.state.referTab = 1);
    }
  }

  @override
  FWidget build(BuildContext context) {
    return FScaffold(
      resizeToAvoidBottomInset: false,
      body: FSafeArea(
        child: FColumn(
          mainAxisSize: MainAxisSize.max,
          children: [
            _MobileLayoutTab(
              this,
              recordCode,
              isShowApplay: isShowApplay,
            ),
            FObx(() {
              if (controller.state.referTab == 0) {
                return FExpanded(
                  child: RecordReferralMobileApplication(
                    patientName,
                    recordCode,
                  ),
                );
              } else {
                return FExpanded(
                  child: _ReferralHistory(
                    recordCode: recordCode,
                    referralType: referralType,
                    // editRefer: editRefer,
                    // patientCode: patientInfo.patientCode,
                  ),
                );
              }
            })
          ],
        ),
      ),
    );
  }
}

class _MobileLayoutTab extends FisView<ConsultationRecordViewController>
    implements FWidget {
  _MobileLayoutTab(
    this.businessParent,
    this.recordCode, {
    this.isShowApplay = false,
  });
  final bool isShowApplay;
  bool get isRefer => controller.state.referTab == 1;
  final String? recordCode;

  ///父级节点
  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: () {
            controller.initReferStatus();
            changeTab(0);
          },
          child: FContainer(
            padding: EdgeInsets.all(10),
            child: FIcon(
              Icons.chevron_left_rounded,
            ),
          ),
        ),
        FExpanded(
          child: FRow(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              if (isShowApplay) ...[
                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: () async {
                  await controller.showReferralHistory(recordCode);
                  if (controller.referralHistoryDetailList.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<ConsultationRecordViewController>();
  final reportManager = Get.find<IReportManager>();
  final String? recordCode;
  final ReferralTypeEnum referralType;
  final ScrollController _scrollController = ScrollController();
  _ReferralHistory({
    this.recordCode,
    required this.referralType,
  });
  @override
  FWidget build(BuildContext context) {
    // return FObx(() {
    return FColumn(
      children: [
        FExpanded(
          child: FAlwaysScrollListView(
            scrollController: _scrollController,
            child: QuickFWidget(ScrollConfiguration(
              behavior:
                  ScrollConfiguration.of(context).copyWith(scrollbars: false),
              child: FListView(
                controller: _scrollController,
                children: controller.referralHistoryDetailList
                    .map(
                      (e) => RecordReferHistoryCard(
                        time: reportManager.getTime(e.referralTime.toString()),
                        referralOutOrganization: e.referralOutOrganizationName,
                        subjectMatter: e.subjectMatter,
                        creator: e.referralOutUserName,
                        creatorCode: e.referralOutUserCode,
                        referralInOrganization: e.referralInOrganizationName,
                        referralInUser: e.referralInUserName,
                        referralStatus: e.referralStatus,
                        referralRecordCode: e.code,
                        referralInOrganizationCode:
                            e.referralInOrganizationCode,
                        referralInUserCode: e.referralInUserCode,
                        recordCode: recordCode,
                      ),
                    )
                    .toList(),
              ),
            )),
          ),
        ),
        if (referralType == ReferralTypeEnum.ReferralOut) _buildButtomOperate(),
      ],
    );
    // });
  }

  FWidget _buildButtomOperate() {
    return FContainer(
      height: 80,
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(vertical: 15),
      child: FFlex(
        direction: Axis.horizontal,
        mainAxisSize: MainAxisSize.min,
        children: [
          _buildButtomOperateItem(
            onTap: () => controller.revokefer(recordCode),
            backgroundColor: FTheme.ins.colorScheme.primary,
            textColor: Colors.white,
            title: i18nBook.remedical.WithDrawn.t,
          ),
        ],
      ),
    );
  }

  FWidget _buildButtomOperateItem({
    required String title,
    required Function onTap,
    Color? backgroundColor,
    Color? textColor,
    BoxDecoration? decoration,
  }) {
    return FExpanded(
      child: FContainer(
        color: backgroundColor,
        decoration: decoration,
        child: FInkWell(
          onTap: () {
            onTap();
          },
          child: FCenter(
            child: FText(
              title,
              style: TextStyle(
                color: textColor,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
