import 'dart:convert';

import 'package:fis_jsonrpc/rpc.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:vitalapp/architecture/defines.dart';
import 'package:vitalapp/architecture/utils/datetime.dart';
import 'package:vitalapp/components/dialog_profile_time_input.dart';
import 'package:vitalapp/consts/rpc_enum_labels.dart';

class HealthModel {
  final List<String>? historyDrugAllergies;
  final List<String>? historyExposure;
  final List<String>? historyPreviousDisease;
  final String? historyPreviousDiseaseOther;

  ///既往疾病史-职业病
  final String? historyPreviousDiseaseOccupationalDisease;

  ///既往疾病史-恶性肿瘤
  final String? historyPreviousDiseaseMalignancy;
  final String? operation;
  final String? trauma;
  final String? transfusion;
  final List<String>? father;
  final String? fatherOther;
  final List<String>? mother;
  final String? motherOther;
  final List<String>? sibling;
  final String? siblingOther;
  final List<String>? children;
  final String? childrenOther;
  final String? historyGenetic;
  final String? historyGeneticName;
  final List<String>? disabilitySituation;
  final String? disabilitySituationOther;
  final String? kitchenExhaustFacilities;
  final String? fuel;
  final String? fuelOther;
  final String? drinkingWater;
  final String? drinkingWaterOther;
  final String? toilet;
  final String? animalPen;
  final String? historyDrugAllergiesOther;
  final VProfileTimeInputModel? operationOne;
  final VProfileTimeInputModel? operationTwo;
  final VProfileTimeInputModel? traumaOne;
  final VProfileTimeInputModel? traumaTwo;
  final VProfileTimeInputModel? transfusionOne;
  final VProfileTimeInputModel? transfusionTwo;
  final Map<String, DateTime> diseaseDiagnosisTime;

  ///详细信息
  final String? workUnit; //工作单位
  final String? contactName; //联系人姓名
  final String? contactPhone; //联系人电话
  final String? permanentlyResideType; //常住类型
  final String? bloodType; //血型
  final String? rh; //RH
  final String? educationLevel; //文化程度
  final String? career; //职业
  final String? maritalStatus; //婚姻状况
  final String? providerPayments; //医疗费用支付方式
  final String? providerPaymentsOther; //医疗费用支付方式

