import 'package:fis_i18n/i18n.dart';
import 'package:get/get.dart';

///组织信息状态
class OrgInfoState {
  final Rx<bool?> _isChinaArea = Rx(false);
  final Rx<bool> _isSafeMode = Rx(false);
  final Rx<bool> _isSearchReferOrg = Rx(false);
  final Rx<bool?> _loadding = Rx(false);
  final Rx<String?> _areaDisplayName = Rx('');
  final Rx<String?> _countryCode = Rx(null);
  final Rx<String?> _provinceCode = Rx(null);
  final Rx<String?> _cityCode = Rx(null);
  final Rx<String?> _organizationName = Rx('');
  final Rx<double?> _timeoutLimit = Rx(1.0);
  final Rx<String> _logoUrl = Rx('');
  final Rx<String?> _parentCode = Rx('');
  final Rx<String?> _parentOrganizationName = Rx('');
  final Rx<String?> _organizationCode = Rx('');
  final Rx<String?> _areaName = Rx('');
  final Rx<String?> _patientType = Rx(null);
  final Rx<List<OrgModel>?> _orgOptions = Rx(<OrgModel>[]);
  final Rx<List<OrgModel>> _allOrgOptions = Rx(<OrgModel>[]);
  final Rx<List<OrgModel>?> _referOrgOptions = Rx(<OrgModel>[]);
  final Rx<List<OrgModel>?> _allReferOrgOptions = Rx(<OrgModel>[]);
  final Rx<List<OrgModel>?> _orgType = Rx(<OrgModel>[
    OrgModel(code: '0', name: i18nBook.remedical.humanAlong.t),
    OrgModel(code: '1', name: i18nBook.remedical.beastAlong.t),
  ]);
  final Rx<List<OrgModel>> _selectReferOrgList = Rx([]);

  final RxString _orgShortCode = RxString('');

  ///医院短码
  String get orgShortCode => _orgShortCode.value;
  set orgShortCode(String val) => _orgShortCode.value = val;

  ///隐私保护是否开启
  bool get isSafeMode => _isSafeMode.value;
  set isSafeMode(bool v) => _isSafeMode.value = v;

  ///当前是否为搜索可转诊机构页面
  bool get isSearchReferOrg => _isSearchReferOrg.value;
  set isSearchReferOrg(bool v) => _isSearchReferOrg.value = v;

  /// 机构显示筛选后的选项集合
  set orgOptions(List<OrgModel>? value) => _orgOptions.value = value;
  List<OrgModel>? get orgOptions => _orgOptions.value;

  /// 所有机构选项集合
  set allOrgOptions(List<OrgModel> value) => _allOrgOptions.value = value;
  List<OrgModel> get allOrgOptions => _allOrgOptions.value;

  /// 上级机构选项集合
  set referOrgOptions(List<OrgModel>? value) => _referOrgOptions.value = value;
  List<OrgModel>? get referOrgOptions => _referOrgOptions.value;

  /// 可转诊上级机构选项集合
  set allReferOrgOptions(List<OrgModel>? value) =>
      _allReferOrgOptions.value = value;
  List<OrgModel>? get allReferOrgOptions => _allReferOrgOptions.value;

  /// 地区外显名称
  set areaDisplayName(String? value) => _areaDisplayName.value = value;
  String? get areaDisplayName => _areaDisplayName.value;

  /// 国家Code
  set countryCode(String? value) => _countryCode.value = value;
  String? get countryCode => _countryCode.value;

  /// 省份Code
  set provinceCode(String? value) => _provinceCode.value = value;
  String? get provinceCode => _provinceCode.value;

  /// 城市Code
  set cityCode(String? value) => _cityCode.value = value;
  String? get cityCode => _cityCode.value;

  /// 是否中国地区
  set isChinaArea(bool? value) => _isChinaArea.value = value;
  bool? get isChinaArea => _isChinaArea.value;

  /// 是否图片加载
  set loadding(bool? value) => _loadding.value = value;
  bool? get loadding => _loadding.value;

  /// 组织名称
  set organizationName(String? value) => _organizationName.value = value;
  String? get organizationName => _organizationName.value;

  /// 超时时限
  set timeoutLimit(double? value) => _timeoutLimit.value = value;
  double? get timeoutLimit => _timeoutLimit.value;

  ///组织Code
  set organizationCode(String? value) => _organizationCode.value = value;
  String? get organizationCode => _organizationCode.value;

  /// logo url
  set logoUrl(String value) => _logoUrl.value = value;
  String get logoUrl => _logoUrl.value;

  ///上级机构
  set parentCode(String? value) => _parentCode.value = value;
  String? get parentCode => _parentCode.value;

  /// 新的上级机构名称
  set parentOrganizationName(String? value) =>
      _parentOrganizationName.value = value;
  String? get parentOrganizationName => _parentOrganizationName.value;

  ///上级机构名称
  String get parentName {
    if (parentCode == null || parentCode!.isEmpty) return '';
    final model = orgOptions!.firstWhereOrNull((x) => x.code == parentCode);
    return model?.name ?? '';
  }

  /// app端地区名称
  set areaName(String? value) => _areaName.value = value;
  String? get areaName => _areaName.value;

  /// 获取组织服务对象列表
  List<OrgModel> get orgType => _orgType.value!;

  ///上级机构
  set patientType(String? value) => _patientType.value = value;
  String? get patientType => _patientType.value;

  /// 选中的项
  set selectReferOrgList(List<OrgModel> value) => _selectReferOrgList(value);
  List<OrgModel> get selectReferOrgList => _selectReferOrgList.value;
}

class OrgModel {
  OrgModel({required this.name, required this.code});
  final String name;
  final String code;
}
