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

using namespace std;

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

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

QTelSalePolicyInfoQuery::~QTelSalePolicyInfoQuery()
{

}

void QTelSalePolicyInfoQuery::init()
{

}

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

	//录入时间
	pDateEditStart->setDate( QDate::currentDate().addDays(-1) );
	pDateEditEnd->setDate( QDate::currentDate() );
}

void QTelSalePolicyInfoQuery::initSignal()
{
	connect( pEditOperatorCode, SIGNAL(editingFinished()), this, SLOT(onOperatorCodeEdited()) );
	connect( pEditOperatorCode, SIGNAL(textChanged(const QString &)), this, SLOT(onOperatorCodeEditing(const QString &)) );
	connect( pButtonQuery, SIGNAL(clicked()), this, SLOT(onQuery()) );
	connect( pButtonReset, SIGNAL(clicked()), this, SLOT(onReset()) );
}

void QTelSalePolicyInfoQuery::onOperatorCodeEditing(const QString & text)
{
	pEditOperatorName->clear();
}

void QTelSalePolicyInfoQuery::onOperatorCodeEdited()
{
	string strStaffCode = pEditOperatorCode->text().toLocal8Bit().data();
	string strStaffName;

	query_staff_info( strStaffCode, strStaffName );

	pEditOperatorName->setText( QString::fromLocal8Bit( strStaffName.c_str() ));
}

void QTelSalePolicyInfoQuery::onQuery()
{
	string strPolicyNo = pEditPolicyNo->text().toLocal8Bit().data();
	string strOperatorCode;
	string strStartDate;
	string strEndDate;

	vector<SPolicyQuery> vPolicyInfo;

	if ( !pEditOperatorName->text().isEmpty() )
	{
		strOperatorCode = pEditOperatorCode->text().toLocal8Bit().data();
	}
	else
	{
		//自动使用操作员的工号为条件
		strOperatorCode = getUserCode();
	}

	if ( pGroupBoxDate->isChecked() )
	{
		strStartDate = pDateEditStart->date().toString( QString::fromLocal8Bit("MM/dd/yyyy") ).toLocal8Bit().data();
		strEndDate = pDateEditEnd->date().toString( QString::fromLocal8Bit("MM/dd/yyyy") ).toLocal8Bit().data();
	}

	try
	{
		QueryTelSalePolicyInfo( strPolicyNo,
			                    strOperatorCode,
			                    strStartDate,
			                    strEndDate,
								vPolicyInfo );

		showData( vPolicyInfo );
	}
	catch ( runtime_error & error )
	{
		QMessageBox::critical( this, QString::fromLocal8Bit("查询错误！"), QString::fromLocal8Bit(error.what()) );
	}
}

void QTelSalePolicyInfoQuery::onReset()
{
	//录入时间
	pDateEditStart->setDate( QDate::currentDate().addDays(-1) );
	pDateEditEnd->setDate( QDate::currentDate() );

	pEditPolicyNo->clear();
	pEditOperatorCode->clear();
	pEditOperatorName->clear();
}

void QTelSalePolicyInfoQuery::showData(std::vector<SPolicyQuery> & vPolicy)
{
	QTableWidgetItem * pItem = NULL;
	QLabel * pLabel = NULL;
	unsigned int nRowIndex = 0;

	pTableWidgetInfo->setRowCount( vPolicy.size() );

	for ( vector<SPolicyQuery>::iterator iter = vPolicy.begin(); iter != vPolicy.end(); ++iter )
	{
		//保单号
		pItem = new QTableWidgetItem( QString::fromLocal8Bit( iter->strPolicySerial.c_str() ));
		pItem->setTextAlignment( Qt::AlignHCenter | Qt::AlignVCenter );

		pTableWidgetInfo->setItem( nRowIndex, 0, pItem );

		//经办人
		pItem = new QTableWidgetItem( QString::fromLocal8Bit( iter->strSalerCode.c_str() ));
		pItem->setTextAlignment( Qt::AlignHCenter | Qt::AlignVCenter );

		pTableWidgetInfo->setItem( nRowIndex, 1, pItem );

		//部门
		pItem = new QTableWidgetItem( QString::fromLocal8Bit( iter->strDeptName.c_str() ));
		pItem->setTextAlignment( Qt::AlignHCenter | Qt::AlignVCenter );

		pTableWidgetInfo->setItem( nRowIndex, 2, pItem );

		//科室
		pItem = new QTableWidgetItem( QString::fromLocal8Bit( iter->strOfficeName.c_str() ));
		pItem->setTextAlignment( Qt::AlignHCenter | Qt::AlignVCenter );

		pTableWidgetInfo->setItem( nRowIndex, 3, pItem );

		//车店联呼
		if ( iter->strCDLH == "1" )
		{
			pItem = new QTableWidgetItem( QString::fromLocal8Bit( "是" ));
		}
		else
		{
			pItem = new QTableWidgetItem( QString::fromLocal8Bit( "否" ));
		}

		pItem->setTextAlignment( Qt::AlignHCenter | Qt::AlignVCenter );

		pTableWidgetInfo->setItem( nRowIndex, 4, pItem );

		//车商代码
		pItem = new QTableWidgetItem( QString::fromLocal8Bit( iter->strAutoTraderCode.c_str() ));
		pItem->setTextAlignment( Qt::AlignHCenter | Qt::AlignVCenter );

		pTableWidgetInfo->setItem( nRowIndex, 5, pItem );

		//车商名称
		pItem = new QTableWidgetItem( QString::fromLocal8Bit( iter->strAutoTraderName.c_str() ));
		pItem->setTextAlignment( Qt::AlignHCenter | Qt::AlignVCenter );

		pTableWidgetInfo->setItem( nRowIndex, 6, pItem );

		//礼品总价值
		pItem = new QTableWidgetItem( QString::number(iter->dGiftPriceSum, 'f', 2) );
		pItem->setTextAlignment( Qt::AlignHCenter | Qt::AlignVCenter );

		pTableWidgetInfo->setItem( nRowIndex, 7, pItem );

		//礼品
		pItem = new QTableWidgetItem( QString::fromLocal8Bit( iter->strGifts.c_str() ));
		pItem->setTextAlignment( Qt::AlignLeft | Qt::AlignVCenter );

		pTableWidgetInfo->setItem( nRowIndex, 8, pItem );

		//录入日期
		pItem = new QTableWidgetItem( QString::fromLocal8Bit( iter->strInputDate.c_str() ));
		pItem->setTextAlignment( Qt::AlignHCenter | Qt::AlignVCenter );

		pTableWidgetInfo->setItem( nRowIndex, 9, pItem );

		++nRowIndex;

	}

	pTableWidgetInfo->resizeColumnsToContents();
}
