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

#include "stdafx.h"
#include "HUNEMFTEST.h"
#include "HUNEMFTESTDlg.h"
#include ".\hunemftestdlg.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

HINSTANCE hDLL=NULL;

int (FAR PASCAL *KeyCard)(int Com,int CardNo,int nBlock,int Encrypt,char *CardPassword,char *SystemCode,
						  char *HotelCode,char *pass,char *Address,char *SDIn,char *STIn,char *SDOut,char *STOut,
						  int LEVEL_PASS,int PassMode,int AddressMode,int AddresssQty,int TimeMode,int V8,int V16,INT V24,
						  int AlwaysOpen,int OpenBolt,int TerminateOld,int ValidTimes);
int (FAR PASCAL *ReadMessage)(int Com,int nBlock,int Encrypt,int *DBCardNo,int *DBCardType,int *DBPassLevel,
							  char *CardPass,char *DBSystemcode,char *DBAddress,char *SDateTime);

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CHUNEMFTESTDlg 对话框



CHUNEMFTESTDlg::CHUNEMFTESTDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHUNEMFTESTDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHUNEMFTESTDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_OpSel);
}

BEGIN_MESSAGE_MAP(CHUNEMFTESTDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
END_MESSAGE_MAP()


// CHUNEMFTESTDlg 消息处理程序

BOOL CHUNEMFTESTDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将\“关于...\”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	SetDlgItemText(IDC_EDIT1,"1234");
	SetDlgItemText(IDC_EDIT2,"1122");
	SetDlgItemText(IDC_EDIT3,"CAE3F5");
	SetDlgItemText(IDC_EDIT4,"01010100700");
	COleDateTime curDateTime=COleDateTime::GetCurrentTime();
	SetDlgItemText(IDC_EDIT5,curDateTime.Format("%y-%m-%d"));
	SetDlgItemText(IDC_EDIT6,curDateTime.Format("%H:%M:%S"));
	COleDateTime dptDateTime=curDateTime+COleDateTimeSpan(1,0,0,0);
	SetDlgItemText(IDC_EDIT7,dptDateTime.Format("%y-%m-%d"));
	SetDlgItemText(IDC_EDIT8,dptDateTime.Format("%H:%M:%S"));
	SetDlgItemText(IDC_EDIT9,"0");
	///////////////////////////////////////////////////////
	SetDlgItemText(IDC_EDIT10,"1");
	SetDlgItemText(IDC_EDIT11,"4");
	SetDlgItemText(IDC_EDIT12,"1");
	SetDlgItemText(IDC_EDIT13,"82A094FFFFFF");
	SetDlgItemText(IDC_EDIT14,"3");
	SetDlgItemText(IDC_EDIT15,"1");
	SetDlgItemText(IDC_EDIT16,"0");
	SetDlgItemText(IDC_EDIT17,"1");
	SetDlgItemText(IDC_EDIT18,CompactCipherTime(curDateTime));

	SetDlgItemText(IDC_EDIT19,"0");
	SetDlgItemText(IDC_EDIT20,"255");
	SetDlgItemText(IDC_EDIT21,"255");
	SetDlgItemText(IDC_EDIT22,"255");
	SetDlgItemText(IDC_EDIT23,"0");
	SetDlgItemText(IDC_EDIT24,"0");
	SetDlgItemText(IDC_EDIT25,"255");

	m_OpSel.SetCurSel(0);
	
	hDLL=AfxLoadLibrary("HUNEMF.dll");
	if(hDLL==NULL)
	{
		MessageBox("加载动态库HUNEMF.dll错误","错误提示",MB_OK);
		SendMessage(WM_CLOSE,0,0);
		return FALSE;
	}

	
	KeyCard=(int (FAR PASCAL *)(int,int,int,int,char *,char *,char *,char *,char *,char *,char *,char *,char *,int,int,int,int,int,int,int,int,int,int,int,int))::GetProcAddress(hDLL,"KeyCard");
	ReadMessage=(int (FAR PASCAL *)(int,int,int,int *,int *,int *,char *,char *,char *,char *))::GetProcAddress(hDLL,"ReadMessage");
	if(!KeyCard || !ReadMessage)
	{
		MessageBox("加载函数错误","错误提示",MB_OK);
		SendMessage(WM_CLOSE,0,0);
		return FALSE;
	}

	return TRUE;  // 除非设置了控件的焦点，否则返回 TRUE
}

