#include <QFileDialog>
#include "QCarDealerAchievementWidget.h"


#include <QMessageBox>
#include <stdexcept>


#include "../../../data/DataManipulation/oracle/ImportToOracle.h"
#include "../../Data/DataManipulation/Excel/LoadFromExcel.h"
#include "../../../util/qt/qt_util.h"

using namespace std;

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

	init();
}

QCarDealerAchievementWidget::~QCarDealerAchievementWidget()
{
}

void QCarDealerAchievementWidget::onImport()
{
	//选择文件
	QString xlsxFilePath = QFileDialog::getOpenFileName( this,
	                                                     "请选择要导入的数据文件",
	                                                     ".", "xlsx文件(*.xlsx)" );
	if ( xlsxFilePath.isEmpty() == true )
	{
		return;
	}

	achievementVector.clear();

	LoadCarDealerAchievementFromXlsx( xlsxFilePath.toStdWString(),
	                                  0,
	                                  1,
	                                  achievementVector );

	//以后要修改
	string userName = "car_dealer";
	string password = "cpic123456";
	string tnsName  = "xmcx1";

	try
	{
		ImportCarDealerAchievementToOracleCpp( userName, password, tnsName, achievementVector );
		
		showCarDealerAchievement();
	}
	catch ( runtime_error & error )
	{
		QMessageBox::critical( this,
		                       "错误",
		                       error.what() );

		return;
	}
}

void QCarDealerAchievementWidget::showCarDealerAchievement()
{
	ui.pTableWidgetAchievement->clearContents();

	QTableWidgetItem * pItem = nullptr;
	int rowIndex = 0;
	int columnIndex = 0;

	ui.pTableWidgetAchievement->setRowCount(achievementVector.size());

	for ( auto iter = achievementVector.begin();
	      iter != achievementVector.end();
	      ++iter, ++rowIndex )
	{
		//年度
		pItem = new QTableWidgetItem(QString::fromStdWString(iter->getTheYear()));
		pItem->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);

		ui.pTableWidgetAchievement->setItem(rowIndex, columnIndex++, pItem);

		//月份
		pItem = new QTableWidgetItem(QString::fromStdWString(iter->getTheMonth()));
		pItem->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);

		ui.pTableWidgetAchievement->setItem(rowIndex, columnIndex++, pItem);

		//车商代码
		pItem = new QTableWidgetItem(QString::fromStdWString(iter->getCarDealerCode()));
		pItem->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);

		ui.pTableWidgetAchievement->setItem(rowIndex, columnIndex++, pItem);

		//车商名称
		pItem = new QTableWidgetItem(QString::fromStdWString(iter->getCarDealerCode()));
		pItem->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);

		ui.pTableWidgetAchievement->setItem(rowIndex, columnIndex++, pItem);

		//店内双签产值
		pItem = new QTableWidgetItem(QString::number(iter->getCheckedAchievement(), 'g', -1));
		pItem->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);

		ui.pTableWidgetAchievement->setItem(rowIndex, columnIndex++, pItem);

		//店内新车开票数
		pItem = new QTableWidgetItem(QString::number(iter->getPolicyAmount(),10));
		pItem->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);

		ui.pTableWidgetAchievement->setItem(rowIndex, columnIndex++, pItem);

		//我司新车签单台次
		pItem = new QTableWidgetItem(QString::number(iter->getCpicAmount()));
		pItem->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);

		ui.pTableWidgetAchievement->setItem(rowIndex, columnIndex++, pItem);

		//人保新车签单台次
		pItem = new QTableWidgetItem(QString::number(iter->getPiccAmount()));
		pItem->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);

		ui.pTableWidgetAchievement->setItem(rowIndex, columnIndex++, pItem);

		//平安新车签单台次
		pItem = new QTableWidgetItem(QString::number(iter->getPinganAmount()));
		pItem->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);

		ui.pTableWidgetAchievement->setItem(rowIndex, columnIndex++, pItem);

		//其他保险公司
		pItem = new QTableWidgetItem(QString::number(iter->getOthersAmount()));
		pItem->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);

		ui.pTableWidgetAchievement->setItem(rowIndex, columnIndex++, pItem);
	}

	resizeTableWidgetTab(ui.pTableWidgetAchievement, 10);
}

void QCarDealerAchievementWidget::init()
{
	initData();
	initWidgets();
	initSignal();
}

void QCarDealerAchievementWidget::initData()
{
}

void QCarDealerAchievementWidget::initWidgets()
{
	setLayout( ui.pLayoutMain );

	resizeTableWidgetTab( ui.pTableWidgetAchievement, 10 );
}

void QCarDealerAchievementWidget::initSignal()
{
	connect( ui.pButtonImport, SIGNAL( clicked() ), this, SLOT( onImport() ) );
}
