//
// Created by Krad on 2021/12/8.
//

#include "AccountTableForm.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QToolButton>
#include <QHeaderView>
#include "components/AccountRoleComboDelegate.h"
#include "guimacros.h"
#include "AlertDialog.h"
#include "db/SQLHelper.h"
#include "models/User.h"
#include <QSqlRecord>
#include "components/SlideableTableView.h"
#include "AccountFormDialog.h"
#include "event/EventCenter.h"
AccountTableForm::AccountTableForm(QWidget* parent) {
	layout = new QVBoxLayout(this);
	layout->setMargin(0);
	QTableView* table = new SlideableTableView(this);
	layout->addWidget(table);
	// TableView for patient
	table->setAlternatingRowColors(true);
	table->setSelectionMode(QAbstractItemView::SingleSelection);
	table->setEditTriggers(QAbstractItemView::NoEditTriggers);
	table->setSelectionBehavior(QAbstractItemView::SelectRows);
	table->verticalHeader()->setDefaultSectionSize(38);
	table->horizontalHeader()->setStretchLastSection(true);
	//data from SQLITE

	auto model = SQLHelper::getTable("Account");
	model->sort(5, Qt::DescendingOrder);
	model->select();
	model->setHeaderData(1, Qt::Horizontal, tr("ID"));
	model->setHeaderData(2, Qt::Horizontal, tr("Name"));
	model->setHeaderData(4, Qt::Horizontal, tr("Role"));
	model->setHeaderData(5, Qt::Horizontal, tr("Comment"));
	table->setModel((QAbstractItemModel*)model);
	table->hideColumn(0);
	table->hideColumn(3);
	AccountRoleComboDelegate* comboDelegate = new AccountRoleComboDelegate(this);
	table->setItemDelegateForColumn(4, comboDelegate);
	table->show();

	//    table->setSortingEnabled(true);
	table->setColumnWidth(1, 250);
	table->setColumnWidth(2, 250);
	table->setColumnWidth(4, 150);

	QWidget* cmdPanel = new QWidget(this);
	cmdPanel->setObjectName("commandWidgetnoBBorder");
	QHBoxLayout* cmdLayout = new QHBoxLayout(cmdPanel);

	cmdLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding));
	QWidget* spacerLine = new QWidget(this);
	spacerLine->setFixedWidth(2);
	spacerLine->setObjectName("verSpaceLine");
	cmdLayout->addWidget(spacerLine);
	ADD_TOOL_BTN_TO_LAYOUT(Add, ":/icons/add.png", cmdLayout);
	ADD_TOOL_BTN_TO_LAYOUT(Edit, ":/icons/details.png", cmdLayout);
	ADD_TOOL_BTN_TO_LAYOUT(Delete, ":/icons/close_circle.png", cmdLayout);
	btnAdd->setText(tr("Add"));
	btnEdit->setText(tr("Edit"));
	btnDelete->setText(tr("Delete"));
	layout->addWidget(cmdPanel);
	//index change
	connect(table, &QTableView::clicked, [=](const QModelIndex& modelIndex) {
		if (currentRow != modelIndex.row())
		{
			currentRow = modelIndex.row();
		}
		});
	//add new account
	connect(btnAdd, &QToolButton::clicked, [=]() {
		AccountFormDialog dialog(this, New);
		dialog.setWindowModality(Qt::WindowModal);
		dialog.setReferenceModel(model);
		if (dialog.exec() == QDialog::Accepted)
		{
			table->selectRow(0);
		}
		});
	connect(btnEdit, &QToolButton::clicked, [=]() {
		if (currentRow < 0)return;
		QMap<QString, QVariant> map;
		auto record = model->record(currentRow);
		for (int i = 0; i < model->columnCount(); i++)
		{
			map[record.fieldName(i)] = record.value(i);
		}
		auto mode = map["UserID"] == User::Current()->getUserID() ? Self : Admin;
		AccountFormDialog dialog(this, mode);
		dialog.setWindowModality(Qt::WindowModal);
		if (mode == Admin)dialog.setAccountInformation(map);
		if (dialog.exec() == QDialog::Accepted)
		{
			model->select();
			table->selectRow(currentRow);
		}
		});
	connect(btnDelete, &QToolButton::clicked, [=]() {
		if (currentRow < 0)return;
		QString id = model->data(model->index(currentRow, 1)).toString();
		if (User::Current()->getUserID() == id)
		{
			//尝试删除自己
			AlertDialog dialog(this);
			dialog.setButtonMode(OkOnly);
			dialog.setWindowModality(Qt::WindowModal);
			dialog.setAlertMessage(tr("Can't delete current log in account!"));
			dialog.exec();
			return;
		}
		AlertDialog dialog(this);
		dialog.setWindowModality(Qt::WindowModal);

		dialog.setAlertMessage(QString(tr("Delete account with ID:\"%1\"!")).arg(id));
		if (dialog.exec() != QDialog::Accepted) return;
		model->removeRow(currentRow);
		model->select();
		table->selectRow(model->rowCount() > currentRow + 1 ? currentRow : currentRow - 1);
		});

	connect(EventCenter::Default(), &EventCenter::ReloadLanguage, [=]() {

		//model->setHeaderData(1, Qt::Horizontal, tr("ID"));
		model->setHeaderData(2, Qt::Horizontal, tr("Name"));
		model->setHeaderData(4, Qt::Horizontal, tr("Role"));
		model->setHeaderData(5, Qt::Horizontal, tr("Comment"));

		btnAdd->setText(tr("Add"));
		btnEdit->setText(tr("Edit"));
		btnDelete->setText(tr("Delete"));
		});


}

AccountTableForm::~AccountTableForm() {

}
