#include "dialogcreateenterprise.h"
#include "ui_dialogcreateenterprise.h"
#include "sqltable.h"
#include "mapplication.h"

#include <QtSql>

dialogCreateEnterprise::dialogCreateEnterprise(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::dialogCreateEnterprise)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose, true);
}

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

void dialogCreateEnterprise::on_pushBack_released()
{

}

void dialogCreateEnterprise::on_pushNext_released()
{
    // TODO: guardar y crear la empresa
    if(ui->editName->text().isEmpty())
    {
        return;
    }

    if(ui->editCIF->text().isEmpty())
    {
        return;
    }

    createEnterprise();
    close();
}

void dialogCreateEnterprise::createEnterprise()
{
    QString CIF = ui->editCIF->text().toUpper();
    QString path = dApp->dataFolder() + CIF + QDir::separator();
    QDir dir;
    dir.mkpath(path);

    QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE", CIF);
    database.setDatabaseName(path + CIF + ".db");
    if(!database.open())
        return;

    // Crear la base de datos:
    for(auto table : dbTables)
    {
        QSqlQuery qry(database);
        if (!qry.exec(table))
        {
            // TODO: mandar un mensaje y repetir si es necesario
        }
    }

    QString ENTERPRISES = "INSERT INTO ENTERPRISES ("
                          "NAME, CIF, DBNAME, PATH"
                          ") VALUES ("
                          ":NAME, :CIF, :DBNAME, :PATH"
                          ");";

    QString ENTERPRISESINFO = "INSERT INTO ENTERPRISESINFO ("
                              "NAME, CIF, ADDRESS, POSTCODE, CITY, PROVINCE, COUNTRY, PHONE, MOBILE, FAX, "
                              "EMAIL, WEB, CIF_COMMUNITY, IAE, CNAE, DCNAE, LOGO1, LOGO2"
                              ") VALUES ("
                              ":NAME, :CIF, :ADDRESS, :POSTCODE, :CITY, :PROVINCE, :COUNTRY, :PHONE, :MOBILE, :FAX, "
                              ":EMAIL, :WEB, :CIF_COMMUNITY, :IAE, :CNAE, :DCNAE, :LOGO1, :LOGO2"
                              ");";

    QSqlQuery qry(database);
    //QSqlQuery *qry = new QSqlQuery(dApp->getDBConnection());
    qry.prepare(ENTERPRISESINFO);
    qry.bindValue(":NAME", ui->editName->text());
    qry.bindValue(":CIF", ui->editCIF->text());
    qry.bindValue(":ADDRESS", ui->editAddress->toPlainText());
    qry.bindValue(":POSTCODE", ui->editCP->text());
    qry.bindValue(":CITY", ui->editCity->text());
    qry.bindValue(":PROVINCE", ui->editProvince->text());
    qry.bindValue(":COUNTRY", ui->editCountry->text());
    qry.bindValue(":PHONE", ui->editPhone->text());
    //qry.bindValue(":MOBILE", ui->edit->text());
    qry.bindValue(":FAX", ui->editFax->text());
    qry.bindValue(":EMAIL", ui->editEmail->text());
    qry.bindValue(":WEB", ui->editWebside->text());
    //qry.bindValue(":CIF_COMMUNITY", ui->edit->text());
    //qry.bindValue(":IAE", ui->editName->text());
    //qry.bindValue(":CNAE", ui->editName->text());
    //qry.bindValue(":DCENAE", ui->editName->text());
    //qry.bindValue(":LOGO1", ui->editName->text());
    //qry.bindValue(":lOGO2", ui->editName->text());
    if (!qry.exec())
    {
        qDebug() << "Error ejecutando el query: " << qry.lastError().text() << "\n";
        return;
    }

    qry.prepare(DBInfoCommand);
    qry.bindValue(":VERSION", dApp->applicationVersion().isEmpty() ? "0" : dApp->applicationVersion());
    qry.bindValue(":SUBVERSION", "0");
    if (!qry.exec())
    {
        qDebug() << "Error ejecutando el query: " << qry.lastError().text() << "\n";
        return;
    }
    qry.clear();
    qry.finish();
    database.close();
    QSqlDatabase::removeDatabase(CIF);

    // 2. Poner la compañia en la lista:
    dApp->EnterpriseList().open();
    QSqlQuery qryList(dApp->EnterpriseList());
    qryList.prepare(ENTERPRISES);
    qryList.bindValue(":NAME",  ui->editName->text());
    qryList.bindValue(":CIF",   ui->editCIF->text());
    qryList.bindValue(":DBNAME", CIF);
    qryList.bindValue(":PATH",  path);
    if (!qryList.exec())
    {
        qDebug() << "Error ejecutando el query: " << qryList.lastError().text() << "\n";
        return;
    }
    dApp->Enterprise().close();

    // 3. Abrir la empresa
    dApp->openCompany(CIF);
}







