#include "newloginform.h"
#include "ui_newloginform.h"

#include "event/posevent.h"
#include "event/fmapplication.h"

#include "preDefine.h"

#include <QDebug>
#include <QVariantMap>
#include <QSettings>


#include <QsLog.h>

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

    is_login = false;

    ui->label_logotitle_2->setText(QString::fromLocal8Bit("非码SaaS点餐小程序门店管理"));

    connect(ui->lineEdit_account_2, &ClickedLineEdit::clicked, this, &NewLoginForm::GetCurrLineEdit);
    connect(ui->lineEdit_partnerid_2, &ClickedLineEdit::clicked, this, &NewLoginForm::GetCurrLineEdit);
    connect(ui->lineEdit_pwd_2, &ClickedLineEdit::clicked, this, &NewLoginForm::GetCurrLineEdit);
    //connect(ui->lineEdit_posno_2, &ClickedLineEdit::clicked, this, &LoginForm::GetCurrLineEdit);
    connect(ui->lineEdit_storeid_2, &ClickedLineEdit::clicked, this, &NewLoginForm::GetCurrLineEdit);

    connect(ui->pushButton_10, &QPushButton::clicked, this, &NewLoginForm::number_btn_click);
    connect(ui->pushButton_11, &QPushButton::clicked, this, &NewLoginForm::number_btn_click);
    connect(ui->pushButton_12, &QPushButton::clicked, this, &NewLoginForm::number_btn_click);
    connect(ui->pushButton_13, &QPushButton::clicked, this, &NewLoginForm::number_btn_click);
    connect(ui->pushButton_14, &QPushButton::clicked, this, &NewLoginForm::number_btn_click);
    connect(ui->pushButton_15, &QPushButton::clicked, this, &NewLoginForm::number_btn_click);
    connect(ui->pushButton_16, &QPushButton::clicked, this, &NewLoginForm::number_btn_click);
    connect(ui->pushButton_17, &QPushButton::clicked, this, &NewLoginForm::number_btn_click);
    connect(ui->pushButton_18, &QPushButton::clicked, this, &NewLoginForm::number_btn_click);
    connect(ui->pushButton_19, &QPushButton::clicked, this, &NewLoginForm::number_btn_click);
    connect(ui->pushButton_clear_2, &QPushButton::clicked, this, &NewLoginForm::number_btn_click);
    connect(ui->pushButton_del_2, &QPushButton::clicked, this, &NewLoginForm::number_btn_click);

    _curr_lineedit = ui->lineEdit_partnerid_2;
    _curr_lineedit->setFocus();

    setWindowFlags(Qt::FramelessWindowHint);


    m_alertForm = new AlertForm(this);



    FMApplication::subscibeEvent(this, PosEvent::s_login_status);
    FMApplication::subscibeEvent(this, PosEvent::s_opt_status);
    FMApplication::subscibeEvent(this, PosEvent::s_show_login);
}

void NewLoginForm::onHideAlert()
{
    m_alertForm->hide();
}

void NewLoginForm::onShowAlert(AlertForm::Type type, const QString &msg)
{

    if(this->isVisible())
    {
        m_alertForm->SetContent(type, msg);
        m_alertForm->show();
    }
}

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

void NewLoginForm::SetStoreInfo()
{
    QString configfile = qApp->applicationDirPath() + "/" + STOREINFO_NAME;

    QString storeid = QSettings(configfile, QSettings::IniFormat).value(INI_BASE_STOREID).toString();
    QString partnerid = QSettings(configfile, QSettings::IniFormat).value(INI_BASE_PARNETID).toString();
    QString userid = QSettings(configfile, QSettings::IniFormat).value(INI_BASE_USERID).toString();
    QString pwd = QSettings(configfile, QSettings::IniFormat).value(INI_BASE_PWD).toString();
    QString stationid = QSettings(configfile, QSettings::IniFormat).value(INI_BASE_STATIONIID).toString();

    if(storeid.isEmpty() || partnerid.isEmpty() || userid.isEmpty() || pwd.isEmpty() || stationid.isEmpty())
        return ;

    ui->lineEdit_partnerid_2->setText(partnerid);
    ui->lineEdit_storeid_2->setText(storeid);
    //ui->lineEdit_posno_2->setText(stationid);
    ui->lineEdit_account_2->setText(userid);
    ui->lineEdit_pwd_2->setText(pwd);
}

