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

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

// CDialogTempSet 对话框

IMPLEMENT_DYNAMIC(CDialogTempSet, CDialogEx)

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

}

CDialogTempSet::~CDialogTempSet()
{
}

void CDialogTempSet::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_TEMP_SET_ALM_SW, m_ComboTempSetAlmSw);
	DDX_Control(pDX, IDC_COMBO_TEMP_SET_ALM_LEVEL, m_ComboTempSetAlmLevel);
	DDX_Control(pDX, IDC_COMBO_TEMP_SET_ALM_REC, m_ComboTempSetAlmRec);
	DDX_Control(pDX, IDC_EDIT_TEMP_SET_ALM_H, m_EditTempSetAlmH);
	DDX_Control(pDX, IDC_EDIT_TEMP_SET_ALM_L, m_EditTempSetAlmL);
	DDX_Control(pDX, IDC_COMBO_TEMP_SET_UNIT, m_ComboTempSetUnit);
}


BEGIN_MESSAGE_MAP(CDialogTempSet, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_TEMP_SET_DEFAULT, &CDialogTempSet::OnBnClickedButtonTempSetDefault)
	ON_BN_CLICKED(IDOK, &CDialogTempSet::OnBnClickedOk)
	ON_CBN_SELCHANGE(IDC_COMBO_TEMP_SET_ALM_SW, &CDialogTempSet::OnCbnSelchangeComboTempSetAlmSw)
	ON_CBN_SELCHANGE(IDC_COMBO_TEMP_SET_ALM_LEVEL, &CDialogTempSet::OnCbnSelchangeComboTempSetAlmLevel)
	ON_CBN_SELCHANGE(IDC_COMBO_TEMP_SET_ALM_REC, &CDialogTempSet::OnCbnSelchangeComboTempSetAlmRec)
END_MESSAGE_MAP()


// CDialogTempSet 消息处理程序


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


BOOL CDialogTempSet::OnInitDialog()
{
	CString a_cs;
	float f;
	CDialogEx::OnInitDialog();

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

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

	m_ComboTempSetAlmRec.AddString(CString(_T("关")));
	m_ComboTempSetAlmRec.AddString(CString(_T("开")));
	m_ComboTempSetAlmRec.SetCurSel(theApp.m_Data->m_i32ParaTempALM_REC);

	f = theApp.m_Data->m_i32ParaTempTEMP_ALM_H;
	f/= 10.0f;
	a_cs.Format(_T("%2.1f"), f);
	m_EditTempSetAlmH.SetWindowTextW(a_cs);

	f = theApp.m_Data->m_i32ParaTempTEMP_ALM_L ;
	f /= 10.0f;
	a_cs.Format(_T("%2.1f"), f);
	m_EditTempSetAlmL.SetWindowTextW(a_cs);

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


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


void CDialogTempSet::OnCbnSelchangeComboTempSetAlmSw()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaTempALM_SW = m_ComboTempSetAlmSw.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_TempAlmSw(theApp.m_Data->m_i32ParaTempALM_SW);
	((CVerbpoDlg *)theApp.m_pMainWnd)->m_TempValueNormal->Set_AlmFlag(theApp.m_Data->m_i32ParaTempALM_SW, 0);
	if (theApp.m_Data->m_i32ParaTempALM_SW == 0)
	{
		//报警 从 开 到 关
		theApp.m_Data->Del_Resp_Temp_Alm_ALL(ALM_TYPE_TEMP, DEL_ALL);
	}
	else
	{
	}
}


void CDialogTempSet::OnCbnSelchangeComboTempSetAlmLevel()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaTempALM_LEVEL = m_ComboTempSetAlmLevel.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_TempAlmLevel(theApp.m_Data->m_i32ParaTempALM_LEVEL);
}


void CDialogTempSet::OnCbnSelchangeComboTempSetAlmRec()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaTempALM_REC = m_ComboTempSetAlmRec.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_TempAlmRec(theApp.m_Data->m_i32ParaTempALM_REC);
}


