import 'package:fis_jsonrpc/rpc.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/architecture/values/features.dart';
import 'package:flyinsono/managers/interfaces/entities/patient_Info.dart';
import 'package:get/get.dart';

class ConsultationDetailState {
  final RxBool _showMore = RxBool(false);
  final RxBool _isEdit = RxBool(false);
  final RxString _consultationCode = RxString('');
  final Rx<PatientInfo> _patientInfo = Rx(PatientInfo(patientCode: ''));
  final Rx<ConsultationDetailDTO> _consultationDetailDTO =
      Rx(ConsultationDetailDTO());
  final Rx<String> _refuseReason = Rx('');
  final Rx<String> _refuseInvitationReason = Rx('');
  final RxBool _isAddPatient = RxBool(false);

  ///是否添加病人
  bool get isAddPatient => _isAddPatient.value;
  set isAddPatient(bool v) => _isAddPatient.value = v;

  ///是否编辑状态
  bool get isEdit => _isEdit.value;
  set isEdit(bool v) => _isEdit.value = v;

  ///是否查看更多
  bool get showMore => _showMore.value;
  set showMore(bool v) => _showMore.value = v;

  ///会诊详情
  ConsultationDetailDTO get consultationDetail => _consultationDetailDTO.value;
  set consultationDetail(ConsultationDetailDTO c) =>
      _consultationDetailDTO.value = c;
  void updateDiseases(String diseases) {
    consultationDetail.diseases = diseases;
    _consultationDetailDTO.update(((val) {
      val?.diseases = diseases;
    }));
  }

  void updateScanPosition(List<String> scanPosition) {
    consultationDetail.scanPositions = scanPosition;
    _consultationDetailDTO.update(((val) {
      val?.scanPositions = scanPosition;
    }));
  }

  void updatePrimaryDiagnosis(String primaryDiagnosis) {
    consultationDetail.primaryDiagnosis = primaryDiagnosis;
    _consultationDetailDTO.update(((val) {
      val?.primaryDiagnosis = primaryDiagnosis;
    }));
  }

  void updateDescription(String description) {
    consultationDetail.description = description;
    _consultationDetailDTO.update(((val) {
      val?.description = description;
    }));
  }

  ///病人信息
  PatientInfo get patientInfo => _patientInfo.value;
  set patientInfo(PatientInfo p) => _patientInfo.value = p;
  void updatePatientPhone(String phone) {
    patientInfo.phone = phone;
    _patientInfo.update(((val) {
      val?.phone = phone;
    })); // 调用 update() 方法通知 GetX 更新 UI
  }

  void updatePatientAge(String age) {
    patientInfo.ageYear = age;
    _patientInfo.update(((val) {
      val?.ageYear = age;
    })); // 调用 update() 方法通知 GetX 更新 UI
  }

  void updatePatientAnamnesis(String anamnesis) {
    patientInfo.anamnesis = anamnesis;
    _patientInfo.update(((val) {
      val?.anamnesis = anamnesis;
    })); // 调用 update() 方法通知 GetX 更新 UI
  }

  void updatePatientSex(String gender) {
    patientInfo.sex = gender;
    _patientInfo.update(((val) {
      val?.sex = gender;
    })); // 调用 update() 方法通知 GetX 更新 UI
  }

  /// 会诊编号
  String get consultationCode => _consultationCode.value;
  set consultationCode(String v) => _consultationCode.value = v;

  /// 拒绝原因
  String get refuseReason => _refuseReason.value;
  set refuseReason(String v) => _refuseReason.value = v;

  /// 拒绝原因
  String get refuseInvitationReason => _refuseInvitationReason.value;
  set refuseInvitationReason(String v) => _refuseInvitationReason.value = v;

  /// 同意或拒绝邀请权限
  bool get auditInvitation =>
      Store.user.hasFeature(FeatureKeys.AuditInvitation);

  /// 同意或拒绝申请权限
  bool get auditApplication =>
      Store.user.hasFeature(FeatureKeys.AuditApplication);

  /// 修改或撤回申请
  bool get updateApplyConsultation =>
      Store.user.hasFeature(FeatureKeys.UpdateApplyConsultation);

  /// 申请机构是否非当前账号机构
  bool get isCurrentOrganizationCode =>
      Store.user.rootOrganizationCode ==
      consultationDetail.applyOrganizationCode;

  /// 是否有修改权限 已撤回&已拒绝&已申请（待审核）
  bool get isModifyAuth =>
      updateApplyConsultation &&
      [
        TransactionStatusEnum.Applied,
        TransactionStatusEnum.Withdrawn,
        TransactionStatusEnum.Rejected
      ].contains(consultationDetail.consultationStatus) &&
      isCurrentOrganizationCode;

  /// 是否显示修改按钮（从后端字段判断）
  bool get isShowModifyButton => consultationDetail.isUpdateConsultationShow;

  /// 是否显示撤回按钮（从后端字段判断）
  bool get isShowWithdrawButton => consultationDetail.isRevokeShow;

  /// 是否显示同意按钮（从后端字段判断）
  bool get isShowAgreeButton => consultationDetail.isApprovalShow;

  /// 是否显示拒绝按钮（从后端字段判断）
  bool get isShowRefuseButton => consultationDetail.isRejectShow;

  /// 是否显示接受邀请
  bool get isShowAcceptInvitationButton =>
      consultationDetail.isAcceptInvitationShow;

  /// 是否显示拒绝邀请
  bool get isShowRefuseInvitationButton =>
      consultationDetail.isRejectInvitationShow;

  /// 是否显示开始会诊
  bool get isShowStartConsultationButton => consultationDetail.isInitiateShow;
}
