#include "AirportWidget.h"
#include "MessageException.h"
#include "Airport.h"
#include "Application.h"
#include "CustomDelegate.h"

#include <QMessageBox>

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

AirportWidget::AirportWidget(QWidget *parent)
	: QWidget(parent)
	, m_PageIndex(0)
{
	ui = new Ui::AirportWidget();
	ui->setupUi(this);

	init();
	doQuery();

}

AirportWidget::~AirportWidget()
{
	delete ui;
}

void AirportWidget::init()
{
	initTableView();
	initModel();
	initService();
	initPagination();
	initFormBuild();
}

void AirportWidget::initTableView()
{
    ui->tvData->setEditTriggers(QAbstractItemView::DoubleClicked);
    CustomDelegate *delegate = new CustomDelegate(this);
    delegate->setClass<Airport>();
    ui->tvData->setItemDelegate(delegate);
}

void AirportWidget::initModel()
{
	m_Model = new AirportModel(this);
	ui->tvData->setModel(m_Model);
    ui->tvData->hideColumns<Airport>();
    connect(m_Model,&QAbstractItemModel::dataChanged, this, &AirportWidget::onDataChange);
}

void AirportWidget::initService()
{
	m_Service = new AirportService(this);
}

void AirportWidget::initFormBuild()
{
    m_FormBuilder = new FormBuilder(this);
}

void AirportWidget::initPagination()
{
	ui->pagination->setPageCount(0);
	ui->pagination->setRecordCount(0);

	connect(ui->pagination, &Pagination::selectPage, [=](int value) {
		doQuery(value);
	});
}

void AirportWidget::doQuery(int pageIndex /*= 0*/)
{
	QString name = ui->leQName->text();
	try
	{
		m_Model->fetchByName(name, pageIndex);
		ui->pagination->setRecordCount(m_Model->getTotalCount());
		ui->pagination->setPageCount(m_Model->getPageCount());
	}
	catch (MessageException &e)
	{
		QMessageBox::critical(this, "错误", e.getMessage());
	}
}

void AirportWidget::onQuery()
{
	doQuery();
}

void AirportWidget::onReset()
{
	ui->leQName->clear();
	doQuery();
}

void AirportWidget::onAdd()
{
    QWidget *widget = m_FormBuilder->build<Airport>("添加机场");
    m_FormBuilder->clear();
	int result = Application::instance()->showModalDialog(widget, this);

	if (result == QDialog::Accepted)
	{
        Airport_ptr obj = m_FormBuilder->getValue<Airport>();
		try
		{
			m_Service->add(obj);
		}
		catch (MessageException &e)
		{
			QMessageBox::critical(this, "错误", e.getMessage());
			return;
		}
		doQuery();
	}
}

void AirportWidget::onEdit()
{
	QModelIndex index = ui->tvData->currentIndex();
	if (!index.isValid())
	{
        QMessageBox::information(this, "提示", "请选中要修改的数据!");
		return;
	}
	Airport_ptr oldObj = m_Model->getCollection()->_get<Airport_ptr>(index.row());
	
    QWidget *widget = m_FormBuilder->build<Airport>("修改机场");
    m_FormBuilder->clear();
    m_FormBuilder->fill(oldObj);

	int result = Application::instance()->showModalDialog(widget, this);

	if (result == QDialog::Accepted)
	{
		try{
            Airport_ptr newObj = m_FormBuilder->getValue<Airport>();
			newObj->setID(oldObj->getID());
			m_Service->updateById(newObj);
		}
		catch (MessageException &e){
			QMessageBox::critical(this, "错误", e.getMessage());
			return;
		}
		doQuery();
	}
}

void AirportWidget::onRemove()
{
	QModelIndex index = ui->tvData->currentIndex();
	if (!index.isValid())
	{
        QMessageBox::information(this, "提示", "请选中要删除的数据!");
		return;
	}
	QMessageBox::StandardButton msgBox;
	msgBox = QMessageBox::question(this, "提示", "确定删除选中的数据吗?", QMessageBox::Yes | QMessageBox::No);

	if (msgBox == QMessageBox::No)
		return;

	QSqlError error = m_Model->qxDeleteRow(index.row());
	if (error.isValid())
	{
		QMessageBox::critical(this, "错误", error.text()); 
		return;
	}
    m_Model->removeRow(index.row());
}

void AirportWidget::onDataChange(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
    if(roles.contains(Qt::DisplayRole) || roles.contains(Qt::EditRole))
    {
        if(m_Model->getLastError().isValid())
        {
            QMessageBox::information(this,"提示",m_Model->getLastErrorAsString());
        }
    }
}


