#include "QRapidInputWidget.h"
#include "DataManipulation.h"

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

	init();
}

QRapidInputWidget::~QRapidInputWidget()
{
}

void QRapidInputWidget::init()
{
	initWidget();
	initSignal();
	initData();
}

void QRapidInputWidget::initWidget()
{
	setLayout(ui.pLayoutMain);

	setAcceptDrops(true);
}

void QRapidInputWidget::initSignal()
{
	connect(ui.pButtonSave,SIGNAL(clicked()), this, SLOT(onSaveTelSalePolicy()));
	connect(ui.pButtonClean,SIGNAL(clicked()), this, SLOT(onCleanTable()));
	connect(ui.pButtonOpenFile,SIGNAL(clicked()), this, SLOT(onOpenFile()));
}

void QRapidInputWidget::initData()
{
}

void QRapidInputWidget::dragEnterEvent(QDragEnterEvent* pEvent)
{
	if (pEvent->mimeData()->hasUrls())
	{
		pEvent->acceptProposedAction();
	}
}

void QRapidInputWidget::dropEvent(QDropEvent* pEvent)
{
	QList<QUrl> listURL = pEvent->mimeData()->urls();
	QString     strFilePath;

	//正则表达式判断文件类型
	QRegularExpression regXlsx(QString::fromLocal8Bit(".xlsx$"),
	                           QRegularExpression::PatternOption::CaseInsensitiveOption);
	QRegularExpression regXls(QString::fromLocal8Bit(".xls$"),
	                          QRegularExpression::PatternOption::CaseInsensitiveOption);
	QRegularExpressionMatch match;

	if (listURL.size() == 0)
	{
		return;
	}

	strFilePath = listURL[0].toLocalFile();

	//是xlsx文件
	match = regXlsx.match(strFilePath);

	if (match.hasMatch())
	{
		readTelsaleXlsxFile(strFilePath.toStdWString(),
		                    m_vPolicy,
		                    ui.pCheckBoxHasTitle->isChecked());
	}

	//是xls文件
	match = regXls.match(strFilePath);

	if (match.hasMatch())
	{
		//readTelsaleXlsFile(strFilePath.toStdWString(), m_vPolicy, ui.pCheckBoxHasTitle->isChecked(), false);
		QMessageBox::critical(this,
		                      QString::fromLocal8Bit("文件类型错误！"),
		                      QString::fromLocal8Bit("文件格式需为Excel 2007及以后！"));
	}


	showPolicy();
}

void QRapidInputWidget::showPolicy()
{
	int               iRowCount = m_vPolicy.size();
	QTableWidgetItem* pItem     = NULL;

	ui.pTableWidgetPolicy->setRowCount(iRowCount);

	for (int iRowIndex = 0; iRowIndex < iRowCount; iRowIndex++)
	{
		//保单号
		pItem = new QTableWidgetItem(QString::fromLocal8Bit(m_vPolicy[iRowIndex].strPolicySerial.c_str()));
		pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
		pItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

		ui.pTableWidgetPolicy->setItem(iRowIndex, 0, pItem);

		//签单日期
		pItem = new QTableWidgetItem(QString::fromLocal8Bit(m_vPolicy[iRowIndex].strSignDate.c_str()));
		pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
		pItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

		ui.pTableWidgetPolicy->setItem(iRowIndex, 1, pItem);

		//车牌号
		pItem = new QTableWidgetItem(QString::fromLocal8Bit(m_vPolicy[iRowIndex].strPlateSerial.c_str()));
		pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
		pItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

		ui.pTableWidgetPolicy->setItem(iRowIndex, 2, pItem);

		//被保险人
		pItem = new QTableWidgetItem(QString::fromLocal8Bit(m_vPolicy[iRowIndex].strCustomerName.c_str()));
		pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
		pItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

		ui.pTableWidgetPolicy->setItem(iRowIndex, 3, pItem);

		//车商代码
		pItem = new QTableWidgetItem(QString::fromLocal8Bit(m_vPolicy[iRowIndex].strAutoTraderCode.c_str()));
		pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
		pItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

		ui.pTableWidgetPolicy->setItem(iRowIndex, 4, pItem);

		//经办人代码
		pItem = new QTableWidgetItem(QString::fromLocal8Bit(m_vPolicy[iRowIndex].strSalerCode.c_str()));
		pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
		pItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);

		ui.pTableWidgetPolicy->setItem(iRowIndex, 5, pItem);

		//经办人名称
		pItem = new QTableWidgetItem(QString::fromLocal8Bit(m_vPolicy[iRowIndex].strSalerName.c_str()));
		pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
		pItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

		ui.pTableWidgetPolicy->setItem(iRowIndex, 6, pItem);

		//部门
		pItem = new QTableWidgetItem(QString::fromLocal8Bit(m_vPolicy[iRowIndex].strSalerDeptName.c_str()));
		pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
		pItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

		ui.pTableWidgetPolicy->setItem(iRowIndex, 7, pItem);

		//科室
		pItem = new QTableWidgetItem(QString::fromLocal8Bit(m_vPolicy[iRowIndex].strSalerOfficeName.c_str()));
		pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
		pItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

		ui.pTableWidgetPolicy->setItem(iRowIndex, 8, pItem);
	}

	ui.pTableWidgetPolicy->resizeColumnsToContents();
}