CString CHUNEMFTESTDlg::CompactCipherTime(COleDateTime DateTime)
{
	CString Scipher;
	char Buffer[16]="";
	DWORD dw;
	WORD Year,Month,Day,Hour,Min,Sec;
	Year=DateTime.GetYear();
	Month=DateTime.GetMonth();
	Day=DateTime.GetDay();
	Hour=DateTime.GetHour();
	Min=DateTime.GetMinute();
	Sec=DateTime.GetSecond();
	
	dw=(Min/4)+Hour*pow(2,4)+Day*pow(2,9)+Month*pow(2,14)+(((Year-8) % 1000)%63)*pow(2,18);
	
	itoa(dw,Buffer,16);
	CString retbuffer(Buffer);
	retbuffer.MakeUpper();

	//SetDlgItemText(IDC_EDIT18,retbuffer);
	return retbuffer;
}

void CHUNEMFTESTDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CHUNEMFTESTDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CHUNEMFTESTDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CHUNEMFTESTDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	CString sCardNo,sSystemCode,sHotelCode,sRoomAddress,sSDIn,sSTIn,sSDOut,sSTOut,sTerminalOld;
	CString sCom,snBlock,sEncrypt,sCardPass,sLevel_pass,sPassMode,sAddressMode,sAddressQty,sPass;
	CString sTimeMode,sV8,sV16,sV24,sAlwaysOpen,sOpenBolt,sValidTimes;

	GetDlgItemText(IDC_EDIT1,sCardNo);
	GetDlgItemText(IDC_EDIT2,sSystemCode);
	GetDlgItemText(IDC_EDIT3,sHotelCode);
	GetDlgItemText(IDC_EDIT4,sRoomAddress);
	GetDlgItemText(IDC_EDIT5,sSDIn);
	GetDlgItemText(IDC_EDIT6,sSTIn);
	GetDlgItemText(IDC_EDIT7,sSDOut);
	GetDlgItemText(IDC_EDIT8,sSTOut);
	GetDlgItemText(IDC_EDIT9,sTerminalOld);

	GetDlgItemText(IDC_EDIT10,sCom);
	GetDlgItemText(IDC_EDIT11,snBlock);
	GetDlgItemText(IDC_EDIT12,sEncrypt);
	GetDlgItemText(IDC_EDIT13,sCardPass);
	GetDlgItemText(IDC_EDIT14,sLevel_pass);
	GetDlgItemText(IDC_EDIT15,sPassMode);
	GetDlgItemText(IDC_EDIT16,sAddressMode);
	GetDlgItemText(IDC_EDIT17,sAddressQty);
	GetDlgItemText(IDC_EDIT18,sPass);

	GetDlgItemText(IDC_EDIT19,sTimeMode);
	GetDlgItemText(IDC_EDIT20,sV8);
	GetDlgItemText(IDC_EDIT21,sV16);
	GetDlgItemText(IDC_EDIT22,sV24);
	GetDlgItemText(IDC_EDIT23,sAlwaysOpen);
	GetDlgItemText(IDC_EDIT24,sOpenBolt);
	GetDlgItemText(IDC_EDIT25,sValidTimes);


	int Com=atoi(sCom.Trim());
	int CardNo=atoi(sCardNo.Trim());
	int nBlock=atoi(snBlock.Trim());
	int Encrypt=atoi(sEncrypt.Trim());
	char CardPassword[20]="";
	strcpy(CardPassword,sCardPass.Trim());
	char SystemCode[20]="";
	strcpy(SystemCode,sSystemCode.Trim());
	char HotelCode[20]="";
	strcpy(HotelCode,sHotelCode.Trim());
	char pass[20]="";
	if(m_OpSel.GetCurSel()==0)
	{
		sPass=CompactCipherTime(COleDateTime::GetCurrentTime());
		SetDlgItemText(IDC_EDIT18,sPass);
	}
	strcpy(pass,sPass.Trim());

	char Address[20]="";
	strcpy(Address,sRoomAddress.Trim());
	char SDIn[20]="";
	char STIn[20]="";
	char SDOut[20]="";
	char STOut[20]="";
	strcpy(SDIn,sSDIn.Trim());
	strcpy(STIn,sSTIn.Trim());
	strcpy(SDOut,sSDOut.Trim());
	strcpy(STOut,sSTOut.Trim());
	int LEVEL_PASS=atoi(sLevel_pass.Trim());
	int PassMode=atoi(sPassMode.Trim());
	int AddressMode=atoi(sAddressMode.Trim());
	int AddressQty=atoi(sAddressQty.Trim());
	int TimeMode=atoi(sTimeMode.Trim());
	int V8=atoi(sV8.Trim());
	int V16=atoi(sV16.Trim());
	int V24=atoi(sV24.Trim());
	int AlwaysOpen=atoi(sAlwaysOpen.Trim());
	int OpenBolt=atoi(sOpenBolt.Trim());
	int TerminateOld=atoi(sTerminalOld.Trim());
	int ValidTimes=atoi(sValidTimes.Trim());

	int ret=KeyCard(Com,CardNo,nBlock,Encrypt,CardPassword,SystemCode,HotelCode,pass,Address,SDIn,STIn,SDOut,STOut,
		LEVEL_PASS,PassMode,AddressMode,AddressQty,TimeMode,V8,V16,V24,AlwaysOpen,OpenBolt,TerminateOld,ValidTimes);

	if(ret!=0)
	{
		CString errormsg;
		errormsg.Format("发卡失败!错误代码:%d",ret);
		MessageBox(errormsg,"错误提示",MB_OK);
		return;
	}

	MessageBox("发卡成功!","错误提示",MB_OK);
}

