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

#include "stdafx.h"
#include "YAServer.h"
#include "DbSettingDlg.h"
#include "afxdialogex.h"
#include "def.h"
#include "INIFILE.h"
#include "Functions/Functions.h"


// CDbSettingDlg 对话框

IMPLEMENT_DYNAMIC(CDbSettingDlg, CDialogEx)

CDbSettingDlg::CDbSettingDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CDbSettingDlg::IDD, pParent)
	, m_host_master(_T(""))
	, m_port_master(0)
	, m_pwd_master(_T(""))
	, m_user_master(_T(""))
	, m_db_master(_T(""))
	, m_use_slave(FALSE)
	, m_host_slave(_T(""))
	, m_port_slave(0)
	, m_user_slave(_T(""))
	, m_pwd_slave(_T(""))
	, m_db_slave(_T(""))
{
	
}

CDbSettingDlg::~CDbSettingDlg()
{
}

void CDbSettingDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, m_host_master);
	DDX_Text(pDX, IDC_EDIT4, m_port_master);
	DDX_Text(pDX, IDC_EDIT6, m_pwd_master);
	DDX_Text(pDX, IDC_EDIT3, m_user_master);
	DDX_Text(pDX, IDC_EDIT2, m_db_master);
	DDX_Control(pDX, IDC_ENCODE_MASTER, m_encode_master);
	DDX_Check(pDX, IDC_CHECK1, m_use_slave);
	DDX_Text(pDX, IDC_HOST_SLAVE, m_host_slave);
	DDX_Text(pDX, IDC_PORT_SLAVE, m_port_slave);
	DDX_Text(pDX, IDC_USER_SLAVE, m_user_slave);
	DDX_Text(pDX, IDC_PWD_SLAVE, m_pwd_slave);
	DDX_Text(pDX, IDC_DB_SLAVE, m_db_slave);
	DDX_Control(pDX, IDC_ENCODE_SLAVE, m_encode_slave);
}


BEGIN_MESSAGE_MAP(CDbSettingDlg, CDialogEx)
	ON_BN_CLICKED(IDOK, &CDbSettingDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDC_CHECK1, &CDbSettingDlg::OnBnClickedCheck1)
END_MESSAGE_MAP()

void CDbSettingDlg::OnBnClickedOk()
{
	UpdateData();
	CString strCBText;
	theApp.dbhost = CFunctions::wc2c(m_host_master.GetBuffer(0));
	theApp.dbuser =  CFunctions::wc2c(m_user_master.GetBuffer(0));
	theApp.dbpwd =  CFunctions::wc2c(m_pwd_master.GetBuffer(0));
	theApp.dbname =  CFunctions::wc2c(m_db_master.GetBuffer(0));
	int nIndex = m_encode_master.GetCurSel();
	m_encode_master.GetLBText( nIndex, strCBText);
	theApp.dbencoding = CFunctions::wc2c(strCBText.GetBuffer(0));
	theApp.dbport = m_port_master;
	theApp.dbbackup =  m_use_slave ? "1" : "0";

	theApp.dbhost_bk = CFunctions::wc2c(m_host_slave.GetBuffer(0));
	theApp.dbuser_bk =  CFunctions::wc2c(m_user_slave.GetBuffer(0));
	theApp.dbpwd_bk =  CFunctions::wc2c(m_pwd_slave.GetBuffer(0));
	theApp.dbname_bk =  CFunctions::wc2c(m_db_slave.GetBuffer(0));
	nIndex = m_encode_slave.GetCurSel();
	if(nIndex == -1) {
		nIndex = 0;
	}
	m_encode_master.GetLBText( nIndex, strCBText);
	theApp.dbencoding_bk = CFunctions::wc2c(strCBText.GetBuffer(0));
	theApp.dbport_bk = m_port_slave;

	theApp.save_db_conf();
	CDialogEx::OnOK();
}

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

	CString strTemp;
	strTemp.Format(_T("gb2312"));
	m_encode_slave.AddString(strTemp);
	m_encode_master.AddString(_T("gb2312"));
	strTemp.Format(_T("utf8"));
	m_encode_slave.AddString(strTemp);
	m_encode_master.AddString(strTemp);

	init_ui();
	return TRUE;
}

void CDbSettingDlg::init_ui()
{
	m_host_master = CFunctions::c2wc(theApp.dbhost.c_str());
	m_user_master = CFunctions::c2wc(theApp.dbuser.c_str());
	m_pwd_master = CFunctions::c2wc(theApp.dbpwd.c_str());
	m_db_master = CFunctions::c2wc(theApp.dbname.c_str());
	int nIndex = m_encode_master.FindString(0, CFunctions::c2wc(theApp.dbencoding.c_str()));
	m_encode_master.SetCurSel(nIndex); //设置第nIndex项为显示的内容
	m_port_master = theApp.dbport;
	m_use_slave = (theApp.dbbackup == "1");

	m_host_slave = CFunctions::c2wc(theApp.dbhost_bk.c_str());
	m_user_slave = CFunctions::c2wc(theApp.dbuser_bk.c_str());
	m_pwd_slave = CFunctions::c2wc(theApp.dbpwd_bk.c_str());
	m_db_slave = CFunctions::c2wc(theApp.dbname_bk.c_str());
	nIndex = m_encode_slave.FindString(0, CFunctions::c2wc(theApp.dbencoding_bk.c_str()));
	m_encode_slave.SetCurSel(nIndex); //设置第nIndex项为显示的内容
	//m_encode_slave.SetWindowTextW(CFunctions::c2wc(theApp.dbencoding_bk.c_str()));
	m_port_slave = theApp.dbport_bk;

	EnableCtrl(m_use_slave);
	UpdateData(FALSE);
}

void CDbSettingDlg::OnBnClickedCheck1()
{
	m_encode_slave.EnableWindow(!m_use_slave);
	(CComboBox*)GetDlgItem(IDC_HOST_SLAVE)->EnableWindow(!m_use_slave);
	(CComboBox*)GetDlgItem(IDC_PORT_SLAVE)->EnableWindow(!m_use_slave);
	(CComboBox*)GetDlgItem(IDC_USER_SLAVE)->EnableWindow(!m_use_slave);
	(CComboBox*)GetDlgItem(IDC_PWD_SLAVE)->EnableWindow(!m_use_slave);
	(CComboBox*)GetDlgItem(IDC_DB_SLAVE)->EnableWindow(!m_use_slave);
	UpdateData();
}

void CDbSettingDlg::EnableCtrl(BOOL status)
{
	(CComboBox*)GetDlgItem(IDC_HOST_SLAVE)->EnableWindow(status);
	(CComboBox*)GetDlgItem(IDC_PORT_SLAVE)->EnableWindow(status);
	(CComboBox*)GetDlgItem(IDC_USER_SLAVE)->EnableWindow(status);
	(CComboBox*)GetDlgItem(IDC_PWD_SLAVE)->EnableWindow(status);
	(CComboBox*)GetDlgItem(IDC_DB_SLAVE)->EnableWindow(status);

	m_encode_slave.EnableWindow(status);
}
