// DigitalChannelDlg.cpp : implementation file
//

#include "stdafx.h"
#include "clientdemo5.h"
#include "resource.h"
#include "DigitalChannelDlg.h"
#include "RemoteChannelDlg.h"
#include "ClientDemo5Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDigitalChannelDlg dialog
static CString dev_type_array[4] = 
{
	"DVR", "NVR", "IPC",  "HVR"
};



CDigitalChannelDlg::CDigitalChannelDlg(CWnd* pParent /*=NULL*/)
	: m_tour_time(10)
	, m_nLastChannelNum(0)
	, m_searchNum(0)
{
	//{{AFX_DATA_INIT(CDigitalChannelDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	memset(m_DeviceInfo, 0, 64 * sizeof(NET_SDK_SEARCH_DEVINFO));
	memset(m_DevicePro, 0, 64 * sizeof(int));
}


void CDigitalChannelDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDigitalChannelDlg)
	DDX_Control(pDX, IDC_LIST_SETTING, m_listconfig);
	DDX_Text(pDX, IDC_EDIT_TOUTTIME, m_tour_time);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDigitalChannelDlg, CDialog)
	//{{AFX_MSG_MAP(CDigitalChannelDlg)
	ON_WM_SHOWWINDOW()
	ON_CBN_SELCHANGE(IDC_COMBO_CHANNEL, &CDigitalChannelDlg::OnCbnSelchangeComboChannel)
	ON_BN_CLICKED(IDC_BTN_ADD, &CDigitalChannelDlg::OnBnClickedButtonAdd)
	ON_BN_CLICKED(IDC_BTN_DEL, &CDigitalChannelDlg::OnBnClickedButtonDel)
	ON_BN_CLICKED(IDOK, &CDigitalChannelDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDC_CHECK_DIGITALENABLE, &CDigitalChannelDlg::OnBnClickedCheckDigitalenable)
	ON_CBN_SELCHANGE(IDC_COMBO_CON, &CDigitalChannelDlg::OnCbnSelchangeComboConnect)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_SETTING, &CDigitalChannelDlg::OnLvnItemchangedListSetting)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_SETTING, &CDigitalChannelDlg::OnNMDblclkListSetting)
	ON_BN_CLICKED(IDC_BTN_REFRUSH, &CDigitalChannelDlg::OnBnClickedButtonRefrush)
	ON_EN_CHANGE(IDC_EDIT_TOUTTIME, &CDigitalChannelDlg::OnEnChangeEditTouttime)


	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDigitalChannelDlg message handlers

