#include "formprojectlist.h"
#include "ui_formprojectlist.h"
#include "formproject.h"
#include "projectdao.h"
#include <QSqlQueryModel>
#include <QMessageBox>

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

    connect(ui->refreshButton, &QPushButton::clicked, this, &formProjectList::refreshList);
    connect(ui->newButton, &QPushButton::clicked, this, &formProjectList::createNewProject);
    connect(ui->editButton, &QPushButton::clicked, this, &formProjectList::editProject);
    connect(ui->deleteButton, &QPushButton::clicked, this, &formProjectList::deleteProject);
}

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

void formProjectList::setupModel()
{
    QSqlQueryModel *model = new QSqlQueryModel(this);
    model->setQuery("SELECT CODE, TITLE, CUSTOMER_NAME, STATE, START_DATE FROM PROJECT ORDER BY CODE");
    model->setHeaderData(0, Qt::Horizontal, tr("Código"));
    model->setHeaderData(1, Qt::Horizontal, tr("Título"));
    model->setHeaderData(2, Qt::Horizontal, tr("Cliente"));
    model->setHeaderData(3, Qt::Horizontal, tr("Estado"));
    model->setHeaderData(4, Qt::Horizontal, tr("Fecha Inicio"));
    ui->projectTable->setModel(model);
}

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

void formProjectList::createNewProject()
{
    formProject *form = new formProject(this);
    form->setNewMode();
    connect(form, &formProject::projectSaved, this, [this]() {
        refreshList();
    });
    form->show();
}

void formProjectList::editProject()
{
    QModelIndex idx = ui->projectTable->currentIndex();
    if (!idx.isValid()) {
        QMessageBox::information(this, tr("Info"), tr("Selecciona un proyecto."));
        return;
    }
    int row = idx.row();
    QString code = ui->projectTable->model()->data(ui->projectTable->model()->index(row, 0)).toString();

    formProject *form = new formProject(this);
    form->loadProject(code);
    connect(form, &formProject::projectSaved, this, [this]() {
        refreshList();
    });
    form->show();
}

void formProjectList::deleteProject()
{
    QModelIndex idx = ui->projectTable->currentIndex();
    if (!idx.isValid()) {
        QMessageBox::information(this, tr("Info"), tr("Selecciona un proyecto."));
        return;
    }
    int row = idx.row();
    QString code = ui->projectTable->model()->data(ui->projectTable->model()->index(row, 0)).toString();

    int ret = QMessageBox::question(this, tr("Confirmar"), tr("¿Eliminar el proyecto %1?").arg(code),
                                     QMessageBox::Yes | QMessageBox::No);
    if (ret == QMessageBox::Yes) {
        if (ProjectDAO::remove(code)) {
            QMessageBox::information(this, tr("Éxito"), tr("Proyecto eliminado."));
            refreshList();
        } else {
            QMessageBox::critical(this, tr("Error"), tr("No se pudo eliminar el proyecto."));
        }
    }
}