BOOL CDialogTempSet::PreTranslateMessage(MSG* pMsg)
{
	// TODO:  在此添加专用代码和/或调用基类
	CRect     rect;
	int i = 0;
	float f = 0.0f;
	CString a_cs;
	// TODO:  在此添加专用代码和/或调用基类

	if (WM_LBUTTONDOWN == pMsg->message&& m_EditTempSetAlmH.GetSafeHwnd() == pMsg->hwnd)
	{

		m_EditTempSetAlmH.GetWindowTextW(a_cs);
		m_EditTempSetAlmH.GetWindowRect(&rect);       //获得文本框矩形,左上角坐标(0,0)   
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_type(NUM_INPUT_DOT | NUM_INPUT_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);
		i = a_cs.Find('.');
		if (i != -1)
		{
			if (i == a_cs.GetLength() - 1)
			{
				a_cs = a_cs.Mid(0, i);
			}
			else
			{
				a_cs = a_cs.Mid(0, i + 2);
			}

		}
		a_cs = input_TempAlmH_check(a_cs);
		f = _ttof(a_cs);
		i = (int)(f*10.0f);
		theApp.m_Data->m_i32ParaTempTEMP_ALM_H = i;
		theApp.m_Data->m_CParameterSet.set_TempAlmH(i);
		m_EditTempSetAlmH.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_TempValueNormal->set_temp_alm_H(theApp.m_Data->m_i32ParaTempTEMP_ALM_H, 0);
		return TRUE;
	}
	else if (WM_LBUTTONDOWN == pMsg->message&& m_EditTempSetAlmL.GetSafeHwnd() == pMsg->hwnd)
	{

		m_EditTempSetAlmL.GetWindowTextW(a_cs);
		m_EditTempSetAlmL.GetWindowRect(&rect);       //获得文本框矩形,左上角坐标(0,0)   
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.set_type(NUM_INPUT_DOT | NUM_INPUT_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_HRAlmL_check(a_cs);
		i = a_cs.Find('.');
		if (i != -1)
		{
			if (i == a_cs.GetLength() - 1)
			{
				a_cs = a_cs.Mid(0, i);
			}
			else
			{
				a_cs = a_cs.Mid(0, i + 2);
			}

		}
		a_cs = input_TempAlmL_check(a_cs);
		f = _ttof(a_cs);
		i = (int)(f*10.0f);
		theApp.m_Data->m_i32ParaTempTEMP_ALM_L = i;
		theApp.m_Data->m_CParameterSet.set_TempAlmL(i);
		m_EditTempSetAlmL.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_TempValueNormal->set_temp_alm_L(theApp.m_Data->m_i32ParaTempTEMP_ALM_L, 0);
		return TRUE;
	}
	return CDialogEx::PreTranslateMessage(pMsg);
}

CString CDialogTempSet::input_TempAlmH_check(CString & cs)
{
	int a = 0;
	float f = 0.0f;
	CString a_cs;

	f = _ttof(cs);
	a = (int)(f*10.0f);

	if (a>CData::m_i16TempAlmHMax)
	{
		a = CData::m_i16TempAlmHMax;
	}
	if (a<CData::m_i16TempAlmHMin)
	{
		a = CData::m_i16TempAlmHMin;
	}

	if (a <= theApp.m_Data->m_i32ParaTempTEMP_ALM_L)
	{
		a = theApp.m_Data->m_i32ParaTempTEMP_ALM_L + 5;
	}
	
	f = a;
	f /= 10.0f;

	a_cs.Format(_T("%2.1f"), f);

	return a_cs;
}


CString CDialogTempSet::input_TempAlmL_check(CString & cs)
{
	int a = 0;
	float f = 0.0f;
	CString a_cs;

	f = _ttof(cs);
	a = (int)(f*10.0f);

	if (a < CData::m_i16TempAlmLMin)
	{
		a = CData::m_i16TempAlmLMin;
	}
	if (a > CData::m_i16TempAlmLMax)
	{
		a = CData::m_i16TempAlmLMax;
	}

	if (a >= theApp.m_Data->m_i32ParaTempTEMP_ALM_H)
	{
		a = theApp.m_Data->m_i32ParaTempTEMP_ALM_H - 5;
	}
	
	f = a;
	f /= 10.0f;

	a_cs.Format(_T("%2.1f"), f);

	return a_cs;
}