BOOL CDigitalChannelDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	//_CWndCS(this);
	m_listconfig.SetExtendedStyle(m_listconfig.GetExtendedStyle() | LVS_EX_DOUBLEBUFFER | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT );

	m_listconfig.InsertColumn(0, _CS("ConfigNet.No"), LVCFMT_LEFT, 40, 0);
	m_listconfig.InsertColumn(1, _CS("DigitalChannel.CfgName"), LVCFMT_LEFT, 130, 3);
	m_listconfig.InsertColumn(2, _CS("Desktop.DevType"), LVCFMT_LEFT, 130, 3);
	m_listconfig.InsertColumn(3, _CS("Upgrade.IPAdress"), LVCFMT_LEFT, 130, 3);
	m_listconfig.InsertColumn(4, _CS("DigitalChannel.RemoteChannel"), LVCFMT_LEFT, 130, 3);
	CComboBox* pComBox = (CComboBox *)GetDlgItem(IDC_COMBO_CON);
	pComBox->ResetContent();
	CString strText = _T("");
	int nInsert = 0;
	if (pComBox != NULL)
	{
		for(int i = 0; i < 2; ++i)
		{
			switch(i)
			{
			case 0:
				strText = _CS("DigitalChannel.SingleLink");
				break;
			case 1:
				strText = _CS("DigitalChannel.MulLink");
				break;
			default: break;
			}
			nInsert = pComBox->AddString(strText);
			pComBox->SetItemData(nInsert, i);
		}
		pComBox->SetCurSel(0);
	}

	GetButton(IDC_CHECK_DIGITALENABLE)->SetCheck(FALSE);
	GetDlgItem(IDC_EDIT_TOUTTIME)->ShowWindow(SW_HIDE);

	GetDlgItem(IDC_CHECK_TIMEASY)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK_TIMEASY)->EnableWindow(FALSE);
	GetDlgItem(IDC_COMBO_CON)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_TOUTTIME)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_ADD)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_DEL)->EnableWindow(FALSE);


	return TRUE;	              // EXCEPTION: OCX Property Pages should return FALSE
}
BOOL CDigitalChannelDlg::InitWndShow()
{
	//设置通道下拉选择框
	int nChanneCount = ((CClientDemo5Dlg*)AfxGetMainWnd())->GetSysInfo()->byChanNum;
	int nChanneDigitalCount = ((CClientDemo5Dlg*)AfxGetMainWnd())->GetSysInfo()->iDigChannel;
	int nInsert = 0;
	CComboBox *pComboBox = GetComboBox(IDC_COMBO_CHANNEL);
	pComboBox->ResetContent();
	CString strText;
	int i = 0;

	for (i = 0; i < nChanneDigitalCount; ++i)
	{
		strText.Format("%d", nChanneCount + i + 1);
		nInsert = pComboBox->AddString(strText);
		pComboBox->SetItemData(nInsert, i);
	}

	if (nChanneDigitalCount > 0)
	{
		strText.Format("%s", _CS("OneClick.All"));
		nInsert = pComboBox->AddString(strText);
		pComboBox->SetItemData(nInsert, nChanneCount + nChanneDigitalCount);
		pComboBox->SetCurSel(0);
		m_nLastChannelNum = 0;
	}


	return TRUE;
}
void CDigitalChannelDlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
	InitWndShow();
	//获取各自配置
	if ( bShow )
	{ 
		int nRet = ((CClientDemo5Dlg*)AfxGetMainWnd())->GetNetDecorder(m_netDec);		
		if ( 1 == nRet || 2 == nRet )
		{
				//不判断了，不去管获取成功还是失败
				
			
			 nRet = ((CClientDemo5Dlg*)AfxGetMainWnd())->GetNetDecorderConfig(&m_netdeccallV4);
				if ( nRet == 1 )
				{
					memcpy(&m_oldnetdeccallV4, &m_netdeccallV4, sizeof(NET_SDK_NetDecorderConfigAll_V4));
				}
				else	//原先就获取过
				{
					memcpy(&m_netdeccallV4, &m_oldnetdeccallV4, sizeof(NET_SDK_NetDecorderConfigAll_V4));
				}
				

			StructToWinData();
		}
	}


}
bool CDigitalChannelDlg::StructToWinData()
{
	SetComBoxCurSel(IDC_COMBO_CHANNEL, 0);
	int nChannelNum = GetComboBoxData(IDC_COMBO_CHANNEL);
	StructToWinData(nChannelNum, true);
	m_nLastChannelNum = nChannelNum;
	return true;
}
bool CDigitalChannelDlg::WinDataToStruct()
{

	int nChannelNum = GetComboBoxData(IDC_COMBO_CHANNEL);	

	//当保存所有通道时候，从第一通道处复制数据
	
		if ( nChannelNum == ((CClientDemo5Dlg*)AfxGetMainWnd())->GetSysInfo()->iDigChannel)
		{
			//先保存第一通道数据
			WinDataToStruct(((CClientDemo5Dlg*)AfxGetMainWnd())->GetSysInfo()->byChanNum);
			for ( int i = 1; i < ((CClientDemo5Dlg*)AfxGetMainWnd())->GetSysInfo()->iDigChannel; ++i)
			{
				memcpy(&m_netdeccallV4.DigitChnConf[i], &m_netdeccallV4.DigitChnConf[0], sizeof(NET_SDK_NetDigitChnConfig));
			}

			return true;
		}
		else
		{
			return WinDataToStruct(nChannelNum);
		}


	return true;
}
bool CDigitalChannelDlg::WinDataToStruct(int nChannelNum)
{
	if (nChannelNum < 0 || nChannelNum > SDK_NET_MAX_CHANNUM)
	{
		return false;
	}
	if (m_listconfig.GetItemCount() != m_vec_onenetdecV4.size())
	{
		return false;
	}

	NET_SDK_NetDigitChnConfig_V4& net_configdec =  m_netdeccallV4.DigitChnConf[nChannelNum];
	net_configdec.Enable = GetButton(IDC_CHECK_DIGITALENABLE)->GetCheck();
	net_configdec.EnCheckTime = GetButton(IDC_CHECK_TIMEASY)->GetCheck();
	//net_configdec.ConnType = GetComboBox(IDC_COMBO_CON)->GetCurSel();
	net_configdec.ConnType = 0;
	net_configdec.TourIntv = m_tour_time;
	net_configdec.SingleConnId = 0;
	net_configdec.ConfNum = m_listconfig.GetItemCount();
	if (net_configdec.ConnType == 0)
	{
		for (int i = 0; i < net_configdec.ConfNum; ++i)
		{
			if (m_listconfig.GetCheck(i))
			{
				net_configdec.SingleConnId = i + 1;
				break;
			}
		}
	}
	else if (net_configdec.ConnType == 1)
	{
	}
	else
	{
		return false;
	}

	for (size_t j = 0; j < m_vec_onenetdecV4.size(); ++j)
	{
		net_configdec.vNetDecorderConf[j] = m_vec_onenetdecV4[j];
	}
	for (int k = 0; k < m_listconfig.GetItemCount(); ++k)
	{
		net_configdec.vNetDecorderConf[k].Enable = m_listconfig.GetCheck(k);
	}
	return true;
}
bool CDigitalChannelDlg::StructToWinData(int nChannelNum, bool bCopy)
{
	if (nChannelNum < 0 || nChannelNum > SDK_NET_MAX_CHANNUM)
	{
		return false;
	}
	

	m_vec_onenetdecV4.clear();
	bool enable = false;
	const NET_SDK_NetDigitChnConfig_V4& netchannel_config =  m_netdeccallV4.DigitChnConf[nChannelNum];
	if (netchannel_config.Enable == 0)
	{
		enable = false;
	}
	else
	{
		enable = true;
	}

	GetButton(IDC_CHECK_DIGITALENABLE)->SetCheck(enable);
	GetButton(IDC_CHECK_TIMEASY)->EnableWindow(enable);
	//GetDlgItem(IDC_COMBO_CON)->EnableWindow(enable);
	GetDlgItem(IDC_COMBO_CON)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_ADD)->EnableWindow(enable);
	GetDlgItem(IDC_BTN_DEL)->EnableWindow(enable);
	GetDlgItem(IDC_LIST_SETTING)->EnableWindow(enable);
	GetDlgItem(IDC_EDIT_TOUTTIME)->EnableWindow(enable);

	GetButton(IDC_CHECK_TIMEASY)->SetCheck(netchannel_config.EnCheckTime);

	m_listconfig.EnableWindow(enable);
	m_listconfig.DeleteAllItems();
	m_tour_time = netchannel_config.TourIntv;
	int i = 0;
	int nNum = (netchannel_config.ConfNum >0)?1:0;

	CString strName = _T("");
	strName.Format("%02d",1);
	CString no = _T("");
	for (i = 0; i < nNum; ++i)
	{
		m_vec_onenetdecV4.push_back(netchannel_config.vNetDecorderConf[i]);	//把内容copy到界面容器

		no.Format( "%d" , i + 1);
		m_listconfig.InsertItem(i , no );


		if(netchannel_config.vNetDecorderConf[i].ConfName[0]==0)
		{
			strcpy((char*)&netchannel_config.vNetDecorderConf[i].ConfName[0],strName);
		}

		m_listconfig.SetItemText(i, 1, netchannel_config.vNetDecorderConf[i].ConfName);
		m_listconfig.SetItemText(i, 2, dev_type_array[netchannel_config.vNetDecorderConf[i].DevType]);
		m_listconfig.SetItemText(i, 3, netchannel_config.vNetDecorderConf[i].Address);
		no.Format( "%d" , netchannel_config.vNetDecorderConf[i].Channel + 1);
		m_listconfig.SetItemText(i, 4, no);
		m_listconfig.SetCheck(i, FALSE);
	}
	//GetComboBox(IDC_COMBO_CON)->SetCurSel(netchannel_config.ConnType);
	if (netchannel_config.ConnType == 1)
	{
		GetDlgItem(IDC_EDIT_TOUTTIME)->ShowWindow(SW_SHOW);
		for (i = 0; i < netchannel_config.ConfNum; ++i)
		{
			if(netchannel_config.vNetDecorderConf[i].Enable)
			{
				m_listconfig.SetCheck(i, TRUE);
			}
			else
			{
				m_listconfig.SetCheck(i, FALSE);
			}
		}
	}
	else
	{
		if (netchannel_config.SingleConnId >= 1 && netchannel_config.SingleConnId <= (unsigned int)netchannel_config.ConfNum)
		{
			m_listconfig.SetCheck(netchannel_config.SingleConnId - 1, TRUE);
		}
		GetDlgItem(IDC_EDIT_TOUTTIME)->ShowWindow(SW_HIDE);
		
	}



	return true;
}
void CDigitalChannelDlg::OnCbnSelchangeComboChannel()
{
	// TODO: 在此添加控件通知处理程序代码
	int nChannelNum = GetComboBoxData(IDC_COMBO_CHANNEL);

	//选择全部通道，默认显示第一通道数据，而且上一通道的数据也不需要保存
	if ( nChannelNum ==((CClientDemo5Dlg*)AfxGetMainWnd())->GetSysInfo()->iDigChannel)
	{
		StructToWinData(0);
		m_nLastChannelNum = nChannelNum;
	}
	//如果原先是全部通道，切换通道不需要保存，只要显示当前通道就可以
	else if ( m_nLastChannelNum == ((CClientDemo5Dlg*)AfxGetMainWnd())->GetSysInfo()->byChanNum + ((CClientDemo5Dlg*)AfxGetMainWnd())->GetSysInfo()->iDigChannel)
	{
		StructToWinData(nChannelNum);
		m_nLastChannelNum = nChannelNum;
	}
	else
	{
		if (nChannelNum != m_nLastChannelNum)
		{
			WinDataToStruct(m_nLastChannelNum);
			StructToWinData(nChannelNum);
			m_nLastChannelNum = nChannelNum;
		}
	}
}

