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

#include "pch.h"
#include "framework.h"
#include "ZGKJ.h"
#include "ZGKJDlg.h"
#include "afxdialogex.h"
#include "RegisterDlg.h"
#include "SupportStaff.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CZGKJDlg 对话框



CZGKJDlg::CZGKJDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_ZGKJ_DIALOG, pParent)
	, Port(_T(""))
	, Password(_T(""))
	, Phone(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CZGKJDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_PORT, Port);
	DDX_Text(pDX, IDC_PORT, Port);
	DDX_Text(pDX, IDC_USER_PASSWORD, Password);
	DDX_Text(pDX, IDC_USER_ID, Phone);
}

BEGIN_MESSAGE_MAP(CZGKJDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_REGISTER, &CZGKJDlg::OnBnClickedRegister)
	ON_BN_CLICKED(IDC_LINK, &CZGKJDlg::OnBnClickedLink)
	ON_BN_CLICKED(IDC_LOGIN, &CZGKJDlg::OnBnClickedLogin)
END_MESSAGE_MAP()


// CZGKJDlg 消息处理程序

BOOL CZGKJDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

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

	// TODO: 在此添加额外的初始化代码
	GetDlgItem(IDC_LOGIN)->EnableWindow(FALSE);
	GetDlgItem(IDC_REGISTER)->EnableWindow(FALSE);
	GetDlgItem(IDC_IPADDRESS1)->SetWindowText(_T("118.24.178.150"));
	GetDlgItem(IDC_PORT)->SetWindowText(_T("3306"));
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

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

void CZGKJDlg::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
	{
		CDialogEx::OnPaint();
	}
}

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



CString CZGKJDlg::Decode(char* pass, int secretkey)
{

	//CString pass2;
	int a = strlen(pass);
	for (int i = 0; i < strlen(pass); i++)
	{
		pass[i] ^= secretkey;
	}
	pass[a] = '\0';
	//pass2.Format(_T("%s"), pass);
	return CString(pass);
}




void CZGKJDlg::OnBnClickedRegister()
{
	// TODO: 在此添加控件通知处理程序代码
	RegisterDlg regDlg;
	regDlg.DoModal();
	return;
}


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

	GetDlgItem(IDC_IPADDRESS1)->GetWindowText(IPAddress);
	GetDlgItemText(IDC_PORT, Port);
	DBINFO DatabasesInfo;
	DatabasesInfo.server_address = CZGKJApp::MyTtoA(IPAddress);
	DatabasesInfo.server_port = CZGKJApp::MyTtoI(Port);
	DatabasesInfo.database_name = "zgkj";
	DatabasesInfo.user_name = "htl";
	DatabasesInfo.password = "fl961511";
	theApp.sql = HtlMysql::GetInstance();

	if (!theApp.sql->Connect(DatabasesInfo))
	{
		const char* e = theApp.sql->GetErrorInformation();
		CString error = CZGKJApp::MyAtoT(e);
		AfxMessageBox(error, MB_OK | MB_ICONERROR);
		return;
	}
	GetDlgItem(IDC_LOGIN)->EnableWindow(TRUE);
	GetDlgItem(IDC_REGISTER)->EnableWindow(TRUE);
	return;
}

void CZGKJDlg::OnBnClickedLogin()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	CString str;
	CString sqlPass;
	const char* phone = CZGKJApp::MyTtoA(Phone);
	
	char order[STR_NUM];
	sprintf_s(order, STR_NUM, "SELECT phone,password,secret_key,department_id FROM tb_staff WHERE phone='%s' ", phone);

	if (!theApp.sql->ExecutiveCommand(order))
	{
		MYSQL_RES* res = theApp.sql->GetResultSet();
		if (res->row_count == 0)
		{
			AfxMessageBox(_T("请核对账号和密码！"));
			return;
		}
		char* tempPhone;
		char* tempPass;
		int tempSecretKey;
		theApp.sql->row = mysql_fetch_row(res);
		tempPhone = theApp.sql->row[0];
		tempPass = theApp.sql->row[1];
		tempSecretKey = atoi(theApp.sql->row[2]);
		theApp.Department_ID = atoi(theApp.sql->row[3]);
		theApp.Phone = theApp.MyAtoT(tempPhone);
		sqlPass = Decode(tempPass, tempSecretKey);
		if (!(Phone == CZGKJApp::MyAtoT(tempPhone) && Password == sqlPass))
		{
			AfxMessageBox(_T("请核对账号和密码！"));
			return;
		}
	}
	else
	{
		AfxMessageBox(theApp.MyAtoT(theApp.sql->GetErrorInformation()), MB_OK | MB_ICONERROR);
		return;
	}
	EndDialog(IDC_LOGIN);
	
}

