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:fis_ui/values/font_sizes.dart';
import 'package:fis_ui/values/spacings.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/utils/string_extension.dart';
import 'package:flyinsono/managers/interfaces/entities/patient_Info.dart';
import 'package:flyinsono/pages/remedical/consultation/controller.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/age_text.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/fcustom_flat_button.dart';
import 'package:get/get.dart';

class PatientCardMobileLayout extends FStatefulWidget implements FPage {
  ///病人信息索引
  final int index;

  ///病人信息词典
  final PatientInfo? patientInfo;

  @override
  final String pageName;

  PatientCardMobileLayout(this.index, this.patientInfo,
      {this.pageName = 'PatientCardMobileLayout', Key? key});

  @override
  FState<FStatefulWidget> createState() {
    return _PatientCardMobileState();
  }
}

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

  bool get isShowReferral => widget.patientInfo!.isValid ?? true;
  //卡片高度
  static const double _cardHeight = 183;

  ///按钮间分割线高度
  static const double _buttonSplitLineHeight = 25;

  ///字体大小
  static const double _fontsize = 14;
  @override
  FWidget build(BuildContext context) {
    final name = widget.patientInfo!.name ?? '';
    String avatar = 'Head';
    if (name.length > 0)
      avatar = widget.patientInfo!.name.toString().substring(0, 1);
    return FContainer(
      height: _cardHeight,
      decoration: BoxDecoration(
        boxShadow: [
          BoxShadow(
            color: Colors.grey[300]!,
            offset: Offset(2.0, 2.0), //阴影xy轴偏移量
            blurRadius: 5.0, //阴影模糊程度
            spreadRadius: 2.0, //阴影扩散程度
          )
        ],
        borderRadius: BorderRadius.all(
          Radius.circular(8),
        ),
        color: Colors.white,
      ),
      margin:
          EdgeInsets.symmetric(vertical: FSpacings.M, horizontal: FSpacings.XL),
      child: FColumn(
        mainAxisSize: MainAxisSize.max,
        children: [
          FSizedBox(height: 10),
          FRow(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildHeadImage(avatar),
              FExpanded(
                child: FContainer(
                  margin: EdgeInsets.only(top: 10),
                  child: FColumn(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      _buildNameAndTag(avatar),
                      _buildPhoneAndIcon(),
                      if (widget.patientInfo!.identityCard != null &&
                          widget.patientInfo!.identityCard!.isNotEmpty) ...[
                        _buildMobileId(),
                      ],
                      if (widget.patientInfo!.birthday != null &&
                          widget.patientInfo!.birthday
                              .toString()
                              .isNotEmpty) ...[
                        _buildAge(),
                      ],
                      _buildHospitalBelonging(),
                    ],
                  ),
                ),
              ),
            ],
          ),
          if (widget.patientInfo!.isReferral!)
            FRow(mainAxisAlignment: MainAxisAlignment.start, children: [
              FContainer(
                margin: EdgeInsets.only(left: 10, bottom: 5),
                padding: EdgeInsets.all(5),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(
                    4,
                  ),
                  color: Colors.red,
                ),
                child: FText(
                  i18nBook.remedical.transferredOut.t,
                  style: TextStyle(
                    color: Colors.white,
                  ),
                ),
              ),
            ])
          else
            const FSizedBox(height: 5),
          FExpanded(
            child: FColumn(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                FDivider(
                  height: 1,
                  thickness: 1,
                  color: Colors.grey[200],
                ),
                FContainer(
                  constraints: BoxConstraints(maxHeight: 39, minHeight: 30),
                  child: FRow(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      FExpanded(
                        child: _buildOperateButtons(),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  ///手机端身份证
  FWidget _buildMobileId() {
    var id = widget.patientInfo!.identityCard ?? '';
    return FRow(
      children: [
        FIcon(
          Icons.perm_identity_outlined,
          size: 17,
          color: FTheme.ins.colorScheme.primary,
        ),
        FText(
          id,
          style: TextStyle(fontSize: _fontsize),
        ),
        FExpanded(child: FSizedBox()),
      ],
    );
  }

  ///头像
  FWidget _buildHeadImage(String avatar) {
    return FContainer(
      margin: EdgeInsets.only(right: 15, left: 10, bottom: 5, top: 10),
      child: FCenter(
        child: (widget.patientInfo!.headImgUrl == null ||
                widget.patientInfo!.headImgUrl == '')
            ? FContainer(
                width: 80,
                height: 80,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(
                    Radius.circular(8),
                  ),
                  color: FTheme.ins.colorScheme.primary,
                ),
                child: FText(
                  avatar,
                  style: TextStyle(
                    fontSize: 22,
                    color: Colors.white,
                  ),
                ),
              ) as FWidget
            : FCard(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadiusDirectional.circular(8)),
                clipBehavior: Clip.antiAlias,
                child: FImage.network(
                  widget.patientInfo!.headImgUrl!,
                  fit: BoxFit.fitWidth,
                  width: 80,
                  height: 80,
                ),
              ),
      ),
    );
  }

  ///标签
  FWidget _buildTag(String avatar) {
    return FContainer(
      width: 90,
      height: 30,
      margin: EdgeInsets.only(left: 0, right: 10),
      child: isShowReferral
          ? FText(
              i18nBook.remedical.registered.t,
              textAlign: TextAlign.right,
              style: TextStyle(
                fontSize: FFontSizes.H5,
                fontWeight: FontWeight.bold,
                color: Color.fromRGBO(124, 182, 90, 1),
                fontFamily: FTheme.ins.localeSetting.fontFamily,
              ),
            )
          : FText(
              i18nBook.remedical.notRegistered.t,
              textAlign: TextAlign.right,
              style: TextStyle(
                fontSize: FFontSizes.H5,
                fontWeight: FontWeight.bold,
                color: Colors.red,
                fontFamily: FTheme.ins.localeSetting.fontFamily,
              ),
            ),
    );
  }

  ///手机和图标
  FWidget _buildPhoneAndIcon() {
    return FRow(
      children: [
        FIcon(
          Icons.phone_android_outlined,
          size: 15,
          color: FTheme.ins.colorScheme.primary,
        ),
        FText(
          widget.patientInfo!.phone ?? '',
          style: TextStyle(fontSize: _fontsize, height: null),
        ),
        FExpanded(child: FSizedBox()),
        FSizedBox(width: 15),
      ],
    );
  }

  ///年龄
  FWidget _buildAge() {
    return FRow(
      children: [
        FIcon(
          Icons.cake_outlined,
          size: 17,
          color: FTheme.ins.colorScheme.primary,
        ),
        AgeText(
          ageUnits: widget.patientInfo?.ageUnits,
          ageDay: widget.patientInfo?.ageDay,
          ageMonth: widget.patientInfo?.ageMonth,
          ageWeek: widget.patientInfo?.ageWeek,
          ageYear: widget.patientInfo?.ageYear,
          style: TextStyle(fontSize: _fontsize, height: null),
        ),
        FExpanded(child: FSizedBox()),
      ],
    );
  }

  ///年龄
  FWidget _buildHospitalBelonging() {
    return FRow(
      children: [
        FIcon(
          Icons.business,
          size: 17,
          color: FTheme.ins.colorScheme.primary,
        ),
        FText(widget.patientInfo?.organizationName ?? ''),
        FExpanded(child: FSizedBox()),
      ],
    );
  }

  ///姓名和标签
  FWidget _buildNameAndTag(String avatar) {
    return FContainer(
      margin: EdgeInsets.only(bottom: 6),
      child: FRow(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          FExpanded(
            child: FText(
              (widget.patientInfo!.name ?? '').fixAutoLines(),
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 18,
              ),
            ),
          ),
          _buildTag(avatar),
        ],
      ),
    );
  }

  ///按钮间分割线
  FWidget _getButtonSplitLine() {
    return FContainer(
      height: _buttonSplitLineHeight,
      child: FVerticalDivider(
        width: 1,
        thickness: 1,
        color: FTheme.ins.colorScheme.line,
      ),
    );
  }

  FWidget _buildOperateButtons() {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        if (isShowReferral)
          FCustomFlatButton(
            i18nBook.remedical.transfer.t,
            fontColor: FTheme.ins.colorScheme.primary,
            () {
              controller.openReferDoctorDiaLog(widget.patientInfo!);
            },
            businessParent: this.widget,
          ),
        if (isShowReferral) _getButtonSplitLine(),
        FCustomFlatButton(
          i18nBook.common.report.t,
          fontColor: FTheme.ins.colorScheme.primary,
          () async {
            controller.clickPatientCard(
              widget.index,
              widget.patientInfo!,
            );
            await controller.openReportsView();
          },
          businessParent: this.widget,
        ),
        _getButtonSplitLine(),
        FCustomFlatButton(
          i18nBook.remedical.check.t,
          fontColor: FTheme.ins.colorScheme.primary,
          () async {
            await controller.clickPatientCard(
              widget.index,
              widget.patientInfo!,
            );
            controller.inspectionRecordsController.openCreatecheckPage(
              widget.patientInfo!.patientCode,
            );
          },
          businessParent: this.widget,
        ),
        _getButtonSplitLine(),
        FCustomFlatButton(
          i18nBook.remedical.image.t,
          fontColor: FTheme.ins.colorScheme.primary,
          () async {
            await controller.clickPatientCard(
              widget.index,
              widget.patientInfo!,
            );
            // await controller.inspectionRecordsController.openCloudImagePage();
          },
          businessParent: this.widget,
        ),
      ],
    );
  }
}
