import 'dart:collection';

import 'package:fis_jsonrpc/rpc.dart';
import 'package:get/get.dart';
import 'package:vitalapp/architecture/utils/datetime.dart';
import 'package:vitalapp/consts/rpc_enum_labels.dart';
import 'package:vitalapp/pages/contract/package_list/widgets/family_doctor_service_package.dart';

class ServicePackageContractState {
  /// 居民编号
  String patientCode = "";

  /// 姓名
  String get name => _dto.value.patientName ?? "";

  /// 年龄
  String get age => _dto.value.birthday != null
      ? DataTimeUtils.calculateAge(_dto.value.birthday!)
      : "";

  /// 手机号
  String get phone => _dto.value.phone ?? "";

  /// 地址
  String get address => _dto.value.patientAddress ?? '';

  /// 身份证
  String get cardNo => _dto.value.cardNo ?? "";

  /// 生日
  DateTime get birthday => _dto.value.birthday!;

  /// 性别
  String get genderDesc => RpcEnumLabels.gender[_dto.value.patientGender]!;

  /// 人群分类编号集合
  List<String>? get crowdLabelCodes => _dto.value.crowdLabels;
  // final RxList<dynamic> _servicePackageItems = RxList.empty();
  final RxList<ServicePackDTO> _servicePackageItems = RxList();

  final RxList<ServiceItemDTO> _serviceItems = RxList();
  final Rx<String?> _userImage = Rx(null);
  final RxList<ServicePackDTO> _selectedServicePackage =
      RxList<ServicePackDTO>();
  final RxString _selectedServicePackageName = RxString('');
  final RxString _selectedServicePackageCode = RxString('');
  final RxString _notes = RxString('');

  final Rx<PatientDTO> _dto = Rx<PatientDTO>(
    PatientDTO(
      patientName: "",
      patientGender: GenderEnum.Unknown,
      phone: "",
      birthday: DateTime(1900, 1, 1),
      patientAddress: "",
      crowdLabels: null,
    ),
  );

  final Rx<DateTime> _serviceStartDate = Rx(DateTime.now());
  final RxInt _serviceTime = RxInt(1);
  final RxBool _isExpendPatient = RxBool(false);

  /// 服务包集合
  List<ServicePackDTO> get servicePackageItems =>
      UnmodifiableListView(_servicePackageItems.toList());
  set servicePackageItems(List<ServicePackDTO> val) =>
      _servicePackageItems.value = val;

  /// 服务包集合
  List<ServiceItemDTO> get serviceItems =>
      UnmodifiableListView(_serviceItems.toList());
  set serviceItems(List<ServiceItemDTO> val) => _serviceItems.value = val;

  /// 选择的服务包集合
  List<ServicePackDTO> get selectedServicePackage =>
      _selectedServicePackage.toList();
  set selectedServicePackage(List<ServicePackDTO> val) =>
      _selectedServicePackage.value = val;

  /// 服务包名
  String get selectedServicePackageName => _selectedServicePackageName.value;
  set selectedServicePackageName(String val) =>
      _selectedServicePackageName.value = val;

  String? get userImage => _userImage.value;
  set userImage(String? val) => _userImage.value = val;

  /// 服务包code
  String get selectedServicePackageCode => _selectedServicePackageCode.value;
  set selectedServicePackageCode(String val) =>
      _selectedServicePackageCode.value = val;

  /// 备注
  String get notes => _notes.value;
  set notes(String val) => _notes.value = val;

  /// 是否展开
  bool get isExpendPatient => _isExpendPatient.value;
  set isExpendPatient(bool val) => _isExpendPatient.value = val;

  /// 服务开始时间
  DateTime get serviceStartDate => _serviceStartDate.value;
  set serviceStartDate(DateTime val) => _serviceStartDate.value = val;

  /// 服务年限
  int get serviceTime => _serviceTime.value;
  set serviceTime(int val) => _serviceTime.value = val;

  final RxString _signingStatus = RxString('签约');

  ///签约状态
  String get signingStatus => _signingStatus.value;
  set signingStatus(String val) => _signingStatus.value = val;

  final RxnString _reasonRefusal = RxnString(null);

  ///是否拒签
  final RxBool _isDenySbaVisa = RxBool(false);
  bool get isDenySbaVisa => _isDenySbaVisa.value;
  set isDenySbaVisa(bool val) {
    _isDenySbaVisa.value = val;
    if (val) {
      signingStatus = "拒签";
    } else {
      signingStatus = "签约";
      // selectedReasonIndex = -1;
    }
  }

  List<String> get reasonRefusals =>
      ['不自愿', '举家外出', '外出上学', '婚出', '服役', '服刑', '其他'];
  final RxInt _selectedReasonIndex = RxInt(-1);

  int get selectedReasonIndex => _selectedReasonIndex.value;
  set selectedReasonIndex(int val) {
    _selectedReasonIndex.value = val;
    if (val != -1) {
      reasonRefusal = reasonRefusals[val];
    }
  }

  ///拒签原因
  String? get reasonRefusal => _reasonRefusal.value;
  set reasonRefusal(String? val) {
    _reasonRefusal.value = ('${_selectedReasonIndex.value}:${val!}');
  }

  void updateDto(PatientDTO dto) {
    _dto.value = dto;
  }
}
