#include "MainWnd.h"
#include "ui_MainWnd.h"
#include "Login.h"
#include <QMouseEvent>
#include <qDebug>
#include "OriginalWnd/OriginalWnd.h"
#include <QDialogButtonBox>
MainWnd::MainWnd(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWnd)
    ,initialIconSet(false)
{
    ui->setupUi(this);
    if(gen_if.getLanguageValue() == 0){

    }else{
        setChineseMode(this);
    }


    // // 初始化 OriginalWnd
    m_originalWnd = new OriginalWnd(this);

    // // 将 OriginalWnd 添加到 stackedWidget_UI
    ui->stackedWidget_UI->addWidget(m_originalWnd);

    bool buttonConnected = connect(ui->tabHomeBtn, &QPushButton::clicked,
                                   this, &MainWnd::on_tabHomeBtn_clicked);
    qDebug() << "Connected tabHomeBtn clicked to on_tabHomeBtn_clicked:" << buttonConnected;

    connect(ui->tabProductionBtn, &QPushButton::clicked,
            this, &MainWnd::on_tabProductionBtn_clicked);

    connect(ui->tabDiagnosisBtn, &QPushButton::clicked,
            this, &MainWnd::on_tabDiagnosisBtn_clicked);

    connect(ui->tabDispositionBtn, &QPushButton::clicked,
            this, &MainWnd::on_tabDispositionBtn_clicked);

    connect(ui->tabModuleBtn, &QPushButton::clicked,
            this, &MainWnd::on_tabModuleBtn_clicked);

    // connect(ui->tabSystemBtn, &QPushButton::clicked,
    //         this, &MainWnd::on_tabSystemBtn_clicked);

    connect(ui->tabProgramBtn, &QPushButton::clicked,
            this, &MainWnd::on_tabProgramBtn_clicked);
    connect(this,&MainWnd::setLanguageSignal,
            this->m_originalWnd,&OriginalWnd::onHandleLanguageSignal);
    // 设置 OriginalWnd 为当前页面
    ui->stackedWidget_UI->setCurrentWidget(m_originalWnd);
    qDebug() << "OriginalWnd 已设置为 stackedWidget 的当前页面";

    initForm();
}

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





void MainWnd::on_tabProductionBtn_clicked()
{

    m_originalWnd->switchToProductionConfig();
    ui->stackedWidget_UI->setCurrentWidget(m_originalWnd);
}

void MainWnd::on_tabHomeBtn_clicked()
{

    m_originalWnd->switchToHomeConfig();
    ui->stackedWidget_UI->setCurrentWidget(m_originalWnd);
}

void MainWnd::on_tabProgramBtn_clicked()
{
    m_originalWnd->switchToProgramConfig();
    ui->stackedWidget_UI->setCurrentWidget(m_originalWnd);

}

void MainWnd::on_tabModuleBtn_clicked()
{
    m_originalWnd->switchToModuleConfig();
    ui->stackedWidget_UI->setCurrentWidget(m_originalWnd);

}

void MainWnd::on_tabDispositionBtn_clicked()
{
    m_originalWnd->switchToDispositionConfig();
    ui->stackedWidget_UI->setCurrentWidget(m_originalWnd);

}

void MainWnd::on_tabDiagnosisBtn_clicked()
{
    m_originalWnd->switchToDiagnosisConfig();
    ui->stackedWidget_UI->setCurrentWidget(m_originalWnd);

}

