// VH73BData.cpp : implementation file
//

#include "stdafx.h"
#include "hhda.h"
#include "hhdaDlg.h"
#include "hhdaData.h"
#include "Progress.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


//////////////////////////////////////////////////////////////////////////
HWND	gDataDownHwnd	=	NULL;	//数据下载窗体句柄
Progress		*pgDlgPress		=	NULL;//窗体的指针
void *phhdaDataThis = NULL;
/////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
static void PopupMenu()
{
	CPoint point;
	GetCursorPos( & point );
	CMenu menuPopup;
	menuPopup.CreatePopupMenu();
	
	//menuPopup.AppendMenu( MF_STRING, 100, _T("Export"));
	menuPopup.AppendMenu( MF_STRING, 100, theApp.g_LoadString("IDWQB_D0157"));
	
	//menuPopup.AppendMenu( MF_SEPARATOR );
	//menuPopup.AppendMenu( MF_STRING, 101, _T("Exit"));
	
	int nCmd = (int) menuPopup.TrackPopupMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD, point.x, point.y, AfxGetMainWnd() );
	switch( nCmd )
	{
	case	100:
		//AfxMessageBox(_T("退出"));
		//CMainFrame::OnEditComm ();
		//::SendMessage(FrameHwnd, WM_CLOSE, 0, 0);
		::PostMessage(gDataDownHwnd,WM_DOWNLOADDATAMSG,0,0);	//导出TXT
		break;
		//case	101:
		//	::PostMessage(gEcpISO6C,WM_CLOSE,0,0);
		//	break;
		
	default:
		break;
	}
}

// VH73BData property page

IMPLEMENT_DYNCREATE(VH73BData, CPropertyPage)

VH73BData::VH73BData() : CPropertyPage(VH73BData::IDD)
{
	//{{AFX_DATA_INIT(VH73BData)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

VH73BData::~VH73BData()
{
}

void VH73BData::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(VH73BData)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(VH73BData, CPropertyPage)
	//{{AFX_MSG_MAP(VH73BData)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_DOWNLOAD, OnButtonDownload)
	ON_BN_CLICKED(IDC_BUTTON_HIGH, OnButtonHigh)
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
	ON_BN_CLICKED(IDC_BUTTON_LISTADD, OnButtonListadd)
	ON_BN_CLICKED(IDC_BUTTON_LISTDEL, OnButtonListdel)
	ON_BN_CLICKED(IDC_BUTTON_GetName, OnBUTTONGetName)
	ON_BN_CLICKED(IDC_BUTTON_DelName, OnBUTTONDelName)
	ON_BN_CLICKED(IDC_BUTTON_AddName, OnBUTTONAddName)
	ON_BN_CLICKED(IDC_BUTTON_SaveName, OnBUTTONSaveName)
	ON_BN_CLICKED(IDC_CHECK_READRECORD, OnCheckReadRecord)
	ON_BN_CLICKED(IDC_BUTTON_DelName2, OnBUTTONDelName2)
	ON_BN_CLICKED(IDC_BUTTON_DATATEST, OnButtonDatatest)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_DOWNLOADDATAMSG, OnDownLoadmsg)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// VH73BData message handlers

void VH73BData::OnInitUIText()	//更新语言
{
	SetWindowText(theApp.g_LoadString("IDWQE_E0001"));//蓝牙界面
	
	GetDlgItem(IDC_STATIC_LISTDOWNLOAD)->SetWindowText(theApp.g_LoadString("IDWQE_E0002"));//蓝牙名称
	GetDlgItem(IDC_STATIC_STARTTIME)->SetWindowText(theApp.g_LoadString("IDWQE_E0003"));//起始时间
	GetDlgItem(IDC_STATIC_ENDTIME)->SetWindowText(theApp.g_LoadString("IDWQE_E0004"));//载止时间
	GetDlgItem(IDC_BUTTON_DOWNLOAD)->SetWindowText(theApp.g_LoadString("IDWQE_E0005"));//下载
	GetDlgItem(IDC_BUTTON_DEL)->SetWindowText(theApp.g_LoadString("IDWQE_E0006"));//删除
	GetDlgItem(IDC_STATIC_NAMELIST)->SetWindowText(theApp.g_LoadString("IDWQE_E0007"));//名单列表
	GetDlgItem(IDC_BUTTON_LISTADD)->SetWindowText(theApp.g_LoadString("IDWQE_E0008"));//增加
	GetDlgItem(IDC_BUTTON_LISTDEL)->SetWindowText(theApp.g_LoadString("IDWQE_E0009"));//删除
	GetDlgItem(IDC_BUTTON_GetName)->SetWindowText(theApp.g_LoadString("IDWQE_E0010"));//获取名单
	GetDlgItem(IDC_BUTTON_DelName)->SetWindowText(theApp.g_LoadString("IDWQE_E0011"));//删除名单
	GetDlgItem(IDC_BUTTON_AddName)->SetWindowText(theApp.g_LoadString("IDWQE_E0012"));//增加名单
	GetDlgItem(IDC_BUTTON_SaveName)->SetWindowText(theApp.g_LoadString("IDWQE_E0013"));//保存名单
	GetDlgItem(IDC_CHECK_READRECORD)->SetWindowText(theApp.g_LoadString("IDWQE_E0061"));//是否读取全部记录
	GetDlgItem(IDC_BUTTON_DelName2)->SetWindowText(theApp.g_LoadString("IDWQE_E0062"));//删除单个名单
	GetDlgItem(IDC_BUTTON_HIGH)->SetWindowText(theApp.g_LoadString("IDWQE_E0063"));//高速下载
	

}

