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

#include "stdafx.h"
#include "Verbpo.h"
#include "DialogBPSet.h"
#include "afxdialogex.h"
#include "VerbpoDlg.h"
#include "UartDataThread.h"

// CDialogBPSet 对话框

IMPLEMENT_DYNAMIC(CDialogBPSet, CDialogEx)

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

}

CDialogBPSet::~CDialogBPSet()
{
}

void CDialogBPSet::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_BP_SET_ALM_SW, m_ComboBPAlmSw);
	DDX_Control(pDX, IDC_COMBO_BP_SET_ALM_LEVEL, m_ComboBPAlmLevel);
	DDX_Control(pDX, IDC_COMBO_BP_SET_ALM_REC, m_ComboBPAlmRec);
	DDX_Control(pDX, IDC_EDIT_BP_SET_ALM_SBP_H, m_EditAlmSBPH);
	DDX_Control(pDX, IDC_EDIT_BP_SET_ALM_SBP_L, m_EditAlmSBPL);
	DDX_Control(pDX, IDC_EDIT_BP_SET_ALM_DBP_H, m_EditAlmDBPH);
	DDX_Control(pDX, IDC_EDIT_BP_SET_ALM_DBP_L, m_EditAlmDBPL);
	DDX_Control(pDX, IDC_EDIT_BP_SET_PRESS, m_EditPress);
	DDX_Control(pDX, IDC_COMBO_TEMP_SET_UNIT, m_ComboBPUnit);
}


BEGIN_MESSAGE_MAP(CDialogBPSet, CDialogEx)
	ON_CBN_SELCHANGE(IDC_COMBO_BP_SET_ALM_SW, &CDialogBPSet::OnCbnSelchangeComboBpSetAlmSw)
	ON_CBN_SELCHANGE(IDC_COMBO_BP_SET_ALM_LEVEL, &CDialogBPSet::OnCbnSelchangeComboBpSetAlmLevel)
	ON_CBN_SELCHANGE(IDC_COMBO_BP_SET_ALM_REC, &CDialogBPSet::OnCbnSelchangeComboBpSetAlmRec)
	ON_BN_CLICKED(IDOK, &CDialogBPSet::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON_BP_SET_RESET, &CDialogBPSet::OnBnClickedButtonBpSetReset)
	ON_BN_CLICKED(IDC_BUTTON_BP_SET_CAL, &CDialogBPSet::OnBnClickedButtonBpSetCal)
	ON_BN_CLICKED(IDC_BUTTON_BP_SET_LEAK, &CDialogBPSet::OnBnClickedButtonBpSetLeak)
END_MESSAGE_MAP()


// CDialogBPSet 消息处理程序