void MainWnd::on_tabSystemBtn_clicked()
{
    QDialog settingsDialog(this);
    QVBoxLayout *mainLayout = new QVBoxLayout(&settingsDialog);
    settingsDialog.setWindowTitle("Settings");
    // Language Group
    QLabel *languageLabel = new QLabel("Language:", &settingsDialog);
    QRadioButton *englishRadio = new QRadioButton("English", &settingsDialog);
    QRadioButton *chineseRadio = new QRadioButton("Chinese", &settingsDialog);

    QButtonGroup *languageGroup = new QButtonGroup(&settingsDialog);
    languageGroup->addButton(englishRadio);
    languageGroup->addButton(chineseRadio);

    QVBoxLayout *languageLayout = new QVBoxLayout;
    languageLayout->addWidget(languageLabel);
    languageLayout->addWidget(englishRadio);
    languageLayout->addWidget(chineseRadio);

    // Theme Group
    QLabel *themeLabel = new QLabel("Theme:", &settingsDialog);
    QRadioButton *lightThemeRadio = new QRadioButton("Light", &settingsDialog);
    QRadioButton *darkThemeRadio = new QRadioButton("Dark", &settingsDialog);

    QButtonGroup *themeGroup = new QButtonGroup(&settingsDialog);
    themeGroup->addButton(lightThemeRadio);
    themeGroup->addButton(darkThemeRadio);

    QVBoxLayout *themeLayout = new QVBoxLayout;
    themeLayout->addWidget(themeLabel);
    themeLayout->addWidget(lightThemeRadio);
    themeLayout->addWidget(darkThemeRadio);

    // Dialog Button Box
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, &settingsDialog);
    connect(buttonBox, &QDialogButtonBox::accepted, &settingsDialog, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, &settingsDialog, &QDialog::reject);

    mainLayout->addLayout(languageLayout);
    mainLayout->addLayout(themeLayout);
    mainLayout->addWidget(buttonBox);
    int init_language_data,init_theme_data;

    init_language_data = gen_if.getLanguageValue();
    init_theme_data = gen_if.getThemeValue();
    if(init_language_data == 0){
        englishRadio->setChecked(true);
    }else{
        chineseRadio->setChecked(true);
        languageLabel->setText("语言");
        englishRadio->setText("英语");
        chineseRadio->setText("中文");
        themeLabel->setText("主题");
        lightThemeRadio->setText("浅色");
        darkThemeRadio->setText("深色");
        settingsDialog.setWindowTitle("设置");



    }
    if(init_theme_data == 0){
       lightThemeRadio->setChecked(true);
    }else{
       darkThemeRadio->setChecked(true);
    }
    // Connect signals and slots
    int current_language_data,current_theme_data;
    current_language_data = gen_if.getLanguageValue();
    current_theme_data = gen_if.getThemeValue();
    connect(languageGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked),
            [&](QAbstractButton *button) {
                if (button == englishRadio) {
                    current_language_data = 0;
                    qDebug() << "Language set to English";
                } else if (button == chineseRadio) {
                    current_language_data = 1;
                    qDebug() << "Language set to Chinese";
                }
            });

    connect(themeGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked),
            [&](QAbstractButton *button) {
                if (button == lightThemeRadio) {
                    current_theme_data = 0;
                    qDebug() << "Theme set to Light";
                } else if (button == darkThemeRadio) {
                    current_theme_data = 1;
                    qDebug() << "Theme set to Dark";
                }
            });
    int result = settingsDialog.exec();
    if (result == QDialog::Accepted) {
        // 处理“OK”按钮点击事件
        if(init_language_data == current_language_data){
            //do nothing
        }else{
            gen_if.setLanguageValue(current_language_data);
            if(current_language_data == 0){
                ui->retranslateUi(this);
                setLanguageSignal(current_language_data);
            }else{
                setChineseMode(this);
                setLanguageSignal(current_language_data);
            }

        }
        if(init_theme_data == current_theme_data){
            //do nothing
        }else{
            gen_if.setThemeValue(current_theme_data);
        }
        qDebug() << "Dialog accepted";
    } else if (result == QDialog::Rejected) {
        // 处理“Cancel”按钮点击事件
        // qDebug() << "Dialog rejected";
    }
}









void MainWnd::initForm()
{
    // 隐藏原生标题栏
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);

    // 窗口背景透明
    setAttribute(Qt::WA_TranslucentBackground, true);


    QPixmap pixmap(":/images/light/logo1.png");
    QPixmap scaledPixmap = pixmap.scaled(90, 49, Qt::KeepAspectRatio);
    ui->label_logo->setPixmap(scaledPixmap);

    Login *login = new Login;
    ui->stackedWidget_UI->addWidget(login);
    ui->stackedWidget_UI->setCurrentIndex(ui->stackedWidget_UI->indexOf(login));

    //登录监听
    ui->label_logo->installEventFilter(this);

    ui->stackedWidget_UI->installEventFilter(this);

    //安装监听事件,点击左侧按钮图标变化
    ui->tabHomeBtn->installEventFilter(this);
    ui->tabProductionBtn->installEventFilter(this);
    ui->tabProgramBtn->installEventFilter(this);
    ui->tabMessageBtn->installEventFilter(this);
    ui->tabModuleBtn->installEventFilter(this);
    ui->tabDispositionBtn->installEventFilter(this);
    ui->tabDiagnosisBtn->installEventFilter(this);
    ui->tabSystemBtn->installEventFilter(this);
    ui->tabHelpBtn->installEventFilter(this);

    QMouseEvent* pressEvent = new QMouseEvent(QEvent::MouseButtonPress,
                                              ui->tabHomeBtn->pos(),
                                              Qt::LeftButton,
                                              Qt::LeftButton,
                                              Qt::NoModifier);
    QApplication::sendEvent(ui->tabHomeBtn, pressEvent);

    delete pressEvent;
}

