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

#include "stdafx.h"
#include "Verbpo.h"
#include "DialogECGSetNormal.h"
#include "afxdialogex.h"
#include "DialogNumInput.h"
#include "VerbpoDlg.h"
#include "UartDataThread.h"

// CDialogECGSetNormal 对话框

IMPLEMENT_DYNAMIC(CDialogECGSetNormal, CDialogEx)

CDialogECGSetNormal::CDialogECGSetNormal(CWnd* pParent /*=NULL*/)
	: CDialogEx(CDialogECGSetNormal::IDD, pParent)
{
	m_i32ECGCal = 0;
}

CDialogECGSetNormal::~CDialogECGSetNormal()
{
}

void CDialogECGSetNormal::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_ECG_ALM_SW, m_ComboECGAlmSw);
	DDX_Control(pDX, IDC_COMBO_ECG_ALM_LEVEL, m_ComboECGAlmLevel);
	DDX_Control(pDX, IDC_COMBO_ECG_ALM_REC, m_ComboECGAlmRec);
	DDX_Control(pDX, IDC_COMBO_ECG_HR_FROM, m_ComboECGHRFrom);
	DDX_Control(pDX, IDC_COMBO_ECG_HR_LEAD, m_ComboECGHRLead);
	DDX_Control(pDX, IDC_COMBO_ECG_LEAD_MODE, m_ComboECGLeadMode);
	DDX_Control(pDX, IDC_EDIT_HR_ALM_H, m_EditHRAlmH);
	DDX_Control(pDX, IDC_EDIT_ECG_HR_L, m_EditHRAlmL);
	DDX_Control(pDX, IDC_COMBO_ECG_WAVE_SPEED, m_ComboECGWaveSpeed);
	DDX_Control(pDX, IDC_COMBO_ECG_TRAP, m_ComboECGTrap);
	DDX_Control(pDX, IDC_COMBO_ECG_PACE_SW, m_ComboECGPaceSw);
	DDX_Control(pDX, IDC_COMBO_BEAT_VOL, m_ComboBeatVol);
}


BEGIN_MESSAGE_MAP(CDialogECGSetNormal, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_ADJ_SW, &CDialogECGSetNormal::OnBnClickedButtonAdjSw)
	ON_BN_CLICKED(IDC_BUTTON_ECG_DEFAULT, &CDialogECGSetNormal::OnBnClickedButtonEcgDefault)
	ON_EN_SETFOCUS(IDC_EDIT_HR_ALM_H, &CDialogECGSetNormal::OnEnSetfocusEditHrAlmH)
	ON_CBN_SELCHANGE(IDC_COMBO_ECG_ALM_SW, &CDialogECGSetNormal::OnCbnSelchangeComboEcgAlmSw)
	ON_CBN_SELCHANGE(IDC_COMBO_ECG_ALM_LEVEL, &CDialogECGSetNormal::OnCbnSelchangeComboEcgAlmLevel)
	ON_CBN_SELCHANGE(IDC_COMBO_ECG_ALM_REC, &CDialogECGSetNormal::OnCbnSelchangeComboEcgAlmRec)
	ON_CBN_SELCHANGE(IDC_COMBO_ECG_HR_FROM, &CDialogECGSetNormal::OnCbnSelchangeComboEcgHrFrom)
	ON_CBN_SELCHANGE(IDC_COMBO_ECG_HR_LEAD, &CDialogECGSetNormal::OnCbnSelchangeComboEcgHrLead)
	ON_CBN_SELCHANGE(IDC_COMBO_ECG_LEAD_MODE, &CDialogECGSetNormal::OnCbnSelchangeComboEcgLeadMode)
	ON_CBN_SELCHANGE(IDC_COMBO_ECG_TRAP, &CDialogECGSetNormal::OnCbnSelchangeComboEcgTrap)
	ON_CBN_SELCHANGE(IDC_COMBO_ECG_PACE_SW, &CDialogECGSetNormal::OnCbnSelchangeComboEcgPaceSw)
	ON_CBN_SELCHANGE(IDC_COMBO_BEAT_VOL, &CDialogECGSetNormal::OnCbnSelchangeComboBeatVol)
END_MESSAGE_MAP()


// CDialogECGSetNormal 消息处理程序


