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/router.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_widget.dart';
import 'package:get/get.dart';

class ReferDoctorDiaLog extends FisView<ConsultationController> {
  const ReferDoctorDiaLog({
    Key? key,
    required this.patientInfo,
  }) : super(key: key);

  final PatientInfo patientInfo;

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        i18nBook.remedical.transfer.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      onClose: () {
        controller.inspectionRecordsController.cleanState();
        router.back();
        controller.state.referTab = 0;
      },
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      children: [
        FContainer(
          width: 700,
          child: FContainer(
            child: FColumn(
              children: [
                DesktopPatientRefer(
                  patientInfo: patientInfo,
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}

/// pc端病人转诊页面
// ignore: must_be_immutable
class DesktopPatientRefer extends FStatefulWidget implements FPage {
  DesktopPatientRefer({
    Key? key,
    required this.patientInfo,
    this.pageName = 'DesktopPatientRefer',
  }) : super(key: key);

  final PatientInfo patientInfo;
  @override
  final String pageName;

  @override
  FState<DesktopPatientRefer> createState() => _DesktopPatientReferState();
}

class _DesktopPatientReferState extends FState<DesktopPatientRefer> {
  final controller = Get.find<ConsultationController>();

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      height: 600,
      child: _buildScannedImageDialog(),
    );
  }

  FWidget _buildScannedImageDialog() {
    return FContainer(
      child: FRow(
        mainAxisSize: MainAxisSize.max,
        children: [
          FContainer(
            margin: EdgeInsets.only(top: 30, left: 15),
            width: 100,
            child: FColumn(
              children: [
                _buildTabList(
                  0,
                  i18nBook.remedical.referralRequest.t,
                ),
                _buildTabList(
                  1,
                  i18nBook.remedical.referralHistory.t,
                ),
              ],
            ),
          ),
          FContainer(
            width: 1,
            color: FTheme.ins.colorScheme.line,
          ),
          FExpanded(
            child: _Referral(
              widget.patientInfo,
              () {
                controller.state.referTab = 0;
                controller.state.isModifyReferralRecord = true;
              },
            ),
          ),
        ],
      ),
    );
  }

  FWidget _buildTabList(int tabIndex, String title) {
    return FObx(
      () {
        return FContainer(
          width: double.infinity,
          height: 50,
          decoration: BoxDecoration(
            color: controller.state.referTab == tabIndex
                ? FTheme.ins.colorScheme.primary
                : Colors.white,
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(4),
              bottomLeft: Radius.circular(4),
            ),
          ),
          child: FCenter(
            child: FTextButton(
              businessParent: this.widget,
              name: "cleanState",
              style: ButtonStyle(
                padding: MaterialStateProperty.all(
                  EdgeInsets.symmetric(vertical: 15, horizontal: 15),
                ),
              ),
              onPressed: () {
                controller.inspectionRecordsController.cleanState();
                controller.state.referTab = tabIndex;
              },
              child: FText(
                title,
                style: TextStyle(
                  color: controller.state.referTab == tabIndex
                      ? Colors.white
                      : Colors.black,
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}

class _Referral extends FStatelessWidget {
  final controller = Get.find<ConsultationController>();
  final PatientInfo patientInfo;
  final VoidCallback? editRefer;
  _Referral(
    this.patientInfo,
    this.editRefer,
  );
  @override
  FWidget build(BuildContext context) {
    return FObx(() {
      if (controller.state.referTab == 0)
        return ReferralApplication(patientInfo);
      else
        return FContainer(
          margin: EdgeInsets.symmetric(vertical: 10),
          child: _ReferralHistory(
            editRefer: editRefer,
            patientCode: patientInfo.patientCode,
          ),
        );
    });
  }
}

class _ReferralHistory extends FStatelessWidget {
  final controller = Get.find<ConsultationController>();
  final reportManager = Get.find<IReportManager>();
  final VoidCallback? editRefer;
  final String? patientCode;

  _ReferralHistory({
    this.editRefer,
    this.patientCode,
  });
  @override
  FWidget build(BuildContext context) {
    return FObx(() {
      return FListView(
        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(),
      );
    });
  }
}