void VH73BData::UpdateStatus()//控制使能与不使能
{
	
	GetDlgItem(IDC_BUTTON_DOWNLOAD)->EnableWindow(ifConnectReader);
	GetDlgItem(IDC_BUTTON_DEL)->EnableWindow(ifConnectReader);
	//GetDlgItem(IDC_BUTTON_LISTADD)->EnableWindow(ifConnectReader);
	//GetDlgItem(IDC_BUTTON_LISTDEL)->EnableWindow(ifConnectReader);
	GetDlgItem(IDC_BUTTON_GetName)->EnableWindow(ifConnectReader);
	GetDlgItem(IDC_BUTTON_DelName)->EnableWindow(ifConnectReader);
	GetDlgItem(IDC_BUTTON_AddName)->EnableWindow(ifConnectReader);
	GetDlgItem(IDC_BUTTON_SaveName)->EnableWindow(ifConnectReader);
	GetDlgItem(IDC_BUTTON_DelName2)->EnableWindow(ifConnectReader);

	LVCOLUMN m_vcolumn;
	CString strText = _T("");
	DWORD dwE	=	0;
	BOOL bFlag	=	FALSE;
	CListCtrl *pListLabel	=	(CListCtrl*)GetDlgItem(IDC_LIST_LABLEID);
	
	
	strText = theApp.g_LoadString("IDWQE_E0054");
	m_vcolumn.mask = LVCF_TEXT;
	m_vcolumn.pszText =strText.GetBuffer(0);
	m_vcolumn.cchTextMax = strText.GetLength();
	dwE = pListLabel->SetColumn(0,&m_vcolumn);


	strText = theApp.g_LoadString("IDWQE_E0055");
	m_vcolumn.mask = LVCF_TEXT;
	m_vcolumn.pszText =strText.GetBuffer(0);
	m_vcolumn.cchTextMax = strText.GetLength();
	dwE = pListLabel->SetColumn(1,&m_vcolumn);

	

	CString strTemp0 = theApp.g_LoadString("IDWQE_E0056");//NO.
	CString strTemp1 = theApp.g_LoadString("IDWQE_E0057");//Date
	CString strTemp2 = theApp.g_LoadString("IDWQE_E0058");//EPC
	CString strTemp3 = theApp.g_LoadString("IDWQE_E0059");//TID
	CString strTemp4 = theApp.g_LoadString("IDWQE_E0060");//USER

	CListCtrl *pListID	=	(CListCtrl*)GetDlgItem(IDC_LIST_ID);
	//pListID->DeleteAllItems();

	//bFlag	=	pListID->GetColumn(1, &listColumnTemp);
	//DWORD dwE = GetLastError();
	strText = theApp.g_LoadString("IDWQE_E0056");
	m_vcolumn.mask = LVCF_TEXT;
	m_vcolumn.pszText =strText.GetBuffer(0);
	m_vcolumn.cchTextMax = strText.GetLength();
	dwE = pListID->SetColumn(0,&m_vcolumn);

	
	strText = theApp.g_LoadString("IDWQE_E0057");
	m_vcolumn.mask = LVCF_TEXT;
	m_vcolumn.pszText =strText.GetBuffer(0);
	m_vcolumn.cchTextMax = strText.GetLength();
	dwE = pListID->SetColumn(1,&m_vcolumn);


	strText = theApp.g_LoadString("IDWQE_E0058");
	m_vcolumn.mask = LVCF_TEXT;
	m_vcolumn.pszText =strText.GetBuffer(0);
	m_vcolumn.cchTextMax = strText.GetLength();
	dwE = pListID->SetColumn(2,&m_vcolumn);


	strText = theApp.g_LoadString("IDWQE_E0059");
	m_vcolumn.mask = LVCF_TEXT;
	m_vcolumn.pszText =strText.GetBuffer(0);
	m_vcolumn.cchTextMax = strText.GetLength();
	dwE = pListID->SetColumn(3,&m_vcolumn);


	strText = theApp.g_LoadString("IDWQE_E0060");
	m_vcolumn.mask = LVCF_TEXT;
	m_vcolumn.pszText =strText.GetBuffer(0);
	m_vcolumn.cchTextMax = strText.GetLength();
	dwE = pListID->SetColumn(4,&m_vcolumn);
	
	return;

}

BOOL VH73BData::OnSetActive() 
{
	// TODO: Add your specialized code here and/or call the base class
	//////////////////////////////////////////////////////////////////////////
	UpdateStatus();//控制使能与不使能

	return CPropertyPage::OnSetActive();
}

BOOL VH73BData::OnKillActive() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CPropertyPage::OnKillActive();
}

