import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/services/index.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/pages/consultation/controller.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/fcustom_flat_button.dart';
import 'package:flyinsono/pages/remedical/report/report_edit/index.dart';
import 'package:flyinsono/views/desktop/index.dart';
import 'package:get/get.dart';
import 'package:fis_common/extensions/string.dart';

class ConsultationCard extends StatelessWidget implements FPage {
  @override
  String get pageName => 'ConsultationCard';

  final controller = Get.find<MobileConsultationController>();
  final ConsultationsRecordCard recordInfo;
  //卡片高度
  static const double _cardHeight = 195;

  /// 操作按钮高度
  static const double _operateButtonHeight = 50;

  /// 内容高度
  static const double _contentHeight = _cardHeight - _operateButtonHeight;

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

  //标题与内容间隔
  final double _sizeBetween = 10;

  final int index;

  ///姓名文字样式
  final TextStyle _nameTextStyle = TextStyle(
    fontSize: FFontSizes.H3,
    color: Colors.black87,
    overflow: TextOverflow.ellipsis,
  );
  ConsultationCard(this.recordInfo, this.index);

  @override
  Widget build(BuildContext context) {
    return FContainer(
      height: _cardHeight,
      margin:
          EdgeInsets.symmetric(vertical: FSpacings.M, horizontal: FSpacings.XL),
      decoration: _getBoxDecoration(),
      child: FColumn(
        children: [
          FGestureDetector(
            name: "consultation_card",
            businessParent: this,
            onTap: () {
              controller.state.currentIndex = index;
              controller.openConsultationDetail(recordInfo.consultationCode);
            },
            behavior: HitTestBehavior.opaque,
            child: FContainer(
              height: _contentHeight,
              padding: EdgeInsets.all(FSpacings.M),
              child: FColumn(
                children: [
                  _buildPatientNameAndStatus(), //病人名和申请状态
                  const FSizedBox(height: FSpacings.M),
                  _buildAgeInfo(recordInfo.age), //年龄
                  const FSizedBox(height: FSpacings.M),
                  _buildGenderInfo(recordInfo.sex), //性别
                  const FSizedBox(height: FSpacings.M),
                  _buildApplayOrg(
                      recordInfo.applyOrganizationName, context), //申请机构
                ],
              ),
            ),
          ),
          _getHorizontalSplitLine(),
          FExpanded(child: _buildOperateButtons()), //操作按钮
        ],
      ),
    );
  }

  ///病人名和状态
  FWidget _buildPatientNameAndStatus() {
    var state = recordInfo.state;
    var stateStr = controller.getConsultationState(state);
    return FRow(
      children: [
        const FSizedBox(width: FSpacings.M),
        FContainer(
          constraints: BoxConstraints(
            maxWidth: MediaQuery.of(Get.context!).size.width / 2 - 32, // 设置最大宽度
          ),
          child: FText(
            recordInfo.patientName.isNullOrEmpty
                ? i18nBook.remedical.notFill.t
                : recordInfo.patientName!,
            style: _nameTextStyle,
          ),
        ),
        const FExpanded(child: FSizedBox()),
        FText(
          stateStr,
          style: _getTextStyleByState(state),
        ),
        const FSizedBox(width: FSpacings.S),
      ],
    );
  }

  //年龄
  FWidget _buildAgeInfo(String? patientAge) {
    var age = patientAge ?? '';
    return FRow(
      children: [
        const FSizedBox(width: FSpacings.M),
        FTitleField(
          i18nBook.remedical.age.t,
          FText(
            age.isEmpty ? i18nBook.remedical.notFill.t : age,
            style: TextStyle(color: Colors.black87),
          ),
          sizeBetween: _sizeBetween,
          minTitleWidth: 135,
          isBold: false,
        ),
      ],
    );
  }

  ///性别
  FWidget _buildGenderInfo(String? sex) {
    var gender = sex ?? '';
    return FRow(children: [
      const FSizedBox(width: FSpacings.M),
      FTitleField(
        i18nBook.remedical.gender.t,
        sizeBetween: _sizeBetween,
        minTitleWidth: 135,
        FText(
          gender.isEmpty ? i18nBook.remedical.notFill.t : gender,
        ),
        isBold: false,
      ),
    ]);
  }

  ///边框装饰器
  BoxDecoration _getBoxDecoration() {
    return 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,
    );
  }

  ///获取状态文本显示样式
  TextStyle _getTextStyleByState(TransactionStatusEnum? state) {
    var color = Color.fromRGBO(124, 182, 90, 1);
    if (state != null &&
        [TransactionStatusEnum.Rejected, TransactionStatusEnum.Withdrawn]
            .contains(state)) {
      color = Color.fromRGBO(242, 75, 71, 1);
    } else if ([TransactionStatusEnum.PendingReport].contains(state)) {
      color = Color.fromRGBO(240, 141, 46, 1);
    }
    return TextStyle(
      color: color,
      fontSize: FFontSizes.H5,
      fontWeight: FontWeight.bold,
    );
  }

  ///水平分割线
  FWidget _getHorizontalSplitLine() {
    return FContainer(
      margin: EdgeInsets.symmetric(horizontal: FSpacings.XL),
      child: FDivider(
        height: 1,
        thickness: 1,
        color: Colors.grey[300],
      ),
    );
  }

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

  ///一排操作按钮
  FWidget _buildOperateButtons() {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        FCustomFlatButton(
          i18nBook.common.image.t,
          () {
            controller.state.currentIndex = index;
            controller.onTapConsultationImages();
          },
          businessParent: this,
          fontColor: FTheme.ins.colorScheme.primary,
        ),
        _getButtonSplitLine(),
        FCustomFlatButton(
          i18nBook.common.report.t,
          () {
            controller.state.currentIndex = index;
            controller.onTabReport();
          },
          businessParent: this,
          fontColor: FTheme.ins.colorScheme.primary,
        ),
        _getButtonSplitLine(),
        if (recordInfo.state == TransactionStatusEnum.End) ...[
          FCustomFlatButton(
            i18nBook.realTimeConsultation.followUp.t,
            () {
              controller.onTapFollowUp(index);
            },
            businessParent: this,
            fontColor: FTheme.ins.colorScheme.primary,
          ),
          _getButtonSplitLine(),
        ],
        FCustomFlatButton(
          i18nBook.realTimeConsultation.appraise.t,
          () async {
            await controller.onTapEvaluation(index);
          },
          businessParent: this,
          fontColor: FTheme.ins.colorScheme.primary,
        ),
      ],
    );
  }

  ///申请机构
  FWidget _buildApplayOrg(String? applyOrganizationName, BuildContext context) {
    var orgName = applyOrganizationName ?? '';
    return FRow(
      children: [
        const FSizedBox(width: FSpacings.M),
        FTitleField(
          i18nBook.realTimeConsultation.applicantMechanism.t,
          sizeBetween: _sizeBetween,
          minTitleWidth: 135,
          maxLargeFieldWidth: MediaQuery.of(context).size.width - 215,
          FText(
            orgName.isEmpty ? i18nBook.remedical.notFill.t : orgName,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
          isBold: false,
        ),
      ],
    );
  }
}