void CDialogECGSetNormal::OnBnClickedButtonAdjSw()
{
	// TODO:  在此添加控件通知处理程序代码
	if (m_i32ECGCal)
	{
		m_i32ECGCal = 0;
	}
	else
	{
		m_i32ECGCal = 1;
	}
	//向下位机发送 心电校准 命令
	((UartDataThread *)theApp.m_pThrdUart)->Send_CMD_ECGCal(m_i32ECGCal);
}


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

int ff = 0;
void CDialogECGSetNormal::OnEnSetfocusEditHrAlmH()
{
	// TODO:  在此添加控件通知处理程序代码
	if (ff==0)
	{
		ff = 1;
		//((CVerbpoDlg *)theApp.m_pMainWnd)->m_CDialogNumInput.DoModal();
		ff = 0;
	}
	
}


BOOL CDialogECGSetNormal::PreTranslateMessage(MSG* pMsg)
{
	CRect     rect;
	CString a_cs;
	// TODO:  在此添加专用代码和/或调用基类
	
	if (WM_LBUTTONDOWN == pMsg->message&& m_EditHRAlmH.GetSafeHwnd() == pMsg->hwnd)
	{
		
		m_EditHRAlmH.GetWindowTextW(a_cs);
		m_EditHRAlmH.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_HRAlmH_check(a_cs);
		theApp.m_Data->m_i32ParaECGHR_ALM_H = _ttoi(a_cs);
		theApp.m_Data->m_CParameterSet.set_ECGHRAlmH(_ttoi(a_cs));
		m_EditHRAlmH.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_ECGValueNormal->set_HR_Alm_H(theApp.m_Data->m_i32ParaECGHR_ALM_H, 0);
		return TRUE;
	}
	else if (WM_LBUTTONDOWN == pMsg->message&& m_EditHRAlmL.GetSafeHwnd() == pMsg->hwnd)
	{

		m_EditHRAlmL.GetWindowTextW(a_cs);
		m_EditHRAlmL.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_HRAlmL_check(a_cs);
		theApp.m_Data->m_i32ParaECGHR_ALM_L = _ttoi(a_cs);
		theApp.m_Data->m_CParameterSet.set_ECGHRAlmL(_ttoi(a_cs));
		m_EditHRAlmL.SetWindowTextW(a_cs);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_ECGValueNormal->set_HR_Alm_L(theApp.m_Data->m_i32ParaECGHR_ALM_L, 0);
		return TRUE;
	}
	return CDialogEx::PreTranslateMessage(pMsg);
}


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

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

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

	m_ComboECGAlmRec.AddString(CString(_T("关")));
	m_ComboECGAlmRec.AddString(CString(_T("开")));
	m_ComboECGAlmRec.SetCurSel(theApp.m_Data->m_i32ParaECGALM_REC);

	m_ComboECGHRFrom.AddString(CString(_T("心电")));
	m_ComboECGHRFrom.AddString(CString(_T("血氧")));
	m_ComboECGHRFrom.SetCurSel(theApp.m_Data->m_i32ParaECGHR_FORM);

	m_ComboECGHRLead.AddString(CString(_T("I")));
	m_ComboECGHRLead.AddString(CString(_T("II")));
	m_ComboECGHRLead.SetCurSel(theApp.m_Data->m_i32ParaECGHR_LEAD);

	m_ComboECGLeadMode.AddString(CString(_T("5导联")));
	m_ComboECGLeadMode.AddString(CString(_T("3导联")));
	m_ComboECGLeadMode.SetCurSel(theApp.m_Data->m_i32ParaECGLEAD_MODE);

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

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

	m_ComboECGWaveSpeed.AddString(CString(_T("25mm/s")));
	m_ComboECGWaveSpeed.AddString(CString(_T("50mm/s")));
	m_ComboECGWaveSpeed.SetCurSel(theApp.m_Data->m_i32ParaECGWAVE_SPEED);

	m_ComboECGTrap.AddString(CString(_T("关")));
	m_ComboECGTrap.AddString(CString(_T("开")));
	m_ComboECGTrap.SetCurSel(theApp.m_Data->m_i32ParaECGECG_TRAP);


	m_ComboECGPaceSw.AddString(CString(_T("关")));
	m_ComboECGPaceSw.AddString(CString(_T("开")));
	m_ComboECGPaceSw.SetCurSel(theApp.m_Data->m_i32ParaECGPACE_SW);

	m_ComboBeatVol.AddString(CString(_T("关")));
	m_ComboBeatVol.AddString(CString(_T("1")));
	m_ComboBeatVol.AddString(CString(_T("2")));
	m_ComboBeatVol.AddString(CString(_T("3")));
	m_ComboBeatVol.SetCurSel(theApp.m_Data->m_i32ParaECGBEAT_VOL);


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