BOOL VH73BData::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	// TODO: Add extra initialization here
	gDataDownHwnd	=	GetSafeHwnd();	//数据下载窗体句柄
	phhdaDataThis   =   this;

	//EPC,TID,DATE
	CListCtrl *pListID	=	(CListCtrl*)GetDlgItem(IDC_LIST_ID);
	DWORD dwStyle = pListID->SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
	dwStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
	pListID->SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)dwStyle);
	pListID->DeleteAllItems();


	//插入列的标题
    pListID->InsertColumn( 0, "NO.", LVCFMT_LEFT, 40 );
    pListID->InsertColumn( 1, "Date", LVCFMT_LEFT, 140 );
    pListID->InsertColumn( 2, "EPC", LVCFMT_LEFT, 160 );
	pListID->InsertColumn( 3, "TID", LVCFMT_LEFT, 140 );
	pListID->InsertColumn( 4, "USER", LVCFMT_LEFT, 120 );

	//名单列表
	CListCtrl *pListLabel	=	(CListCtrl*)GetDlgItem(IDC_LIST_LABLEID);
	DWORD dwStylf = pListLabel->SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
	dwStylf |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
	pListLabel->SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)dwStyle);
	pListLabel->DeleteAllItems();
	
	
	//插入列的标题
    pListLabel->InsertColumn(0, "No.", LVCFMT_LEFT, 60);
	pListLabel->InsertColumn(1, "LableID", LVCFMT_LEFT, 200);
	

	//初始化开始时间和结束时间
	CDateTimeCtrl *PDateFrom=(CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_STARTTIME);
	CDateTimeCtrl *PDateTo=(CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_ENDTIME);
	PDateFrom->SetFormat("yyyy-MM-dd HH:mm:ss");
	SYSTEMTIME systime;
	GetLocalTime(&systime);//本地时间
	systime.wDay=1;
	systime.wHour=0;
	systime.wMinute=0;
	systime.wSecond=0;
	PDateFrom->SetTime(&systime);
	PDateTo->SetFormat("yyyy-MM-dd HH:mm:ss");
	
	GetLocalTime(&systime);//本地时间
	systime.wHour=23;
	systime.wMinute=59;
	systime.wSecond=59;
	PDateTo->SetTime(&systime);

	//////////////////////////////////////////////////////////////////////////
	UpdateStatus();//控制使能与不使能

	GetDlgItem(IDC_BUTTON_DATATEST)->ShowWindow(SW_HIDE);
	//数据界面
	::PostMessage(gDataDownHwnd,WM_DOWNLOADDATAMSG,2,0);	//convert language
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void VH73BData::OnDestroy() 
{
	CPropertyPage::OnDestroy();
	
	// TODO: Add your message handler code here
	
}

void VH73BData::OnCheckReadRecord() 
{
	// TODO: Add your control notification handler code here
	//是否显示调试窗口
	RECT rcTemp	=	{0,0,0,};
	//初始化开始时间和结束时间
	CDateTimeCtrl *PDateFrom=(CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_STARTTIME);
	CDateTimeCtrl *PDateTo=(CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_ENDTIME);

	if ( ((CButton*)GetDlgItem(IDC_CHECK_READRECORD))->GetCheck() == 1 )
	{
		//选中，显示调试输出
		PDateFrom->EnableWindow(FALSE);
		PDateTo->EnableWindow(FALSE);

	}
	else
	{
		PDateFrom->EnableWindow(TRUE);
		PDateTo->EnableWindow(TRUE);
		
	}
	
}

