DCS/ruiyiweiUX/Assets/Scripts/Views/PatientInfoPanel.cs

568 lines
19 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class PatientInfoPanel : BasePanel
{
[Header("患者信息输入")]
public TMP_InputField NameInput;
public TMP_InputField AdmissionIdInput;
public TMP_Dropdown GenderDropdown; // 0: 男, 1: 女
public TMP_InputField AgeInput;
public TMP_InputField HeightInput;
public TMP_InputField WeightInput;
// public TMP_InputField IdNumberInput;
[Header("按钮控制")]
public Button HomeButton;
public Button ConfirmButton;
public Button BackButton;
public Button LoadFromFileButton; // 从文件加载按钮
public Button ExportToFileButton; // 导出到文件按钮
// public Button StartSessionButton; // 开始会话按钮
[Header("会话状态显示")]
// public GameObject SessionStatusPanel; // 会话状态面板
// public TextMeshProUGUI SessionStatusText; // 会话状态文本
// public TextMeshProUGUI AdminUserText; // 管理员用户显示
// public Button EndSessionButton; // 结束会话按钮
private PatientInfoSettingsViewModel _vm;
private IPatientInfoService _patientService;
// private PatientSessionService _sessionService;
private IAuthenticationService _authService;
private TabNavigationSystem _tabNavigation;
public override void Init()
{
InitializeButtons();
InitializeServices();
InitializeFieldDisplay();
// InitializeTabNavigation();
// UpdateUI();
LoadCurrentPatientInfo();
// 延迟设置自定义键盘,确保 CustomKeyboardManager 已初始化
StartCoroutine(SetupCustomKeyboardDelayed());
}
private System.Collections.IEnumerator SetupCustomKeyboardDelayed()
{
// 等待一帧确保所有 Awake 都已执行
yield return null;
// 检查 CustomKeyboardManager 是否存在
if (CustomKeyboardManager.Instance == null)
{
Debug.LogError("CustomKeyboardManager not found in scene! Please add CustomKeyboardManager to Canvas.");
yield break;
}
DisableSystemKeyboardForInputFields();
}
private void InitializeButtons()
{
if (HomeButton != null)
{
HomeButton.onClick.AddListener(() => ReturnToHome());
}
if (ConfirmButton != null)
{
ConfirmButton.onClick.AddListener(ApplyAndBack);
}
if (BackButton != null)
{
BackButton.onClick.AddListener(() => ClosePanel());
}
if (LoadFromFileButton != null)
{
LoadFromFileButton.onClick.AddListener(OnLoadFromFileClicked);
}
if (ExportToFileButton != null)
{
ExportToFileButton.onClick.AddListener(OnExportToFileClicked);
}
// if (StartSessionButton != null)
// {
// StartSessionButton.onClick.AddListener(OnStartSessionClicked);
// }
// if (EndSessionButton != null)
// {
// EndSessionButton.onClick.AddListener(OnEndSessionClicked);
// }
}
private void InitializeFieldDisplay()
{
SetInputPlaceholder(NameInput, "请输入患者姓名");
SetInputPlaceholder(AdmissionIdInput, "请输入住院号");
SetInputPlaceholder(AgeInput, "年龄(岁)");
SetInputPlaceholder(HeightInput, "身高cm");
SetInputPlaceholder(WeightInput, "体重kg");
}
private void SetInputPlaceholder(TMP_InputField inputField, string placeholder)
{
if (inputField?.placeholder == null)
{
return;
}
var placeholderText = inputField.placeholder.GetComponent<TextMeshProUGUI>();
if (placeholderText != null)
{
placeholderText.text = placeholder;
}
}
private void InitializeServices()
{
_vm = new PatientInfoSettingsViewModel();
_vm.OnPatientInfoSaved += OnPatientInfoSaved;
_patientService = ServiceLocator.Get<IPatientInfoService>();
// _sessionService = ServiceLocator.Get<PatientSessionService>();
_authService = ServiceLocator.Get<IAuthenticationService>();
// 注册会话事件
// if (_sessionService != null)
// {
// _sessionService.OnSessionStarted += OnSessionStarted;
// _sessionService.OnSessionEnded += OnSessionEnded;
// }
}
private void ApplyAndBack()
{
var info = new PatientInfo
{
Name = NameInput?.text ?? "",
HospitalId = AdmissionIdInput?.text ?? "",
Gender = GenderDropdown?.value == 1 ? "女" : "男",
Age = int.TryParse(AgeInput?.text ?? "0", out int age) ? age : 0,
Height = float.TryParse(HeightInput?.text ?? "0", out float height) ? height : 0f,
Weight = float.TryParse(WeightInput?.text ?? "0", out float weight) ? weight : 0f,
// IdNumber = IdNumberInput?.text ?? ""
};
_vm.SavePatientInfo(info);
}
private void LoadCurrentPatientInfo()
{
if (_patientService != null)
{
var info = _patientService.GetCurrentPatient();
if (info != null)
{
PopulateFields(info);
}
else
{
ClearFields();
}
}
}
/// <summary>
/// 从文件加载病人信息
/// </summary>
private void OnLoadFromFileClicked()
{
// 显示文件选择对话框
FileSelectionDialog.Show(
"选择病人信息文件",
_patientService?.GetFileFilter() ?? "所有文件|*.*",
(filePath) => {
if (!string.IsNullOrEmpty(filePath))
{
LoadPatientFromFile(filePath);
}
},
() => Debug.Log("取消文件选择")
);
}
/// <summary>
/// 导出病人信息到文件(包含测量数据)
/// </summary>
private void OnExportToFileClicked()
{
var currentPatient = GetCurrentInputPatient();
if (string.IsNullOrEmpty(currentPatient.Name))
{
ConfirmDialog.Show("提示", "请先填写病人信息再导出", () => { });
return;
}
// 检查是否有会话数据
// bool hasSessionData = _sessionService?.IsSessionActive ?? false;
bool hasSessionData = false;
string dialogTitle = hasSessionData ? "导出完整患者数据" : "导出患者基本信息";
string defaultFileName = hasSessionData ?
$"{currentPatient.Name}_完整数据_{System.DateTime.Now:yyyyMMdd_HHmmss}.json" :
$"{currentPatient.Name}_基本信息.json";
// 显示保存文件对话框
FileSelectionDialog.ShowSave(
dialogTitle,
"JSON文件|*.json|CSV文件|*.csv",
defaultFileName,
(filePath) => {
if (!string.IsNullOrEmpty(filePath))
{
if (hasSessionData && _patientService != null)
{
var success = _patientService.ExportCompletePatientData(currentPatient, filePath);
var message = success ? "完整患者数据导出成功" : "完整患者数据导出失败";
ConfirmDialog.Show(success ? "成功" : "错误", message, () => { });
}
else
{
ExportPatientToFile(currentPatient, filePath);
}
}
},
() => Debug.Log("取消文件保存")
);
}
private void LoadPatientFromFile(string filePath)
{
if (_patientService != null)
{
var patient = _patientService.LoadPatientFromFile(filePath);
if (patient != null)
{
PopulateFields(patient);
ConfirmDialog.Show("成功", $"成功加载病人信息:{patient.Name}", () => { });
}
else
{
ConfirmDialog.Show("错误", "文件加载失败,请检查文件格式", () => { });
}
}
}
private void ExportPatientToFile(PatientInfo patient, string filePath)
{
if (_patientService != null)
{
bool success = _patientService.ExportPatientToFile(patient, filePath);
if (success)
{
ConfirmDialog.Show("成功", "病人信息导出成功", () => { });
}
else
{
ConfirmDialog.Show("错误", "文件导出失败", () => { });
}
}
}
private PatientInfo GetCurrentInputPatient()
{
return new PatientInfo
{
Name = NameInput?.text ?? "",
HospitalId = AdmissionIdInput?.text ?? "",
Gender = GenderDropdown?.value == 1 ? "女" : "男",
Age = int.TryParse(AgeInput?.text ?? "0", out int age) ? age : 0,
Height = float.TryParse(HeightInput?.text ?? "0", out float height) ? height : 0f,
Weight = float.TryParse(WeightInput?.text ?? "0", out float weight) ? weight : 0f,
// IdNumber = IdNumberInput?.text ?? ""
};
}
/// <summary>
/// 初始化Tab导航
/// </summary>
private void InitializeTabNavigation()
{
// 添加或获取Tab导航组件
_tabNavigation = GetComponent<TabNavigationSystem>();
if (_tabNavigation == null)
{
_tabNavigation = gameObject.AddComponent<TabNavigationSystem>();
}
// 手动设置导航顺序
_tabNavigation.NavigationOrder.Clear();
_tabNavigation.AutoDetectInputFields = false; // 手动设置顺序
// 按逻辑顺序添加InputField
if (NameInput != null) _tabNavigation.AddNavigationElement(NameInput);
if (AdmissionIdInput != null) _tabNavigation.AddNavigationElement(AdmissionIdInput);
if (GenderDropdown != null) _tabNavigation.AddNavigationElement(GenderDropdown);
if (AgeInput != null) _tabNavigation.AddNavigationElement(AgeInput);
if (HeightInput != null) _tabNavigation.AddNavigationElement(HeightInput);
if (WeightInput != null) _tabNavigation.AddNavigationElement(WeightInput);
// if (IdNumberInput != null) _tabNavigation.AddNavigationElement(IdNumberInput);
Debug.Log($"PatientInfoPanel: 初始化Tab导航共 {_tabNavigation.NavigationOrder.Count} 个输入框");
}
private void OnPatientInfoSaved(bool success, string message)
{
if (success)
{
// 保存成功,返回主界面
ClosePanel();
}
else
{
// 显示错误信息
ConfirmDialog.Show("保存病人信息失败:", message, () => { });
// Debug.LogError($"保存病人信息失败: {message}");
}
}
private void PopulateFields(PatientInfo info)
{
if (NameInput != null) NameInput.text = info.Name ?? "";
if (AdmissionIdInput != null) AdmissionIdInput.text = info.HospitalId ?? "";
if (GenderDropdown != null) GenderDropdown.value = info.Gender == "女" ? 1 : 0;
if (AgeInput != null) AgeInput.text = info.Age == 0 ? "" : info.Age.ToString();
if (HeightInput != null) HeightInput.text = info.Height == 0 ? "" : info.Height.ToString();
if (WeightInput != null) WeightInput.text = info.Weight == 0 ? "" : info.Weight.ToString();
// if (IdNumberInput != null) IdNumberInput.text = info.IdNumber ?? "";
}
private void ClearFields()
{
if (NameInput != null) NameInput.text = "";
if (AdmissionIdInput != null) AdmissionIdInput.text = "";
if (GenderDropdown != null) GenderDropdown.value = 0;
if (AgeInput != null) AgeInput.text = "";
if (HeightInput != null) HeightInput.text = "";
if (WeightInput != null) WeightInput.text = "";
}
// public void ClosePanel()
// {
// UIManager.Instance.HidePanel<PatientInfoPanel>();
// }
#region
/// <summary>
/// 开始患者会话
/// </summary>
private void OnStartSessionClicked()
{
var currentPatient = GetCurrentInputPatient();
if (string.IsNullOrEmpty(currentPatient.Name))
{
ConfirmDialog.Show("提示", "请先填写患者基本信息", () => { });
return;
}
var currentUser = _authService?.CurrentUser;
if (currentUser == null)
{
ConfirmDialog.Show("错误", "未登录管理员账号,无法开始会话", () => { });
return;
}
// if (_sessionService != null)
// {
// bool success = _sessionService.StartSession(currentPatient, currentUser.Username);
// if (success)
// {
// ConfirmDialog.Show("成功", $"已为患者 {currentPatient.Name} 开始会话", () => { });
// UpdateSessionUI();
// }
// else
// {
// ConfirmDialog.Show("错误", "开始会话失败", () => { });
// }
// }
}
/// <summary>
/// 结束患者会话
/// </summary>
// private void OnEndSessionClicked()
// {
// ConfirmDialog.Show("确认", "确定要结束当前患者会话吗?",
// () =>
// {
// if (_sessionService != null)
// {
// _sessionService.EndSession();
// UpdateSessionUI();
// }
// });
// }
/// <summary>
/// 会话开始事件处理
/// </summary>
private void OnSessionStarted(PatientSession session)
{
Debug.Log($"会话已开始: {session.PatientInfo.Name}");
// UpdateSessionUI();
}
/// <summary>
/// 会话结束事件处理
/// </summary>
private void OnSessionEnded(PatientSession session)
{
Debug.Log($"会话已结束: {session.PatientInfo.Name}");
// UpdateSessionUI();
}
/// <summary>
/// 更新UI状态
/// </summary>
// private void UpdateUI()
// {
// UpdateSessionUI();
// }
/// <summary>
/// 更新会话相关UI
/// </summary>
// private void UpdateSessionUI()
// {
// bool hasActiveSession = _sessionService?.IsSessionActive ?? false;
// 显示/隐藏会话状态面板
// if (SessionStatusPanel != null)
// {
// SessionStatusPanel.SetActive(hasActiveSession);
// }
// if (hasActiveSession)
// {
// var session = _sessionService.CurrentSession;
// if (SessionStatusText != null)
// {
// SessionStatusText.text = $"会话状态: {GetSessionStatusText(session.Status)}";
// }
// if (AdminUserText != null)
// {
// AdminUserText.text = $"管理员: {session.AdminUserId}";
// }
// }
// 控制按钮状态
// if (StartSessionButton != null)
// {
// StartSessionButton.interactable = !hasActiveSession;
// }
// if (EndSessionButton != null)
// {
// EndSessionButton.interactable = hasActiveSession;
// }
// }
/// <summary>
/// 获取会话状态文本
/// </summary>
private string GetSessionStatusText(SessionStatus status)
{
switch (status)
{
case SessionStatus.Started: return "已开始";
case SessionStatus.Measuring: return "测量中";
case SessionStatus.MeasurementComplete: return "测量完成";
case SessionStatus.Completed: return "已完成";
default: return "未知";
}
}
#endregion
#region
/// <summary>
/// 禁用InputField的系统键盘,改用自定义键盘
/// </summary>
private void DisableSystemKeyboardForInputFields()
{
// 为不同输入框设置不同的键盘布局
// 姓名 - 拼音键盘(支持中文)
if (NameInput != null)
CustomKeyboardManager.SetupInputField(NameInput, KeyboardLayout.Pinyin);
// else
// Debug.LogWarning("PatientInfoPanel: NameInput is null!");
// 住院号 - 默认键盘(字母+数字)
if (AdmissionIdInput != null)
CustomKeyboardManager.SetupInputField(AdmissionIdInput, KeyboardLayout.Default);
else
Debug.LogWarning("PatientInfoPanel: AdmissionIdInput is null!");
// 年龄 - 数字键盘
if (AgeInput != null)
{
CustomKeyboardManager.SetupInputField(AgeInput, KeyboardLayout.Numeric, (value) => {
if (!string.IsNullOrEmpty(value) && !int.TryParse(value, out _))
{
ConfirmDialog.Show("提示", "年龄必须是数字", () => { });
AgeInput.text = "";
}
});
}
else
Debug.LogWarning("PatientInfoPanel: AgeInput is null!");
// 身高 - 数字键盘
if (HeightInput != null)
{
CustomKeyboardManager.SetupInputField(HeightInput, KeyboardLayout.Numeric, (value) => {
if (!string.IsNullOrEmpty(value) && !float.TryParse(value, out _))
{
ConfirmDialog.Show("提示", "身高必须是数字", () => { });
HeightInput.text = "";
}
});
}
else
Debug.LogWarning("PatientInfoPanel: HeightInput is null!");
// 体重 - 数字键盘
if (WeightInput != null)
{
CustomKeyboardManager.SetupInputField(WeightInput, KeyboardLayout.Numeric, (value) => {
if (!string.IsNullOrEmpty(value) && !float.TryParse(value, out _))
{
ConfirmDialog.Show("提示", "体重必须是数字", () => { });
WeightInput.text = "";
}
});
}
else
Debug.LogWarning("PatientInfoPanel: WeightInput is null!");
}
#endregion
void OnDestroy()
{
// 清理事件订阅
if (_vm != null)
_vm.OnPatientInfoSaved -= OnPatientInfoSaved;
// if (_sessionService != null)
// {
// _sessionService.OnSessionStarted -= OnSessionStarted;
// _sessionService.OnSessionEnded -= OnSessionEnded;
// }
// 关闭自定义键盘(如果正在显示)
if (CustomKeyboardManager.Instance != null && CustomKeyboardManager.Instance.IsKeyboardShowing())
{
CustomKeyboardManager.Instance.HideKeyboard();
}
}
}