#include "userwindow.h"
#include "ui_userwindow.h"

UserWindow::UserWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::UserWindow)
{
    ui->setupUi(this);
    this->setAttribute(Qt::WA_DeleteOnClose,1);

    table = new ShowTable(this);

    connect(this,SIGNAL(sendata(QSqlDatabase,QString)),table,SLOT(dowork(QSqlDatabase,QString)));
}

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

void UserWindow::getdata(QSqlDatabase db1,QString tablename1) {
    db = db1;
    tablename = tablename1;
}

void UserWindow::on_usrtable_clicked()
{
    emit sendata(db,"user");
    table->show();
}


//根据username查找用户，并将用户放入一张新表单显示到tableview窗口
void UserWindow::on_find_usr_clicked()
{
    QSqlDatabase db2;
    QString datasql;
    QSqlQuery sql_query(db2);
    QString sql = "create table if not exists "
                  "temtable(id integer primary key autoincrement,"
                  "username text not NULL ,password text not NULL);";
    if (!sql_query.exec(sql)) {
        return;
    }

    bool ok;
    QString text = QInputDialog::getText(this, tr("查询"),
                                 tr("查找用户"), QLineEdit::Normal,
                                 tr("请输入用户名"), &ok);
    if (ok) {
        QString str = QString("SELECT * FROM user WHERE username = '%1'").arg(text);
        QSqlQuery query(QSqlDatabase::database("HB"));
        if (!query.exec(str)) {
            QMessageBox::about(this,"提示","没有找到用户");
            return;
        }
        while (query.next()) {
            datasql = QString("insert into temtable(username,password) "
                          "values('%1','%2');").arg(query.value(1).toString()).arg(query.value(2).toString());
            QSqlQuery sql_query(db2);
            if (!sql_query.exec(datasql)) {
                QMessageBox::about(this,"提示","向临时表插入数据失败");
                return;
            }
        }
        emit sendata(db2,"temtable");
        table->show();

        datasql = QString("DELETE FROM temtable");
        if (!query.exec(datasql)) {
            QMessageBox::about(this,"提示","删除临时表失败!");
            return;
        }
    }
    else
        return;
}

void UserWindow::on_delet_usr_clicked()
{
    bool ok;
    QString text = QInputDialog::getText(this, tr("删除用户"),
                                 tr("查找用户"), QLineEdit::Normal,
                                 tr("请输入用户名"), &ok);
    if (ok) {
        QString str = QString("DELETE FROM user WHERE username = '%1'").arg(text);
        QSqlQuery sql_query(db);
        if (!sql_query.exec(str)) {
            QMessageBox::about(this,"提示","删除用户失败");
            return;
        }
        QMessageBox::about(this,"提示","删除成功");
    }
    else
        return;

}

void UserWindow::on_goback_clicked()
{
    this->parentWidget()->show();
    this->hide();
}

void UserWindow::on_updata_usr_clicked()
{
    bool ok;
    QString text = QInputDialog::getText(this, tr("修改用户信息"),
                                 tr("查找用户"), QLineEdit::Normal,
                                 tr("请输入需要修改的用户"), &ok);
    if (ok) {
        int ret = QMessageBox::question(this, tr("修改选项"),
                                           "修改用户名/密码？(Yes-用户名|No-密码)",
                                           QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
        if (ret == QMessageBox::Yes ) {
            QString newname = QInputDialog::getText(this, tr("提示"),
                                         tr("修改用户名！"), QLineEdit::Normal,
                                         tr("请输入要修改的用户名"), &ok);

            QString str = QString("UPDATE user SET username = '%1' WHERE username = '%2'").arg(newname).arg(text);
            QSqlQuery sql_query(db);
            if (!sql_query.exec(str)) {
                QMessageBox::about(this,"提示","修改失败");
                return;
            }
            QMessageBox::about(this,"提示","修改成功");
        }

        if (ret == QMessageBox::No) {
            QString newpassword = QInputDialog::getText(this, tr("提示"),
                                         tr("修改密码"), QLineEdit::Normal,
                                         tr("请输入需要修改的密码"), &ok);

            QString str = QString("UPDATE user SET password = '%1' WHERE username = '%2'").arg(newpassword).arg(text);
            QSqlQuery sql_query(db);
            if (!sql_query.exec(str)) {
                QMessageBox::about(this,"提示","修改失败");
                return;
            }
           QMessageBox::about(this,"提示","修改成功");
        }
    }
    else
        return;
}
