#include "WidgetLogin.h"

#include <QPushButton>

#include "app/Application.h"

WidgetLogin::WidgetLogin()
{
    this->resize(1920, 1080);


    QUI::label(labBkg_, this, 0, 0, 1920, 1080, "");
    labBkg_.setPixmap(QPixmap("assets/ui/bkgLogin.png"));

    int w = 517;
    int h = 534;
    int x = (1920 - w) *0.5;
    int y = (1080 - h) *0.5;

    //QUI::label(labLoginBkg_, this, x, y, w, h, "");
    //labLoginBkg_.setPixmap(QPixmap("assets/ui/login.png"));


    QUI::lineedit(lineUsername_, this, 870, 447, 300, 36);
    QUI::lineedit(linePasswd_, this, 870, 510, 300, 36);

    std::string sty = "QLineEdit {"
        "   background-color: #2859b0;" // 背景颜色
        "   color: #ffffff;"            // 文字颜色
        "   border: 0px solid #ccc;"    // 边框
        "   border-radius: 5px;"        // 圆角
        "   padding: 5px;"              // 内边距
        "   font-size: 16px;"           // 字体大小
        "   font-weight: bold;"         // 字体加粗
        "}";
    lineUsername_.setStyleSheet(sty.c_str());
    linePasswd_.setStyleSheet(sty.c_str());

    // 提示信息框
    QUI::label(labMsg_, this, 838, 630, 350, 36, "");

    // 登录按钮
    QUI::button(btnLoigin_, this, 838, 588, 338, 46, "登    入", "background-color:#2a82e4; color:white;font-size: 14px;font-weight:bold;");
    QObject::connect(&btnLoigin_, &QPushButton::clicked, this, &WidgetLogin::slotBtnCilckLogin);
}


void WidgetLogin::slotBtnCilckLogin()
{
    if (0)
    {
        std::string account = lineUsername_.text().toStdString();
        std::string passwd = linePasswd_.text().toStdString();
        std::string err;

        if (account.empty())
        {
            labMsg_.setText(std::string("请输入用户名" + err).c_str());
            return;
        }
        if (passwd.empty())
        {
            labMsg_.setText(std::string("请输入用密码" + err).c_str());
            return;
        }

        auto& op = Application::instance().getOperator();
        bool ret = op.login(account, passwd, err);
        if (!ret)
        {
            labMsg_.setText(std::string("登录失败：" + err).c_str());
            return;
        }
    }

    // 检查登录用户名和密码
    emit signalLoginSuccess();
}