#include "dialogchecktask.h"
#include "ui_dialogchecktask.h"

DialogCheckTask::DialogCheckTask(ConnectorToServer* connectorToServer, QString type, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DialogCheckTask),
    connectorToServer(connectorToServer),
    fimTasksWidget(nullptr),
    flChanged(false),
    id_task(0),
    type("fim")
{
    ui->setupUi(this);

    this->type = type;

    if(type == "fim")
    {
        fimTasksWidget = new FIMtasksWidget(nullptr, TypeListTreeAMMFIM::listOneTask, this);

        ui->verticalLayout_3->addWidget(fimTasksWidget);

        ui->plainText->setReadOnly(true);
        ui->plainText->setObjectName("plainText");
    }
    else
    {
        ui->plainText->setVisible(false);
        ui->label->setVisible(false);
        ui->label_2->setVisible(false);
        ui->btnRight->setText(tr("Completed"));
        ui->btnWrong->setText(tr("New"));
        ui->btnWrong->setIcon(QIcon(QStringLiteral(":/resources/icons/circleGray.png")));

        ui->btnRight->setEnabled(false);
        ui->btnWrong->setEnabled(false);
    }
}

DialogCheckTask::~DialogCheckTask()
{
    if(fimTasksWidget)
        delete fimTasksWidget;
    delete ui;
}

void DialogCheckTask::setTask(TaskAmmFim* task)
{
    this->task = *task;
    id_task = task->getID();

    if(type == "fim")
    {
        //Задача
        fimTasksWidget->setOneTaskFim(&this->task);

        //Репорт
        outReport(this->task.report);
    }
    else if(type == "amm")
    {
        if(this->task.status == "new")
            ui->btnRight->setEnabled(true);
        else
            ui->btnWrong->setEnabled(true);
    }
}

TaskAmmFim DialogCheckTask::getTask()
{
    return this->task;
}

void DialogCheckTask::setModule(Module *module)
{
    this->module = *module;
    id_task = this->module.getID();
}

void DialogCheckTask::outReport(FIMReport report)
{
    QString str;
    /*
    str = QString("<b>Report ID: %1</b>").arg(QString::number(report.id));
    ui->plainText->appendHtml(str);
    ui->plainText->appendHtml("<br>");
    */

    for(FIMReportItem item : report.itemList)
    {
        /*
        //Item ID
        str = QString("<p>Item ID: %1</p>").arg(QString::number(item.id));
        ui->plainText->appendHtml(str);
        */

        //Title
        str = QString("<b>%1</b>").arg(item.procedure.title);
        ui->plainText->appendHtml(str);

        //DMcode
        str = QString("<p>%1</p>").arg(item.procedure.dmCode);
        ui->plainText->appendHtml(str);

        //Result
        str = item.procedure.result;
        if(str == "viewed")
            str = tr("viewed");
        else if(str == "completed")
            str = tr("completed");
        str = QString("<p>%1</p>").arg(str);
        ui->plainText->appendHtml(str);

        //Text
        str = QString("<p style=\"color:green;\">%1</p>").arg(item.text);
        ui->plainText->appendHtml(str);

        ui->plainText->appendHtml("<br>");
    }
}

void DialogCheckTask::on_btnWrong_clicked()
{
    TypeQueryToDB typeQuery;
    QString status;
    QString msgString;

    if(type == "fim")
    {
        typeQuery = TypeQueryToDB::TYPE_QUERY_CHANGE_STATUS_REPORT_TASK_FIM_TO_TRAINEE;
        status = "failed";
        msgString = tr("Change task status?\nThe status will be set:\n'failed'");
    }
    else
    {
        typeQuery = TypeQueryToDB::TYPE_QUERY_CHANGE_STATUS_REPORT_TASK_AMM_TO_TRAINEE;
        status = "new";
        msgString = tr("Change task status?\nThe status will be set:\n'new'");
    }

    if(QMessageBox::warning(this, tr("Attention!"), msgString, QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok)
    {
        connectorToServer->sendQueryToDB(typeQuery, id_task, (void*)&status);
        this->parentWidget()->close();
        flChanged = true;
    }
}

void DialogCheckTask::on_btnRight_clicked()
{
    TypeQueryToDB typeQuery;
    QString status;
    QString msgString;

    if(type == "fim")
    {
        typeQuery = TypeQueryToDB::TYPE_QUERY_CHANGE_STATUS_REPORT_TASK_FIM_TO_TRAINEE;
        status = "completed";
        msgString = tr("Change task status?\nThe status will be set:\n'completed'");
    }
    else
    {
        typeQuery = TypeQueryToDB::TYPE_QUERY_CHANGE_STATUS_REPORT_TASK_AMM_TO_TRAINEE;
        status = "completed";
        msgString = tr("Change task status?\nThe status will be set:\n'completed'");
    }

    if(QMessageBox::warning(this, tr("Attention!"), msgString, QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok)
    {
        connectorToServer->sendQueryToDB(typeQuery, id_task, (void*)&status);
        this->parentWidget()->close();
        flChanged = true;
    }
}
