#include "formthird.h"
#include "ui_formthird.h"
#include "mapplication.h"
#include "formbase.h"

#include <QFileDialog>
#include <QtSql>

formThird::formThird(QString aID, int aEditMode, QWidget *parent) :
    formBase(aID, aEditMode, parent),
    ui(new Ui::formThird),
    rowid(-1)
{
    ui->setupUi(this);
    ui->tabClient->setVisible(false);
    ui->tabSupplier->setVisible(false);
    ui->editDischargeDate->setDate(QDate::currentDate());
    ui->editUpdateDate->setDate(QDate::currentDate());

    connect(ui->Logo, &AvatarWidget::addClicked, this, &formThird::LoadLogo);
    //connect(ui->Logo, &AvatarWidget::clicked, this, &formThird::LoadLogo);

    mEditMode = false;
}

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

void formThird::openDocument(QString id)
{
    mEditMode = true;
    //ui->editCode->setText(id);
    //ui->editCode->setReadOnly(true);

    dApp->Enterprise().open();
    QSqlQuery qry = QSqlQuery(dApp->Enterprise());

    QString command = QString("SELECT * FROM THIRD WHERE ID = '%1';").arg(id);
    if (qry.exec(command))
    {
        qry.first();

        ui->editName->setText(qry.value(qry.record().indexOf("NAME")).toString());
        ui->editNickname->setText(qry.value(qry.record().indexOf("NICKNAME")).toString());
        ui->editCIF->setText(qry.value(qry.record().indexOf("CIF")).toString());
        ui->editIntraCode->setText(qry.value(qry.record().indexOf("INTRA_FC")).toString());

        ui->comboState->setCurrentIndex(qry.value(qry.record().indexOf("STATE")).toInt());
        ui->comboClient->setCurrentIndex(qry.value(qry.record().indexOf("CLIENT_STATE")).toInt());
        ui->comboClient->setCurrentIndex(qry.value(qry.record().indexOf("SUPPLIER_STATE")).toInt());
        ui->editClientCode->setText(qry.value(qry.record().indexOf("CLIENT_CODE")).toString());
        ui->editSupplierCode->setText(qry.value(qry.record().indexOf("SUPPLIER_CODE")).toString());

        ui->editAddress->setPlainText(qry.value(qry.record().indexOf("ADDRESS")).toString());
        ui->editCP->setText(qry.value(qry.record().indexOf("POSTCODE")).toString());
        ui->editCity->setText(qry.value(qry.record().indexOf("CITY")).toString());
        ui->editProvince->setText(qry.value(qry.record().indexOf("PROVINCE")).toString());
        ui->comboCountry->setCurrentText(qry.value(qry.record().indexOf("COUNTRY_ID")).toString());

        ui->editPhone->setText(qry.value(qry.record().indexOf("PHONE")).toString());
        ui->editFax->setText(qry.value(qry.record().indexOf("FAX")).toString());
        ui->editMobile->setText(qry.value(qry.record().indexOf("MOBILE")).toString());
        ui->editEmail->setText(qry.value(qry.record().indexOf("EMAIL")).toString());
        ui->editWebside->setText(qry.value(qry.record().indexOf("WEBSIDE")).toString());

        ui->editPublicNotes->setHtml(qry.value(qry.record().indexOf("NOTE_PUBLIC")).toString());
        ui->editPrivateNotes->setHtml(qry.value(qry.record().indexOf("NOTE_PRIVATE")).toString());

        ui->editDischargeDate->setDate(qry.value(qry.record().indexOf("CREATEDAT")).toDate());
        ui->editUpdateDate->setDate(qry.value(qry.record().indexOf("UPDATEDAT")).toDate());
    }
    else
    {
        qDebug() << "Error ejecutando el query: " << qry.lastError().text() << "\n";
    }

    dApp->Enterprise().close();
}

void formThird::closeDocument()
{
    close();
    qDebug() << " Terceros:  closeDocument";
}

void formThird::on_cbClient_currentIndexChanged(int index)
{
    if(index == 0)
        ui->tabClient->setVisible(false);
    else
        ui->tabClient->setVisible(true);
}

void formThird::on_cbSupplier_currentIndexChanged(int index)
{
    if(index == 0)
        ui->tabSupplier->setVisible(false);
    else
        ui->tabSupplier->setVisible(true);
}

void formThird::on_buttonSave_released()
{
    save();
}

void formThird::LoadLogo()
{
    QString imageFile = QFileDialog::getOpenFileName(this,
                                             tr("Elige una imagen"),
                                             "",
                                             tr("Todas las imagenes(*.png *.jpg *.gif *.bmp)"));
    /*
    if(imageFile.isEmpty())
        return;

    imageProfile = ImageCropperDialog::getCroppedImage(imageFile, 600, 400, CropperShape::SQUARE);
    if (imageProfile.isNull())
        return;

    ui->profilePicture->setAvatarPixmap(imageProfile);
    */
    ui->Logo->setAvatarPath(imageFile);
}