bool MainWnd::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == this->ui->stackedWidget_UI)
    {
        onCurrentWidgetChanged();
    }

    if (obj == this->ui->label_logo)
    {
        //判断事件类型是否为鼠标事件
        if (event->type() == QEvent::MouseButtonPress)
        {
            //转换为鼠标事件
            QMouseEvent* mouseenevt = static_cast<QMouseEvent*>(event);
            //判断鼠标左键点击
            if (mouseenevt->button() == Qt::LeftButton)
            {
                Login *login = new Login;
                ui->stackedWidget_UI->addWidget(login);
                ui->stackedWidget_UI->setCurrentIndex(ui->stackedWidget_UI->indexOf(login));
                return true;
            }
        }
    }

    switch(event->type())
        {
        case QEvent::MouseButtonPress:
        {
            if(obj == ui->tabHomeBtn && ui->tabHomeBtn->isEnabled())
            {
                ui->tabHomeBtn->setIcon(QIcon(":/images/deep/home.png"));
                ui->tabHomeBtn->setChecked(true);
                if(ui->tabProductionBtn->isChecked() == false)
                {
                    ui->tabProductionBtn->setIcon(QIcon(":/images/light/Productionn assistance.png"));
                }
                if(ui->tabProgramBtn->isChecked() == false)
                {
                    ui->tabProgramBtn->setIcon(QIcon(":/images/light/Program.png"));
                }
                if(ui->tabMessageBtn->isChecked() == false)
                {
                    ui->tabMessageBtn->setIcon(QIcon(":/images/light/Message.png"));
                }
                if(ui->tabModuleBtn->isChecked() == false)
                {
                    ui->tabModuleBtn->setIcon(QIcon(":/images/light/Module.png"));
                }
                if(ui->tabDispositionBtn->isChecked() == false)
                {
                    ui->tabDispositionBtn->setIcon(QIcon(":/images/light/Disposition.png"));
                }
                if(ui->tabDiagnosisBtn->isChecked() == false)
                {
                    ui->tabDiagnosisBtn->setIcon(QIcon(":/images/light/Diagnosis.png"));
                }
                if(ui->tabSystemBtn->isChecked() == false)
                {
                    ui->tabSystemBtn->setIcon(QIcon(":/images/light/System.png"));
                }
                if(ui->tabHelpBtn->isChecked() == false)
                {
                    ui->tabHelpBtn->setIcon(QIcon(":/images/light/Help.png"));
                }
            }
            if(obj == ui->tabProductionBtn && ui->tabProductionBtn->isEnabled())
            {
                ui->tabProductionBtn->setIcon(QIcon(":/images/deep/Productionn assistance.png"));
                ui->tabProductionBtn->setChecked(true);
                if(ui->tabHomeBtn->isChecked() == false)
                {
                    ui->tabHomeBtn->setIcon(QIcon(":/images/light/home.png"));
                }
                if(ui->tabProgramBtn->isChecked() == false)
                {
                    ui->tabProgramBtn->setIcon(QIcon(":/images/light/Program.png"));
                }
                if(ui->tabMessageBtn->isChecked() == false)
                {
                    ui->tabMessageBtn->setIcon(QIcon(":/images/light/Message.png"));
                }
                if(ui->tabModuleBtn->isChecked() == false)
                {
                    ui->tabModuleBtn->setIcon(QIcon(":/images/light/Module.png"));
                }
                if(ui->tabDispositionBtn->isChecked() == false)
                {
                    ui->tabDispositionBtn->setIcon(QIcon(":/images/light/Disposition.png"));
                }
                if(ui->tabDiagnosisBtn->isChecked() == false)
                {
                    ui->tabDiagnosisBtn->setIcon(QIcon(":/images/light/Diagnosis.png"));
                }
                if(ui->tabSystemBtn->isChecked() == false)
                {
                    ui->tabSystemBtn->setIcon(QIcon(":/images/light/System.png"));
                }
                if(ui->tabHelpBtn->isChecked() == false)
                {
                    ui->tabHelpBtn->setIcon(QIcon(":/images/light/Help.png"));
                }
            }
            if(obj == ui->tabProgramBtn && ui->tabProgramBtn->isEnabled())
            {
                ui->tabProgramBtn->setIcon(QIcon(":/images/deep/Program.png"));
                ui->tabProgramBtn->setChecked(true);
                if(ui->tabHomeBtn->isChecked() == false)
                {
                    ui->tabHomeBtn->setIcon(QIcon(":/images/light/home.png"));
                }
                if(ui->tabProductionBtn->isChecked() == false)
                {
                    ui->tabProductionBtn->setIcon(QIcon(":/images/light/Productionn assistance.png"));
                }
                if(ui->tabMessageBtn->isChecked() == false)
                {
                    ui->tabMessageBtn->setIcon(QIcon(":/images/light/Message.png"));
                }
                if(ui->tabModuleBtn->isChecked() == false)
                {
                    ui->tabModuleBtn->setIcon(QIcon(":/images/light/Module.png"));
                }
                if(ui->tabDispositionBtn->isChecked() == false)
                {
                    ui->tabDispositionBtn->setIcon(QIcon(":/images/light/Disposition.png"));
                }
                if(ui->tabDiagnosisBtn->isChecked() == false)
                {
                    ui->tabDiagnosisBtn->setIcon(QIcon(":/images/light/Diagnosis.png"));
                }
                if(ui->tabSystemBtn->isChecked() == false)
                {
                    ui->tabSystemBtn->setIcon(QIcon(":/images/light/System.png"));
                }
                if(ui->tabHelpBtn->isChecked() == false)
                {
                    ui->tabHelpBtn->setIcon(QIcon(":/images/light/Help.png"));
                }
            }
            if(obj == ui->tabMessageBtn && ui->tabMessageBtn->isEnabled())
            {
                ui->tabMessageBtn->setIcon(QIcon(":/images/deep/Message.png"));
                ui->tabMessageBtn->setChecked(true);
                if(ui->tabHomeBtn->isChecked() == false)
                {
                    ui->tabHomeBtn->setIcon(QIcon(":/images/light/home.png"));
                }
                if(ui->tabProductionBtn->isChecked() == false)
                {
                    ui->tabProductionBtn->setIcon(QIcon(":/images/light/Productionn assistance.png"));
                }
                if(ui->tabProgramBtn->isChecked() == false)
                {
                    ui->tabProgramBtn->setIcon(QIcon(":/images/light/Program.png"));
                }
                if(ui->tabModuleBtn->isChecked() == false)
                {
                    ui->tabModuleBtn->setIcon(QIcon(":/images/light/Module.png"));
                }
                if(ui->tabDispositionBtn->isChecked() == false)
                {
                    ui->tabDispositionBtn->setIcon(QIcon(":/images/light/Disposition.png"));
                }
                if(ui->tabDiagnosisBtn->isChecked() == false)
                {
                    ui->tabDiagnosisBtn->setIcon(QIcon(":/images/light/Diagnosis.png"));
                }
                if(ui->tabSystemBtn->isChecked() == false)
                {
                    ui->tabSystemBtn->setIcon(QIcon(":/images/light/System.png"));
                }
                if(ui->tabHelpBtn->isChecked() == false)
                {
                    ui->tabHelpBtn->setIcon(QIcon(":/images/light/Help.png"));
                }
            }
            if(obj == ui->tabModuleBtn && ui->tabModuleBtn->isEnabled())
            {
                ui->tabModuleBtn->setIcon(QIcon(":/images/deep/Module.png"));
                ui->tabModuleBtn->setChecked(true);
                if(ui->tabHomeBtn->isChecked() == false)
                {
                    ui->tabHomeBtn->setIcon(QIcon(":/images/light/home.png"));
                }
                if(ui->tabProductionBtn->isChecked() == false)
                {
                    ui->tabProductionBtn->setIcon(QIcon(":/images/light/Productionn assistance.png"));
                }
                if(ui->tabProgramBtn->isChecked() == false)
                {
                    ui->tabProgramBtn->setIcon(QIcon(":/images/light/Program.png"));
                }
                if(ui->tabMessageBtn->isChecked() == false)
                {
                    ui->tabMessageBtn->setIcon(QIcon(":/images/light/Message.png"));
                }
                if(ui->tabDispositionBtn->isChecked() == false)
                {
                    ui->tabDispositionBtn->setIcon(QIcon(":/images/light/Disposition.png"));
                }
                if(ui->tabDiagnosisBtn->isChecked() == false)
                {
                    ui->tabDiagnosisBtn->setIcon(QIcon(":/images/light/Diagnosis.png"));
                }
                if(ui->tabSystemBtn->isChecked() == false)
                {
                    ui->tabSystemBtn->setIcon(QIcon(":/images/light/System.png"));
                }
                if(ui->tabHelpBtn->isChecked() == false)
                {
                    ui->tabHelpBtn->setIcon(QIcon(":/images/light/Help.png"));
                }
            }
            if(obj == ui->tabDispositionBtn && ui->tabDispositionBtn->isEnabled())
            {
                ui->tabDispositionBtn->setIcon(QIcon(":/images/deep/Disposition.png"));
                ui->tabDispositionBtn->setChecked(true);
                if(ui->tabHomeBtn->isChecked() == false)
                {
                    ui->tabHomeBtn->setIcon(QIcon(":/images/light/home.png"));
                }
                if(ui->tabProductionBtn->isChecked() == false)
                {
                    ui->tabProductionBtn->setIcon(QIcon(":/images/light/Productionn assistance.png"));
                }
                if(ui->tabProgramBtn->isChecked() == false)
                {
                    ui->tabProgramBtn->setIcon(QIcon(":/images/light/Program.png"));
                }
                if(ui->tabMessageBtn->isChecked() == false)
                {
                    ui->tabMessageBtn->setIcon(QIcon(":/images/light/Message.png"));
                }
                if(ui->tabModuleBtn->isChecked() == false)
                {
                    ui->tabModuleBtn->setIcon(QIcon(":/images/light/Module.png"));
                }
                if(ui->tabDiagnosisBtn->isChecked() == false)
                {
                    ui->tabDiagnosisBtn->setIcon(QIcon(":/images/light/Diagnosis.png"));
                }
                if(ui->tabSystemBtn->isChecked() == false)
                {
                    ui->tabSystemBtn->setIcon(QIcon(":/images/light/System.png"));
                }
                if(ui->tabHelpBtn->isChecked() == false)
                {
                    ui->tabHelpBtn->setIcon(QIcon(":/images/light/Help.png"));
                }
            }
            if(obj == ui->tabDiagnosisBtn && ui->tabDiagnosisBtn->isEnabled())
            {
                ui->tabDiagnosisBtn->setIcon(QIcon(":/images/deep/Diagnosis.png"));
                ui->tabDiagnosisBtn->setChecked(true);
                if(ui->tabHomeBtn->isChecked() == false)
                {
                    ui->tabHomeBtn->setIcon(QIcon(":/images/light/home.png"));
                }
                if(ui->tabProductionBtn->isChecked() == false)
                {
                    ui->tabProductionBtn->setIcon(QIcon(":/images/light/Productionn assistance.png"));
                }
                if(ui->tabProgramBtn->isChecked() == false)
                {
                    ui->tabProgramBtn->setIcon(QIcon(":/images/light/Program.png"));
                }
                if(ui->tabMessageBtn->isChecked() == false)
                {
                    ui->tabMessageBtn->setIcon(QIcon(":/images/light/Message.png"));
                }
                if(ui->tabModuleBtn->isChecked() == false)
                {
                    ui->tabModuleBtn->setIcon(QIcon(":/images/light/Module.png"));
                }
                if(ui->tabDispositionBtn->isChecked() == false)
                {
                    ui->tabDispositionBtn->setIcon(QIcon(":/images/light/Disposition.png"));
                }
                if(ui->tabSystemBtn->isChecked() == false)
                {
                    ui->tabSystemBtn->setIcon(QIcon(":/images/light/System.png"));
                }
                if(ui->tabHelpBtn->isChecked() == false)
                {
                    ui->tabHelpBtn->setIcon(QIcon(":/images/light/Help.png"));
                }
            }
            if(obj == ui->tabSystemBtn && ui->tabSystemBtn->isEnabled())
            {
                ui->tabSystemBtn->setIcon(QIcon(":/images/deep/System.png"));
                ui->tabSystemBtn->setChecked(true);
                if(ui->tabHomeBtn->isChecked() == false)
                {
                    ui->tabHomeBtn->setIcon(QIcon(":/images/light/home.png"));
                }
                if(ui->tabProductionBtn->isChecked() == false)
                {
                    ui->tabProductionBtn->setIcon(QIcon(":/images/light/Productionn assistance.png"));
                }
                if(ui->tabProgramBtn->isChecked() == false)
                {
                    ui->tabProgramBtn->setIcon(QIcon(":/images/light/Program.png"));
                }
                if(ui->tabMessageBtn->isChecked() == false)
                {
                    ui->tabMessageBtn->setIcon(QIcon(":/images/light/Message.png"));
                }
                if(ui->tabModuleBtn->isChecked() == false)
                {
                    ui->tabModuleBtn->setIcon(QIcon(":/images/light/Module.png"));
                }
                if(ui->tabDispositionBtn->isChecked() == false)
                {
                    ui->tabDispositionBtn->setIcon(QIcon(":/images/light/Disposition.png"));
                }
                if(ui->tabDiagnosisBtn->isChecked() == false)
                {
                    ui->tabDiagnosisBtn->setIcon(QIcon(":/images/light/Diagnosis.png"));
                }
                if(ui->tabHelpBtn->isChecked() == false)
                {
                    ui->tabHelpBtn->setIcon(QIcon(":/images/light/Help.png"));
                }
            }
            if(obj == ui->tabHelpBtn && ui->tabHelpBtn->isEnabled())
            {
                ui->tabHelpBtn->setIcon(QIcon(":/images/deep/Help.png"));
                ui->tabHelpBtn->setChecked(true);
                if(ui->tabHomeBtn->isChecked() == false)
                {
                    ui->tabHomeBtn->setIcon(QIcon(":/images/light/home.png"));
                }
                if(ui->tabProductionBtn->isChecked() == false)
                {
                    ui->tabProductionBtn->setIcon(QIcon(":/images/light/Productionn assistance.png"));
                }
                if(ui->tabProgramBtn->isChecked() == false)
                {
                    ui->tabProgramBtn->setIcon(QIcon(":/images/light/Program.png"));
                }
                if(ui->tabMessageBtn->isChecked() == false)
                {
                    ui->tabMessageBtn->setIcon(QIcon(":/images/light/Message.png"));
                }
                if(ui->tabModuleBtn->isChecked() == false)
                {
                    ui->tabModuleBtn->setIcon(QIcon(":/images/light/Module.png"));
                }
                if(ui->tabDispositionBtn->isChecked() == false)
                {
                    ui->tabDispositionBtn->setIcon(QIcon(":/images/light/Disposition.png"));
                }
                if(ui->tabDiagnosisBtn->isChecked() == false)
                {
                    ui->tabDiagnosisBtn->setIcon(QIcon(":/images/light/Diagnosis.png"));
                }
                if(ui->tabSystemBtn->isChecked() == false)
                {
                    ui->tabSystemBtn->setIcon(QIcon(":/images/light/System.png"));
                }
            }
            break;
        }

        case QEvent::HoverEnter:
        {
            if(obj == ui->tabHomeBtn && ui->tabHomeBtn->isEnabled())
            {
                ui->tabHomeBtn->setIcon(QIcon(":/images/deep/home.png"));
            }
            if(obj == ui->tabProductionBtn && ui->tabProductionBtn->isEnabled())
            {
                ui->tabProductionBtn->setIcon(QIcon(":/images/deep/Productionn assistance.png"));
            }
            if(obj == ui->tabProgramBtn && ui->tabProgramBtn->isEnabled())
            {
                ui->tabProgramBtn->setIcon(QIcon(":/images/deep/Program.png"));
            }
            if(obj == ui->tabMessageBtn && ui->tabMessageBtn->isEnabled())
            {
                ui->tabMessageBtn->setIcon(QIcon(":/images/deep/Message.png"));
            }
            if(obj == ui->tabModuleBtn && ui->tabModuleBtn->isEnabled())
            {
                ui->tabModuleBtn->setIcon(QIcon(":/images/deep/Module.png"));
            }
            if(obj == ui->tabDispositionBtn && ui->tabDispositionBtn->isEnabled())
            {
                ui->tabDispositionBtn->setIcon(QIcon(":/images/deep/Disposition.png"));
            }
            if(obj == ui->tabDiagnosisBtn && ui->tabDiagnosisBtn->isEnabled())
            {
                ui->tabDiagnosisBtn->setIcon(QIcon(":/images/deep/Diagnosis.png"));
            }
            if(obj == ui->tabSystemBtn && ui->tabSystemBtn->isEnabled())
            {
                ui->tabSystemBtn->setIcon(QIcon(":/images/deep/System.png"));
            }
            if(obj == ui->tabHelpBtn && ui->tabHelpBtn->isEnabled())
            {
                ui->tabHelpBtn->setIcon(QIcon(":/images/deep/Help.png"));
            }
            break;
        }

        case QEvent::HoverLeave:
        {
            if(obj == ui->tabHomeBtn && ui->tabHomeBtn->isChecked() == false)
            {
                ui->tabHomeBtn->setIcon(QIcon(":/images/light/home.png"));
                ui->tabHomeBtn->setChecked(false);
            }
            if(obj == ui->tabProductionBtn && ui->tabProductionBtn->isChecked() == false)
            {
                ui->tabProductionBtn->setIcon(QIcon(":/images/light/Productionn assistance.png"));
                ui->tabProductionBtn->setChecked(false);
            }
            if(obj == ui->tabProgramBtn && ui->tabProgramBtn->isChecked() == false)
            {
                ui->tabProgramBtn->setIcon(QIcon(":/images/light/Program.png"));
                ui->tabProgramBtn->setChecked(false);
            }
            if(obj == ui->tabMessageBtn && ui->tabMessageBtn->isChecked() == false)
            {
                ui->tabMessageBtn->setIcon(QIcon(":/images/light/Message.png"));
                ui->tabMessageBtn->setChecked(false);
            }
            if(obj == ui->tabModuleBtn && ui->tabModuleBtn->isChecked() == false)
            {
                ui->tabModuleBtn->setIcon(QIcon(":/images/light/Module.png"));
                ui->tabModuleBtn->setChecked(false);
            }
            if(obj == ui->tabDispositionBtn && ui->tabDispositionBtn->isChecked() == false)
            {
                ui->tabDispositionBtn->setIcon(QIcon(":/images/light/Disposition.png"));
                ui->tabDispositionBtn->setChecked(false);
            }
            if(obj == ui->tabDiagnosisBtn && ui->tabDiagnosisBtn->isChecked() == false)
            {
                ui->tabDiagnosisBtn->setIcon(QIcon(":/images/light/Diagnosis.png"));
                ui->tabDiagnosisBtn->setChecked(false);
            }
            if(obj == ui->tabSystemBtn && ui->tabSystemBtn->isChecked() == false)
            {
                ui->tabSystemBtn->setIcon(QIcon(":/images/light/System.png"));
                ui->tabSystemBtn->setChecked(false);
            }
            if(obj == ui->tabHelpBtn && ui->tabHelpBtn->isChecked() == false)
            {
                ui->tabHelpBtn->setIcon(QIcon(":/images/light/Help.png"));
                ui->tabHelpBtn->setChecked(false);
            }
        }
        default:
            break;
    }

    return QWidget::eventFilter(obj, event);
}