void VH73BData::OnButtonDownload() 
{
	// TODO: Add your control notification handler code here
	//下载
	UpdateData(TRUE);
	int   nYear,nMonth,nDate,nHou,nMin,nSec;
	apiReturn res;
	int i,getcount,nTotal;
	int recordlen	=	0;
	int j=0;
	BYTE stime[6],etime[6],data[254*10];
	CString str,strtmp;
	int nS,nRow;

	int nRecordNums = 0;//记录总数
	int niDiRecord = 0;//第几条

	TagData tag;
	
	if(!ifConnectReader)
	{
		//MessageBox("Can't connect a handset !","Notice",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_B0015"), theApp.g_LoadString("IDWQE_B0016"), MB_OK | MB_ICONERROR);
		return;
	}

	CDateTimeCtrl *PDateFrom=(CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_STARTTIME);
	CDateTimeCtrl *PDateTo=(CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_ENDTIME);

	CString m_StartTime;
	CString m_EndTime;
	SYSTEMTIME system;
	PDateFrom->GetTime(&system);
	m_StartTime.Format("%04d-%02d-%02d %02d:%02d:%02d",system.wYear,system.wMonth,system.wDay,system.wHour,system.wMinute,system.wSecond);
	//sTime="2008-06-01 00:00:00";
	
	PDateTo->GetTime(&system);
	//eTime="2008-06-30 23:59:59";
	m_EndTime.Format("%04d-%02d-%02d %02d:%02d:%02d",system.wYear,system.wMonth,system.wDay,system.wHour,system.wMinute,system.wSecond);

	if ( ((CButton*)GetDlgItem(IDC_CHECK_READRECORD))->GetCheck() == 1 )
	{
		m_StartTime = _T("");
		m_EndTime = _T("");
	}

	if((m_StartTime.GetLength()==0)&&(m_EndTime.GetLength()==0))
	{
		stime[0]=0;
		stime[1]=0;
		stime[2]=0;
		stime[3]=0;
		stime[4]=0;
		stime[5]=0;

		etime[0]=0;
		etime[1]=0;
		etime[2]=0;
		etime[3]=0;
		etime[4]=0;
		etime[5]=0;
	}
	else
	{
		if(m_StartTime.GetLength()==0)
		{
			return;
		}
		if(m_EndTime.GetLength()==0)
		{
			return;
		}
		sscanf(m_StartTime, "%d-%d-%d %d:%d:%d",&nYear,&nMonth,&nDate,&nHou,&nMin,&nSec);   
	    CTime BeginTime(nYear,   nMonth,   nDate,   nHou,   nMin,   nSec); 

		sscanf(m_EndTime, "%d-%d-%d %d:%d:%d",&nYear,&nMonth,&nDate,&nHou,&nMin,&nSec);   
	    CTime   EndTime(nYear,   nMonth,   nDate,   nHou,   nMin,   nSec); 

		if(BeginTime>EndTime)
		{
			//MessageBox("End Time must be greater then Start Time  !","Notice",MB_ICONWARNING);
			MessageBox(theApp.g_LoadString("IDWQE_E0014"), theApp.g_LoadString("IDWQE_E0015"), MB_ICONWARNING);
			return;
		}
		stime[0]=BeginTime.GetYear()%100;
		stime[1]=BeginTime.GetMonth();
		stime[2]=BeginTime.GetDay();
		stime[3]=BeginTime.GetHour();
		stime[4]=BeginTime.GetMinute();
		stime[5]=BeginTime.GetSecond(); 

		etime[0]=EndTime.GetYear()%100;
		etime[1]=EndTime.GetMonth();
		etime[2]=EndTime.GetDay();
		etime[3]=EndTime.GetHour();
		etime[4]=EndTime.GetMinute();
		etime[5]=EndTime.GetSecond(); 
	}

	//先发一个总数记录
	res = VH_GetRecordNum(m_hScanner, (ReaderDate *)stime, (ReaderDate *)etime, &nRecordNums);
	if ( res != _OK )
	{
		//MessageBox("Download false!", "Notice",MB_ICONINFORMATION);
		MessageBox(theApp.g_LoadString("IDWQE_E0016"), theApp.g_LoadString("IDWQE_E0017"), MB_ICONINFORMATION);
		return;
	}


	//进度条准备工作
	if ( NULL != pgDlgPress )
	{
		pgDlgPress->ShowWindow(SW_SHOW);
		//nRecordNums = 6;//this is a test!!!
		pgDlgPress->m_Progress.SetRange32(0, nRecordNums);
		pgDlgPress->m_Progress.SetStep(1);
		pgDlgPress->m_Progress.SetPos(niDiRecord++);
		
	}

	CListCtrl *m_IDList	=	(CListCtrl*)GetDlgItem(IDC_LIST_ID);
	m_IDList->DeleteAllItems();

	getcount=0;

	while(1)
	{
		//res = GetRecord(m_hScanner, (ReaderDate *)stime, (ReaderDate *)etime, &nTotal, &recordlen, data);
		res = VH_GetRecord(m_hScanner, (ReaderDate *)stime, (ReaderDate *)etime, &nTotal, &niDiRecord, data);
		if(res != _OK)
		{
			//MessageBox("Download false!", "Notice",MB_ICONINFORMATION);
			if ( NULL != pgDlgPress )
			{
				pgDlgPress->ShowWindow(SW_HIDE);
			}
			MessageBox(theApp.g_LoadString("IDWQE_E0016"), theApp.g_LoadString("IDWQE_E0017"), MB_ICONINFORMATION);

			///输入参数:	lOpt 为何种方式通信(0--TCP/IP, 1--RS232,2--RS485, 3--USB)
			switch ( ConnectMode )
			{
			case 1://RS232
				//断开读写器
				DisconnectScanner(m_hScanner);
				break;
			case 3://USB
				//断开读写器
				VH_DisconnectScannerUsb(m_hScanner);
				break;
				
			}
			
			ifConnectReader=0;
			NewConnect=0;
			m_HardVersion=0;
			m_SoftVersion=0;
			
			::PostMessage(gMainHwnd,WM_LISTUSERDATA,3,0);	//跳到第一个界面

			break;
		}

		if((getcount==0)&&(nTotal==0))
		{
			//MessageBox("Empty record in handset!","Notice",MB_ICONINFORMATION);
			if ( NULL != pgDlgPress )
			{
				pgDlgPress->ShowWindow(SW_HIDE);
			}
			MessageBox(theApp.g_LoadString("IDWQE_E0018"), theApp.g_LoadString("IDWQE_E0019"), MB_ICONINFORMATION);

			///输入参数:	lOpt 为何种方式通信(0--TCP/IP, 1--RS232,2--RS485, 3--USB)
			switch ( ConnectMode )
			{
			case 1://RS232
				//断开读写器
				DisconnectScanner(m_hScanner);
				break;
			case 3://USB
				//断开读写器
				VH_DisconnectScannerUsb(m_hScanner);
				break;
				
			}
			
			ifConnectReader=0;
			NewConnect=0;
			m_HardVersion=0;
			m_SoftVersion=0;
			
			::PostMessage(gMainHwnd,WM_LISTUSERDATA,3,0);	//跳到第一个界面

			break;
		}

		if(nTotal == 0)
		{
			break;
		}

		//进度提示
		if ( NULL != pgDlgPress )
		{
			pgDlgPress->m_Progress.SetPos(niDiRecord++);
		}

		//if ( niDiRecord == nTotal )
		//{
		//	break;
		//}

		//for(i=0;i<nTotal;i++)
		for(i=0;i<1;i++)
		{
			nS=m_IDList->GetItemCount();
			str.Format("%d",nS+1);
			nRow=m_IDList->InsertItem(nS,str);//改为0从第一行开始插入

			memset(&tag,0,250);
			memcpy(&tag,&data[i*250+4],250);

			str.Format("%04d-%02d-%02d %02d:%02d:%02d",tag.DateTime[0]+2000,tag.DateTime[1],tag.DateTime[2],tag.DateTime[3],tag.DateTime[4],tag.DateTime[5]); 
			m_IDList->SetItemText(nRow,1,str);

			if(tag.TagType==0x04)
			{
				str.Empty(); 
				for(j=0;j<tag.EPCLen*2;j++)
				{
					strtmp.Format("%02X",tag.EPC[j]);
					str+=strtmp;
				}
				m_IDList->SetItemText(nRow,2,str); 

				str.Empty();
				for(j=0;j<tag.TIDLen*2;j++)
				{
					strtmp.Format("%02X",tag.TID[j]);
					str+=strtmp;
				}
				m_IDList->SetItemText(nRow,3,str); 

				str.Empty();
				for(j=0;j<tag.USERLen*2;j++)
				{
					strtmp.Format("%02X",tag.USER[j]);
					str+=strtmp;
				}
				m_IDList->SetItemText(nRow,4,str); 
			}
			else
			{
				str.Empty(); 
				for(j=0;j<tag.EPCLen*2;j++)
				{
					strtmp.Format("%02X",tag.EPC[j]);
					str+=strtmp;
				}
				m_IDList->SetItemText(nRow,2,str); 

				str.Empty();
				for(j=0;j<tag.USERLen*2;j++)
				{
					strtmp.Format("%02X",tag.USER[j]); 
					str+=strtmp;
				}
				m_IDList->SetItemText(nRow,4,str); 
			}
		}

		
		getcount++;
	}

	if((getcount != 0)&&(res == _OK))
	{
		CString sTot;
		CString sTit;
		sTot.Format("%02d %02d", niDiRecord,getcount);
		//str	=	_T("Download successfully!");
		//sTit=	_T("Notice");
		str	=	theApp.g_LoadString("IDWQE_E0020");
		sTit=	theApp.g_LoadString("IDWQE_E0021");

		str	+=	sTot;
		if ( NULL != pgDlgPress )
		{
			pgDlgPress->ShowWindow(SW_HIDE);
		}
		MessageBox(str, sTit, MB_ICONINFORMATION);
	}

	UpdateData(FALSE);

	return;
}