void CDigitalChannelDlg::OnBnClickedButtonAdd()
{
	int count = m_listconfig.GetItemCount();
	
	if(count >=1)
	{
		return ;
	}
	else
	{
		CRemoteChannelDlg dlg;
		dlg.m_searchNum = m_searchNum;

		memcpy(dlg.m_DeviceInfo, m_DeviceInfo, 64 * sizeof(NET_SDK_SEARCH_DEVINFO));
		memcpy(dlg.m_DevicePro, m_DevicePro, 64 * sizeof(int));
		dlg.SetConfNum(count + 1);
		if (dlg.DoModal() == IDOK)
		{
		
			
		
				NET_SDK_NetDecorderConfig_V4 netdcf;
				if (dlg.GetOneCfg(&netdcf))
				{
					bool existed = false;
					int  index = -1;
					for (int i = 0; i < m_vec_onenetdecV4.size(); ++i)
					{
						if (strcmp(m_vec_onenetdecV4[i].Address, netdcf.Address) == 0)
						{
							existed = true;
							index = i;
							MessageBox(_CS("DigitalChannel.Existed"), _CS("OneClick.Prompt"));
							break;
						}
					}
					
					if (existed)
					{
						m_listconfig.SetFocus();
						for (int i = 0; i < m_listconfig.GetItemCount(); ++i)
						{
							m_listconfig.SetItemState(i, 0, LVIS_SELECTED);
						}
						m_listconfig.SetItemState(index, LVIS_SELECTED, LVIS_SELECTED);
						return;
					}

					for (int nch = 0; nch < SDK_NET_MAX_CHANNUM; ++nch)
					{
						for(int ncfcnt = 0; ncfcnt < 4; ++ncfcnt)
						{
							if ((strcmp(m_netdeccallV4.DigitChnConf[nch].vNetDecorderConf[ncfcnt].Address, netdcf.Address) == 0) && (m_netdeccallV4.DigitChnConf[nch].vNetDecorderConf[ncfcnt].Port==netdcf.Port))
							{
								CString cstrTip = _CS("DigitalChannel.Existed");
								CString cstrSplit = _T(" : ");
								CString cstrShow = _CS("DigitalChannel.Channel");
								CString cstrChn;
								cstrChn.Format(" %d", nch + 1);
								CString cstrText = cstrTip + cstrSplit + cstrShow + cstrChn;
								MessageBox(cstrText, _CS("OneClick.Prompt"));
								((CComboBox*)GetDlgItem(IDC_COMBO_CHANNEL))->SetCurSel(nch);
								StructToWinData(nch, true);
								m_nLastChannelNum = nch;
								return;
							}
						}
					}

					m_vec_onenetdecV4.push_back(netdcf);


					CString no = _T("");
					no.Format( "%d" , count + 1);
					m_listconfig.InsertItem(count , no );

					m_listconfig.SetItemText(count, 1, netdcf.ConfName);
					m_listconfig.SetItemText(count, 2, dev_type_array[netdcf.DevType]);
					m_listconfig.SetItemText(count, 3, netdcf.Address);
					no.Format("%d", netdcf.Channel + 1);
					m_listconfig.SetItemText(count, 4, no);
					m_listconfig.SetCheck(count, TRUE);
				}
			}
		
		m_searchNum = dlg.m_searchNum;
		memcpy(m_DeviceInfo, dlg.m_DeviceInfo, 64 * sizeof(NET_SDK_SEARCH_DEVINFO));
		memcpy(m_DevicePro, dlg.m_DevicePro, 64 * sizeof(int));
	}
	
}