void formThird::save()
{
    QString command;

    if (mEditMode)
        command = "UPDATE THIRD SET "
                  "FORM = :FORM, CIF = :CIF, NAME = :NAME, NICKNAME = :NICKNAME, FAMILY = :FAMILY, "
                  "INTRA_FC = :INTRA_FC, STATE = :STATE, CLIENT_STATE = :CLIENT_STATE, CLIENT_CODE = :CLIENT_CODE, "
                  "CLIENT_ACCOUNT = :CLIENT_ACCOUNT, SUPPLIER_STATE = :SUPPLIER_STATE, SUPPLIER_CODE = :SUPLLIR_CODE, "
                  "SUPPLIER_ACCOUNT = :SUPPLIER_ACCOUNT, ADDRESS = :ADDRESS, POSTCODE = :POSTCODE, CITY = :CITY, "
                  "PROVINCE = :PROVINCE, COUNTRY_ID = :COUNTRY_ID, PHONE = :PHONE, FAX = :FAX, "
                  "EMAIL = :EMAIL, WEBSIDE = :WEBSIDE, PAYMENT_METHOD = :PAYMENT_METHOD, PAYMENT_TYPE = :PAYMENT_TYPE, "
                  "LOGO = :LOGO, NOTE_PUBLIC = :NOTE_PUBLIC, NOTE_PRIVATE = :NOTE_PRIVATE, UPDATEDAT = :UPDATEDAT, "
                  "CREATEDAT = :CREATEDAT, CREATEDBY = :CREATEBY "
                  "WHERE CIF = '" + ui->editCIF->text() +
                  "';";
    else
        command = "INSERT INTO THIRD ("
                  "FORM, CIF, NAME, NICKNAME, FAMILY, INTRA_FC, STATE, CLIENT_STATE, CLIENT_CODE, "
                  "CLIENT_ACCOUNT, SUPPLIER_STATE, SUPPLIER_CODE, SUPPLIER_ACCOUNT, ADDRESS, "
                  "POSTCODE, CITY, PROVINCE, COUNTRY_ID, PHONE, FAX, EMAIL, WEBSIDE, "
                  "PAYMENT_METHOD, PAYMENT_TYPE, LOGO, NOTE_PUBLIC, NOTE_PRIVATE, UPDATEDAT, "
                  "CREATEDAT, CREATEDBY"
                  ") VALUES ("
                  ":FORM, :CIF, :NAME, :NICKNAME, :FAMILY, :INTRA_FC, :STATE, :CLIENT_STATE, :CLIENT_CODE, "
                  ":CLIENT_ACCOUNT, :SUPPLIER_STATE, :SUPPLIER_CODE, :SUPPLIER_ACCOUNT, :ADDRESS, "
                  ":POSTCODE, :CITY, :PROVINCE, :COUNTRY_ID, :PHONE, :FAX, :EMAIL, :WEBSIDE, "
                  ":PAYMENT_METHOD, :PAYMENT_TYPE, :LOGO, :NOTE_PUBLIC, :NOTE_PRIVATE, :UPDATEDAT, "
                  ":CREATEDAT, :CREATEDBY"
                  ");";

    qDebug() << command;

    dApp->Enterprise().open();
    QSqlQuery qry = QSqlQuery(dApp->Enterprise());
    qry.prepare(command);

    //qry.bindValue(":FORM", );
    qry.bindValue(":CIF", ui->editCIF->text());
    qry.bindValue(":NAME", ui->editName->text());
    qry.bindValue(":NICKNAME", ui->editNickname->text());
    //qry.bindValue(":FAMILY", );
    qry.bindValue(":INTRA_FC", ui->editIntraCode->text());
    qry.bindValue(":STATE", ui->comboState->currentIndex());

    qry.bindValue(":CLIENT_STATE", ui->comboClient->currentIndex());
    qry.bindValue(":CLIENT_CODE", ui->editClientCode->text());
    qry.bindValue(":CLIENT_ACCOUNT", ui->editAccountClient->text());

    qry.bindValue(":SUPPLIER_STATE", ui->comboSupplier->currentIndex());
    qry.bindValue(":SUPPLIER_CODE", ui->editSupplierCode->text());
    qry.bindValue(":SUPPLIER_ACCOUNT", ui->editAccountSupplier->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_ID", ui->comboCountry->currentText());
    //qry.bindValue(":COUNTRY", ui->editIntraCode->text());

    qry.bindValue(":PHONE", ui->editPhone->text());
    qry.bindValue(":FAX", ui->editFax->text());
    qry.bindValue(":MOBILE", ui->editMobile->text());
    qry.bindValue(":EMAIL", ui->editEmail->text());
    qry.bindValue(":WEBSIDE", ui->editWebside->text());

    //qry.bindValue(":PAYMENT_METHOD", ui->editPhone->text());
    //qry.bindValue(":PAYMENT_TYPE", ui->editFax->text());

    //qry.bindValue(":LOGO", ui->editMobile->text());
    qry.bindValue(":NOTE_PUBLIC", ui->editPublicNotes->toHtml());
    qry.bindValue(":NOTE_PRIVATE", ui->editPrivateNotes->toHtml());

    qry.bindValue(":UPDATEDAT", ui->editUpdateDate->date());
    qry.bindValue(":CREATEDAT", ui->editDischargeDate->date());
    //qry.bindValue(":CREATEDBY", ui->editWebside->text());

    if (!qry.exec())
    {
        qDebug() << "Error ejecutando el query: " << qry.lastError().text() << "\n";
    }
    else
    {
        setEditMode();
    }

    dApp->Enterprise().close();
}

void formThird::setEditMode()
{
    mEditMode = true;
    ui->editDischargeDate->setReadOnly(true);
}


void formThird::on_editName_textChanged(const QString &arg1)
{
    mNeedSave = true;
}


void formThird::on_comboClient_currentIndexChanged(int index)
{
    /*
    // Ocultar pestaña
    int indexToHide = 2;
    QWidget *tab = ui->tabWidget->widget(indexToHide);
    hiddenTabs[indexToHide] = tab; // Guarda la pestaña en un mapa
    ui->tabWidget->removeTab(indexToHide);

    // Mostrar pestaña
    int indexToShow = 2;
    if (hiddenTabs.contains(indexToShow)) {
        ui->tabWidget->insertTab(indexToShow, hiddenTabs[indexToShow]);
        hiddenTabs.remove(indexToShow);
    }
    */
}