void CDialogECGSetNormal::OnCbnSelchangeComboEcgAlmSw()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaECGALM_SW = m_ComboECGAlmSw.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_ECGAlmSw(theApp.m_Data->m_i32ParaECGALM_SW);

	//设置、绘制报警开关标识
	((CVerbpoDlg *)theApp.m_pMainWnd)->m_ECGValueNormal->Set_AlmFlag(theApp.m_Data->m_i32ParaECGALM_SW, 0);
	if (theApp.m_Data->m_i32ParaECGALM_SW == 0)
	{
		//报警 从 开 到 关
		theApp.m_Data->Del_ECG_Alm_ALL(ALM_TYPE_ECG, DEL_ALL);
		theApp.m_Data->Del_ECG_Alm_ALL(ALM_TYPE_ST, DEL_ALL);
		
	}
	else
	{
	}
}


void CDialogECGSetNormal::OnCbnSelchangeComboEcgAlmLevel()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaECGALM_LEVEL = m_ComboECGAlmLevel.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_ECGAlmLevel(theApp.m_Data->m_i32ParaECGALM_LEVEL);
}


void CDialogECGSetNormal::OnCbnSelchangeComboEcgAlmRec()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaECGALM_REC = m_ComboECGAlmRec.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_ECGAlmRec(theApp.m_Data->m_i32ParaECGALM_REC);
}


void CDialogECGSetNormal::OnCbnSelchangeComboEcgHrFrom()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaECGHR_FORM = m_ComboECGHRFrom.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_ECGHRFrom(theApp.m_Data->m_i32ParaECGHR_FORM);
	((CVerbpoDlg *)theApp.m_pMainWnd)->m_ECGValueNormal->set_HR_from(theApp.m_Data->m_i32ParaECGHR_FORM, 0);
}


void CDialogECGSetNormal::OnCbnSelchangeComboEcgHrLead()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaECGHR_LEAD = m_ComboECGHRLead.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_ECGHRLead(theApp.m_Data->m_i32ParaECGHR_LEAD);
}


void CDialogECGSetNormal::OnCbnSelchangeComboEcgLeadMode()
{
	// TODO:  在此添加控件通知处理程序代码
	INT i = 0, j = 0;
	theApp.m_Data->m_i32ParaECGLEAD_MODE = m_ComboECGLeadMode.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_ECGLeadMode(theApp.m_Data->m_i32ParaECGLEAD_MODE);
	theApp.m_Data->m_i32ParaECGLEAD_MODE = theApp.m_Data->m_i32ParaECGLEAD_MODE;

	//从3导切换到5导联
	if (theApp.m_Data->m_i32ParaECGLEAD_MODE == 0)
	{
		((CVerbpoDlg *)theApp.m_pMainWnd)->NormalComboECG1Lead->AddString(_T("V"));
		((CVerbpoDlg *)theApp.m_pMainWnd)->NormalComboECG2Lead->AddString(_T("V"));
	}
	//从5导切换到3导联
	else
	{
		((CVerbpoDlg *)theApp.m_pMainWnd)->NormalComboECG1Lead->DeleteString(6);
		((CVerbpoDlg *)theApp.m_pMainWnd)->NormalComboECG2Lead->DeleteString(6);
		if (theApp.m_Data->m_i32ShowNormalECGChannel1Type == ECG_WAVE_V1)
		{
			for (j = ECG_WAVE_I; j <= ECG_WAVE_V1; j++)
			{
				if (j != theApp.m_Data->m_i32ShowNormalECGChannel2Type)
				{
					break;
				}
			}
			((CVerbpoDlg *)theApp.m_pMainWnd)->NormalComboECG1Lead->SetCurSel(j);
			theApp.m_Data->m_i32ShowNormalECGChannel1Type = j;
		}
		if (theApp.m_Data->m_i32ShowNormalECGChannel2Type == ECG_WAVE_V1)
		{
			for (j = ECG_WAVE_I; j <= ECG_WAVE_V1; j++)
			{
				if (j != theApp.m_Data->m_i32ShowNormalECGChannel1Type)
				{
					break;
				}
			}
			((CVerbpoDlg *)theApp.m_pMainWnd)->NormalComboECG2Lead->SetCurSel(j);
			theApp.m_Data->m_i32ShowNormalECGChannel2Type = j;
		}
	}

	for (i = 0; i < 7;i++)
	{
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_pCECGWaveN[i]->Erase();
		
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_pCECGWaveN[i]->Reset_WaveDataBuff();
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_pCECGWaveN[i]->set_lead_type(i);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_pCECGWaveN[i]->Draw_LeadType();
	}
	//如果不在全导联界面
	if ((((CVerbpoDlg *)theApp.m_pMainWnd)->m_i32Style)!=2)
	{
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_pCECGWaveN[0]->set_lead_type(theApp.m_Data->m_i32ShowNormalECGChannel1Type);
		((CVerbpoDlg *)theApp.m_pMainWnd)->m_pCECGWaveN[1]->set_lead_type(theApp.m_Data->m_i32ShowNormalECGChannel2Type);
	}
	
	//向下位机发送 心电校准 命令
	((UartDataThread *)theApp.m_pThrdUart)->Send_CMD_LeadSystem(theApp.m_Data->m_i32ParaECGLEAD_MODE);
}


