#include "logindlg.h"
#include "ui_logindlg.h"
#include "sqliteassistant/sqliteassistant.h"
#include "myhelper.h"
LoginDlg::LoginDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDlg)
{
    ui->setupUi(this);
    m_MainWindow = new MainWindow();
    lab_tip = new QLabel(this);
    timer_tip = new QTimer(this);
    lab_tip->setSizeIncrement(200,20);
    connect(timer_tip,&QTimer::timeout,this,[=](){
        timer_tip->stop();
        lab_tip->close();
    });
    connect(ui->pbn_close,&QPushButton::clicked,this,&MainWindow::close);
    connect(ui->pbn_login,&QPushButton::clicked,this,[=](){
        if(!LoginJudgment()){
            return;
        }else{
            m_MainWindow->show();
            this->close();
        }
    });
    setWindowTitle("用户登陆");
    LoadStyleSheet();
    InitGui();
}
void LoginDlg::LoadStyleSheet()
{
    QFile qss(":/image/Login_Dialog.qss");
    qss.open(QFile::ReadOnly);
    this->setStyleSheet(qss.readAll());
    qss.close();
}

bool LoginDlg::LoginJudgment()
{
    lab_tip->setStyleSheet("QLabel{background: transparent;border: 1px solid transparent;"
                           "color:rgb(255,0,0);font-weight: bold;font: 25 10pt Microsoft YaHei;padding: 1px;}");
    lab_tip->move(this->width()/2 - 90,this->height()- ui->pbn_login->height());

    QString user = "admin";
    QString pwd = "123456";
    QString rootuser = "root";

    QString m_user = ui->lineEdit_user->text();
    QString m_pwd = ui->lineEdit_pwd->text();
    if(m_user == user && m_pwd == pwd ){
        return true;
    }
    if(m_user == rootuser && m_pwd == pwd ){
        //config->show();
        this->close();
    }
    if(m_user == user && m_pwd != pwd){
        lab_tip->setText("密码错误，请重新输入");
        ui->lineEdit_pwd->clear();
        ui->lineEdit_pwd->setFocus();
        lab_tip->adjustSize();
        lab_tip->show();
        timer_tip->start(2000);
        return false;
    }
    if(m_user != user && m_pwd == pwd){
        lab_tip->setText("用户名错误，请重新输入");
        ui->lineEdit_user->clear();
        ui->lineEdit_user->setFocus();
        lab_tip->adjustSize();
        lab_tip->show();
        timer_tip->start(2000);
        return false;
    }
    if(m_user != user && m_pwd != pwd){
        lab_tip->setText("用户名和密码错误，请重新输入");
        ui->lineEdit_pwd->clear();
        ui->lineEdit_user->clear();
        ui->lineEdit_user->setFocus();
        lab_tip->adjustSize();
        lab_tip->show();
        timer_tip->start(2000);
        return false;
    }
    return false;
}
LoginDlg::~LoginDlg()
{
    delete ui;
}

void LoginDlg::InitGui()
{
    //setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    ui->lab_title->setText("用户登陆");
    ui->lab_name->setText("车辆ID管理系统");
    ui->lineEdit_user->setFocus();
}