void CDigitalChannelDlg::OnBnClickedButtonDel()
{

		int index = m_listconfig.GetSelectionMark();
		if( m_listconfig.GetItemCount() <= 0 || index < 0 || index >= m_listconfig.GetItemCount())
		{
			return;
		}
		std::vector<NET_SDK_NetDecorderConfig_V4>::iterator iter = m_vec_onenetdecV4.begin();
		advance(iter, index);
		if (iter != m_vec_onenetdecV4.end())
		{
			m_vec_onenetdecV4.erase(iter);
		}
		m_listconfig.DeleteAllItems();
		CString no = _T("");
		for (size_t i = 0; i < m_vec_onenetdecV4.size(); ++i)
		{
			no.Format( "%d" , i + 1);
			m_listconfig.InsertItem(i , no );

			m_listconfig.SetItemText(i, 1, m_vec_onenetdecV4[i].ConfName);
			m_listconfig.SetItemText(i, 2, dev_type_array[m_vec_onenetdec[i].DevType]);
			m_listconfig.SetItemText(i, 3, m_vec_onenetdecV4[i].Address);
			no.Format( "%d" , m_vec_onenetdecV4[i].Channel + 1);
			m_listconfig.SetItemText(i, 4, no);
			if (m_vec_onenetdecV4[i].Enable)
			{
				m_listconfig.SetCheck(i, TRUE);
			}
		}
	
	
}

