#include "%1.h"
#include "MessageException.h"
#include "%2.h"
#include "Application.h"

#include <QMessageBox>

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

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

	init();
	doQuery();
}

%1::~%1()
{
	delete ui;
}

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

void %1::initTableView()
{
	ui->tvData->setEditTriggers(QAbstractItemView::NoEditTriggers);
	ui->tvData->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
	ui->tvData->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui->tvData->setAlternatingRowColors(true);
	ui->tvData->verticalHeader()->hide();
    ui->tvData->setAlternatingRowColors(true);
}

void %1::initModel()
{
	m_Model = new %3(this);
	ui->tvData->setModel(m_Model);
    ui->tvData->hideColumns<%2>();
}

void %1::initService()
{
	m_Service = new %4(this);
}

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

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

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

void %1::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 %1::onQuery()
{
	doQuery();
}

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

void %1::onAdd()
{
    QWidget *widget = m_FormBuilder->build<%2>("添加%5");
    m_FormBuilder->clear();
	int result = Application::instance()->showModalDialog(widget, this);

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

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

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

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

void %1::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());
}


