#include "formtemplatelist.h"
#include "ui_formtemplatelist.h"
#include "templatedao.h"
#include "formtemplateeditor.h"
#include <QSqlQueryModel>
#include <QMessageBox>

formTemplateList::formTemplateList(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::formTemplateList)
{
    ui->setupUi(this);
    setupModel();

    connect(ui->refreshButton, &QPushButton::clicked, this, &formTemplateList::refreshList);
    connect(ui->newButton, &QPushButton::clicked, this, &formTemplateList::createNewTemplate);
    connect(ui->editButton, &QPushButton::clicked, this, &formTemplateList::editTemplate);
    connect(ui->deleteButton, &QPushButton::clicked, this, &formTemplateList::deleteTemplate);
}

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

void formTemplateList::setupModel()
{
    QSqlQueryModel *model = new QSqlQueryModel(this);
    model->setQuery("SELECT ID, NAME, DESCRIPTION, DOCUMENT_TYPE FROM TEMPLATE ORDER BY NAME");
    model->setHeaderData(0, Qt::Horizontal, tr("ID"));
    model->setHeaderData(1, Qt::Horizontal, tr("Nombre"));
    model->setHeaderData(2, Qt::Horizontal, tr("Descripción"));
    model->setHeaderData(3, Qt::Horizontal, tr("Tipo"));
    ui->templateTable->setModel(model);
    ui->templateTable->setColumnHidden(0, true);
}

void formTemplateList::refreshList()
{
    QSqlQueryModel *model = qobject_cast<QSqlQueryModel*>(ui->templateTable->model());
    if (model)
        model->setQuery(model->query().lastQuery());
}

void formTemplateList::createNewTemplate()
{
    formTemplateEditor *editor = new formTemplateEditor(this);
    connect(editor, &formTemplateEditor::templateSaved, this, [this]() {
        refreshList();
    });
    editor->show();
}

void formTemplateList::editTemplate()
{
    QModelIndex idx = ui->templateTable->currentIndex();
    if (!idx.isValid()) {
        QMessageBox::information(this, tr("Info"), tr("Selecciona una plantilla primero."));
        return;
    }
    int row = idx.row();
    int id = ui->templateTable->model()->data(ui->templateTable->model()->index(row, 0)).toInt();

    Template tpl = TemplateDAO::getById(id);
    formTemplateEditor *editor = new formTemplateEditor(this);
    editor->loadTemplate(tpl);
    connect(editor, &formTemplateEditor::templateSaved, this, [this]() {
        refreshList();
    });
    editor->show();
}

void formTemplateList::deleteTemplate()
{
    QModelIndex idx = ui->templateTable->currentIndex();
    if (!idx.isValid()) {
        QMessageBox::information(this, tr("Info"), tr("Selecciona una plantilla primero."));
        return;
    }
    int row = idx.row();
    int id = ui->templateTable->model()->data(ui->templateTable->model()->index(row, 0)).toInt();

    int ret = QMessageBox::question(this, tr("Confirmar"), tr("¿Eliminar esta plantilla?"),
                                     QMessageBox::Yes | QMessageBox::No);
    if (ret == QMessageBox::Yes) {
        if (TemplateDAO::remove(id)) {
            QMessageBox::information(this, tr("Éxito"), tr("Plantilla eliminada."));
            refreshList();
        } else {
            QMessageBox::critical(this, tr("Error"), tr("No se pudo eliminar la plantilla."));
        }
    }
}