//高速下载数据，取记录数据
short _stdcall VH_GetRecordData(BYTE * data)
{
	VH73BData *pThis = (VH73BData*)phhdaDataThis;
	pThis->JieXieData(data);

	return 0;
}

void VH73BData::JieXieData(BYTE * data)
{
	int recordlen	=	0;
	int j=0;
	CString str,strtmp;
	int nS,nRow;
	
	TagData tag;
	CListCtrl *m_IDList	=	(CListCtrl*)GetDlgItem(IDC_LIST_ID);

	nS=m_IDList->GetItemCount();
	str.Format("%d",nS+1);
	nRow=m_IDList->InsertItem(nS,str);//改为0从第一行开始插入
	
	memset(&tag,0,250);
	memcpy(&tag,&data[5],250);
	
	str.Format("%04d-%02d-%02d %02d:%02d:%02d",tag.DateTime[0]+2000,tag.DateTime[1],tag.DateTime[2],tag.DateTime[3],tag.DateTime[4],tag.DateTime[5]); 
	m_IDList->SetItemText(nRow,1,str);
	
	if(tag.TagType==0x04)
	{
		str.Empty(); 
		for(j=0;j<tag.EPCLen*2;j++)
		{
			strtmp.Format("%02X",tag.EPC[j]);
			str+=strtmp;
		}
		m_IDList->SetItemText(nRow,2,str); 
		
		str.Empty();
		for(j=0;j<tag.TIDLen*2;j++)
		{
			strtmp.Format("%02X",tag.TID[j]);
			str+=strtmp;
		}
		m_IDList->SetItemText(nRow,3,str); 
		
		str.Empty();
		for(j=0;j<tag.USERLen*2;j++)
		{
			strtmp.Format("%02X",tag.USER[j]);
			str+=strtmp;
		}
		m_IDList->SetItemText(nRow,4,str); 
	}
	else
	{
		str.Empty(); 
		for(j=0;j<tag.EPCLen*2;j++)
		{
			strtmp.Format("%02X",tag.EPC[j]);
			str+=strtmp;
		}
		m_IDList->SetItemText(nRow,2,str); 
		
		str.Empty();
		for(j=0;j<tag.USERLen*2;j++)
		{
			strtmp.Format("%02X",tag.USER[j]); 
			str+=strtmp;
		}
		m_IDList->SetItemText(nRow,4,str); 
	}

	return;
}

void VH73BData::OnButtonHigh() 
{
	// TODO: Add your control notification handler code here

	CListCtrl *m_IDList	=	(CListCtrl*)GetDlgItem(IDC_LIST_ID);
	m_IDList->DeleteAllItems();


	//高速下载
	VH_GetRecordHigh(m_hScanner, VH_GetRecordData);

	return;
}

void VH73BData::OnButtonDel() 
{
	// TODO: Add your control notification handler code here
	//删除
	if(!ifConnectReader)
	{
		//MessageBox("Can't connect a handset !","Notice",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_B0015"), theApp.g_LoadString("IDWQE_B0016"), MB_OK | MB_ICONERROR);
		return;
	}
	apiReturn res;
	int k;
	//k=MessageBox("Are you sure to erase all records in handset?","Notice",MB_YESNO|MB_ICONWARNING);
	k=MessageBox(theApp.g_LoadString("IDWQE_E0022"), theApp.g_LoadString("IDWQE_E0023"), MB_YESNO|MB_ICONWARNING);
	if (k!=IDYES)
	{
		return;
	}
	res=DeleteAllRecord(m_hScanner);
	if(res == _OK)
	{
		//MessageBox("Erase successfully!","Notice",MB_ICONINFORMATION);
		CListCtrl *m_IDList	=	(CListCtrl*)GetDlgItem(IDC_LIST_ID);
		m_IDList->DeleteAllItems();
		MessageBox(theApp.g_LoadString("IDWQE_E0024"), theApp.g_LoadString("IDWQE_E0025"), MB_ICONINFORMATION);
	}
	else
	{
		//MessageBox("fail to erase records in handset !","Notice",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_E0026"), theApp.g_LoadString("IDWQE_E0027"), MB_ICONWARNING);
	}

	return;
}

void VH73BData::OnButtonListadd() 
{
	// TODO: Add your control notification handler code here
	//名单增加
	UpdateData(TRUE);
	
	CString str;
	CString	m_LableID;

	GetDlgItem(IDC_EDIT2)->GetWindowText(m_LableID);	
	str=m_LableID.SpanIncluding("0123456789ABCDEFabcdef");
	if(str.GetLength()!=m_LableID.GetLength())
	{
		//MessageBox("Please input HEX!","Warning",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_E0028"), theApp.g_LoadString("IDWQE_E0029"), MB_ICONWARNING);
		GetDlgItem(IDC_EDIT2)->SetFocus();
		((CEdit *) GetDlgItem(IDC_EDIT2))->SetSel(0,-1);
		UpdateData(FALSE);
		return;
	}
	
	if((str.GetLength()==0)||((str.GetLength()%4)!=0))
	{
		//MessageBox("EPC号的长度必须是4的倍数","Warning",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_E0030"), theApp.g_LoadString("IDWQE_E0031"), MB_ICONWARNING);
		return;
	}
	
	//名单列表
	CListCtrl *m_LableList	=	(CListCtrl*)GetDlgItem(IDC_LIST_LABLEID);
	int count=m_LableList->GetItemCount();
	
	str.Format("%d",count+1); 
	int row=m_LableList->InsertItem(count,str); 
	
    m_LableList->SetItemText(row,1,m_LableID); 

	return;
}