void CDigitalChannelDlg::OnBnClickedOk()
{	
	WinDataToStruct();
	int nRet = 1;
	int nChannelNum = GetComboBoxData(IDC_COMBO_CHANNEL);	
	int nChannelCount = ((CClientDemo5Dlg*)AfxGetMainWnd())->GetSysInfo()->iDigChannel; 


	if(nChannelNum==nChannelCount)
	{
		
			for ( int i = 0; i < nChannelCount; i ++ )
			{
				//比较多少通道数据更改就保存多少通道的数据	
				if (memcmp(&m_netdeccallV4.DigitChnConf[i], &m_oldnetdeccallV4.DigitChnConf[i], sizeof(NET_SDK_NetDigitChnConfig_V4)) )
				{
					nRet = ((CClientDemo5Dlg*)AfxGetMainWnd())->SetNetDecorderConfig(m_netdeccallV4.DigitChnConf[i],i);
											//只要有一个通道保存失败，就认为是失败
					if ( nRet <= 0 )
					{
						break;
					}
				}
			}
		
		
	}
	else
	{
		
			
			if (memcmp(&m_netdeccallV4.DigitChnConf[nChannelNum], &m_oldnetdeccallV4.DigitChnConf[nChannelNum], sizeof(NET_SDK_NetDigitChnConfig_V4)) )
			{
								nRet = ((CClientDemo5Dlg*)AfxGetMainWnd())->SetNetDecorderConfig(m_netdeccallV4.DigitChnConf[nChannelNum],nChannelNum);
			}
		
		
	}



}

