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

#include <QFileDialog>
#include <QtSql>
#include "../dao/thirddao.h"

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;
    rowid = id.toInt();

    ThirdDAO::Data data;
    if (ThirdDAO::read(rowid, data)) {
        ui->editName->setText(data.name);
        ui->editNickname->setText(data.nickname);
        ui->editCIF->setText(data.cif);
        ui->editIntraCode->setText(data.intraFc);

        ui->comboState->setCurrentIndex(data.state);
        ui->comboClient->setCurrentIndex(data.clientState);
        ui->editClientCode->setText(data.clientCode);
        ui->editAccountClient->setText(data.clientAccount);
        ui->comboSupplier->setCurrentIndex(data.supplierState);
        ui->editSupplierCode->setText(data.supplierCode);
        ui->editAccountSupplier->setText(data.supplierAccount);

        ui->editAddress->setPlainText(data.address);
        ui->editCP->setText(data.postcode);
        ui->editCity->setText(data.city);
        ui->editProvince->setText(data.province);
        ui->comboCountry->setCurrentText(data.countryId);

        ui->editPhone->setText(data.phone);
        ui->editFax->setText(data.fax);
        ui->editMobile->setText(data.mobile);
        ui->editEmail->setText(data.email);
        ui->editWebside->setText(data.website);

        ui->editPublicNotes->setHtml(data.notePublic);
        ui->editPrivateNotes->setHtml(data.notePrivate);

        ui->editDischargeDate->setDate(data.createdAt);
        ui->editUpdateDate->setDate(data.updatedAt);
    } else {
        qDebug() << "Error reading third with ID:" << id;
    }
}

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()
{
    ThirdDAO::Data data;
    data.cif = ui->editCIF->text();
    data.name = ui->editName->text();
    data.nickname = ui->editNickname->text();
    data.intraFc = ui->editIntraCode->text();
    data.state = ui->comboState->currentIndex();
    data.clientState = ui->comboClient->currentIndex();
    data.clientCode = ui->editClientCode->text();
    data.clientAccount = ui->editAccountClient->text();
    data.supplierState = ui->comboSupplier->currentIndex();
    data.supplierCode = ui->editSupplierCode->text();
    data.supplierAccount = ui->editAccountSupplier->text();
    data.address = ui->editAddress->toPlainText();
    data.postcode = ui->editCP->text();
    data.city = ui->editCity->text();
    data.province = ui->editProvince->text();
    data.countryId = ui->comboCountry->currentText();
    data.phone = ui->editPhone->text();
    data.fax = ui->editFax->text();
    data.mobile = ui->editMobile->text();
    data.email = ui->editEmail->text();
    data.website = ui->editWebside->text();
    data.notePublic = ui->editPublicNotes->toHtml();
    data.notePrivate = ui->editPrivateNotes->toHtml();
    data.updatedAt = ui->editUpdateDate->date();
    data.createdAt = ui->editDischargeDate->date();

    bool ok;
    if (mEditMode) {
        data.id = rowid;
        ok = ThirdDAO::update(data);
    } else {
        ok = ThirdDAO::create(data);
    }

    if (ok) {
        setEditMode();
    } else {
        qDebug() << "Error saving third";
    }
}

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);
    }
    */
}



