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/states/states.dart';
import 'package:flyinsono/architecture/utils/common_util.dart';

import 'package:flyinsono/pages/remedical/consultation_record_view/controller.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/models.dart';

/// PC端转诊输入框
class RecordReferralApplication
    extends FisView<ConsultationRecordViewController>
    implements FInteractiveContainer {
  final String patientName;
  @override
  final String pageName;

  RecordReferralApplication(
    this.patientName, {
    this.pageName = 'RecordReferralApplication',
  });

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      child: _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 ? 150 : 200,
            child: FRow(
              children: [
                FText(title),
                isRequired ?? false
                    ? FText(
                        '*',
                        style: TextStyle(color: Colors.red),
                      )
                    : FSizedBox(),
              ],
            ),
          ),
          FExpanded(
            child: referralItem,
          ),
        ],
      ),
    );
  }

  /// 转入机构
  FWidget _buildReferOrg() {
    final referOrg = FSelect<FSelectOptionModel, String>(
      source: controller.referOrg,
      hintText: i18nBook.remedical.pleaseSelect.translate(
        [i18nBook.remedical.transferOrganization.t],
      ),
      fontFamily: FTheme.ins.localeSetting.fontFamily,
      value: controller.referralOrganizationCode,
      clearable: false,
      height: 40,
      width: 400,
      optionLabelExtractor: (data) => data.title,
      optionValueExtractor: (data) => data.value,
      onSelectChanged: (value, index) {
        controller.referralUserCode = null;
        controller.getUserListAsync(value!);
        controller.referralOrganizationCode = value;
      },
    );
    return referOrg;
  }

  FWidget _buildImage(String? isImage) {
    var child;
    isImage!.isEmpty
        ? child = FImage.asset(
            'assets/images/avatar_man.png',
            width: 50,
            height: 50,
            fit: BoxFit.cover,
          )
        : child = FImage.network(
            isImage,
            width: 50,
            height: 50,
            fit: BoxFit.cover,
          );
    return FContainer(
      margin: EdgeInsets.only(
        right: 15,
      ),
      child: FClipRRect(
        borderRadius: BorderRadius.circular(
          25,
        ),
        child: child,
      ),
    );
  }

  /// 转入医生
  FWidget _buildReferDoctor() {
    final fontStyle = TextStyle(
      fontSize: 14,
    );
    final List<UserDoctorModel> referUserList =
        controller.referUserList.where((element) {
      return element.code != Store.user.code;
    }).toList();
    final referOrg = FSelect<UserDoctorModel, String>(
      source: referUserList,
      hintText: i18nBook.remedical.pleaseSelect.translate(
        [i18nBook.remedical.transferDoctor.t],
      ),
      fontFamily: FTheme.ins.localeSetting.fontFamily,
      value: controller.referralUserCode,
      clearable: false,
      height: 40,
      specialWidgetHeight: 80,
      width: 400,
      optionLabelExtractor: (data) => data.name,
      optionValueExtractor: (data) => data.code,
      onSelectChanged: (value, index) {
        controller.referralUserCode = value!;
      },
      specialWidget: (data) => FContainer(
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: 0.5,
              color: FTheme.ins.colorScheme.line,
            ),
          ),
        ),
        child: FRow(
          children: [
            _buildImage(data.headImageUrl),
            FColumn(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                FText(
                  data.name,
                  style: fontStyle,
                ),
                // if (data.roleCodes!.isNotEmpty)
                FText(
                  data.roleName ?? '',
                  style: fontStyle,
                ),
                if (data.fieldList!.isNotEmpty)
                  FText(
                    i18nBook.user.skilledAreaWithDevider.t +
                        data.fieldList!.join("，"),
                    style: fontStyle,
                  ),
              ],
            )
          ],
        ),
      ),
    );
    return referOrg;
  }

  FWidget _buildReferral() {
    return FColumn(
      children: [
        FExpanded(
          child: FContainer(
            padding: EdgeInsets.only(
              top: 25,
              left: 25,
              right: 25,
            ),
            child: FColumn(
              children: [
                _buildReferralItem(
                  i18nBook.remedical.patientDisplayName.t,
                  FText(patientName),
                ),
                _buildReferralItem(
                  i18nBook.remedical.transferOutAgency.t,
                  FText(
                    controller.organizationName,
                  ),
                ),
                _buildReferralItem(
                  i18nBook.remedical.transferSubjectMatter.t,
                  FTextField(
                    maxLines: 3,
                    maxLength: 100,
                    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.subjectMatter,
                    ),
                    onChanged: (val) => controller.subjectMatter = val,
                  ),
                  true,
                ),
                _buildReferralItem(
                  i18nBook.remedical.transferOrganization.t,
                  _buildReferOrg(),
                  true,
                ),
                _buildReferralItem(
                  i18nBook.remedical.transferDoctor.t,
                  FisBuilder<ConsultationRecordViewController>(
                    id: 'transfer_doctor',
                    builder: (_) {
                      return _buildReferDoctor();
                    },
                  ),
                  true,
                ),
                _buildReferralItem(
                  i18nBook.remedical.transferApply.t,
                  FText(Store.user.displayName),
                ),
              ],
            ),
          ),
        ),
        _buildButtomOperate(),
      ],
    );
  }

  FWidget _buildButtomOperate() {
    return FContainer(
      height: 80,
      decoration: BoxDecoration(
        border: Border(
          top: BorderSide(
            width: 0.5,
            color: FTheme.ins.colorScheme.line,
          ),
        ),
      ),
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(vertical: 15),
      child: FFlex(
        direction: Axis.horizontal,
        mainAxisSize: MainAxisSize.min,
        children: [
          FElevatedButton(
            businessParent: this,
            name: "submitRefer:${controller.patientCode}",
            onPressed: () {
              CommonUtil.debounce(
                () => controller.submitRefer(
                  controller.patientCode,
                ),
              );
            },
            style: ButtonStyle(
              padding: MaterialStateProperty.all(
                EdgeInsets.symmetric(
                  vertical: 15,
                  horizontal: 25,
                ),
              ),
            ),
            child: FText(i18nBook.common.submit.t),
          ),
          FSizedBox(
            width: 20,
          ),
          FOutlinedButton(
            name: "cleanState",
            businessParent: this,
            onPressed: () {
              controller.initReferStatus();
            },
            style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all(
                Colors.white,
              ),
              padding: MaterialStateProperty.all(
                EdgeInsets.symmetric(
                  vertical: 15,
                  horizontal: 25,
                ),
              ),
            ),
            child: FText(
              i18nBook.common.cancel.t,
              style: TextStyle(
                color: Color.fromRGBO(
                  61,
                  146,
                  252,
                  1,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
