#include "changepasswordform.h"
#include "ui_changepasswordform.h"
#include <QSettings>
#include "loappconfig.h"
#include "x_uimsgboxok.h"
#include "LoUIClass/taesclass.h"
#include "QTextCodec"
ChangePasswordForm::ChangePasswordForm(QWidget *parent) :
    LoQDialog(parent),
    ui(new Ui::ChangePasswordForm)
{
    ui->setupUi(this);
    ui->lineEdit->setEchoMode(QLineEdit::Password);
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);
    ui->lineEdit_3->setEchoMode(QLineEdit::Password);
    ui->pushButton_2->setProperty("ssType", "progManageTool");
    ui->pushButton_2->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButton->setProperty("ssType", "progManageTool");
    ui->pushButton->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    setStyleSheet("background-color: #D8D8D8;");
    ui->lineEdit->setStyleSheet("background-color: #FFFFFF;");
    ui->lineEdit_2->setStyleSheet("background-color: #FFFFFF;");
    ui->lineEdit_3->setStyleSheet("background-color: #FFFFFF;");
    connect(this, SIGNAL(accepted()), this, SLOT(onAccepted()));
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(onOkButton()));
}

ChangePasswordForm::~ChangePasswordForm()
{
    delete ui;
}
void ChangePasswordForm::onAccepted()
{

}
void ChangePasswordForm::onOkButton()
{
    //如果旧密码错误
    LoAppConfig *cfg = LoAppConfig::getInstance();
    QSettings *settings = new QSettings(cfg->OrganizationName(), cfg->ApplicationName());
#ifndef MACRO_YUANHENG_VERSION

    QString strMiWenPs=settings->value("advUiPs").toString();
#else
    QString strMiWenPs=settings->value("advUiPs_YuanHeng").toString();

#endif
    QString result = QTextCodec::codecForName("GBK")->toUnicode(QByteArray::fromBase64(strMiWenPs.toLocal8Bit()));
//    QByteArray miwen = strMiWenPs.toLatin1();
//     if(miwen.size()>=3)
//    {
//        miwen[0]=miwen[0]-'1';
//        miwen[1]=miwen[1]-'7';
//        miwen[2]=miwen[2]-'5';
//        miwen[3]=miwen[3]-'8';
//        miwen[4]=miwen[4]-'0';
//        miwen[5]=miwen[5]-'1';
//    }
    QString strPs=result;


    if(strPs.isEmpty())
    {
#ifndef MACRO_YUANHENG_VERSION
        strPs = "888";
#else
        strPs = "Cargo10065!@#";

#endif
    }

    QString strOldPs=ui->lineEdit->text();
    if(strOldPs.isEmpty())
    {
        X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("Please input old password"),this,1);
       pDlg->exec();
       ui->lineEdit->setFocus();
        return;
    }
    if(ui->lineEdit->text()!=strPs)
    {
        X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("Old password is wrong"),this,1);
       pDlg->exec();
        ui->lineEdit->setFocus();
        return;
    }
    if(ui->lineEdit_2->text().length()<6)
    {
        X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("Please enter a password with more than 6 characters"),this,1);
       pDlg->exec();
        ui->lineEdit_2->setFocus();
        return;
    }
    if(ui->lineEdit_3->text().length()<6)
    {
        X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("Please enter a password with more than 6 characters"),this,1);
       pDlg->exec();
        ui->lineEdit_3->setFocus();
        return;
    }
    if(ui->lineEdit_2->text()!=ui->lineEdit_3->text())//两次输入新密码不一致
    {
        X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("The new password is not consistent in two times"),this,1);
       pDlg->exec();
        ui->lineEdit_2->setFocus();
        return;
    }
    QString strNewPs=ui->lineEdit_2->text();
    if(strNewPs.isEmpty())
    {
        X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("Please enter a new password"),this,1);
       pDlg->exec();
        ui->lineEdit_2->setFocus();
        return;

    }
    else {

        QString  str=ui->lineEdit_2->text();
        QByteArray a = str.toLocal8Bit().toBase64();

        QString newStr(a);
//        QByteArray New_mingwen = str.toLatin1();
//        New_mingwen[0]=New_mingwen[0]+'1';
//        New_mingwen[1]=New_mingwen[1]+'7';
//        New_mingwen[2]=New_mingwen[2]+'5';
//        New_mingwen[3]=New_mingwen[3]+'8';
//        New_mingwen[4]=New_mingwen[4]+'0';
//        New_mingwen[5]=New_mingwen[5]+'1';
//        QString strtemp=New_mingwen;
#ifndef MACRO_YUANHENG_VERSION

        settings->setValue("advUiPs",newStr);
#else
        settings->setValue("advUiPs_YuanHeng",newStr);

#endif

    }
    X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("Password changed successfully"),this,1);
   pDlg->exec();
   this->accept();
   // emit sigAcceptData(ui->lineEdit_2->text());
}
