#include <Qtwidgets/QtWidgets>
#include "QConfigurationWidget.h"
#include "SystemDataQuery.h"
#include "SystemData.h"

QConfigurationWidget::QConfigurationWidget(QWidget *parent)
	: QWidget(parent)
{
	setupUi(this);

	init();
	initWidget();
	initSignal();
	initData();
}

QConfigurationWidget::~QConfigurationWidget()
{

}

void QConfigurationWidget::init()
{

}

void QConfigurationWidget::initWidget()
{
	setLayout( pLayoutMain );

	setMaximumWidth( 800 );
}

void QConfigurationWidget::initSignal()
{
	connect( pEditAutoTraderCode, SIGNAL(editingFinished()), this, SLOT(onAutoTraderCodeEdited()));
	connect( pEditAutoTraderCode, SIGNAL(textChanged(const QString &)), this, SLOT(onAutoTraderCodeChanged()) );
	connect( pButtonSave, SIGNAL(clicked()), this, SLOT(onSave()) );
	connect( pButtonReset, SIGNAL(clicked()), this, SLOT(onReset()) );
}

void QConfigurationWidget::initData()
{
	pEditAutoTraderCode->setText( QString::fromLocal8Bit( pParameters->getParameter("默认车商代码").c_str() ));

	emit pEditAutoTraderCode->editingFinished();
}

void QConfigurationWidget::onAutoTraderCodeEdited()
{
	string strAutoTraderCode = pEditAutoTraderCode->text().trimmed().toUpper().toLocal8Bit().data();
	string strAutoTraderName;

	query_auto_trader_info( strAutoTraderCode, strAutoTraderName );

	pEditAutoTraderName->setText( QString::fromLocal8Bit( strAutoTraderName.c_str() ));
}

void QConfigurationWidget::onAutoTraderCodeChanged()
{
	pEditAutoTraderName->clear();
}

void QConfigurationWidget::onSave()
{
	if ( pEditAutoTraderName->text().isEmpty() )
	{
		emit pEditAutoTraderCode->editingFinished();
	}
	
	if ( pEditAutoTraderName->text().isEmpty() )
	{
		QMessageBox::critical( this, QString::fromLocal8Bit("错误！"), QString::fromLocal8Bit("车商代码错误！") );

		return;
	}

	pParameters->setParameter( "默认车商代码", pEditAutoTraderCode->text().toLocal8Bit().data() );
}

void QConfigurationWidget::onReset()
{
	initData();
}
