import 'package:fis_common/index.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.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/states/states.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/status_label.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/controller.dart';

/// 转诊历史卡片
class RecordReferHistoryCard extends FisView<ConsultationRecordViewController>
    implements FPage {
  /// 检查记录时间
  final String? time;

  /// 转出机构
  final String? referralOutOrganization;

  /// 转诊事由
  final String? subjectMatter;

  /// 转出申请人
  final String? creator;

  /// 转出申请人Code
  final String? creatorCode;

  /// 转入机构
  final String? referralInOrganization;

  /// 转入机构code
  final String? referralInOrganizationCode;

  /// 转入医生
  final String? referralInUser;

  /// 转入医生code
  final String? referralInUserCode;

  /// 当前状态
  final RecordReferralStatusEnum? referralStatus;

  /// 当前转诊code
  final String? referralRecordCode;

  /// 病人code
  final String? patientCode;

  /// 拒绝原因
  final String? rejectReason;

  /// 修改转诊tab
  final VoidCallback? editRefer;

  final String? recordCode;

  @override
  final String pageName;

  /// 外边距
  final EdgeInsets margin = const EdgeInsets.only(
    right: 20,
    left: 20,
    top: 10,
    bottom: 10,
  );

  /// 内边距
  final EdgeInsets padding = const EdgeInsets.only(
    top: 20,
    left: 15,
  );

  /// 阴影
  final BoxShadow boxShadow = BoxShadow(
    color: FColorHelper.mixColor(
      Colors.black12,
      Colors.black38,
      70,
    ),
    offset: Offset(3.0, 3.0), //阴影y轴偏移量
    blurRadius: 6, //阴影模糊程度
    spreadRadius: 0, //阴影扩散程度
  );

  RecordReferHistoryCard({
    Key? key,
    this.pageName = 'RecordReferHistoryCard',
    this.time,
    this.referralOutOrganization,
    this.subjectMatter,
    this.creator,
    this.referralInOrganization,
    this.referralInUser,
    this.referralStatus,
    this.referralRecordCode,
    this.editRefer,
    this.referralInOrganizationCode,
    this.referralInUserCode,
    this.patientCode,
    this.rejectReason,
    this.creatorCode,
    this.recordCode,
  }) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    final child = FAnimatedContainer(
      duration: const Duration(milliseconds: 200),
      margin: margin,
      padding: padding,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: const BorderRadius.all(
          Radius.circular(8),
        ),
        boxShadow: [
          boxShadow,
        ],
      ),
      child: FColumn(
        mainAxisSize: MainAxisSize.min,
        children: [
          FContainer(
            child: _buildInspectionImageInfo(),
          ),
          // referralStatus != ReferralStatusEnum.Accepted
          //     ? FContainer(
          //         padding: EdgeInsets.all(10),
          //         margin: EdgeInsets.only(right: 15),
          //         decoration: BoxDecoration(
          //           border: Border(
          //             top: BorderSide(
          //               color: Color.fromRGBO(216, 216, 216, 1),
          //               width: 0.5,
          //             ),
          //           ),
          //         ),
          //         child: _buildActionBar(),
          //       )
          //     : FContainer(),
        ],
      ),
    );

    return child;
  }

  /// 文本
  FWidget _buildRowTitle(String title, dynamic value, [Color? textColor]) {
    return FRow(
      children: [
        FContainer(
          width: 200,
          height: 30,
          child: FAlign(
            alignment: Alignment.centerLeft,
            child: FText(
              title,
              style: TextStyle(
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        ),
        FExpanded(
          child: (value is String)
              ? FContainer(
                  child: FText(
                    value,
                    style: TextStyle(
                      color: textColor ?? Colors.black,
                    ),
                  ),
                )
              : value,
        ),
      ],
    );
  }

  /// 文本组
  FWidget _buildRowTitleList() {
    return FExpanded(
      child: FContainer(
        padding: EdgeInsets.only(
          left: 20,
          bottom: 10,
        ),
        child: FColumn(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildRowTitle(
              i18nBook.remedical.transferTime.t,
              time ?? '',
            ),
            _buildRowTitle(
              i18nBook.remedical.transferOutAgency.t,
              referralOutOrganization ?? '',
            ),
            _buildRowTitle(
              i18nBook.remedical.transferSubjectMatter.t,
              subjectMatter ?? '',
            ),
            _buildRowTitle(
              i18nBook.remedical.transferApply.t,
              creator ?? '',
            ),
            _buildRowTitle(
              i18nBook.remedical.transferOrganization.t,
              referralInOrganization ?? '',
            ),
            _buildRowTitle(
              i18nBook.remedical.transferDoctor.t,
              referralInUser ?? '',
            ),
            _buildRowTitle(
              i18nBook.common.status.t,
              statusConvert(referralStatus),
            ),
            if ((rejectReason != null) &&
                referralStatus == ReferralStatusEnum.Rejected)
              _buildRowTitle(
                i18nBook.user.refuseReason.t,
                rejectReason,
                Colors.red,
              )
          ],
        ),
      ),
    );
  }

  String statusConvert(RecordReferralStatusEnum? referralStatus) {
    if (referralStatus == null) {
      return "";
    }
    switch (referralStatus) {
      case RecordReferralStatusEnum.ReferralIn:
        if (referralInUserCode == Store.user.code) {
          return i18nBook.remedical.transferred.t;
        } else {
          return i18nBook.remedical.transferredOut.t;
        }
      case RecordReferralStatusEnum.Processed:
        return i18nBook.remedical.processed.t;
      case RecordReferralStatusEnum.Withdrawn:
        return i18nBook.remedical.hasWithDrawn.t;
    }
  }

  /// 状态tip
  FWidget _buildStateTip() {
    return StatusLabel(
      title: i18nBook.remedical.pending.t,
      color: _buildReportStatusColor(),
    );
  }

  Color? _buildReportStatusColor() {
    return Color.fromRGBO(
      240,
      141,
      46,
      1,
    );
    // if (status == ReferralStatusEnum.Accepted)
    //   return Color.fromRGBO(124, 182, 90, 1);
    // else {
    //   return Color.fromRGBO(
    //     240,
    //     141,
    //     46,
    //     1,
    //   );
    // }
  }

  FWidget _buildTextButton(String text, Function onPressed,
      [Color? textColors]) {
    return FTextButton(
      name: "$text",
      businessParent: this,
      onPressed: () => onPressed.call(),
      style: ButtonStyle(
        side: MaterialStateProperty.all(
          BorderSide(
            color: Colors.grey,
            width: 0.5,
          ),
        ),
      ),
      child: FText(
        text,
        style: TextStyle(
          fontSize: 14,
          color: textColors ?? FTheme.ins.colorScheme.primary,
        ),
      ),
    );
  }

  /// 操作栏
  FWidget _buildActionBar() {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        if (creatorCode == Store.user.code)
          _buildTextButton(
            i18nBook.remedical.WithDrawn.t,
            () {
              controller.revokefer(recordCode);
            },
          )
      ],
    );
  }

  FWidget _buildInspectionImageInfo() {
    return FColumn(
      children: [
        // FRow(
        //   mainAxisAlignment: MainAxisAlignment.end,
        //   children: [
        //     FContainer(
        //       alignment: Alignment.centerRight,

        //       /// TODO:[Gavin] 多语言-UI-整合
        //       width: i18nBook.isCurrentChinese ? 120 : 200,
        //       child: _buildStateTip(),
        //     ),
        //   ],
        // ),
        FContainer(
          child: FRow(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildRowTitleList(),
            ],
          ),
        ),
      ],
    );
  }
}
