
//#include <occi.h>
//#include <QTextCodec>
#include <QtWidgets/QMessageBox>
#include "QLoginWidget.h"
#include "SystemData.h"
#include "SystemDataQuery.h"

QLoginWidget::QLoginWidget(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);

	init();
}

QLoginWidget::~QLoginWidget()
{
	
}

void QLoginWidget::init()
{
	initWidget();
	initSignal();

	//pCodecLocal = QTextCodec::codecForLocale();
	//pCodecUTF8 = QTextCodec::codecForName( "UTF-8" );

	m_isLoginSuccess = false;
}

void QLoginWidget::initWidget()
{
	setLayout( ui.pLayoutMain );
}

void QLoginWidget::initSignal()
{
	connect( ui.pButtonLogin, SIGNAL(clicked()), this, SLOT(login()) );
	connect( ui.pButtonCancel, SIGNAL(clicked()), this, SLOT(cancel()) );
	connect( ui.pEditUserCode, SIGNAL(editingFinished()), this, SLOT(getUserName()) );
}

void QLoginWidget::login()
{
	string strPassword( (char *)ui.pEditPassword->text().toLocal8Bit().data() );
	
	if ( strPassword == m_userPassword )
	{
		m_isLoginSuccess = true;

		//保存用户名和密码
		setUserCode( m_userCode );
		setUserName( m_userName );

		close();
	}
	else
	{
		QMessageBox::warning( this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("密码错误！") );
	}
}

void QLoginWidget::cancel()
{
	m_isLoginSuccess = false;

	close();
}

void QLoginWidget::getUserName()
{
	m_userCode = ui.pEditUserCode->text().toLocal8Bit().data();

	try
	{
		QueryUserInfo( m_userCode, m_userName, m_userPassword );

		ui.pEditUserName->setText( QString::fromLocal8Bit(m_userName.c_str()));
	}
	catch ( runtime_error & exp )
	{
		QMessageBox::critical( this, "错误", QString::fromUtf8( exp.what() ));
	}
}
