// DialogOXYSet.cpp : 实现文件
//

#include "stdafx.h"
#include "Verbpo.h"
#include "DialogOXYSet.h"
#include "afxdialogex.h"
#include "VerbpoDlg.h"

// CDialogOXYSet 对话框

IMPLEMENT_DYNAMIC(CDialogOXYSet, CDialogEx)

CDialogOXYSet::CDialogOXYSet(CWnd* pParent /*=NULL*/)
	: CDialogEx(CDialogOXYSet::IDD, pParent)
{

}

CDialogOXYSet::~CDialogOXYSet()
{
}

void CDialogOXYSet::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_OXY_SET_ALM_SW, m_ComboxOxyAlmSw);
	DDX_Control(pDX, IDC_COMBO_OXY_SET_ALM_LEVEL, m_ComboxOxyAlmLevel);
	DDX_Control(pDX, IDC_COMBO_OXY_SET_ALM_REC, m_ComboxOxyAlmRec);
	DDX_Control(pDX, IDC_EDIT_OXY_SET_OXY_ALM_H, m_EditOxyAlmH);
	DDX_Control(pDX, IDC_EDIT_OXY_SET_OXY_ALM_L, m_EditOxyAlmL);
	DDX_Control(pDX, IDC_EDIT_OXY_SET_PR_ALM_H2, m_EditOxyPRAlmH);
	DDX_Control(pDX, IDC_EDIT_OXY_SET_PR_ALM_L2, m_EditOxyPRAlmL);
}


BEGIN_MESSAGE_MAP(CDialogOXYSet, CDialogEx)
	ON_BN_CLICKED(IDOK, &CDialogOXYSet::OnBnClickedOk)
	ON_CBN_SELCHANGE(IDC_COMBO_OXY_SET_ALM_SW, &CDialogOXYSet::OnCbnSelchangeComboOxySetAlmSw)
	ON_CBN_SELCHANGE(IDC_COMBO_OXY_SET_ALM_LEVEL, &CDialogOXYSet::OnCbnSelchangeComboOxySetAlmLevel)
	ON_CBN_SELCHANGE(IDC_COMBO_OXY_SET_ALM_REC, &CDialogOXYSet::OnCbnSelchangeComboOxySetAlmRec)
END_MESSAGE_MAP()


// CDialogOXYSet 消息处理程序


void CDialogOXYSet::OnBnClickedOk()
{
	// TODO:  在此添加控件通知处理程序代码
	CDialogEx::OnOK();
}


BOOL CDialogOXYSet::OnInitDialog()
{
	CString a_cs;
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_ComboxOxyAlmSw.AddString(CString(_T("关")));
	m_ComboxOxyAlmSw.AddString(CString(_T("开")));
	m_ComboxOxyAlmSw.SetCurSel(theApp.m_Data->m_i32ParaOxyALM_SW);

	m_ComboxOxyAlmLevel.AddString(CString(_T("低")));
	m_ComboxOxyAlmLevel.AddString(CString(_T("中")));
	m_ComboxOxyAlmLevel.AddString(CString(_T("高")));
	m_ComboxOxyAlmLevel.SetCurSel(theApp.m_Data->m_i32ParaOxyALM_LEVEL);

	m_ComboxOxyAlmRec.AddString(CString(_T("关")));
	m_ComboxOxyAlmRec.AddString(CString(_T("开")));
	m_ComboxOxyAlmRec.SetCurSel(theApp.m_Data->m_i32ParaOxyALM_REC);

	a_cs.Format(_T("%d"), theApp.m_Data->m_i32ParaOxyOXY_ALM_H);
	m_EditOxyAlmH.SetWindowTextW(a_cs);

	a_cs.Format(_T("%d"), theApp.m_Data->m_i32ParaOxyOXY_ALM_L);
	m_EditOxyAlmL.SetWindowTextW(a_cs);

	a_cs.Format(_T("%d"), theApp.m_Data->m_i32ParaOxyOXY_PR_ALM_H);
	m_EditOxyPRAlmH.SetWindowTextW(a_cs);

	a_cs.Format(_T("%d"), theApp.m_Data->m_i32ParaOxyOXY_PR_ALM_L);
	m_EditOxyPRAlmL.SetWindowTextW(a_cs);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常:  OCX 属性页应返回 FALSE
}


