#include "dialogedittrainee.h"
#include "ui_dialogedittrainee.h"
#include <QPushButton>

DialogEditTrainee::DialogEditTrainee(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogEditTrainee),
    traineeInput()
{
    ui->setupUi(this);
    verify();

    ui->editName->setProperty("mandatoryField", true);
    ui->editLogin->setProperty("mandatoryField", true);
    ui->editPassword->setProperty("mandatoryField", true);
}

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

void DialogEditTrainee::setTrainee(Trainee trainee)
{
    traineeInput = trainee;

    ui->editName->setText(trainee.getName());
    ui->editLogin->setText(trainee.getLogin());
    ui->editPassword->setText(trainee.getPassword());

    ui->checkArchived->setChecked(trainee.getArchived());
    ui->checkLoggedIn->setChecked(trainee.getLoggedIn());
}

Trainee DialogEditTrainee::getTrainee()
{
    Trainee trainee = traineeInput;

    trainee.setName(ui->editName->text());
    trainee.setLogin(ui->editLogin->text());
    trainee.setPassword(ui->editPassword->text());

    trainee.setArchived(ui->checkArchived->isChecked());
    trainee.setLoggedIn(ui->checkLoggedIn->isChecked());

    return trainee;
}

void DialogEditTrainee::on_editName_textChanged(const QString &arg1)
{
    verify();
}

void DialogEditTrainee::on_editLogin_textChanged(const QString &arg1)
{
    verify();
}

void DialogEditTrainee::on_editPassword_textChanged(const QString &arg1)
{
    verify();
}

void DialogEditTrainee::verify()
{
    if(ui->editName->text().trimmed() == QStringLiteral("") ||
            ui->editLogin->text().trimmed() == QStringLiteral("") ||
            ui->editPassword->text().trimmed() == QStringLiteral(""))
        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    else
        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
}