  HealthModel({
    required this.historyDrugAllergies,
    required this.historyExposure,
    required this.historyPreviousDisease,
    required this.historyPreviousDiseaseOther,
    required this.historyPreviousDiseaseOccupationalDisease,
    required this.historyPreviousDiseaseMalignancy,
    required this.operation,
    required this.trauma,
    required this.transfusion,
    required this.father,
    required this.fatherOther,
    required this.mother,
    required this.motherOther,
    required this.sibling,
    required this.siblingOther,
    required this.children,
    required this.childrenOther,
    required this.historyGenetic,
    required this.historyGeneticName,
    required this.disabilitySituation,
    required this.disabilitySituationOther,
    required this.kitchenExhaustFacilities,
    required this.fuel,
    required this.fuelOther,
    required this.drinkingWater,
    required this.drinkingWaterOther,
    required this.toilet,
    required this.animalPen,
    required this.historyDrugAllergiesOther,
    required this.operationOne,
    required this.operationTwo,
    required this.traumaOne,
    required this.traumaTwo,
    required this.transfusionOne,
    required this.transfusionTwo,
    required this.diseaseDiagnosisTime,
    required this.workUnit,
    required this.contactName,
    required this.contactPhone,
    required this.permanentlyResideType,
    required this.bloodType,
    required this.rh,
    required this.educationLevel,
    required this.career,
    required this.maritalStatus,
    required this.providerPayments,
    required this.providerPaymentsOther,
  });
  factory HealthModel.fromJson(Map<String, dynamic> jsonMap) {
    return HealthModel(
      historyDrugAllergies:
          jsonMap["historyDrugAllergies"]?.cast<String>().toList(),
      historyExposure: jsonMap["historyExposure"]?.cast<String>().toList(),
      historyPreviousDisease:
          jsonMap["historyPreviousDisease"]?.cast<String>().toList(),
      historyPreviousDiseaseOther: jsonMap["historyPreviousDiseaseOther"],
      historyPreviousDiseaseOccupationalDisease:
          jsonMap["historyPreviousDiseaseOccupationalDisease"],
      historyPreviousDiseaseMalignancy:
          jsonMap["historyPreviousDiseaseMalignancy"],
      operation: jsonMap["operation"],
      trauma: jsonMap["trauma"],
      transfusion: jsonMap["transfusion"],
      father: jsonMap["father"]?.cast<String>().toList(),
      fatherOther: jsonMap["fatherOther"],
      mother: jsonMap["mother"]?.cast<String>().toList(),
      motherOther: jsonMap["motherOther"],
      sibling: jsonMap["sibling"]?.cast<String>().toList(),
      siblingOther: jsonMap["siblingOther"],
      children: jsonMap["children"]?.cast<String>().toList(),
      childrenOther: jsonMap["childrenOther"],
      historyGenetic: jsonMap["historyGenetic"],
      historyGeneticName: jsonMap["historyGeneticName"],
      disabilitySituation:
          jsonMap["disabilitySituation"]?.cast<String>().toList(),
      disabilitySituationOther: jsonMap["disabilitySituationOther"],
      kitchenExhaustFacilities: jsonMap["kitchenExhaustFacilities"],
      fuel: jsonMap["fuel"],
      fuelOther: jsonMap["fuelOther"],
      drinkingWater: jsonMap["drinkingWater"],
      drinkingWaterOther: jsonMap["drinkingWaterOther"],
      toilet: jsonMap["toilet"],
      animalPen: jsonMap["animalPen"],
      historyDrugAllergiesOther: jsonMap["historyDrugAllergiesOther"],
      operationOne: jsonMap["operationOne"] != null
          ? VProfileTimeInputModel.fromJson(jsonDecode(jsonMap["operationOne"]))
          : null,
      operationTwo: jsonMap["operationTwo"] != null
          ? VProfileTimeInputModel.fromJson(jsonDecode(jsonMap["operationTwo"]))
          : null,
      traumaOne: jsonMap["traumaOne"] != null
          ? VProfileTimeInputModel.fromJson(jsonDecode(jsonMap["traumaOne"]))
          : null,
      traumaTwo: jsonMap["traumaTwo"] != null
          ? VProfileTimeInputModel.fromJson(jsonDecode(jsonMap["traumaTwo"]))
          : null,
      transfusionOne: jsonMap["transfusionOne"] != null
          ? VProfileTimeInputModel.fromJson(
              jsonDecode(jsonMap["transfusionOne"]))
          : null,
      transfusionTwo: jsonMap["transfusionTwo"] != null
          ? VProfileTimeInputModel.fromJson(
              jsonDecode(jsonMap["transfusionTwo"]))
          : null,
      diseaseDiagnosisTime:
          Map<String, dynamic>.from(jsonDecode(jsonMap["diseaseDiagnosisTime"]))
              .map((key, value) {
        return MapEntry(key, DateTime.parse(value));
      }),
      workUnit: jsonMap["workUnit"],
      contactName: jsonMap["contactName"],
      contactPhone: jsonMap["contactPhone"],
      permanentlyResideType: jsonMap["permanentlyResideType"],
      bloodType: jsonMap["bloodType"],
      rh: jsonMap["rh"],
      educationLevel: jsonMap["educationLevel"],
      career: jsonMap["career"],
      maritalStatus: jsonMap["maritalStatus"],
      providerPayments: jsonMap["providerPayments"],
      providerPaymentsOther: jsonMap["providerPaymentsOther"],
    );
  }