void CDigitalChannelDlg::OnBnClickedCheckDigitalenable()
{
	BOOL enable = GetButton(IDC_CHECK_DIGITALENABLE)->GetCheck();
	if(enable)
	{
		GetDlgItem(IDC_CHECK_TIMEASY)->EnableWindow(TRUE);
		GetDlgItem(IDC_COMBO_CON)->EnableWindow(TRUE);
		GetDlgItem(IDC_EDIT_TOUTTIME)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(TRUE);
		GetDlgItem(IDC_BTN_DEL)->EnableWindow(TRUE);	
		m_listconfig.EnableWindow(TRUE);
	}
	else
	{
		GetDlgItem(IDC_CHECK_TIMEASY)->EnableWindow(FALSE);

		GetDlgItem(IDC_COMBO_CON)->EnableWindow(FALSE);
		GetDlgItem(IDC_EDIT_TOUTTIME)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(FALSE);
		GetDlgItem(IDC_BTN_DEL)->EnableWindow(FALSE);
		m_listconfig.EnableWindow(FALSE);
	}
	GetDlgItem(IDC_COMBO_CON)->EnableWindow(FALSE);
}

void CDigitalChannelDlg::OnCbnSelchangeComboConnect()
{
	// TODO: 在此添加控件通知处理程序代码
	int index = GetComboBox(IDC_COMBO_CON)->GetCurSel();
	if (index == 0)
	{
	
		GetDlgItem(IDC_EDIT_TOUTTIME)->ShowWindow(SW_HIDE);
		
		int count = m_listconfig.GetItemCount();
		for (int i = 0; i < count; ++i)
		{
			m_listconfig.SetCheck(i, FALSE);
		}
	}
	else if (index == 1)
	{

		GetDlgItem(IDC_EDIT_TOUTTIME)->ShowWindow(SW_SHOW);
	
	}
}

void CDigitalChannelDlg::OnLvnItemchangedListSetting(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	
	DWORD dwPos = GetMessagePos();
	CPoint point( LOWORD(dwPos), HIWORD(dwPos) );

	m_listconfig.ScreenToClient(&point);

	LVHITTESTINFO lvinfo;
	lvinfo.pt = point;
	lvinfo.flags = LVHT_ABOVE;

	UINT nFlag;
	int nItem = m_listconfig.HitTest(point, &nFlag);
	m_vec_onenetdecV4[0].Enable = TRUE;


	//判断是否点在checkbox上
	if(nFlag == LVHT_ONITEMSTATEICON && nItem >= 0)
	{
		m_listconfig.SetItemState(nItem, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);

		if(m_listconfig.GetCheck(nItem))
		{
			int index = GetComboBox(IDC_COMBO_CON)->GetCurSel();
			if (index == 0)
			{
				int count = m_listconfig.GetItemCount();
				for (int i = 0; i < count; ++i)
				{
					if (i != nItem)
					{
						if(m_listconfig.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED)
						{
							m_listconfig.SetItemState(i, 0, LVIS_SELECTED|LVIS_FOCUSED);
						}
						m_listconfig.SetCheck(i, FALSE);
						
						m_vec_onenetdec[i].Enable = false;
					}
					else
					{
						m_vec_onenetdec[i].Enable = true;
					}
				}
			}
			else
			{
				int select_c = 0;
 				int count = m_listconfig.GetItemCount();
 				for (int i = 0; i < count; ++i)
 				{
					if (i != nItem)
					{
						if(m_listconfig.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED)
						{
							m_listconfig.SetItemState(i, 0, LVIS_SELECTED|LVIS_FOCUSED);
						}
						if(m_listconfig.GetCheck(i))
						{
							m_vec_onenetdec[i].Enable = true;
							++select_c;
						}
					}
 				}
				if (select_c >= SDK_NET_MAX_MULTINET_NUM)
				{
					m_listconfig.SetCheck(nItem, FALSE);
					m_vec_onenetdec[nItem].Enable = false;
					CString strText = _CS("DigitalChannel.MaxMultiNet") + _T(": 8");
					MessageBox(strText);
				}
				else
				{
					m_vec_onenetdec[nItem].Enable = true;
				}
			}
		}
	}

	*pResult = 0;
}

