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

#include "stdafx.h"
#include "Verbpo.h"
#include "DialogRespSet.h"
#include "afxdialogex.h"
#include "VerbpoDlg.h"
#include "StaticValueRespNormal.h"
#include "UartDataThread.h"

// CDialogRespSet 对话框

IMPLEMENT_DYNAMIC(CDialogRespSet, CDialogEx)

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

}

CDialogRespSet::~CDialogRespSet()
{
}

void CDialogRespSet::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_RESP_SET_ALM_SW, m_ComboRespAlmSw);
	DDX_Control(pDX, IDC_COMBO_RESP_SET_ALM_LEVEL, m_ComboRespAlmLevel);
	DDX_Control(pDX, IDC_COMBO_RESP_SET_ALM_REC, m_ComboRespAlmRec);
	DDX_Control(pDX, IDC_COMBO_RESP_SET_APNEA_TIME, m_ComboRespAPNEATime);
	DDX_Control(pDX, IDC_EDIT_RESP_SET_ALM_H, m_EditRRAlmH);
	DDX_Control(pDX, IDC_EDIT_RESP_SET_ALM_L, m_EditRRAlmL);
}


BEGIN_MESSAGE_MAP(CDialogRespSet, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_RESP_SET_DEFAULT, &CDialogRespSet::OnBnClickedButtonRespSetDefault)
	ON_BN_CLICKED(IDOK, &CDialogRespSet::OnBnClickedOk)
	ON_CBN_SELCHANGE(IDC_COMBO_RESP_SET_ALM_SW, &CDialogRespSet::OnCbnSelchangeComboRespSetAlmSw)
	ON_CBN_SELCHANGE(IDC_COMBO_RESP_SET_ALM_LEVEL, &CDialogRespSet::OnCbnSelchangeComboRespSetAlmLevel)
	ON_CBN_SELCHANGE(IDC_COMBO_RESP_SET_ALM_REC, &CDialogRespSet::OnCbnSelchangeComboRespSetAlmRec)
	ON_CBN_SELCHANGE(IDC_COMBO_RESP_SET_APNEA_TIME, &CDialogRespSet::OnCbnSelchangeComboRespSetApneaTime)
END_MESSAGE_MAP()


// CDialogRespSet 消息处理程序


void CDialogRespSet::OnBnClickedButtonRespSetDefault()
{
	// TODO:  在此添加控件通知处理程序代码
}


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

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

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

	m_ComboRespAlmRec.AddString(CString(_T("关")));
	m_ComboRespAlmRec.AddString(CString(_T("开")));
	m_ComboRespAlmRec.SetCurSel(theApp.m_Data->m_i32ParaRespALM_REC);

	m_ComboRespAPNEATime.AddString(CString(_T("关")));
	m_ComboRespAPNEATime.AddString(CString(_T("15s")));
	m_ComboRespAPNEATime.AddString(CString(_T("30s")));
	m_ComboRespAPNEATime.AddString(CString(_T("45s")));
	m_ComboRespAPNEATime.SetCurSel(theApp.m_Data->m_i32ParaRespAPNEA_TIME);

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

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

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


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


void CDialogRespSet::OnCbnSelchangeComboRespSetAlmSw()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaRespALM_SW = m_ComboRespAlmSw.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_RespAlmSw(theApp.m_Data->m_i32ParaRespALM_SW);
	((CVerbpoDlg *)theApp.m_pMainWnd)->m_RespValueNormal->Set_AlmFlag(theApp.m_Data->m_i32ParaRespALM_SW, 0);
	if (theApp.m_Data->m_i32ParaRespALM_SW == 0)
	{
		//报警 从 开 到 关
		theApp.m_Data->Del_Resp_Temp_Alm_ALL(ALM_TYPE_RESP, DEL_ALL);
	}
	else
	{
	}
}


void CDialogRespSet::OnCbnSelchangeComboRespSetAlmLevel()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaRespALM_LEVEL = m_ComboRespAlmLevel.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_RespAlmLevel(theApp.m_Data->m_i32ParaRespALM_LEVEL);
}


void CDialogRespSet::OnCbnSelchangeComboRespSetAlmRec()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaRespALM_REC = m_ComboRespAlmRec.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_RespAlmRec(theApp.m_Data->m_i32ParaRespALM_REC);
}


void CDialogRespSet::OnCbnSelchangeComboRespSetApneaTime()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaRespAPNEA_TIME = m_ComboRespAPNEATime.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_RespAPNEATime(theApp.m_Data->m_i32ParaRespAPNEA_TIME);

	//向下位机发送 窒息时间 命令
	((UartDataThread *)theApp.m_pThrdUart)->Send_CMD_APNEAAlm(theApp.m_Data->m_i32ParaRespAPNEA_TIME);
}


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

		m_EditRRAlmH.GetWindowTextW(a_cs);
		m_EditRRAlmH.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_RRAlmH_check(a_cs);
		theApp.m_Data->m_i32ParaRespRR_ALM_H = _ttoi(a_cs);
		theApp.m_Data->m_CParameterSet.set_RespRRAlmH(_ttoi(a_cs));
		m_EditRRAlmH.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_RespValueNormal->set_resp_alm_H(theApp.m_Data->m_i32ParaRespRR_ALM_H, 0);
		return TRUE;
	}
	else if (WM_LBUTTONDOWN == pMsg->message&& m_EditRRAlmL.GetSafeHwnd() == pMsg->hwnd)
	{

		m_EditRRAlmL.GetWindowTextW(a_cs);
		m_EditRRAlmL.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_RRAlmL_check(a_cs);
		theApp.m_Data->m_i32ParaRespRR_ALM_L = _ttoi(a_cs);
		theApp.m_Data->m_CParameterSet.set_RespRRAlmL(_ttoi(a_cs));
		m_EditRRAlmL.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_RespValueNormal->set_resp_alm_L(theApp.m_Data->m_i32ParaRespRR_ALM_L, 0);
		return TRUE;
	}
	return CDialogEx::PreTranslateMessage(pMsg);
}

//检测呼吸率报警上限阈值是否正确
CString CDialogRespSet::input_RRAlmH_check(CString & cs)
{
	int a = 0;
	CString a_cs;

	a = _ttoi(cs);

	if (a>CData::m_i16RRAlmHMax)
	{
		a = CData::m_i16RRAlmHMax;
	}
	if (a<CData::m_i16RRAlmHMin)
	{
		a = CData::m_i16RRAlmHMin;
	}
	if (a <= theApp.m_Data->m_i32ParaRespRR_ALM_L)
	{
		a = theApp.m_Data->m_i32ParaRespRR_ALM_L + 3;
	}
	
	a_cs.Format(_T("%d"), a);

	return a_cs;
}

//检测呼吸率报警下限阈值是否正确
CString CDialogRespSet::input_RRAlmL_check(CString & cs)
{
	int a = 0;
	CString a_cs;

	a = _ttoi(cs);

	if (a<CData::m_i16RRAlmLMin)
	{
		a = CData::m_i16RRAlmLMin;
	}
	if (a > CData::m_i16RRAlmLMax)
	{
		a = CData::m_i16RRAlmLMax;
	}

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

	return a_cs;
}