void MainWnd::onCurrentWidgetChanged() {
    QWidget *currentWidget = ui->stackedWidget_UI->currentWidget();
    if (currentWidget) {
        QString currentWidgetName = currentWidget->objectName();

        if (currentWidgetName == "Login") {
            QString styleSheet = "QToolButton:disabled { color: #808080; background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #F8FBFF, stop: 1 #E7ECF3); }"; // 设置禁用状态下的文本颜色为灰色
            ui->tabHomeBtn->setDisabled(true);
            ui->tabHomeBtn->setIcon(QIcon(":/images/light/home.png"));
            ui->tabHomeBtn->setStyleSheet(styleSheet);
            ui->tabProductionBtn->setDisabled(true);
            ui->tabProductionBtn->setStyleSheet(styleSheet);
            ui->tabProgramBtn->setDisabled(true);
            ui->tabProgramBtn->setStyleSheet(styleSheet);
            ui->tabMessageBtn->setDisabled(true);
            ui->tabMessageBtn->setStyleSheet(styleSheet);
            ui->tabModuleBtn->setDisabled(true);
            ui->tabModuleBtn->setStyleSheet(styleSheet);
            ui->tabDispositionBtn->setDisabled(true);
            ui->tabDispositionBtn->setStyleSheet(styleSheet);
            ui->tabDiagnosisBtn->setDisabled(true);
            ui->tabDiagnosisBtn->setStyleSheet(styleSheet);
            ui->tabSystemBtn->setDisabled(true);
            ui->tabSystemBtn->setStyleSheet(styleSheet);
            ui->tabHelpBtn->setDisabled(true);
            ui->tabHelpBtn->setStyleSheet(styleSheet);
        } else {
            ui->tabHomeBtn->setDisabled(false);
            if (!initialIconSet) {
                ui->tabHomeBtn->setIcon(QIcon(":/images/deep/home.png"));
                initialIconSet = true;
            }
            ui->tabProductionBtn->setDisabled(false);
            ui->tabProgramBtn->setDisabled(false);
            ui->tabMessageBtn->setDisabled(false);
            ui->tabModuleBtn->setDisabled(false);
            ui->tabDispositionBtn->setDisabled(false);
            ui->tabDiagnosisBtn->setDisabled(false);
            ui->tabSystemBtn->setDisabled(false);
            ui->tabHelpBtn->setDisabled(false);
        }
    }
}

