// DBConfig.cpp : implementation file
//

#include "stdafx.h"
#include "ylgl.h"
#include "DBConfig.h"

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

/////////////////////////////////////////////////////////////////////////////
// DBConfig dialo
/////////////////////////////////////////////////////////////////////////////
// DBConfig dialog

DBConfig::DBConfig(CWnd* pParent /*=NULL*/)
	: CDialog(DBConfig::IDD, pParent)
{
	//{{AFX_DATA_INIT(DBConfig)
	m_bSql = 1;
	m_server = _T("");
	m_user = _T("");
	m_psw = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
}

void DBConfig::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DBConfig)
	DDX_Radio(pDX, IDC_RADIO1, m_bSql);
	DDX_Text(pDX, IDC_EDIT1, m_server);
	DDX_Text(pDX, IDC_EDIT2, m_user);
	DDX_Text(pDX, IDC_EDIT3, m_psw);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(DBConfig, CDialog)
	//{{AFX_MSG_MAP(DBConfig)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DBConfig message handlers

BOOL DBConfig::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CString configpath=g_mainpath+"\\dbconfig.dat";
	CFile fp;
	if(fp.Open (configpath, CFile::modeRead))
	{
	DBSAVE db;
	memset(&db, 0, sizeof(db));
	fp.Read (&db, sizeof(db));
	fp.Close ();

	char server[50];
	char user[50];
	char psw[50];
	memset(server, 0, sizeof(char)*50);
	memset(user, 0, sizeof(char)*50);
	memset(psw, 0, sizeof(char)*50);
	for(int i=0; i<50; i++)
	{
		if(db.server [i])
			server[i]=500-db.server [i];
		if(db.user [i])
			user[i]=500-db.user [i];
 		if(db.psw [i])
			psw[i]=500-db.psw [i];
	}
	m_server=server ;
	m_user=user ;
	m_psw=psw ;
	UpdateData(false);
	}

	EnableCtrl();

	GetDlgItem(IDC_EDIT1)->SetFocus();
	return false;  // return TRUE  unless you set the focus to a control
}

void DBConfig::OnRadio1() 
{
	// TODO: Add your control notification handler code here
	EnableCtrl();
}

void DBConfig::OnRadio2() 
{
	// TODO: Add your control notification handler code here
	EnableCtrl();
}

void DBConfig::EnableCtrl()
{
	UpdateData();
	GetDlgItem(IDC_EDIT1)->EnableWindow(m_bSql);
	GetDlgItem(IDC_EDIT2)->EnableWindow(m_bSql);
	GetDlgItem(IDC_EDIT3)->EnableWindow(m_bSql);
}

void DBConfig::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData();
	if(m_server.GetLength ()>50)
		m_server=m_server.Left (50);
	if(m_user.GetLength ()>50)
		m_user=m_user.Left (50);
	if(m_psw.GetLength ()>50)
		m_psw=m_psw.Left (50);

	DBSAVE db;
	memset(&db, 0, sizeof(db));
	for(int i=0; i<50; i++)
	{
		if(m_server.GetLength ()>i)
			db.server [i]=500-m_server.GetAt (i);
		if(m_user.GetLength ()>i)
			db.user [i]=500-m_user.GetAt (i);
		if(m_psw.GetLength ()>i)
			db.psw [i]=500-m_psw.GetAt (i);
	}
	db.bsql=m_bSql;

	CString path=g_mainpath+"\\dbconfig.dat";
	CFile fp;
	fp.Open (path, CFile::modeCreate|CFile::modeWrite);
	fp.Write (&db, sizeof(db));
	fp.Close ();

	AfxMessageBox("����ɹ�! ����������Ч.", MB_ICONINFORMATION);
	CDialog::OnOK();
}