void VH73BData::OnButtonListdel() 
{
	// TODO: Add your control notification handler code here
	//名单删除
	//名单列表
	CListCtrl *m_LableList	=	(CListCtrl*)GetDlgItem(IDC_LIST_LABLEID);

	POSITION sSelPos = NULL;
	while(sSelPos = m_LableList->GetFirstSelectedItemPosition())
	{
		int nSelItem = -1;
		nSelItem = m_LableList->GetNextSelectedItem(sSelPos);
		if(nSelItem >= 0 && nSelItem<m_LableList->GetItemCount())
		{
			m_LableList->DeleteItem(nSelItem); 
		}
	}

	return;

}

void VH73BData::OnBUTTONGetName() 
{
	// TODO: Add your control notification handler code here
	//获取名单
	apiReturn res;
	int nAddr,nCount,nTotal,nDataLen;
	BYTE data[32*17];
	int dchar;
	CString str;
	int nS,nRow;
	int getcount=0;
	
	if(!ifConnectReader)
	{
		//MessageBox("Can't connect a handset !","Notice",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_B0015"), theApp.g_LoadString("IDWQE_B0016"), MB_OK | MB_ICONERROR);
		return;
	}
	
	//名单列表
	CListCtrl *m_LableList	=	(CListCtrl*)GetDlgItem(IDC_LIST_LABLEID);
	m_LableList->DeleteAllItems();
	nAddr=0;
	while(1)
	{
		nCount=0;
		memset(data,0,32*17);
		res = GetLableID(m_hScanner,nAddr,nCount, &nTotal, &nDataLen, data);
		if(res != _OK)
		{
			//MessageBox("Download false!", "Notice",MB_ICONINFORMATION);
			MessageBox(theApp.g_LoadString("IDWQE_E0032"), theApp.g_LoadString("IDWQE_E0033"), MB_ICONINFORMATION);
			break;
		}
		
		if((getcount==0)&&(nTotal==0))
		{
			//MessageBox("Empty LableID in handset!","Notice",MB_ICONINFORMATION);
			MessageBox(theApp.g_LoadString("IDWQE_E0034"), theApp.g_LoadString("IDWQE_E0035"), MB_ICONINFORMATION);
			break;
		}
		
		int k = 0;
		int len=0;
		int sumlen=0;
		CString olddata;
		//len=data[k]*2;
		sumlen=1;
		for(int i=0;i<nTotal;i++)
		{
			nS=m_LableList->GetItemCount();
			str.Format("%d",nS+1);
			nRow=m_LableList->InsertItem(nS,str);
			
			str.Empty(); 
			len=data[k];
			for(int j=0;j<len*2;j++)
			{
				dchar=data[k+1+j]>>4;
				olddata.Format("%X",dchar);
				str=str+olddata;
				dchar=data[k+1+j]&0x0F;
				olddata.Format("%X",dchar);
				str=str+olddata;
			}
			m_LableList->SetItemText(nRow,1,str);

			k += len*2+1;
		}
		
		if(nTotal == 0)
		{
			break;
		}
		else
		{
			nAddr=nAddr+nTotal;
		}
		
		getcount++;
	}
	
	if((getcount != 0)&&(res == _OK))
	{
		str.Format(theApp.g_LoadString("IDWQE_E0036"));
		//MessageBox(str,"Notice",MB_ICONINFORMATION);
		MessageBox(str,theApp.g_LoadString("IDWQE_E0037"), MB_ICONINFORMATION);
	}
	
	UpdateData(FALSE);

	return;
}

void VH73BData::OnBUTTONDelName() 
{
	// TODO: Add your control notification handler code here
	//删除名单
	apiReturn res;
	int k;
	
	if(!ifConnectReader)
	{
		//MessageBox("Can't connect a handset !","Notice",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_B0015"), theApp.g_LoadString("IDWQE_B0016"), MB_OK | MB_ICONERROR);
		return;
	}
	
	//k=MessageBox("Are you sure to erase all LableID in handset?","Notice",MB_YESNO|MB_ICONWARNING);
	k=MessageBox(theApp.g_LoadString("IDWQE_E0038"), theApp.g_LoadString("IDWQE_E0039"), MB_YESNO|MB_ICONWARNING);
	if (k!=IDYES) 
	{
		return;
	}
	res=DelLableID(m_hScanner);
	if(res == _OK)
	{
		//MessageBox("Erase successfully!","Notice",MB_ICONINFORMATION);
		MessageBox(theApp.g_LoadString("IDWQE_E0040"), theApp.g_LoadString("IDWQE_E0041"), MB_ICONINFORMATION);
	}
	else
	{
		//MessageBox("fail to erase LableID in handset !","Notice",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_E0042"), theApp.g_LoadString("IDWQE_E0043"), MB_ICONWARNING);
	}
	
	return;
}