  Map<String, dynamic> toJson() {
    Map<String, dynamic> jsonMap = <String, dynamic>{};
    {
      if (historyDrugAllergies != null) {
        jsonMap["historyDrugAllergies"] = historyDrugAllergies;
      }
      if (historyExposure != null) jsonMap["historyExposure"] = historyExposure;

      if (historyPreviousDisease != null) {
        jsonMap["historyPreviousDisease"] = historyPreviousDisease;
      }

      if (historyPreviousDiseaseOther != null) {
        jsonMap["historyPreviousDiseaseOther"] = historyPreviousDiseaseOther;
      }
      if (historyPreviousDiseaseOccupationalDisease != null) {
        jsonMap["historyPreviousDiseaseOccupationalDisease"] =
            historyPreviousDiseaseOccupationalDisease;
      }
      if (historyPreviousDiseaseMalignancy != null) {
        jsonMap["historyPreviousDiseaseMalignancy"] =
            historyPreviousDiseaseMalignancy;
      }
      if (operation != null) jsonMap["operation"] = operation;

      if (trauma != null) jsonMap["trauma"] = trauma;

      if (transfusion != null) jsonMap["transfusion"] = transfusion;

      if (father != null) jsonMap["father"] = father;

      if (fatherOther != null) jsonMap["fatherOther"] = fatherOther;

      if (mother != null) jsonMap["mother"] = mother;

      if (motherOther != null) jsonMap["motherOther"] = motherOther;

      if (sibling != null) jsonMap["sibling"] = sibling;

      if (siblingOther != null) jsonMap["siblingOther"] = siblingOther;

      if (children != null) jsonMap["children"] = children;

      if (childrenOther != null) jsonMap["childrenOther"] = childrenOther;

      if (historyGenetic != null) jsonMap["historyGenetic"] = historyGenetic;

      if (historyGeneticName != null) {
        jsonMap["historyGeneticName"] = historyGeneticName;
      }

      if (disabilitySituation != null) {
        jsonMap["disabilitySituation"] = disabilitySituation;
      }

      if (disabilitySituationOther != null) {
        jsonMap["disabilitySituationOther"] = disabilitySituationOther;
      }

      if (kitchenExhaustFacilities != null) {
        jsonMap["kitchenExhaustFacilities"] = kitchenExhaustFacilities;
      }

      if (fuel != null) jsonMap["fuel"] = fuel;

      if (fuelOther != null) jsonMap["fuelOther"] = fuelOther;

      if (drinkingWater != null) jsonMap["drinkingWater"] = drinkingWater;

      if (drinkingWaterOther != null) {
        jsonMap["drinkingWaterOther"] = drinkingWaterOther;
      }

      if (toilet != null) jsonMap["toilet"] = toilet;

      if (animalPen != null) jsonMap["animalPen"] = animalPen;

      if (historyDrugAllergiesOther != null) {
        jsonMap["historyDrugAllergiesOther"] = historyDrugAllergiesOther;
      }

      if (operationOne != null) {
        jsonMap["operationOne"] = jsonEncode(operationOne?.toJson());
      }

      if (operationTwo != null) {
        jsonMap["operationTwo"] = jsonEncode(operationTwo?.toJson());
      }

      if (traumaOne != null) {
        jsonMap["traumaOne"] = jsonEncode(traumaOne?.toJson());
      }

      if (traumaTwo != null) {
        jsonMap["traumaTwo"] = jsonEncode(traumaTwo?.toJson());
      }

      if (transfusionOne != null) {
        jsonMap["transfusionOne"] = jsonEncode(transfusionOne?.toJson());
      }

      if (transfusionTwo != null) {
        jsonMap["transfusionTwo"] = jsonEncode(transfusionTwo?.toJson());
      }

      jsonMap["diseaseDiagnosisTime"] = jsonEncode(diseaseDiagnosisTime
          .map((key, value) => MapEntry(key, value.toIso8601String())));

      if (workUnit != null) {
        jsonMap["workUnit"] = workUnit;
      }
      if (contactName != null) {
        jsonMap["contactName"] = contactName;
      }
      if (contactPhone != null) {
        jsonMap["contactPhone"] = contactPhone;
      }
      if (permanentlyResideType != null) {
        jsonMap["permanentlyResideType"] = permanentlyResideType;
      }
      if (bloodType != null) {
        jsonMap["bloodType"] = bloodType;
      }
      if (rh != null) {
        jsonMap["rh"] = rh;
      }
      if (educationLevel != null) {
        jsonMap["educationLevel"] = educationLevel;
      }
      if (career != null) {
        jsonMap["career"] = career;
      }
      if (maritalStatus != null) {
        jsonMap["maritalStatus"] = maritalStatus;
      }

      if (providerPayments != null) {
        jsonMap["providerPayments"] = providerPayments;
      }

      if (providerPaymentsOther != null) {
        jsonMap["providerPaymentsOther"] = providerPaymentsOther;
      }

      return jsonMap;
    }
  }
}