BOOL CDialogBPSet::PreTranslateMessage(MSG* pMsg)
{
	// TODO:  在此添加专用代码和/或调用基类
	CRect     rect;
	CString a_cs;
	// TODO:  在此添加专用代码和/或调用基类
	if (WM_LBUTTONDOWN == pMsg->message&& m_EditAlmSBPH.GetSafeHwnd() == pMsg->hwnd)
	{

		m_EditAlmSBPH.GetWindowTextW(a_cs);
		m_EditAlmSBPH.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_SBPAlmH_check(a_cs);
		theApp.m_Data->m_i32ParaBPSBP_ALM_H = _ttoi(a_cs);
		theApp.m_Data->m_CParameterSet.set_BPSBPAlmH(_ttoi(a_cs));
		m_EditAlmSBPH.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_BPValueNormal->set_alm_H(theApp.m_Data->m_i32ParaBPSBP_ALM_H, theApp.m_Data->m_i32ParaBPDBP_ALM_H, 0);
		return TRUE;
	}
	else if (WM_LBUTTONDOWN == pMsg->message&& m_EditAlmSBPL.GetSafeHwnd() == pMsg->hwnd)
	{

		m_EditAlmSBPL.GetWindowTextW(a_cs);
		m_EditAlmSBPL.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_SBPAlmL_check(a_cs);
		theApp.m_Data->m_i32ParaBPSBP_ALM_L = _ttoi(a_cs);
		theApp.m_Data->m_CParameterSet.set_BPSBPAlmL(_ttoi(a_cs));
		m_EditAlmSBPL.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_BPValueNormal->set_alm_L(theApp.m_Data->m_i32ParaBPSBP_ALM_L, theApp.m_Data->m_i32ParaBPDBP_ALM_L, 0);
		return TRUE;
	}
	else if (WM_LBUTTONDOWN == pMsg->message&& m_EditAlmDBPH.GetSafeHwnd() == pMsg->hwnd)
	{

		m_EditAlmDBPH.GetWindowTextW(a_cs);
		m_EditAlmDBPH.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_DBPAlmH_check(a_cs);
		theApp.m_Data->m_i32ParaBPDBP_ALM_H = _ttoi(a_cs);
		theApp.m_Data->m_CParameterSet.set_BPDBPAlmH(_ttoi(a_cs));
		m_EditAlmDBPH.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_BPValueNormal->set_alm_H(theApp.m_Data->m_i32ParaBPSBP_ALM_H, theApp.m_Data->m_i32ParaBPDBP_ALM_H, 0);
		return TRUE;
	}
	else if (WM_LBUTTONDOWN == pMsg->message&& m_EditAlmDBPL.GetSafeHwnd() == pMsg->hwnd)
	{

		m_EditAlmDBPL.GetWindowTextW(a_cs);
		m_EditAlmDBPL.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_DBPAlmL_check(a_cs);
		theApp.m_Data->m_i32ParaBPDBP_ALM_L = _ttoi(a_cs);
		theApp.m_Data->m_CParameterSet.set_BPDBPAlmL(_ttoi(a_cs));
		m_EditAlmDBPL.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_BPValueNormal->set_alm_L(theApp.m_Data->m_i32ParaBPSBP_ALM_L, theApp.m_Data->m_i32ParaBPDBP_ALM_L, 0);
		return TRUE;
	}
	else if (WM_LBUTTONDOWN == pMsg->message&& m_EditPress.GetSafeHwnd() == pMsg->hwnd)
	{

		m_EditPress.GetWindowTextW(a_cs);
		m_EditPress.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_Press_check(a_cs);
		theApp.m_Data->m_i32ParaBPPress = _ttoi(a_cs);
		theApp.m_Data->m_CParameterSet.set_Press(_ttoi(a_cs));
		m_EditPress.SetWindowTextW(a_cs);

		//向下位机发送 呼吸增益 命令
		((UartDataThread *)theApp.m_pThrdUart)->Send_CMD_NBPPrePressure(0,theApp.m_Data->m_i32ParaBPPress);
		return TRUE;
	}

	return CDialogEx::PreTranslateMessage(pMsg);
}

CString CDialogBPSet::input_SBPAlmH_check(CString & cs)
{
	int a = 0;
	CString a_cs;
	a = _ttoi(cs);

	if (a>CData::m_i16SBPAlmHMax)
	{
		a = CData::m_i16SBPAlmHMax;
	}
	if (a<CData::m_i16SBPAlmHMin)
	{
		a = CData::m_i16SBPAlmHMin;
	}

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

	return a_cs;
}


CString CDialogBPSet::input_SBPAlmL_check(CString & cs)
{
	int a = 0;
	CString a_cs;
	a = _ttoi(cs);

	if (a>CData::m_i16SBPAlmLMax)
	{
		a = CData::m_i16SBPAlmLMax;
	}
	if (a<CData::m_i16SBPAlmLMin)
	{
		a = CData::m_i16SBPAlmLMin;
	}

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

	if (a >= theApp.m_Data->m_i32ParaBPSBP_ALM_H)
	{
		a = theApp.m_Data->m_i32ParaBPSBP_ALM_H - 3;
	}

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

	return a_cs;
}


