#include "gen_interface.h"
QJsonObject Gen_Interface::getInfofromDb(){
    QJsonObject data;
    QJsonArray fieldsArray;
    QSqlDatabase db = QSqlDatabase::database();
    if (!db.isOpen()) {
        qWarning() << "数据库未打开";
        return data;
    }

    // 查询 systemConfig 表
    QSqlQuery queryBond(db);
    queryBond.prepare(R"(
        SELECT Function, Description,setValue
        FROM systemConfig
    )");
    if (queryBond.exec()) {
        while (queryBond.next()) {
            QJsonObject field;
            field["Function"] = queryBond.value("Function").toString();
            field["Description"] = queryBond.value("Description").toString();
            field["setValue"] = queryBond.value("setValue").toInt();
            fieldsArray.append(field);
        }


        // qDebug() << "Fetched fields:" << fieldsArray;
    } else {
        qWarning() << "查询 systemConfig 失败:" << queryBond.lastError().text();
    }
    data["systemConfig"]=fieldsArray;
    // qDebug() << "Fetched data:" << data;
    return data;

}
int Gen_Interface::getLanguageValue(){
    int data;
    QJsonArray fieldsArray;
    QSqlDatabase db = QSqlDatabase::database();
    if (!db.isOpen()) {
        qWarning() << "数据库未打开";
        return -1;
    }

    // 查询 systemConfig 表
    QSqlQuery queryBond(db);
    queryBond.prepare(R"(
        SELECT Function, Description,setValue
        FROM systemConfig
    )");
    if (queryBond.exec()) {
        while (queryBond.next()) {
            QJsonObject field;

            field["Function"] = queryBond.value("Function").toString();
            if(field["Function"] == "language"){
                data = queryBond.value("setValue").toInt();
                return data;
            }
        }


        // qDebug() << "Fetched fields:" << fieldsArray;
    } else {
        qWarning() << "查询 systemConfig 失败:" << queryBond.lastError().text();
    }
    return -1;

}
int Gen_Interface::getThemeValue(){
    int data;
    QJsonArray fieldsArray;
    QSqlDatabase db = QSqlDatabase::database();
    if (!db.isOpen()) {
        qWarning() << "数据库未打开";
        return -1;
    }

    // 查询 systemConfig 表
    QSqlQuery queryBond(db);
    queryBond.prepare(R"(
        SELECT Function, Description,setValue
        FROM systemConfig
    )");
    if (queryBond.exec()) {
        while (queryBond.next()) {
            QJsonObject field;

            field["Function"] = queryBond.value("Function").toString();
            if(field["Function"] == "theme"){
                data = queryBond.value("setValue").toInt();
                return data;
            }
        }


        // qDebug() << "Fetched fields:" << fieldsArray;
    } else {
        qWarning() << "查询 systemConfig 失败:" << queryBond.lastError().text();
    }
    return -1;
}
void Gen_Interface::setLanguageValue(int data){
    QSqlDatabase db = QSqlDatabase::database();
    if (!db.isOpen()) {
        qWarning() << "数据库未打开";
    }
    QString sql = QString("UPDATE systemConfig SET setValue = '%1' WHERE Function = 'language'").arg(data);
    QSqlQuery query;
    if (query.exec(sql)) {
        qDebug() << "Update successful";
    } else {
        qDebug() << "Update failed:" << query.lastError().text();
    }
}
void Gen_Interface::setThemeValue(int data){
    QSqlDatabase db = QSqlDatabase::database();
    if (!db.isOpen()) {
        qWarning() << "数据库未打开";
    }
    QString sql = QString("UPDATE systemConfig SET setValue = '%1' WHERE Function = 'theme'").arg(data);
    QSqlQuery query;
    if (query.exec(sql)) {
        qDebug() << "Update successful";
    } else {
        qDebug() << "Update failed:" << query.lastError().text();
    }
}
