#include "LoginWindowForm.h"
#include "ui_LoginWindowForm.h"

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

    // 初始化窗体
    initForm();

    // 绑定输入框回车事件 只在密码输入框执行
    connect(ui->iptPassword, &QLineEdit::returnPressed, this, [=](){
        emit ui->btnLogin->clicked();
    });

    LogUtil::PrintLog("INFO", "应用启动成功");

    /** 临时代码 为了调试方便自动登录 **/
    ui->iptUsername->setText("adminszh");
    ui->iptPassword->setText("111111");
    QTimer::singleShot(200, this, [=](){
        emit ui->btnLogin->clicked();
    });
}

LoginWindowForm::~LoginWindowForm()
{
    qDebug() << "~LoginWindowForm";
    delete ui;
}

void LoginWindowForm::mouseMoveEvent(QMouseEvent *event)
{
    QWidget::mouseMoveEvent(event);
    QPoint y = event->globalPos();//鼠标相对于桌面左上角的位置，鼠标全局位置
    QPoint x = y - this->last;

    this->move(x);
}
void LoginWindowForm::mousePressEvent(QMouseEvent *event)
{
    QWidget::mousePressEvent(event);
    QPoint y = event->globalPos();//鼠标相对于桌面左上角的位置，鼠标全局位置
    QPoint x = this->geometry().topLeft();//窗口左上角相对于桌面左上角的位置，窗口位置
    this->last = y - x;//定值，不变的
}
void LoginWindowForm::mouseReleaseEvent(QMouseEvent *event)
{
    QWidget::mouseReleaseEvent(event);
    this->last = QPoint();//鼠标松开的时候，将z值初始化
}

void LoginWindowForm::initForm()
{
    setWindowFlags(Qt::FramelessWindowHint); // 设置无边框

    // 设置窗口位置和大小
    resize(1366, 768);

    QRect screenRect = QApplication::screens().at(0)->availableGeometry();
    qint16 screenWidth = screenRect.width();
    qint16 screenHeight = screenRect.height();
    move((screenWidth - 1366) / 2, (screenHeight - 768) / 2);

    // 加载样式表
    QString qssStyle = QssFileUtils::loadQssFileContent(":/qss/loginForm.css");
    if (qssStyle.isEmpty() == false) {
        this->setStyleSheet(qssStyle); // 仅本窗口生效
    }

    // 初始化输入框的icon
    QIcon iconUser(":/image/login/icon_user.png");  // 图标路径需替换为实际资源路径
    QIcon iconPass(":/image/login/icon_pass.png");  // 图标路径需替换为实际资源路径
    ui->iptUsername->addAction(iconUser, QLineEdit::LeadingPosition);  // 左侧添加图标
    ui->iptPassword->addAction(iconPass, QLineEdit::LeadingPosition);
    ui->iptUsername->setFocus();

    // 初始化软件名称
    ui->labelTitle->setText(SettingConfig::getInstance().CLIENT_TITLE);
}

void LoginWindowForm::on_btnLogin_clicked()
{
    // 查询数据库内是否有账号
    QString account = ui->iptUsername->text();
    QString password = ui->iptPassword->text();
    bool isSuccess = false;

    SysUserService userServ;
    QMap<QString, QVariant> loginUser;

    if (!(account.isEmpty() || password.isEmpty())) {
        isSuccess = userServ.verifySysUser(account, password, loginUser);
        if (isSuccess) {
            ui->iptPassword->clear();
            ui->iptUsername->clear();

            ProMemory::getInstance().setLoginUser(loginUser);
//            qDebug() << loginUser.value("userRole").toStringList() << loginUser.value("userRoleIndex").toStringList();
            onLoginSuccessHandler();
        } else {
            CustomMessageBox::failure(this, "登录失败", "账号或密码错误，请重新输入！");
            ui->iptPassword->clear();
            ui->iptPassword->setFocus();
        }
    } else {
        CustomMessageBox::warning(this, "提示", "账号或密码不能为空！");
    }
}

void LoginWindowForm::on_btnMinWin_clicked()
{
    setWindowState(Qt::WindowMinimized | windowState());
}

void LoginWindowForm::on_btnCloseWin_clicked()
{
    LogUtil::PrintLog("INFO", "应用退出");
    emit close();
}

void LoginWindowForm::onLoginSuccessHandler()
{
    LogUtil::PrintLog("INFO", QString("[%1]登录成功").arg(ProMemory::getInstance().getLoginUser().value("name").toString()));

    MainWindowForm * mainWin = new MainWindowForm();
    mainWin->show();
    mainWin->activateWindow();
    mainWin->setLoginUsername(ProMemory::getInstance().getLoginUser().value("name").toString());

    // 设置弹窗遮罩层效果的父窗口
    MaskWidget::instance()->setMainWidget(mainWin);

    // 绑定退出事件
    connect(mainWin, &MainWindowForm::logout, this, &LoginWindowForm::onLogoutHandler);

    this->hide();
}

void LoginWindowForm::onLogoutHandler()
{
    this->show();
    ui->iptUsername->setFocus();
}
