import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/managers/interfaces/entities/patient_Info.dart';
import 'package:flyinsono/pages/remedical/consultation/controller.dart';
import 'package:flyinsono/pages/remedical/consultation/state.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/refer_mobile_select.dart';
import 'package:get/get.dart';

/// PC端转诊输入框
class ReferralMobileApplication extends FStatelessWidget
    implements FInteractiveContainer {
  final PatientInfo patientInfo;
  final consultationController = Get.find<ConsultationController>();
  final controller = Get.find<ConsultationController>();
  final textFocusNode = FocusNode();
  @override
  final String pageName;
  ReferralMobileApplication(this.patientInfo,
      {this.pageName = 'ReferralMobileApplication'});

  @override
  FWidget build(BuildContext context) {
    return _buildReferral();
  }

  FWidget _buildReferralItem(String title, FWidget referralItem,
      [bool? isRequired]) {
    return FContainer(
      margin: EdgeInsets.symmetric(
        vertical: 15,
      ),
      child: FRow(
        children: [
          FContainer(
            /// TODO:[Gavin] 多语言-UI-整合
            width: i18nBook.isCurrentChinese ? 100 : 130,
            child: FRow(
              mainAxisSize: MainAxisSize.min,
              children: [
                FExpanded(
                  child: FText(
                    title,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                FSizedBox(
                  width: 20,
                  child: isRequired ?? false
                      ? FText(
                          '*',
                          style: TextStyle(color: Colors.red),
                        )
                      : FSizedBox(),
                )
              ],
            ),
          ),
          FExpanded(
            child: referralItem,
          ),
        ],
      ),
    );
  }

  FWidget _buildReferral() {
    return FGestureDetector(
      name: "unfocus",
      businessParent: this,
      onTap: () {
        textFocusNode.unfocus();
      },
      child: FColumn(
        children: [
          FExpanded(
            child: FSingleChildScrollView(
              child: FContainer(
                padding: EdgeInsets.only(
                  top: 25,
                  left: 25,
                  right: 25,
                ),
                child: FColumn(
                  children: [
                    _buildReferralItem(
                      i18nBook.remedical.patientDisplayName.t,
                      FText(patientInfo.name ?? ''),
                    ),
                    _buildReferralItem(
                      i18nBook.remedical.transferOutAgency.t,
                      FText(
                        consultationController
                            .patientInfoController.organizationName,
                      ),
                    ),
                    _buildReferralItem(
                      i18nBook.remedical.transferSubjectMatter.t,
                      FTextField(
                        maxLines: 3,
                        maxLength: 100,
                        focusNode: textFocusNode,
                        decoration: InputDecoration(
                          hintText: i18nBook.common.input.translate(
                              [i18nBook.remedical.transferSubjectMatter.t]),
                          hintStyle: TextStyle(
                            fontSize: 16,
                          ),
                          enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                              color: Colors.white.withOpacity(0.5),
                              width: 0.5,
                              style: BorderStyle.solid,
                            ),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                              color: FTheme.ins.colorScheme.primary,
                              width: 0.5,
                              style: BorderStyle.solid,
                            ),
                          ),
                          filled: true,
                        ),
                        controller: TextEditingController(
                          text: controller.state.subjectMatter,
                        ),
                        onChanged: (val) =>
                            controller.state.subjectMatter = val,
                      ),
                      true,
                    ),
                    _buildReferralItem(
                      i18nBook.remedical.transferOrganization.t,
                      FObx(
                        () => ReferSelectSheet(
                          hintText: i18nBook.common.Select.t,
                          source: controller.state.referOrg,
                          value: controller.state.referralOrganizationCode,
                          tipTitle: i18nBook.remedical.pleaseSelect.translate(
                            [i18nBook.remedical.transferOrganization.t],
                          ),
                          inkWellTitle: i18nBook.homePage.notYet.t +
                              i18nBook.remedical.transferOrganization.t,
                          onChange: (value) {
                            controller.state.referralOrganizationCode = '';
                            controller.inspectionRecordsController
                                .getUserListAsync(value!);
                            controller.state.referralOrganizationCode = value;
                            controller.state.referralUserCode = null;
                          },
                        ),
                      ),
                      true,
                    ),
                    _buildReferralItem(
                      i18nBook.remedical.transferDoctor.t,
                      FObx(() {
                        final List<UserDoctorModel> referUserList = controller
                            .state.referUserList
                            .where((element) => element.code != Store.user.code)
                            .toList();
                        return ReferSelectSheet(
                          hintText: i18nBook.common.Select.t,
                          tipTitle: i18nBook.remedical.pleaseSelect.translate(
                            [i18nBook.remedical.transferDoctor.t],
                          ),
                          source: referUserList,
                          inkWellTitle:
                              controller.state.referralOrganizationCode == '' ||
                                      controller
                                              .state.referralOrganizationCode ==
                                          null
                                  ? i18nBook.remedical.select.t +
                                      i18nBook.remedical.transferOrganization.t
                                  : i18nBook.remedical.hasNoChooseAgain.t,
                          value: controller.state.referralUserCode,
                          isCustomMade: true,
                          onChange: (value) {
                            controller.state.referralUserCode = value!;
                          },
                        );
                      }),
                      true,
                    ),
                    _buildReferralItem(
                      i18nBook.remedical.transferApply.t,
                      FText(Store.user.displayName),
                    ),
                  ],
                ),
              ),
            ),
          ),
          _buildButtomOperate(),
        ],
      ),
    );
  }

  FWidget _buildButtomOperate() {
    return FContainer(
      height: 80,
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(vertical: 15),
      child: FFlex(
        direction: Axis.horizontal,
        mainAxisSize: MainAxisSize.min,
        children: [
          _buildButtomOperateItem(
            onTap: () {
              controller.inspectionRecordsController.cleanState();
              router.back();
              controller.state.referTab = 0;
            },
            decoration: BoxDecoration(
              border: Border.all(width: 0.5, color: Colors.grey),
            ),
            title: i18nBook.common.cancel.t,
          ),
          _buildButtomOperateItem(
            onTap: () => controller.inspectionRecordsController.submitRefer(
              patientInfo.patientCode,
              patientInfo.referralRecordCode ?? '',
            ),
            backgroundColor: FTheme.ins.colorScheme.primary,
            textColor: Colors.white,
            title: i18nBook.common.submit.t,
          ),
        ],
      ),
    );
  }

  FWidget _buildButtomOperateItem({
    required String title,
    required Function onTap,
    Color? backgroundColor,
    Color? textColor,
    BoxDecoration? decoration,
  }) {
    return FExpanded(
      child: FContainer(
        color: backgroundColor,
        decoration: decoration,
        child: FInkWell(
          onTap: () {
            onTap();
          },
          child: FCenter(
            child: FText(
              title,
              style: TextStyle(
                color: textColor,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
