#include "clEditPassword.h"
#include "ui_clEditPassword.h"

#include <QValidator>
#include "CAppInfo.h"
#include "logproc.h"
#include "awMsgBox.h"
#include <QRegExpValidator>
#include <QRegExp>
clEditPassword::clEditPassword(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::clEditPassword)
{
    ui->setupUi(this);

    setStyleSheet(g_appInfoPtr->m_sQssStr);

	qRegisterMetaType<CEditPassword>("CEditPassword");
	connect(g_httpBllPtr.get(), &CHttpBll::sgnEditPassword, this, &clEditPassword::onEditPassword);
}

clEditPassword::~clEditPassword()
{
    awMsgBox::clear(this);
    delete ui;
}


void clEditPassword::setUI(const int nWidth, const int nHeight)
{
    setGeometry(0, 0, nWidth, nHeight);
    QRegExp regx("[a-zA-Z0-9]{6,18}");
    ui->edt_oldPassword->setGeometry(g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*30,
                                     g_appInfoPtr->m_fRate*360, g_appInfoPtr->m_fRate*40);
	QValidator *validator = new QRegExpValidator(regx, ui->edt_newPassword);
	ui->edt_newPassword->setValidator(validator);
	ui->edt_newPassword->setGeometry(ui->edt_oldPassword->x(), ui->edt_oldPassword->y() + ui->edt_oldPassword->height() + g_appInfoPtr->m_fRate*20,
                                     ui->edt_oldPassword->width(), ui->edt_oldPassword->height());
	QValidator *validator1 = new QRegExpValidator(regx, ui->edt_newPassword1);
	ui->edt_newPassword1->setValidator(validator1);
    ui->edt_newPassword1->setGeometry(ui->edt_oldPassword->x(), ui->edt_newPassword->y() + ui->edt_newPassword->height() + g_appInfoPtr->m_fRate*20,
                                    ui->edt_oldPassword->width(), ui->edt_oldPassword->height());
    ui->btn_savePassword->setGeometry(ui->edt_oldPassword->x(), ui->edt_newPassword1->y() + ui->edt_newPassword1->height() + g_appInfoPtr->m_fRate*20,
                                      g_appInfoPtr->m_fRate*80, g_appInfoPtr->m_fRate*36);
}

void clEditPassword::on_btn_savePassword_clicked()
{
    if (ui->edt_oldPassword->text().isEmpty())
	{
        ShowMsg(QString::fromLocal8Bit("请先输入原始密码"), this, MSG_ICON_TYPE::mit_error);
        return;
	}

    if (ui->edt_newPassword->text() != ui->edt_newPassword1->text())
    {
        ShowMsg(QString::fromLocal8Bit("两次输入密码不一致"), this, MSG_ICON_TYPE::mit_error);
        return;
    }

    if(ui->edt_newPassword->text().length() < 6 ||
            ui->edt_newPassword->text().length() > 18)
    {
        ShowMsg(QString::fromLocal8Bit("请输入6到18位数字或字母"), this, MSG_ICON_TYPE::mit_error);
        return;
    }

    CHttpRequestPackage hrp;
    hrp.sUri = "/api/ecs_core/student/password";
    hrp.nRequestType = RequestType::rtEditPassword;
    hrp.eParamType = HttpParamType::hptUrl;
	hrp.sParamList.push_back(QString("newPassword,%1").arg(ui->edt_newPassword->text()));
	hrp.sParamList.push_back(QString("password,%1").arg(ui->edt_oldPassword->text()));    
    g_httpBllPtr->put(hrp);
}

void clEditPassword::onEditPassword(CEditPassword editPassword)
{
	if (editPassword.nCode == 200)
	{
        ShowMsg(QString::fromLocal8Bit("保存密码成功"), this, MSG_ICON_TYPE::mit_succeed);
	}
	else
	{
		if (editPassword.sMessage != "")
		{
            ShowMsg(editPassword.sMessage, this, MSG_ICON_TYPE::mit_error);
		}
		else
		{
            ShowMsg(QString::fromLocal8Bit("保存密码失败"), this, MSG_ICON_TYPE::mit_error);
		}
	}
}