class PatientInfoState {
  final Rx<NavAnchorType> _anchor = NavAnchorType.base.obs;

  /// 导航锚点
  NavAnchorType get anchor => _anchor.value;
  set anchor(NavAnchorType val) => _anchor.updateValue(val);

  final Rx<PatientDTO> _dto = Rx<PatientDTO>(
    PatientDTO(),
  );

  final RxString _code = RxString("");
  final RxString _name = RxString("");
  final Rx<CardTypeEnum> _cardType = Rx<CardTypeEnum>(CardTypeEnum.Identity);
  final RxString _cardNo = RxString("");
  final Rx<GenderEnum> _gender = Rx<GenderEnum>(GenderEnum.Male);
  final RxString _nation = RxString("");
  final Rx<DateTime?> _birthday = Rx<DateTime?>(null);
  final RxString _age = RxString("");
  final RxString _phoneNo = RxString("");
  final RxString _address = RxString("");
  final RxString _permanentResidenceAddress = RxString("");
  final RxString _createdOrgName = RxString("");
  final RxString _createdDoctorName = RxString("");
  final RxString _contractedDoctorName = RxString("");
  final RxString _createTime = RxString("");
  final RxList<String> _historyDrugAllergies = RxList<String>();
  final RxList<String> _historyExposure = RxList<String>();
  final RxList<String> _historyPreviousDisease = RxList<String>();
  final RxString _historyPreviousDiseaseOther = RxString('');
  final RxString _historyPreviousDiseaseMalignancy = RxString('');
  final RxString _historyPreviousDiseaseOccupationalDisease = RxString('');
  final RxString _operation = RxString('');
  final RxString _trauma = RxString('');
  final RxString _transfusion = RxString('');
  final RxList<String> _father = RxList<String>();
  final RxString _fatherOther = RxString('');
  final RxList<String> _mother = RxList<String>();
  final RxString _motherOther = RxString('');
  final RxList<String> _sibling = RxList<String>();
  final RxString _siblingOther = RxString('');
  final RxList<String> _children = RxList<String>();
  final RxString _childrenOther = RxString('');
  final RxString _historyGenetic = RxString('');
  final RxString _historyGeneticName = RxString('');
  final RxList<String> _disabilitySituation = RxList<String>();
  final RxString _disabilitySituationOther = RxString('');
  final RxString _kitchenExhaustFacilities = RxString('');
  final RxString _fuel = RxString('');
  final RxString _fuelOther = RxString('');
  final RxString _drinkingWater = RxString('');
  final RxString _drinkingWaterOther = RxString('');
  final RxString _toilet = RxString('');
  final RxString _animalPen = RxString('');
  final RxString _historyDrugAllergiesOther = RxString('');
  final _operationOne = Rx<VProfileTimeInputModel?>(null);
  final _operationTwo = Rx<VProfileTimeInputModel?>(null);
  final _traumaOne = Rx<VProfileTimeInputModel?>(null);
  final _traumaTwo = Rx<VProfileTimeInputModel?>(null);
  final _transfusionOne = Rx<VProfileTimeInputModel?>(null);
  final _transfusionTwo = Rx<VProfileTimeInputModel?>(null);
  final _diseaseDiagnosisTime = <String, DateTime>{}.obs;
  final RxString _workUnit = RxString(''); //工作单位
  final RxString _contactName = RxString(''); //联系人姓名
  final RxString _contactPhone = RxString(''); //联系人电话
  final RxString _permanentlyResideType = RxString(''); //常住类型
  final RxString _bloodType = RxString(''); //血型
  final RxString _rh = RxString(''); //RH
  final RxString _educationLevel = RxString(''); //文化程度
  final RxString _career = RxString(''); //职业
  final RxString _maritalStatus = RxString(''); //婚姻状况
  final RxString _providerPayments = RxString(''); //医疗费用支付方式
  final RxString _providerPaymentsOther = RxString(''); //医疗费用支付方式

  ///其他支付方式
  String get providerPaymentsOther => _providerPaymentsOther.value;
  set providerPaymentsOther(String val) => _providerPaymentsOther.value = val;

