import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/values/spacings.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:fis_common/index.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:intl/intl.dart';
import 'package:fis_jsonrpc/rpc.dart';
import '../index.dart';

class RecordPatientReferHistoryDialog
    extends FisView<ConsultationRecordViewController> implements FPage {
  const RecordPatientReferHistoryDialog({
    Key? key,
  }) : super(key: key);

  @override
  String get pageName => 'RecordPatientReferHistoryDialog';

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        i18nBook.remedical.referralHistory.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      onClose: () {
        router.back();
      },
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      children: [
        FContainer(
          margin: EdgeInsets.all(15),
          width: 970,
          height: 600,
          child: FColumn(
            children: [
              FExpanded(
                child: QuickFWidget(
                  ScrollConfiguration(
                    behavior: ScrollConfiguration.of(context)
                        .copyWith(scrollbars: false),
                    child: _buildReferralHistory(),
                  ),
                ),
              ),
              FSizedBox(
                height: FSpacings.L,
              ),
              _OperationButtonsRow(),
            ],
          ),
        ),
      ],
    );
  }

  FWidget _buildReferralHistory() {
    return FisBuilder<ConsultationRecordViewController>(
      init: ConsultationRecordViewController(),
      id: "record_refer_history_table",
      builder: (_) {
        return FTable<ReferralHistoryDetailData>(
          autoHeight: false,
          noDataHintText: i18nBook.common.noData.t,
          columns: _buildTableColumns(),
          source: controller.referralHistoryDetailList,
          loading: controller.isTableLoading,
          onRowSelected: (value, index, idxs) {},
          onRowTap: (index) {},
          onAllRowsSelected: (value, idxs) => {},
        );
      },
    );
  }

  FWidget _OperationButtonsRow() {
    return FisBuilder<ConsultationRecordViewController>(
      init: ConsultationRecordViewController(),
      id: "record_refer_operation_buttons",
      builder: (_) {
        if (controller.typeFilter == RecordProcessStateEnum.ReferralOut) {
          return _buildButtons();
        } else {
          return FContainer();
        }
      },
    );
  }

  FWidget _buildButtons() {
    return FContainer(
      width: 100,
      height: 40,
      child: _buildTextButton(
        i18nBook.common.revoke.t,
        () => controller.revokefer(),
      ),
    );
  }

  FWidget _buildTextButton(String textString, Function onPressed) {
    return FOutlinedButton(
      name: textString,
      businessParent: this,
      onPressed: () => onPressed.call(),
      child: FText(
        textString,
        style: TextStyle(color: Colors.black),
      ),
      style: ButtonStyle(
        padding: MaterialStateProperty.all(
          EdgeInsets.symmetric(
            vertical: 8,
            horizontal: 4,
          ),
        ),
      ),
    );
  }

  /// 配置表格列
  List<FTableColumn<ReferralHistoryDetailData>> _buildTableColumns() {
    return <FTableColumn<ReferralHistoryDetailData>>[
      FTableColumn<ReferralHistoryDetailData>(
        headerText: i18nBook.remedical.patientDisplayName.t,
        maxWidth: 150,
        render: (rowData, index) => FText(
          rowData.patientName.isNullOrEmpty
              ? i18nBook.remedical.notFill.t
              : rowData.patientName!,
          style: const TextStyle(
            fontSize: 16,
          ),
        ),
      ),
      FTableColumn<ReferralHistoryDetailData>(
        headerText: i18nBook.remedical.transferTime.t,
        render: (rowData, index) => FContainer(
          padding: EdgeInsets.symmetric(vertical: 15, horizontal: 2),
          child: FText(
            DateFormat("yyy-MM-dd HH:mm:ss").format(
              rowData.referralTime?.toLocal() ?? DateTime.now(),
            ),
            style: const TextStyle(
              fontSize: 16,
            ),
          ),
        ),
      ),
      FTableColumn<ReferralHistoryDetailData>(
        headerText: i18nBook.remedical.transferOutAgency.t,
        render: (rowData, index) => FCenter(
          child: FText(
            rowData.referralOutOrganizationName.isNullOrEmpty
                ? i18nBook.remedical.notFill.t
                : rowData.referralOutOrganizationName!,
            style: const TextStyle(
              fontSize: 16,
            ),
          ),
        ),
      ),
      FTableColumn<ReferralHistoryDetailData>(
        headerText: i18nBook.remedical.transferSubjectMatter.t,
        maxWidth: 100,
        render: (rowData, index) => FCenter(
            child: FTextTooltip(
          message: _label(rowData.subjectMatter),
          child: FText(
            _label(rowData.subjectMatter),
            style: const TextStyle(
              fontSize: 16,
            ),
            maxLines: 3,
            overflow: TextOverflow.ellipsis,
          ),
        )),
      ),
      FTableColumn<ReferralHistoryDetailData>(
        headerText: i18nBook.remedical.transferApply.t,
        maxWidth: 110,
        render: (rowData, index) => FCenter(
          child: FText(
            rowData.referralOutUserName.isNullOrEmpty
                ? i18nBook.remedical.notFill.t
                : rowData.referralOutUserName!,
            style: const TextStyle(
              fontSize: 16,
            ),
          ),
        ),
      ),
      FTableColumn<ReferralHistoryDetailData>(
        headerText: i18nBook.remedical.transferOrganization.t,
        render: (rowData, index) => FCenter(
          child: FText(
            rowData.referralInOrganizationName.toString(),
            style: const TextStyle(
              fontSize: 16,
            ),
          ),
        ),
      ),
      FTableColumn<ReferralHistoryDetailData>(
        headerText: i18nBook.remedical.transferDoctor.t,
        textAlign: TextAlign.center,
        render: (rowData, index) => FCenter(
          child: FText(
            rowData.referralInUserName.toString(),
            style: const TextStyle(
              fontSize: 16,
            ),
          ),
        ),
      ),
      FTableColumn<ReferralHistoryDetailData>(
        headerText: i18nBook.common.status.t,
        textAlign: TextAlign.center,
        render: (rowData, index) => FCenter(
          child: FText(
            statusConvert(
                rowData.referralStatus, rowData.referralOutUserCode ?? ''),
            style: const TextStyle(
              fontSize: 16,
            ),
          ),
        ),
      ),
    ];
  }

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

  /// 年龄文案
  String _label(String? age) {
    if (age == null || age.isEmpty) {
      return i18nBook.remedical.notFill.t;
    }
    return age;
  }
}
