#include "cdlglogin.h"
#include "ui_cdlglogin.h"
#include "cworker.h"
#include "cworkerdriver.h"
#include "logic.h"
#include "mainwindow.h"
CDlgLogin::CDlgLogin(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CDlgLogin)
{
    ui->setupUi(this);
    ui->label_Info->setStyleSheet("color:red");
    ui->lineEdit_PWD->setEchoMode(QLineEdit::Password);
    ui->lineEdit_PWD->m_bNumOnly=false;
}

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

void CDlgLogin::showEvent(QShowEvent *)
{
	ui->comboBox_name->setEditText(m_name);
    m_Info.clear();
    ui->label_Info->setText(m_Info);
    m_PWD.clear();
    ui->lineEdit_PWD->setText(m_PWD);
	ui->comboBox_name->setFocus();
}

void CDlgLogin::on_checkBox_DisplayInput_clicked(bool checked)
{
    if(!checked)
    {
        ui->lineEdit_PWD->setEchoMode(QLineEdit::Password);
    }
    else
    {
        ui->lineEdit_PWD->setEchoMode(QLineEdit::Normal);
    }
}



void CDlgLogin::on_pushButton_OK_clicked()
{
    m_name=ui->comboBox_name->currentText();
    m_PWD=ui->lineEdit_PWD->text();

    CWorkerDriver* pWorkerDriver=GET_CARD(M_WORKERDRIVER,CWorkerDriver);
    CWorker* pWorker=GET_CHDIRECT(M_WORKER,CWorker);
    pWorker->m_workName=m_name;
    bool bGetWorker= pWorkerDriver->GetWorker(M_WORKER);
    if(!bGetWorker||pWorker->m_workPWD!=m_PWD)
    {
        m_Info=QObject::trUtf8("Unknown user name "
                                     "or password.");
        pWorker->m_workLevel= LEVEL_OPERATOR;
		pWorker->m_workName="Not Login";
        ui->label_Info->setText(m_Info);
        m_PWD.clear();
        ui->lineEdit_PWD->setText(m_PWD);
        ui->lineEdit_PWD->setFocus();
		m_gSystem.EvtLog(QObject::tr("OP"), QString("Login failed User:%1").arg(m_name));
        return ;
    }

	m_gSystem.EvtLog(QObject::tr("OP"), QString("Login. User:%1").arg(pWorker->m_workName));
    accept();
    m_gMainWindow->LoginFresh();
}

void CDlgLogin::on_pushButton_Cancel_clicked()
{
    m_name.clear();
    m_PWD.clear();

    /*CWorkerDriver* pWorkerDriver=GET_CARD(M_WORKERDRIVER,CWorkerDriver);
    CWorker* pWorker=GET_CHDIRECT(M_WORKER,CWorker);
    pWorker->m_workName=_str_workername[0];
    pWorker->m_workPWD="";
    pWorker->m_workLevel=1;
    pWorkerDriver->GetWorker(M_WORKER);*/

    reject();
    m_gMainWindow->LoginFresh();
}