  ///医疗费用支付方式
  String get providerPayments => _providerPayments.value;
  set providerPayments(String val) => _providerPayments.value = val;

  ///婚姻状况
  String get maritalStatus => _maritalStatus.value;
  set maritalStatus(String val) => _maritalStatus.value = val;

  ///职业
  String get career => _career.value;
  set career(String val) => _career.value = val;

  ///文化程度
  String get educationLevel => _educationLevel.value;
  set educationLevel(String val) => _educationLevel.value = val;

  ///RH
  String get rh => _rh.value;
  set rh(String val) => _rh.value = val;

  ///血型
  String get bloodType => _bloodType.value;
  set bloodType(String val) => _bloodType.value = val;

  ///常住类型
  String get permanentlyResideType => _permanentlyResideType.value;
  set permanentlyResideType(String val) => _permanentlyResideType.value = val;

  ///联系人电话
  String get contactPhone => _contactPhone.value;
  set contactPhone(String val) => _contactPhone.value = val;

  ///联系人姓名
  String get contactName => _contactName.value;
  set contactName(String val) => _contactName.value = val;

  ///工作单位
  String get workUnit => _workUnit.value;
  set workUnit(String val) => _workUnit.value = val;

  ///疾病时间
  Map<String, DateTime> get diseaseDiagnosisTime => _diseaseDiagnosisTime;
  set diseaseDiagnosisTime(Map<String, DateTime> value) {
    _diseaseDiagnosisTime.value = value;
  }

  String healthCode = '';

  ///手术1
  VProfileTimeInputModel? get operationOne => _operationOne.value;
  set operationOne(VProfileTimeInputModel? val) =>
      _operationOne.updateValue(val);

  ///手术2
  VProfileTimeInputModel? get operationTwo => _operationTwo.value;
  set operationTwo(VProfileTimeInputModel? val) => _operationTwo.value = val;

  ///外伤1
  VProfileTimeInputModel? get traumaOne => _traumaOne.value;
  set traumaOne(VProfileTimeInputModel? val) => _traumaOne.updateValue(val);

  ///外伤2
  VProfileTimeInputModel? get traumaTwo => _traumaTwo.value;
  set traumaTwo(VProfileTimeInputModel? val) => _traumaTwo.updateValue(val);

  ///输血1
  VProfileTimeInputModel? get transfusionOne => _transfusionOne.value;
  set transfusionOne(VProfileTimeInputModel? val) =>
      _transfusionOne.updateValue(val);

  ///输血2
  VProfileTimeInputModel? get transfusionTwo => _transfusionTwo.value;
  set transfusionTwo(VProfileTimeInputModel? val) =>
      _transfusionTwo.updateValue(val);

  ///药物过敏史-其他
  String get historyDrugAllergiesOther => _historyDrugAllergiesOther.value;
  set historyDrugAllergiesOther(String val) =>
      _historyDrugAllergiesOther.value = val;

  ///药物过敏史
  List<String> get historyDrugAllergies => _historyDrugAllergies.toList();
  set historyDrugAllergies(List<String> val) {
    _historyDrugAllergies.value = val;
    if (!val.contains('其他')) {
      historyDrugAllergiesOther = '';
    }
  }

  ///暴露史
  List<String> get historyExposure => _historyExposure.toList();
  set historyExposure(List<String> val) => _historyExposure.value = val;

  ///既往疾病史
  List<String> get historyPreviousDisease => _historyPreviousDisease.toList();
  set historyPreviousDisease(List<String> val) {
    _historyPreviousDisease.value = val;
    if (!val.contains('其他')) {
      historyPreviousDiseaseOther = '';
    }
    if (!val.contains('职业病')) {
      historyPreviousDiseaseOther = '';
    }
    if (!val.contains('恶性肿瘤')) {
      historyPreviousDiseaseOther = '';
    }
    for (var key in diseaseDiagnosisTime.keys) {
      if (!val.contains(key)) {
        diseaseDiagnosisTime.remove(key);
      }
    }
  }

  ///既往疾病史-其他
  String get historyPreviousDiseaseOther => _historyPreviousDiseaseOther.value;
  set historyPreviousDiseaseOther(String val) =>
      _historyPreviousDiseaseOther.value = val;