void NewLoginForm::showfull()
{
    if(!is_login)
    {
        SetStoreInfo();
        this->showFullScreen();
        return ;
    }

    this->hide();
    //emit showmainform();
    DEFAULTPOSTEVENT(PosEvent::s_show_mainform, "");
}

bool NewLoginForm::event(QEvent *e)
{

    if(e->type() == PosEvent::s_opt_status)
    {
        QVariantMap value;

        GETEVENTINFO(value,e,QVariantMap);

        if(value.contains(EVENT_KEY_ORDERSTATUS) && value[EVENT_KEY_ORDERSTATUS].toInt() == 0)
        {
            if(!this->isHidden())
                onShowAlert(AlertForm::LOADING, value[EVENT_KEY_MSG].toString());
            return true;
        }

        if(value.contains(EVENT_KEY_STATUS) && value[EVENT_KEY_STATUS].toBool() == false)
        {
            QLOG_INFO() << value[EVENT_KEY_MSG].toString();
            if(!this->isHidden())
                onShowAlert(AlertForm::ERROR, value[EVENT_KEY_MSG].toString());
        }
        else if(value.contains(EVENT_KEY_STATUS) && value[EVENT_KEY_STATUS].toBool() == true)
        {
            if(!this->isHidden())
                onShowAlert(AlertForm::SUCCESS, value[EVENT_KEY_MSG].toString());
        }
        return true;
    }

    if(e->type() == PosEvent::s_show_login)
    {
        this->showfull();
        return true;
    }

    if(e->type() == PosEvent::s_login_status)
    {
        QVariantMap value;

        GETEVENTINFO(value,e,QVariantMap);

        if(value.contains(EVENT_KEY_STATUS) && value[EVENT_KEY_STATUS].toBool() == false)
        {
            QLOG_INFO() << value[EVENT_KEY_MSG].toString();
            if(!this->isHidden())
                onShowAlert(AlertForm::ERROR, value[EVENT_KEY_MSG].toString());
        }
        else if(value.contains(EVENT_KEY_STATUS) && value[EVENT_KEY_STATUS].toBool() == true)
        {
            this->hide();
            onHideAlert();
            is_login -= true;
            //emit showmainform();
            DEFAULTPOSTEVENT(PosEvent::s_show_mainform, "");
        }
        return true;
    }

    return QDialog::event(e);
}

void NewLoginForm::GetCurrLineEdit()
{
    _curr_lineedit = qobject_cast<ClickedLineEdit*>(sender());
}

void NewLoginForm::on_pushButton_login_2_clicked()
{
    QVariantMap map;

    if(ui->lineEdit_account_2->text().isEmpty() ||
       ui->lineEdit_pwd_2->text().isEmpty() ||
       ui->lineEdit_partnerid_2->text().isEmpty() ||
       /*ui->lineEdit_posno_2->text().isEmpty() ||*/
       ui->lineEdit_storeid_2->text().isEmpty() )
    {
        onShowAlert(AlertForm::ERROR, QString::fromLocal8Bit("商户号/门店号/POS编号/账号/密码均不能为空"));
        return ;
    }

    map.insert(JSON_STOREID, ui->lineEdit_storeid_2->text());
//    map.insert(JSON_STATIONID, ui->lineEdit_posno_2->text());
    map.insert(JSON_KEY_USERID, ui->lineEdit_account_2->text());
    map.insert(JSON_KEY_PWD, ui->lineEdit_pwd_2->text());
    map.insert(JSON_KEY_PARTNERID, ui->lineEdit_partnerid_2->text());

    POSTEVENTTYPE(PosEvent::s_login_storeinfo, map, QVariantMap);
}

void NewLoginForm::number_btn_click()
{
    QPushButton *btn = qobject_cast<QPushButton*>(sender());

    if(btn == ui->pushButton_clear_2)
    {
        _curr_lineedit->clear();
    }
    else if(btn == ui->pushButton_del_2)
    {
        QString text = _curr_lineedit->text();

        _curr_lineedit->setText(text.left(text.size() - 1));

    }
    else
    {
        _curr_lineedit->setText(_curr_lineedit->text() + btn->property("number").toString());
    }
}


void NewLoginForm::on_pushButton_close_clicked()
{
    this->hide();
    //emit showfloat();
    DEFAULTPOSTEVENT(PosEvent::s_show_float, "");
}