void QRapidInputWidget::onSaveTelSalePolicy()
{
	vector<TelSalePolicyGift> vGifts;

	m_vErrorPolicy.clear();

	for (vector<SPolicyRecord>::iterator iter = m_vPolicy.begin(); iter != m_vPolicy.end(); ++iter)
	{
		try
		{
			SaveTelSalePolicyInfo(*iter, vGifts);
		}
		catch (runtime_error& excpt)
		{
			m_vErrorPolicy.push_back(*iter);

			QString strInfo = QString::fromLocal8Bit("保单") + QString::fromLocal8Bit((*iter).strPolicySerial.c_str()) + QString::
			                  fromLocal8Bit("保存错误！\n") +
			                  QString::fromLocal8Bit("错误信息：") + QString::fromLocal8Bit(excpt.what());

			QMessageBox::critical(this, QString::fromLocal8Bit("保存错误"), strInfo);
		}
	}

	m_vPolicy = m_vErrorPolicy;

	showPolicy();
}

void QRapidInputWidget::onCleanTable()
{
	m_vErrorPolicy.clear();
	m_vPolicy.clear();

	ui.pTableWidgetPolicy->setRowCount(0);
	ui.pTableWidgetPolicy->clearContents();
}

void QRapidInputWidget::onOpenFile()
{
	QSettings reg("HKEY_CURRENT_USER\\Software\\TelsalePolicyInfoManager", QSettings::NativeFormat);
	QString   strPath = reg.value(QString::fromLocal8Bit("打开地址")).toString();

	QStringList listFileNames = QFileDialog::getOpenFileNames(this,
	                                                          QString::fromLocal8Bit("打开Excel文件"),
	                                                          strPath,
	                                                          QString::fromLocal8Bit("Excel文件 (*.xls *.xlsx)"));

	//记录一下打开的路径
	if (listFileNames.size() > 0)
	{
		strPath = listFileNames[0].left(listFileNames[0].lastIndexOf(QString::fromLocal8Bit("/")) + 1);

		reg.setValue(QString::fromLocal8Bit("打开地址"), strPath);
	}
	else
	{
		return;
	}

	//正则表达式判断文件类型
	QRegularExpression regXlsx(QString::fromLocal8Bit(".xlsx$"),
	                           QRegularExpression::PatternOption::CaseInsensitiveOption);
	QRegularExpression regXls(QString::fromLocal8Bit(".xls$"),
	                          QRegularExpression::PatternOption::CaseInsensitiveOption);
	QRegularExpressionMatch match;

	strPath = listFileNames[0];

	//是xlsx文件
	match = regXlsx.match(strPath);

	if (match.hasMatch())
	{
		readTelsaleXlsxFile(strPath.toStdWString(), m_vPolicy, ui.pCheckBoxHasTitle->isChecked());
	}

	//是xls文件
	match = regXls.match(strPath);

	if (match.hasMatch())
	{
		//readTelsaleXlsFile( strPath.toLocal8Bit().data(), m_vPolicy, ui.pCheckBoxHasTitle->isChecked(), false );
		QMessageBox::critical(this,
		                      QString::fromLocal8Bit("文件类型错误！"),
		                      QString::fromLocal8Bit("文件格式需为Excel 2007及以后！"));
	}


	showPolicy();
}