void VH73BData::OnBUTTONDelName2() 
{
	// TODO: Add your control notification handler code here
	//删除单个或多个名单
	apiReturn res;
	int nCount,nLen;
	BYTE data[30*17];
	CString str;
	CString sTemp;
	int str_len;
	
	if(!ifConnectReader)
	{
		//MessageBox("Can't connect a handset !","Notice",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_B0015"), theApp.g_LoadString("IDWQE_B0016"), MB_OK | MB_ICONERROR);
		return;
	}
	
	//名单列表
	CListCtrl *m_LableList	=	(CListCtrl*)GetDlgItem(IDC_LIST_LABLEID);
	UINT uSelectedCount	=	m_LableList->GetSelectedCount();
	int nItem	=	0;
	int ilistCount	=	m_LableList->GetItemCount();
	int ilcnt		=	0;
	
	if ( uSelectedCount > 0 )
	{
		
		
		POSITION pos = m_LableList->GetFirstSelectedItemPosition();
		while (pos)
		//if ( pos )	//当多个选中时把if 改为 while
		{
			nItem = m_LableList->GetNextSelectedItem(pos);
			TRACE(_T("Item %d was Selected!\n"),nItem);
			//you could do your own processing on nItem here
	
			str=m_LableList->GetItemText(nItem,1);
			str_len=str.GetLength()/2;
			
			memset(data,0,30*17);
			for(int j=0;j<str_len;j++)
			{
				data[j+1]=(BYTE)strtol((CString)str[j*2]+(CString)str[2*j+1],NULL,16);
			}
			
			nCount=1;
			nLen=str_len+1;
			data[0]=str_len/2;
			res=DelSingleLableID(m_hScanner,nCount,nLen,data);
			if ( _OK != res )
			{
				break;
			}
		
			m_LableList->DeleteItem(nItem);
			
			
			
			for ( ilcnt = nItem; ilcnt < ilistCount-1; ilcnt++)
			{
				m_LableList->GetItemText(ilcnt, 0, sTemp.GetBuffer(MAX_PATH), MAX_PATH);
				sTemp.ReleaseBuffer();
				nItem	=	atoi(sTemp);
				sTemp.Format(_T("%d"), nItem-1);
				m_LableList->SetItemText(ilcnt, 0, sTemp);
			}
		
		}
		
	}
	else
	{
		AfxMessageBox(_T("请选中表格中有数据的行!"));
		
	}


	if(res == _OK)
	{
		//MessageBox("Erase successfully!","Notice",MB_ICONINFORMATION);
		MessageBox(theApp.g_LoadString("IDWQE_E0040"), theApp.g_LoadString("IDWQE_E0041"), MB_ICONINFORMATION);
	}
	else
	{
		//MessageBox("fail to erase LableID in handset !","Notice",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_E0042"), theApp.g_LoadString("IDWQE_E0043"), MB_ICONWARNING);
	}
	
	return;
}

void VH73BData::OnBUTTONAddName() 
{
	// TODO: Add your control notification handler code here
	//增加名单
	apiReturn res;
	int nCount,nLen;
	BYTE data[30*17];
	CString str;
	int str_len;
	
	if(!ifConnectReader)
	{
		//MessageBox("Can't connect a handset !","Notice",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_B0015"), theApp.g_LoadString("IDWQE_B0016"), MB_OK | MB_ICONERROR);
		return;
	}
	
	//名单列表
	CListCtrl *m_LableList	=	(CListCtrl*)GetDlgItem(IDC_LIST_LABLEID);
	int nRow=m_LableList->GetItemCount();
	for(int i=0;i<nRow;i++)
	{
		str=m_LableList->GetItemText(i,1);
		str_len=str.GetLength()/2;
		
		memset(data,0,30*17);
		for(int j=0;j<str_len;j++)
		{
			data[j+1]=(BYTE)strtol((CString)str[j*2]+(CString)str[2*j+1],NULL,16);
		}
		
		nCount=1;
		nLen=str_len+1;
		data[0]=str_len/2;
		res=AddLableID(m_hScanner,nCount,17,data);
		
		if(res!=_OK)
		{
			break;
		}
	}
	
	if(res!=_OK)
	{
		//MessageBox("fail to Add LableID in handset !","Notice",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_E0044"), theApp.g_LoadString("IDWQE_E0045"), MB_ICONWARNING);
	}
	else
	{
		//MessageBox("Add successfully!","Notice",MB_ICONINFORMATION);
		MessageBox(theApp.g_LoadString("IDWQE_E0046"), theApp.g_LoadString("IDWQE_E0047"), MB_ICONINFORMATION);
	}

	return;
}

void VH73BData::OnBUTTONSaveName() 
{
	// TODO: Add your control notification handler code here
	//保存名单
	apiReturn res;
	
	if(!ifConnectReader)
	{
		//MessageBox("Can't connect a handset !","Notice",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_B0015"), theApp.g_LoadString("IDWQE_B0016"), MB_OK | MB_ICONERROR);
		return;
	}
	
	res=SaveLableID(m_hScanner);
	if(res == _OK)
	{
		//MessageBox("Save successfully!","Notice",MB_ICONINFORMATION);
		MessageBox(theApp.g_LoadString("IDWQE_E0048"), theApp.g_LoadString("IDWQE_E0049"), MB_ICONINFORMATION);
	}
	else
	{
		//MessageBox("fail to save LableID in handset !","Notice",MB_ICONWARNING);
		MessageBox(theApp.g_LoadString("IDWQE_E0050"), theApp.g_LoadString("IDWQE_E0051"), MB_ICONWARNING);
	}

	return;

}

void VH73BData::OnButtonDatatest() 
{
	// TODO: Add your control notification handler code here
	
}

BOOL VH73BData::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class

	if(WM_RBUTTONDOWN == pMsg->message )
	{
		if ( pMsg->hwnd == GetDlgItem(IDC_LIST_ID)->m_hWnd )
		{
			PopupMenu();
			return TRUE;
		}
		//else if ( pMsg->hwnd == GetDlgItem(IDC_LIST2)->m_hWnd )
		//{
		//	//PopupMenuList2();
		//	return TRUE;
		//}
	}
	return CPropertyPage::PreTranslateMessage(pMsg);
}