void CDialogECGSetNormal::OnCbnSelchangeComboEcgTrap()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaECGECG_TRAP = m_ComboECGTrap.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_ECGTrap(theApp.m_Data->m_i32ParaECGECG_TRAP);
	//向下位机发送 心电工频陷波 命令
	((UartDataThread *)theApp.m_pThrdUart)->Send_CMD_ECGTrap(theApp.m_Data->m_i32ParaECGECG_TRAP);
}


void CDialogECGSetNormal::OnCbnSelchangeComboEcgPaceSw()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaECGPACE_SW = m_ComboECGPaceSw.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_ECGPaceSw(theApp.m_Data->m_i32ParaECGPACE_SW);
	((CVerbpoDlg *)theApp.m_pMainWnd)->m_ECGValueNormal->set_pace(theApp.m_Data->m_i32ParaECGPACE_SW, 0);

	//向下位机发送 起搏 命令
	((UartDataThread *)theApp.m_pThrdUart)->Send_CMD_ECGTrap(theApp.m_Data->m_i32ParaECGPACE_SW);
}


void CDialogECGSetNormal::OnCbnSelchangeComboBeatVol()
{
	// TODO:  在此添加控件通知处理程序代码
	theApp.m_Data->m_i32ParaECGBEAT_VOL = m_ComboBeatVol.GetCurSel();
	theApp.m_Data->m_CParameterSet.set_ECGBeatVol(theApp.m_Data->m_i32ParaECGBEAT_VOL);
	switch (theApp.m_Data->m_i32ParaECGBEAT_VOL)
	{
	case 0:
		theApp.m_Data->m_i32BeatVol = -10000;
		break;

	case 1:
		theApp.m_Data->m_i32BeatVol = -1398;
		break;

	case 2:
		theApp.m_Data->m_i32BeatVol = -444;
		break;

	case 3:
		theApp.m_Data->m_i32BeatVol = 0;
		break;
	}
	
}

//检测心律报警上限阈值是否正确
CString CDialogECGSetNormal::input_HRAlmH_check(CString & cs)
{
	int a = 0;
	CString a_cs;

	a = _ttoi(cs);
	
	if (a>CData::m_i16HRAlmHMax)
	{
		a = CData::m_i16HRAlmHMax;
	}
	if (a<CData::m_i16HRAlmHMin)
	{
		a = CData::m_i16HRAlmHMin;
	}

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

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

	return a_cs;
}

//检测心律报警下限阈值是否正确
CString CDialogECGSetNormal::input_HRAlmL_check(CString & cs)
{
	int a = 0;
	CString a_cs;

	a = _ttoi(cs);

	if (a>CData::m_i16HRAlmLMax)
	{
		a = CData::m_i16HRAlmLMax;
	}
	if (a<CData::m_i16HRAlmLMin)
	{
		a = CData::m_i16HRAlmLMin;
	}
	if (a >= theApp.m_Data->m_i32ParaECGHR_ALM_H)
	{
		a = theApp.m_Data->m_i32ParaECGHR_ALM_H - 3;
	}
	
	a_cs.Format(_T("%d"), a);
	
	return a_cs;
}