void MainWnd::setChineseMode(QMainWindow *MainWnd){
    MainWnd->setWindowTitle(QCoreApplication::translate("MainWnd", "主界面", nullptr));
    ui->tabHomeBtn->setText(QCoreApplication::translate("MainWnd", "首页", nullptr));
    ui->label_logo->setText(QString());
    ui->tabProductionBtn->setText(QCoreApplication::translate("MainWnd", "产品助手", nullptr));
    ui->tabProgramBtn->setText(QCoreApplication::translate("MainWnd", "进程", nullptr));
    ui->tabMessageBtn->setText(QCoreApplication::translate("MainWnd", "信息", nullptr));
    ui->tabModuleBtn->setText(QCoreApplication::translate("MainWnd", "校准", nullptr));
    ui->tabDispositionBtn->setText(QCoreApplication::translate("MainWnd", "模块配置", nullptr));
    ui->tabDiagnosisBtn->setText(QCoreApplication::translate("MainWnd", "诊断", nullptr));
    ui->tabSystemBtn->setText(QCoreApplication::translate("MainWnd", "系统信息", nullptr));
    ui->tabHelpBtn->setText(QCoreApplication::translate("MainWnd", "帮助", nullptr));
    ui->pushButton->setText(QCoreApplication::translate("MainWnd", "关闭", nullptr));
}

void MainWnd::on_pushButton_clicked()
{
    //加确认关闭提示确认窗口
    // 创建一个消息框
    QMessageBox msgBox;
    if(gen_if.getLanguageValue() == 0){
        msgBox.setWindowTitle("Confirm Close");
        msgBox.setText("Are you sure you want to close the application?");
    }else{
        msgBox.setWindowTitle("确认关闭");
        msgBox.setText("你确定要关闭程序吗");
    }

    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);
    // 显示消息框并获取用户的选择
    int ret = msgBox.exec();

    switch (ret) {
    case QMessageBox::Yes:
        // 用户点击了确认按钮，执行关闭操作
        qApp->quit();
        break;
    case QMessageBox::No:
        // 用户点击了取消按钮，不执行任何操作
        break;
    default:
        // 其他情况
        break;
    }
    // qApp->quit();
}

// QJsonObject MainWnd::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 MainWnd::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 MainWnd::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 MainWnd::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 MainWnd::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();
//     }
// }