  ///既往疾病史-职业病
  String get historyPreviousDiseaseOccupationalDisease =>
      _historyPreviousDiseaseOccupationalDisease.value;
  set historyPreviousDiseaseOccupationalDisease(String val) =>
      _historyPreviousDiseaseOccupationalDisease.value = val;

  ///既往疾病史-恶性肿瘤
  String get historyPreviousDiseaseMalignancy =>
      _historyPreviousDiseaseMalignancy.value;
  set historyPreviousDiseaseMalignancy(String val) =>
      _historyPreviousDiseaseMalignancy.value = val;

  ///手术
  String get operation => _operation.value;
  set operation(String val) {
    _operation.value = val;
    if (val != '2') {
      operationOne = null;
      operationTwo = null;
    }
  }

  ///外伤
  String get trauma => _trauma.value;
  set trauma(String val) {
    _trauma.value = val;
    if (val != '2') {
      transfusionOne = null;
      transfusionTwo = null;
    }
  }

  ///输血
  String get transfusion => _transfusion.value;
  set transfusion(String val) {
    _transfusion.value = val;
    if (val != '2') {
      transfusionOne = null;
      transfusionTwo = null;
    }
  }

  ///父亲
  List<String> get father => _father.toList();
  set father(List<String> val) {
    _father.value = val;
    if (!val.contains('其他')) {
      fatherOther = '';
    }
  }

  ///父亲-其他病史
  String get fatherOther => _fatherOther.value;
  set fatherOther(String val) => _fatherOther.value = val;

  ///母亲
  List<String> get mother => _mother.toList();
  set mother(List<String> val) {
    _mother.value = val;
    if (!val.contains('其他')) {
      motherOther = '';
    }
  }

  ///母亲-其他病史
  String get motherOther => _motherOther.value;
  set motherOther(String val) => _motherOther.value = val;

  ///兄弟姐妹
  List<String> get sibling => _sibling.toList();
  set sibling(List<String> val) {
    _sibling.value = val;
    if (!val.contains('其他')) {
      siblingOther = '';
    }
  }

  ///兄弟姐妹-其他病史
  String get siblingOther => _siblingOther.value;
  set siblingOther(String val) => _siblingOther.value = val;

  ///子女
  List<String> get children => _children.toList();
  set children(List<String> val) {
    _children.value = val;
    if (!val.contains('其他')) {
      childrenOther = '';
    }
  }

  ///子女-其他病史
  String get childrenOther => _childrenOther.value;
  set childrenOther(String val) => _childrenOther.value = val;

  ///遗传病史
  String get historyGenetic => _historyGenetic.value;
  set historyGenetic(String val) {
    if (_historyGenetic.value != val) {
      _historyGenetic.value = val;
      if (val != '2') {
        historyGeneticName = '';
      }
    }
  }

  ///遗传病史-疾病名称
  String get historyGeneticName => _historyGeneticName.value;
  set historyGeneticName(String val) => _historyGeneticName.value = val;

  ///残疾情况
  List<String> get disabilitySituation => _disabilitySituation.toList();
  set disabilitySituation(List<String> val) {
    _disabilitySituation.value = val;
    if (!val.contains('8')) {
      disabilitySituationOther = '';
    }
  }

  ///残疾情况-其他残疾
  String get disabilitySituationOther => _disabilitySituationOther.value;
  set disabilitySituationOther(String val) =>
      _disabilitySituationOther.value = val;

  ///厨房排风设施
  String get kitchenExhaustFacilities => _kitchenExhaustFacilities.value;
  set kitchenExhaustFacilities(String val) =>
      _kitchenExhaustFacilities.value = val;

  ///燃料类型
  String get fuel => _fuel.value;
  set fuel(String val) {
    if (_fuel.value != val) {
      _fuel.value = val;
      if (val != '6') {
        fuelOther = '';
      }
    }
  }

  ///燃料类型-其他
  String get fuelOther => _fuelOther.value;
  set fuelOther(String val) => _fuelOther.value = val;

  ///饮水
  String get drinkingWater => _drinkingWater.value;
  set drinkingWater(String val) {
    if (_drinkingWater.value != val) {
      _drinkingWater.value = val;
      if (val != '6') {
        drinkingWaterOther = '';
      }
    }
  }