//WM_DOWNLOADDATAMSG WM_DOWNLOADDATAMSG
afx_msg LRESULT VH73BData::OnDownLoadmsg(WPARAM wParam, LPARAM lParam)
{
	int iwParam	=	(int)wParam;
	int ilParam	=	(int)lParam;
	
	if ( 0 == iwParam )
	{
		//右键
		//导出写TXT文件
		CListCtrl *m_ListID = (CListCtrl*)GetDlgItem(IDC_LIST_ID);
			int nRow	=	m_ListID->GetItemCount();	//取得有多少行
			if ( nRow <= 0 )
			{
				if ( 0 == ilParam )
				{
					//MessageBox("List is empty!","VI-720", MB_ICONERROR|MB_OK );
					MessageBox(theApp.g_LoadString("IDWQB_D0159"),theApp.g_LoadString("IDWQB_D0142"), MB_ICONERROR|MB_OK );
				}
				return 1;
			}


			CString filename;
			//取当前日期，时间
			SYSTEMTIME sysTime;

			GetLocalTime(&sysTime);
			CString strTime;
			
			strTime.Format("%04d%02d%02d.csv", sysTime.wYear, sysTime.wMonth, sysTime.wDay);

			if ( 0 == ilParam )
			{
				//CFileDialog dlg(FALSE,NULL,NULL,OFN_EXPLORER ,_T("TXT files(*.txt)|*.TXT"));
				CFileDialog dlg(FALSE,_T("csv"),strTime,
					OFN_EXPLORER,
					_T("csv files(*.csv)|*.csv|\0"),NULL);
				
				dlg.m_ofn.lpstrTitle	=	_T("export");
				//dlg.m_ofn.lpstrTitle	=	theApp.g_LoadString("IDWQB_D0157");
				if(dlg.DoModal()==IDOK)
				{
					filename=dlg.GetPathName();	//取得全路径名
				}
				else
				{
					return 1;
				}
			}
			else 
			{
				filename = "";
				filename += g_szCurPath;
				filename += strTime;
			}

			
			int i	=	0;
			char *chRecv	=	new char[2*1024*1024];
			int chCnt	=	0;
			CString strT;

			while ( i < nRow )
			{
				//strT	=	m_ListID.GetItemText(i,0);//NO
				//memcpy(chRecv+chCnt, strT, strT.GetLength());
				//chCnt	+=	strT.GetLength();

				//*(chRecv + chCnt++)	=	',';

				*(chRecv + chCnt++)	= '\x09';	//"\x27";
				strT	=	m_ListID->GetItemText(i,1);//date
				memcpy(chRecv+chCnt, strT, strT.GetLength());
				chCnt	+=	strT.GetLength();

				*(chRecv + chCnt++)	=	',';
				*(chRecv + chCnt++)	= '\x09';
				strT	=	m_ListID->GetItemText(i,2);//ID
				memcpy(chRecv+chCnt, strT, strT.GetLength());
				chCnt	+=	strT.GetLength();

				//*(chRecv + chCnt++)	=	',';

				//strT	=	m_ListID.GetItemText(i,3);//Times
				//memcpy(chRecv+chCnt, strT, strT.GetLength());
				//chCnt	+=	strT.GetLength();

				//*(chRecv + chCnt++)	=	',';

				//strT	=	m_ListID.GetItemText(i,4);//Length
				//memcpy(chRecv+chCnt, strT, strT.GetLength());
				//chCnt	+=	strT.GetLength();
				
				//*(chRecv + chCnt++)	=	',';

				*(chRecv + chCnt++)	=	0x0D;
				*(chRecv + chCnt++)	=	0x0A;

				i++;

			}// end of while ( iCnt < nRow )


			*(chRecv + chCnt)	=	0x00;

			WriteBufFile(chRecv, chCnt, filename,0);

			delete[] chRecv;

			if ( 0 == ilParam )
			{
				//AfxMessageBox("Export finished!");
				//MessageBox("Export finished!","VI-720", MB_ICONINFORMATION|MB_OK );
				MessageBox(theApp.g_LoadString("IDWQB_D0158"),theApp.g_LoadString("IDWQB_D0158"), MB_ICONINFORMATION|MB_OK );
				ShellExecute(NULL,"open",filename,NULL,NULL,SW_SHOWNORMAL);
			

			}


		
	}
	else if ( 1 == iwParam )
	{
		if ( 0 == ilParam )
		{
			//copy
			CString source = _T("11112222");

			CListBox *m_list	=	(CListBox *)GetDlgItem(IDC_LIST2);
			
			source = "";
			int isel = m_list->GetCurSel();
			if ( isel >= 0 )
			{
				m_list->GetText(m_list->GetCurSel(), source);
			}

			if(OpenClipboard()) 
			{ 
				HGLOBAL   clipbuffer; 
				char   *   buffer; 
				EmptyClipboard(); 
				clipbuffer   =   GlobalAlloc(GMEM_DDESHARE,   source.GetLength()+1); 
				buffer   =   (char*)GlobalLock(clipbuffer); 
				lstrcpy(buffer,   LPCSTR(source)); 
				GlobalUnlock(clipbuffer); 
				SetClipboardData(CF_TEXT,clipbuffer); 
				CloseClipboard(); 

				GetDlgItem(IDC_EDIT10)->SetFocus();
				((CEdit *) GetDlgItem(IDC_EDIT10))->SetSel(0,-1);
			} 
			
		}
	}
	else if ( 2 == iwParam )
	{
		OnInitUIText();	//更新语言
	}
	else if ( 4 == iwParam )
	{
		if ( 0 == ilParam )
		{
			UpdateStatus();//控制使能与不使能
		}
	}

	return 0;
	
}