CString CDialogBPSet::input_DBPAlmH_check(CString & cs)
{
	int a = 0;
	CString a_cs;

	a = _ttoi(cs);

	if (a>CData::m_i16DBPAlmHMax)
	{
		a = CData::m_i16DBPAlmHMax;
	}
	if (a<CData::m_i16DBPAlmHMin)
	{
		a = CData::m_i16DBPAlmHMin;
	}

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

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

	return a_cs;
}

CString CDialogBPSet::input_DBPAlmL_check(CString & cs)
{
	int a = 0;
	CString a_cs;

	a = _ttoi(cs);

	if (a>CData::m_i16DBPAlmLMax)
	{
		a = CData::m_i16DBPAlmLMax;
	}
	if (a<CData::m_i16DBPAlmLMin)
	{
		a = CData::m_i16DBPAlmLMin;
	}

	if (a >= theApp.m_Data->m_i32ParaBPSBP_ALM_L)
	{
		a = theApp.m_Data->m_i32ParaBPSBP_ALM_L - 3;
	}

	if (a >= theApp.m_Data->m_i32ParaBPDBP_ALM_H)
	{
		a = theApp.m_Data->m_i32ParaBPDBP_ALM_H - 3;
	}

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


	return a_cs;
}

CString  CDialogBPSet::input_Press_check(CString & cs)
{
	int a = 0;
	CString a_cs;
	a = _ttoi(cs);


	if (a<30)
	{
		a = 30;
	}
	if (a > BP_PRESS)
	{
		a = BP_PRESS;
	}
	a_cs.Format(_T("%d"), a);
	return a_cs;
}

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

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

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

	m_ComboBPAlmRec.AddString(CString(_T("关")));
	m_ComboBPAlmRec.AddString(CString(_T("开")));
	m_ComboBPAlmRec.SetCurSel(theApp.m_Data->m_i32ParaBPALM_REC);

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

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

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

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

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

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


void CDialogBPSet::OnCbnSelchangeComboBpSetAlmSw()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaBPALM_SW = m_ComboBPAlmSw.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_BPAlmSw(theApp.m_Data->m_i32ParaBPALM_SW);
	((CVerbpoDlg *)theApp.m_pMainWnd)->m_BPValueNormal->Set_AlmFlag(theApp.m_Data->m_i32ParaBPALM_SW, 0);
	if (theApp.m_Data->m_i32ParaBPALM_SW == 0)
	{
		//报警 从 开 到 关
		theApp.m_Data->Del_Oxy_NIBP_Alm_ALL(ALM_TYPE_NIBP, DEL_ALL);
	}
	else
	{
	}
}


void CDialogBPSet::OnCbnSelchangeComboBpSetAlmLevel()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaBPALM_LEVEL = m_ComboBPAlmLevel.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_BPAlmLevel(theApp.m_Data->m_i32ParaBPALM_LEVEL);
}


void CDialogBPSet::OnCbnSelchangeComboBpSetAlmRec()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaBPALM_REC = m_ComboBPAlmRec.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_BPAlmRec(theApp.m_Data->m_i32ParaBPALM_REC);
}


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


void CDialogBPSet::OnBnClickedButtonBpSetReset()
{
	// TODO:  在此添加控件通知处理程序代码
	//向下位机发送 血压复位 命令
	((UartDataThread *)theApp.m_pThrdUart)->Send_CMD_NBPReset();
}


void CDialogBPSet::OnBnClickedButtonBpSetCal()
{
	// TODO:  在此添加控件通知处理程序代码
	//向下位机发送 血压校准 命令
	((UartDataThread *)theApp.m_pThrdUart)->Send_CMD_NBPCal();
}


void CDialogBPSet::OnBnClickedButtonBpSetLeak()
{
	// TODO:  在此添加控件通知处理程序代码
	//向下位机发送 漏气检测 命令
	((UartDataThread *)theApp.m_pThrdUart)->Send_CMD_NBPPneumatic();
}
