#include "dashboardNamingDialog.h"
#include "ui_dashboardNamingDialog.h"

#include <QRegularExpression>
#include <QRegularExpressionValidator>

DashboardNamingDialog::DashboardNamingDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::dashboardNamingDialog)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    m_strUsedFor = "create";

    ui->labelError->setText("");
    ui->lineEditName->setText("");
    QRegularExpression rx("[^\\s]+$");
    QRegularExpressionValidator *validator = new QRegularExpressionValidator(rx, this);
    ui->lineEditName->setValidator(validator);

    connect(ui->btnConfirm, SIGNAL(clicked()), this, SLOT(onBtnClicked_confirm()));
    connect(ui->btnCancle, SIGNAL(clicked()), this, SLOT(onBtnClicked_cancle()));
    connect(ui->btnClose, SIGNAL(clicked()), this, SLOT(onBtnClicked_close()));
    connect(ui->lineEditName, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged_name(const QString&)));
}

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

// void DashboardNamingDialog::showEvent(QShowEvent* event)
// {
//     Q_UNUSED(event);
//     ui->labelError->setText("");
//     ui->lineEditName->setText("");
//     ui->lineEditName->setFocus();

//     QDialog::showEvent(event);
// }

void DashboardNamingDialog::showUsedForCreat()
{
    ui->labeWindowlTitle->setText(QString::fromWCharArray(L"创建数据看板"));
    ui->labelError->setText("");
    ui->lineEditName->setText("");
    ui->lineEditName->setFocus();
    m_strUsedFor = "create";
    show();
}
void DashboardNamingDialog::showUsedForRename()
{
    ui->labeWindowlTitle->setText(QString::fromWCharArray(L"重命名数据看板"));
    ui->labelError->setText("");
    ui->lineEditName->setText("");
    ui->lineEditName->setFocus();
    m_strUsedFor = "rename";
    show();
}

void DashboardNamingDialog::showErrorInfo(const QString& strError)
{
    ui->labelError->setText(strError);
}

void DashboardNamingDialog::onTextChanged_name(const QString& strText)
{
    Q_UNUSED(strText);

    ui->labelError->setText("");
}

void DashboardNamingDialog::onBtnClicked_confirm()
{
    QString strName = ui->lineEditName->text();
    if(strName.isEmpty())
    {
        QString strError = QString::fromStdWString(L"看板名称不能为空");
        ui->labelError->setText(strError);
        return;
    }
    emit dashboardName(strName, m_strUsedFor);
}

void DashboardNamingDialog::onBtnClicked_cancle()
{
    hide();
    emit sgl_hide();
}

void DashboardNamingDialog::onBtnClicked_close()
{
    hide();
    emit sgl_hide();
}
