#include "loginwidget.h"
#include "wordartwidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QComboBox>
#include <QPushButton>
#include <QCheckBox>
#include <QMouseEvent>
#include <QLineEdit>
#include <QSpacerItem>
#include <QPainter>

#include "global.h"
#include "wordartwidget.h"
#include "cmessagebox.h"

LoginWidget::LoginWidget(QWidget *parent)
    : QWidget(parent)
{
    initialize();
    createConnections();
}

LoginWidget::~LoginWidget()
{

}



void LoginWidget::show()
{
    QWidget::show();
    m_pLoginWidget->show();
    // 登录窗口显示，就清除密码
    m_pPasswordLineEdit->clear();
}

void LoginWidget::setBackgroundImage(const QString &path)
{
    mBackImagePath = path;
    mBackPixmap = QPixmap(path);
}

bool LoginWidget::isLogin()
{
    return mLogin;
}

void LoginWidget::mousePressEvent(QMouseEvent *event)
{
    if(!m_bFixed) {
        m_bMove = true;
        m_spanPoint = event->globalPos() - this->pos();
    }
}

void LoginWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(!m_bFixed)
    {
        if(m_bMove)
        {
            this->move(event->globalPos() - m_spanPoint);
        }
    }
}

void LoginWidget::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    m_bMove = false;
}

void LoginWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    //painter.setOpacity(m_fBgImageOpacity);
    //    painter.setPen(Qt::NoPen);
    //    painter.setBrush(Qt::gray);
    if(mBackImagePath.isEmpty())
        painter.drawPixmap(QRect(0, 0, this->width(), this->height()), QPixmap(":/res/image/login/bg01.png"));
    else
        painter.drawPixmap(QRect(0, 0, this->width(), this->height()), mBackPixmap);
}

