#include "Login.h"
#include "OriginalWnd/OriginalWnd.h"
#include "ui_Login.h"
#include <QStackedWidget>
#include <qDebug>
#include <QSettings>

Login::Login(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Login)
{
    ui->setupUi(this);
    initForm();

    if(gen_if.getLanguageValue() == 0){

    }else{
        setChineseMode(this);
    }
}

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

void Login::initForm()
{
    QPixmap pixmap(":/images/logo.png");
    QPixmap scaledPixmap = pixmap.scaled(283, 40, Qt::KeepAspectRatio);
    ui->label_logo->setPixmap(scaledPixmap);

    ui->userNameLineEdit->setPlaceholderText("Please enter the account number");
    ui->passLineEdit->setPlaceholderText("Please enter your password");

    connect(ui->userNameLineEdit, &QLineEdit::textChanged, this, &Login::onUserNameChanged);
}
void Login::setChineseMode(QMainWindow *Login){
    Login->setWindowTitle(QCoreApplication::translate("Login", "主界面", nullptr));
    ui->SinginButton->setText(QCoreApplication::translate("Login", "登录", nullptr));
    ui->label_logo->setText(QString());
    ui->label_pass->setText(QString());
    ui->label_user->setText(QString());
    ui->ExitButton->setText(QCoreApplication::translate("Login", "退出", nullptr));
    ui->userNameLineEdit->setPlaceholderText("请输入账号");
    ui->passLineEdit->setPlaceholderText("请输入密码");
}
void Login::onUserNameChanged(const QString &userName) {
    QSettings settings("YourCompany_2", "YourAppName_2");
    settings.setValue("userName", userName);
}

void Login::on_SinginButton_clicked()
{
    if (QStackedWidget *parent = qobject_cast<QStackedWidget*>(this->parentWidget())) {
        OriginalWnd *originalWnd = new OriginalWnd(this);
        parent->addWidget(originalWnd);
        parent->setCurrentIndex(parent->indexOf(originalWnd));
    }
}

void Login::on_ExitButton_clicked()
{
    qApp->quit();
}