  ///饮水-其他
  String get drinkingWaterOther => _drinkingWaterOther.value;
  set drinkingWaterOther(String val) => _drinkingWaterOther.value = val;

  ///厕所
  String get toilet => _toilet.value;
  set toilet(String val) => _toilet.value = val;

  ///禽兽栏
  String get animalPen => _animalPen.value;
  set animalPen(String val) => _animalPen.value = val;

  /// 居民编号
  String get code => _code.value;
  set code(String val) => _code.updateValue(val);

  /// 姓名
  String get name => _name.value;
  set name(String val) => _name.updateValue(val);

  /// 年龄
  String get age => _age.value;

  /// 手机号
  String get phoneNo => _phoneNo.value;
  set phoneNo(String val) => _phoneNo.updateValue(val);

  /// 地址
  String get address => _address.value;
  set address(String val) => _address.updateValue(val);

//户籍地址
  String get permanentResidenceAddress => _permanentResidenceAddress.value;
  set permanentResidenceAddress(String val) =>
      _permanentResidenceAddress.updateValue(val);

  /// 头像URL
  String? get avatarUrl => null; //TODO:

  /// 性别
  GenderEnum get gender => _gender.value;
  set gender(GenderEnum val) => _gender.updateValue(val);

  /// 性别描述
  String get genderDesc => RpcEnumLabels.gender[gender]!;

  /// 人群分类名称集合
  List<String>? get labelNames => _dto.value.labelNames;

  /// 民族
  String get nation => _nation.value;
  set nation(String val) => _nation.updateValue(val);

  /// 证件类型
  CardTypeEnum get cardType => _cardType.value;
  set cardType(CardTypeEnum val) => _cardType.updateValue(val);

  /// 证件号
  String get cardNo => _cardNo.value;
  set cardNo(String val) {
    _cardNo.updateValue(val);
    if (cardType == CardTypeEnum.Identity) {
      birthday = extractBirthDateFromIDCard(val);
    }
  }

  DateTime extractBirthDateFromIDCard(String idCardNumber) {
    final idCardRegex = RegExp(r'^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})(\d|X)$');
    final match = idCardRegex.firstMatch(idCardNumber);

    if (match != null) {
      final year = int.parse(match.group(2)!);
      final month = int.parse(match.group(3)!);
      final day = int.parse(match.group(4)!);

      final birthDate = DateTime(year, month, day);

      return birthDate;
    }

    return DateTime(1970, 1, 1); // 返回一个默认值，表示无法提取出生日期
  }

  /// 生日
  DateTime? get birthday => _birthday.value;
  set birthday(DateTime? val) {
    if (val != _birthday.value && val != null) {
      _birthday.value = val;
      _age.value = DataTimeUtils.calculateAge(val);
    }
  }

  ///建档医院
  String get createdOrgName => _createdOrgName.value;
  set createdOrgName(String val) => _createdOrgName.updateValue(val);

  ///建档医师
  String get createdDoctorName => _createdDoctorName.value;
  set createdDoctorName(String val) => _createdDoctorName.updateValue(val);

  //建档时间
  String get createTime => _createTime.value;
  set createTime(String val) => _createTime.updateValue(val);

  ///责任医师
  String get contractedDoctorName => _contractedDoctorName.value;
  set contractedDoctorName(String val) =>
      _contractedDoctorName.updateValue(val);

  void updateDto(PatientDTO dto) {
    _dto.value = dto;
    code = dto.code ?? "";
    name = dto.patientName ?? "";
    cardType = dto.cardType;
    cardNo = dto.cardNo ?? "";
    gender = dto.patientGender;
    nation = dto.nationality ?? "";
    birthday = dto.birthday ?? DateTime(1970, 1, 1);
    phoneNo = dto.phone ?? "";
    address = dto.patientAddress ?? "";
    permanentResidenceAddress = dto.permanentResidenceAddress ?? "";
    createdOrgName = dto.createdOrgName ?? "";
    createdDoctorName = dto.createdDoctorName ?? "";
    contractedDoctorName = dto.contractedDoctorName ?? "";
    createTime = DateFormat("yyyy-MM-dd")
        .format(dto.createTime?.toLocal() ?? DateTime.now());
  }
}

enum NavAnchorType {
  base,
  detail,
  health,
}