void CHUNEMFTESTDlg::OnDestroy()
{
	if(hDLL!=NULL)
		AfxFreeLibrary(hDLL);

	CDialog::OnDestroy();
	// TODO: 在此处添加消息处理程序代码
}

void CHUNEMFTESTDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString sCom,snBlock,sEncrypt,sCardPass;
	GetDlgItemText(IDC_EDIT10,sCom);
	GetDlgItemText(IDC_EDIT11,snBlock);
	GetDlgItemText(IDC_EDIT12,sEncrypt);
	GetDlgItemText(IDC_EDIT13,sCardPass);

	int Com=atoi(sCom.Trim());
	int nBlock=atoi(snBlock.Trim());
	int Encrypt=atoi(sEncrypt.Trim());
	char CardPassword[20]="";
	strcpy(CardPassword,sCardPass.Trim());

	int DBCardNo,DBCardType,DBPassLevel;
	DBCardNo=DBCardType=DBPassLevel=0;
	char DBSystemcode[20]="";
	char DBAddress[20]="";
	char SDateTime[20]="";
	
	int ret=ReadMessage(Com,nBlock,Encrypt,&DBCardNo,&DBCardType,&DBPassLevel,CardPassword,DBSystemcode,DBAddress,SDateTime);	
	if(ret!=0)
	{
		CString errormsg;
		errormsg.Format("读卡失败!错误代码:%d",ret);
		MessageBox(errormsg,"错误提示",MB_OK);
		return;
	}

	if(DBCardType!=0)
	{
		MessageBox("非宾客卡!","错误提示",MB_OK);
		return;
	}

	CString cardInfo;
	cardInfo.Format("CardNo:%d,CardType:%d,PassLevel:%d,Systemcode:%s,RoomAddress:%s,EDateTime:%s",DBCardNo,DBCardType,DBPassLevel,
		DBSystemcode,DBAddress,SDateTime);
	MessageBox(cardInfo,"系统提示",MB_OK);
}