void CDialogOXYSet::OnCbnSelchangeComboOxySetAlmSw()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaOxyALM_SW = m_ComboxOxyAlmSw.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_OxyAlmSw(theApp.m_Data->m_i32ParaOxyALM_SW);
	((CVerbpoDlg *)theApp.m_pMainWnd)->m_SPO2ValueNormal->Set_AlmFlag(theApp.m_Data->m_i32ParaOxyALM_SW, 0);
	if (theApp.m_Data->m_i32ParaOxyALM_SW == 0)
	{
		//报警 从 开 到 关
		theApp.m_Data->Del_Oxy_NIBP_Alm_ALL(ALM_TYPE_OXY, DEL_ALL);
	}
	else
	{
	}
}


void CDialogOXYSet::OnCbnSelchangeComboOxySetAlmLevel()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaOxyALM_LEVEL = m_ComboxOxyAlmLevel.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_OxyAlmLevel(theApp.m_Data->m_i32ParaOxyALM_LEVEL);
}


void CDialogOXYSet::OnCbnSelchangeComboOxySetAlmRec()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaOxyALM_REC = m_ComboxOxyAlmRec.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_OxyAlmRec(theApp.m_Data->m_i32ParaOxyALM_REC);
}


BOOL CDialogOXYSet::PreTranslateMessage(MSG* pMsg)
{
	CRect     rect;
	CString a_cs;
	// TODO:  在此添加专用代码和/或调用基类
	if (WM_LBUTTONDOWN == pMsg->message&& m_EditOxyAlmH.GetSafeHwnd() == pMsg->hwnd)
	{
		//输入血氧饱和度的上限
		m_EditOxyAlmH.GetWindowTextW(a_cs);
		m_EditOxyAlmH.GetWindowRect(&rect);       //获得文本框矩形,左上角坐标(0,0)   
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_type(NUM_INPUT_NO_DOT | NUM_INPUT_NO_SYMBOL);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_title(CString(_T("血氧饱和度上限")));
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_result(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_pos(rect.left, rect.top);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.DoModal();
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.get_result(a_cs);
		a_cs = input_OxyAlmH_check(a_cs);
		theApp.m_Data->m_i32ParaOxyOXY_ALM_H = _ttoi(a_cs);
		theApp.m_Data->m_CParameterSet.set_OxyAlmH(_ttoi(a_cs));
		m_EditOxyAlmH.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_SPO2ValueNormal->set_spo2_alm_H(theApp.m_Data->m_i32ParaOxyOXY_ALM_H, 0);
		return TRUE;
	}
	else if (WM_LBUTTONDOWN == pMsg->message&& m_EditOxyAlmL.GetSafeHwnd() == pMsg->hwnd)
	{
		//输入血氧饱和度的下限
		m_EditOxyAlmL.GetWindowTextW(a_cs);
		m_EditOxyAlmL.GetWindowRect(&rect);       //获得文本框矩形,左上角坐标(0,0)   
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_type(NUM_INPUT_NO_DOT | NUM_INPUT_NO_SYMBOL);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_title(CString(_T("血氧饱和度下限")));
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_result(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_pos(rect.left, rect.top);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.DoModal();
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.get_result(a_cs);
		a_cs = input_OxyAlmL_check(a_cs);
		theApp.m_Data->m_i32ParaOxyOXY_ALM_L = _ttoi(a_cs);
		theApp.m_Data->m_CParameterSet.set_OxyAlmL(_ttoi(a_cs));
		m_EditOxyAlmL.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_SPO2ValueNormal->set_spo2_alm_L(theApp.m_Data->m_i32ParaOxyOXY_ALM_L, 0);
		return TRUE;
	}
	else if (WM_LBUTTONDOWN == pMsg->message&& m_EditOxyPRAlmH.GetSafeHwnd() == pMsg->hwnd)
	{

		m_EditOxyPRAlmH.GetWindowTextW(a_cs);
		m_EditOxyPRAlmH.GetWindowRect(&rect);       //获得文本框矩形,左上角坐标(0,0)   
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_type(NUM_INPUT_NO_DOT | NUM_INPUT_NO_SYMBOL);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_title(CString(_T("脉率上限")));
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_result(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_pos(rect.left, rect.top);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.DoModal();
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.get_result(a_cs);
		a_cs = input_PRAlmH_check(a_cs);
		theApp.m_Data->m_i32ParaOxyOXY_PR_ALM_H = _ttoi(a_cs);
		theApp.m_Data->m_CParameterSet.set_OxyPRAlmH(_ttoi(a_cs));
		m_EditOxyPRAlmH.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_SPO2ValueNormal->set_PR_alm_H(theApp.m_Data->m_i32ParaOxyOXY_PR_ALM_H, 0);
		return TRUE;
	}
	else if (WM_LBUTTONDOWN == pMsg->message&& m_EditOxyPRAlmL.GetSafeHwnd() == pMsg->hwnd)
	{

		m_EditOxyPRAlmL.GetWindowTextW(a_cs);
		m_EditOxyPRAlmL.GetWindowRect(&rect);       //获得文本框矩形,左上角坐标(0,0)   
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_type(NUM_INPUT_NO_DOT | NUM_INPUT_NO_SYMBOL);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_title(CString(_T("脉率下限")));
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_result(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_pos(rect.left, rect.top);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.DoModal();
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.get_result(a_cs);
		a_cs = input_PRAlmL_check(a_cs);
		theApp.m_Data->m_i32ParaOxyOXY_PR_ALM_L = _ttoi(a_cs);
		theApp.m_Data->m_CParameterSet.set_OxyPRAlmL(_ttoi(a_cs));
		m_EditOxyPRAlmL.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_SPO2ValueNormal->set_PR_alm_L(theApp.m_Data->m_i32ParaOxyOXY_PR_ALM_L, 0);
		return TRUE;
	}
	return CDialogEx::PreTranslateMessage(pMsg);
}

//检测血氧饱和度报警上限阈值是否正确
CString CDialogOXYSet::input_OxyAlmH_check(CString & cs)
{
	int a = 0;
	CString a_cs;

	a = _ttoi(cs);

	if (a>CData::m_i16Spo2AlmHMax)
	{
		a = CData::m_i16Spo2AlmHMax;
	}
	if (a<CData::m_i16Spo2AlmHMin)
	{
		a = CData::m_i16Spo2AlmHMin;
	}

	if (a <= theApp.m_Data->m_i32ParaOxyOXY_ALM_L)
	{
		a = theApp.m_Data->m_i32ParaOxyOXY_ALM_L + 3;
	}
	
	a_cs.Format(_T("%d"), a);

	return a_cs;
}

//检测血氧饱和度报警下限阈值是否正确
CString CDialogOXYSet::input_OxyAlmL_check(CString & cs)
{
	int a = 0;
	CString a_cs;

	a = _ttoi(cs);

	if (a>CData::m_i16Spo2AlmLMax)
	{
		a = CData::m_i16Spo2AlmLMax;
	}
	if (a<CData::m_i16Spo2AlmLMin)
	{
		a = CData::m_i16Spo2AlmLMin;
	}

	if (a >= theApp.m_Data->m_i32ParaOxyOXY_ALM_H)
	{
		a = theApp.m_Data->m_i32ParaOxyOXY_ALM_H - 3;
	}
	
	a_cs.Format(_T("%d"), a);

	return a_cs;
}

//检测脉率报警上限阈值是否正确
CString CDialogOXYSet::input_PRAlmH_check(CString & cs)
{
	int a = 0;
	CString a_cs;

	a = _ttoi(cs);

	if (a>CData::m_i16PRAlmHMax)
	{
		a = CData::m_i16PRAlmHMax;
	}
	if (a<CData::m_i16PRAlmHMin)
	{
		a = CData::m_i16PRAlmHMin;
	}

	if (a <= theApp.m_Data->m_i32ParaOxyOXY_PR_ALM_L)
	{
		a = theApp.m_Data->m_i32ParaOxyOXY_PR_ALM_L + 3;
	}

	a_cs.Format(_T("%d"), a);

	return a_cs;
}

//检测脉率报警下限阈值是否正确
CString CDialogOXYSet::input_PRAlmL_check(CString & cs){
	int a = 0;
	CString a_cs;

	a = _ttoi(cs);

	if (a>CData::m_i16PRAlmLMax)
	{
		a = CData::m_i16PRAlmLMax;
	}
	if (a<CData::m_i16PRAlmLMin)
	{
		a = CData::m_i16PRAlmLMin;
	}

	if (a >= theApp.m_Data->m_i32ParaOxyOXY_PR_ALM_H)
	{
		a = theApp.m_Data->m_i32ParaOxyOXY_PR_ALM_H - 1;
	}

	a_cs.Format(_T("%d"), a);

	return a_cs;
}