void CDigitalChannelDlg::OnNMDblclkListSetting(NMHDR *pNMHDR, LRESULT *pResult)
{
	int nCurChannelNum = GetComboBoxData(IDC_COMBO_CHANNEL);
	if(nCurChannelNum == ((CClientDemo5Dlg*)AfxGetMainWnd())->GetSysInfo()->iDigChannel)
	{
		nCurChannelNum =((CClientDemo5Dlg*)AfxGetMainWnd())->GetSysInfo()->byChanNum;
	}
	int nSelect = m_listconfig.GetSelectionMark();
	if( m_listconfig.GetItemCount() < 0 || nSelect < 0)
	{
		return;
	}

		if(nSelect >= (int)m_vec_onenetdecV4.size())
		{
			return;
		}

	CRemoteChannelDlg dlg;
	dlg.m_searchNum = m_searchNum;

	memcpy(dlg.m_DeviceInfo, m_DeviceInfo, 64 * sizeof(NET_SDK_SEARCH_DEVINFO));
	memcpy(dlg.m_DevicePro, m_DevicePro, 64 * sizeof(int));

	dlg.SetConfig(m_vec_onenetdecV4[nSelect]);

	
	if (dlg.DoModal() == IDOK)
	{

			CString no = _T("");
			NET_SDK_NetDecorderConfig_V4 netdcf;
			dlg.GetOneCfg(&netdcf);
		
		


			for (int nch = 0; nch < SDK_NET_MAX_CHANNUM; ++nch)
			{
				for(int ncfcnt = 0; ncfcnt < 4; ++ncfcnt)
				{
					
					if ((strcmp(m_netdeccallV4.DigitChnConf[nch].vNetDecorderConf[ncfcnt].Address, netdcf.Address) == 0) && (m_netdeccallV4.DigitChnConf[nch].vNetDecorderConf[ncfcnt].Port==netdcf.Port))
					{
						//如果和当前通道相同就跳出
						if(nCurChannelNum==nch)
						{
							break;
						}
					
						CString cstrTip = _CS("DigitalChannel.Existed");
						CString cstrSplit = _T(" : ");
						CString cstrShow = _CS("DigitalChannel.Channel");
						CString cstrChn;
						cstrChn.Format(" %d", nch + 1);
						CString cstrText = cstrTip + cstrSplit + cstrShow + cstrChn;
						MessageBox(cstrText, _CS("OneClick.Prompt"));
						((CComboBox*)GetDlgItem(IDC_COMBO_CHANNEL))->SetCurSel(nch);
						StructToWinData(nch, true);
						m_nLastChannelNum = nch;
						return;
					}


				}
			}


			memcpy(&m_vec_onenetdecV4[nSelect], &netdcf, sizeof(NET_SDK_NetDecorderConfig_V4));

			m_listconfig.SetItemText(nSelect, 1, m_vec_onenetdecV4[nSelect].ConfName);
			m_listconfig.SetItemText(nSelect, 2, dev_type_array[m_vec_onenetdecV4[nSelect].DevType]);
			m_listconfig.SetItemText(nSelect, 3, m_vec_onenetdecV4[nSelect].Address);
			no.Format( "%d" , m_vec_onenetdecV4[nSelect].Channel + 1);
			m_listconfig.SetItemText(nSelect, 4, no);
		}
		
	
	m_searchNum = dlg.m_searchNum;
	memcpy(m_DeviceInfo, dlg.m_DeviceInfo, 64 * sizeof(NET_SDK_SEARCH_DEVINFO));
	memcpy(m_DevicePro, dlg.m_DevicePro, 64 * sizeof(int));
	*pResult = 0;
}

void CDigitalChannelDlg::OnBnClickedButtonRefrush()
{
	int nRet = 0;
	nRet = ((CClientDemo5Dlg*)AfxGetMainWnd())->GetNetDecorderConfig(&m_netdeccallV4);
	
	if ( 1 == nRet || 2 == nRet )
	{
	
			if ( nRet == 1 )
			{
				memcpy(&m_oldnetdeccallV4, &m_netdeccallV4, sizeof(NET_SDK_NetDecorderConfigAll_V4));
			}
			else //原先就获取过
			{
				memcpy(&m_netdeccallV4, &m_oldnetdeccallV4, sizeof(NET_SDK_NetDecorderConfigAll_V4));
			}

		StructToWinData();
	}
}

void CDigitalChannelDlg::OnEnChangeEditTouttime()
{

	if(m_tour_time < 10)
	{
		m_tour_time = 10;
	}
	else if (m_tour_time > 600)
	{
		m_tour_time = 600;
	}
}