void LoginWidget::initialize()
{
    mLogin = false;
    m_bFixed = false;
    //this->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setFixedSize(600, 400);

    m_pMainVLayout = new QVBoxLayout;

//    m_pLoginWaitWidget = new LoginWaitWidget;
//    m_pLoginWaitWidget->setDotColor("#58F335");
//    m_pLoginWaitWidget->setTextColor("#FFFFFF");
//    m_pLoginWaitWidget->hide();
//    m_pLoginWaitWidget->setFixedSize(400, 400);
//    m_pLoginWaitWidget->setText("正在登录，请稍等...");
//    m_pMainVLayout->addWidget(m_pLoginWaitWidget, 0, Qt::AlignHCenter);

    m_pLoginWidget = new QWidget;
    m_pLoginWidget->setObjectName("LoginWidget");
    m_pLoginWidget->setFixedSize(600, 400);
    m_pLoginVLayout = new QVBoxLayout;
    m_pLoginWidget->setLayout(m_pLoginVLayout);
    m_pMainVLayout->addWidget(m_pLoginWidget);

    m_pTitleWidget = new WordArtWidget(this);
    m_pTitleWidget->setObjectName("LoginTitleWidget");
    m_pTitleWidget->setFixedSize(320, 80);
    m_pTitleWidget->setText("登录管理系统");
    m_pTitleWidget->setOutColor("#41D3E9");
    m_pLoginVLayout->addWidget(m_pTitleWidget, 0, Qt::AlignHCenter);

    m_pRememberHLayout = new QHBoxLayout;
    m_pRememberAccountButton = new QCheckBox(this);
    m_pRememberAccountButton->setText("记住账号");
    m_pRememberAccountButton->setObjectName("LoginWidgetRoleRememberAccountButton");
    m_pRememberPasswordButton = new QCheckBox(this);
    m_pRememberPasswordButton->setText("记住密码");
    m_pRememberPasswordButton->setObjectName("LoginWidgetRoleRememberPasswordButton");
    m_pRememberHLayout->addStretch();
    m_pRememberHLayout->addWidget(m_pRememberAccountButton, 0, Qt::AlignHCenter);
    m_pRememberHLayout->addWidget(m_pRememberPasswordButton, 0, Qt::AlignHCenter);
    m_pRememberHLayout->addStretch();
    m_pLoginVLayout->addLayout(m_pRememberHLayout);
    // m_pRememberAccountButton->hide();
    // m_pRememberPasswordButton->hide();

    m_pAccountWidget = new QWidget;
    m_pAccountWidget->setObjectName("AccountWidget");
    m_pAccountWidget->setFixedSize(270, 150);
    m_pAccoutGLayout = new QGridLayout;
    m_pAccountWidget->setLayout(m_pAccoutGLayout);

    mRoleLabel = new QLabel;
    mRoleLabel->setText("角色：");
    mRoleLabel->setObjectName("LoginWidgetRoleLabel");
    mRoleComboBox = new QComboBox;
    mRoleComboBox->setFixedSize(190, 33);
    mRoleComboBox->setObjectName("LoginWidgetRoleComboBox");
    mRoleComboBox->addItem("员工");
    mRoleComboBox->addItem("工程师");
    mRoleComboBox->addItem("主管");
    m_pAccoutGLayout->addWidget(mRoleLabel, 0, 0);
    m_pAccoutGLayout->addWidget(mRoleComboBox, 0, 1);

    m_pAccountLabel = new QLabel;
    m_pAccountLabel->setText("账号：");
    m_pAccountLabel->setObjectName("LoginWidgetAccountLabel");
    m_pAccountComboBox = new QComboBox;
    m_pAccountComboBox->setObjectName("LoginWidgetAccountComboBox");
    m_pAccountComboBox->setFixedSize(190, 33);
    m_pAccountComboBox->setEditable(true);
    QLineEdit* accountLineEdit = m_pAccountComboBox->lineEdit();
    accountLineEdit->setPlaceholderText("账号");
    m_pAccoutGLayout->addWidget(m_pAccountLabel, 1, 0);
    m_pAccoutGLayout->addWidget(m_pAccountComboBox, 1, 1);

    m_pPasswordLabel = new QLabel;
    m_pPasswordLabel->setText("密码：");
    m_pPasswordLabel->setObjectName("LoginWidgetPasswordLabel");
    m_pPasswordLineEdit = new QLineEdit;
    m_pPasswordLineEdit->setFixedSize(190, 33);
    m_pPasswordLineEdit->setObjectName("LoginWidgetPasswordLineEdit");
    m_pPasswordLineEdit->setPlaceholderText("密码");
    m_pPasswordLineEdit->setEchoMode(QLineEdit::Password);
    m_pAccoutGLayout->addWidget(m_pPasswordLabel, 2, 0);
    m_pAccoutGLayout->addWidget(m_pPasswordLineEdit, 2, 1);


    m_pLoginVLayout->addWidget(m_pAccountWidget, 0, Qt::AlignHCenter);

    m_pButtonHLayout = new QHBoxLayout;
    m_pLoginButton = new QPushButton(this);
    m_pLoginButton->setFixedSize(100, 40);
    m_pLoginButton->setText("登录");
    m_pLoginButton->setObjectName("LoginButton");
    m_pReturnButton = new QPushButton;
    m_pReturnButton->setFixedSize(100, 40);
    m_pReturnButton->setText("返回");
    m_pReturnButton->setObjectName("ReturnButton");
    m_pButtonHLayout->addWidget(m_pLoginButton);
    m_pButtonHLayout->addWidget(m_pReturnButton);
    m_pLoginVLayout->addLayout(m_pButtonHLayout);

    this->setLayout(m_pMainVLayout);

}

void LoginWidget::createConnections()
{
    connect(m_pReturnButton, &QPushButton::clicked, this, &LoginWidget::signalReturn);


    connect(m_pLoginButton, &QPushButton::clicked,
            this, &LoginWidget::slotLogin);

//    connect(m_pLoginWaitWidget, &LoginWaitWidget::signalCancel,
//            this, &LoginWidget::slotCancel);

}

void LoginWidget::slotShowPassword()
{
    m_pPasswordLineEdit->setEchoMode(QLineEdit::Normal);
}

void LoginWidget::slotHidePassword()
{
    m_pPasswordLineEdit->setEchoMode(QLineEdit::Password);
}

void LoginWidget::slotLogin()
{
    QString id = m_pAccountComboBox->currentText();
    QString password = m_pPasswordLineEdit->text();
    if(id.isEmpty()) {      
        CMessageBox msgBox;
        msgBox.setMessageType(CMessageBox::TipBox);
        msgBox.setButtonNum(1);
        msgBox.setText(QString("请输入您的账号ID."));
        msgBox.exec();

        return;
    }

    if(password.isEmpty()) {       
        CMessageBox msgBox;
        msgBox.setMessageType(CMessageBox::TipBox);
        msgBox.setButtonNum(1);
        msgBox.setText(QString("请输入您的账号密码."));
        msgBox.exec();
        return;
    }

    // m_pLoginWidget->hide();
//    m_pLoginWaitWidget->show();
//    m_pLoginWaitWidget->start();
    this->hide();

    emit loggedIn();
    mLogin = true;
}

void LoginWidget::slotCancel()
{
//    m_pLoginWaitWidget->stop();
//    m_pLoginWaitWidget->hide();
    m_pLoginWidget->show();
}

