#include "OriginalWnd.h"
#include "ui_OriginalWnd.h"
#include "OriginalWnd/Demo001_1.h"
#include "OriginalWnd/Demo001_2.h"
#include "OriginalWnd/Demo001_3.h"
#include "OriginalWnd/treeviewmanager.h"
#include <QDebug>
#include <QJsonDocument>
#include <QFile>
#include <QFileDialog>
#include <QDateTime>
#include <QTimer>
#include <QSettings>

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

    // 初始化 TreeViewManager
    treeViewManager = new TreeViewManager(this, ui->widget_2);
    if (!treeViewManager) {
        qWarning() << "TreeViewManager 初始化失败";
        return;
    }

    qDebug() << "UI 初始化完成";

    initForm();
}

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

QWidget* OriginalWnd::getWidgetLeft() const
{
    return ui->widget;
}

void OriginalWnd::initForm()
{
    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &OriginalWnd::updateTime);
    timer->start(100);

    ui->toolButton->setIcon(QIcon(":/images/light/stop.png"));
    ui->toolButton_2->setIcon(QIcon(":/images/light/start.png"));
    ui->toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    ui->toolButton_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);


    ui->label_userlogo->setPixmap(QPixmap(":/images/light/user.png").scaled(20, 20, Qt::KeepAspectRatio));

    QSettings settings("OrganizationName__", "ApplicationName__");
    int lastSavedIndex = settings.value("lastIndex", 1).toInt();
    if (lastSavedIndex == 1) {
        Demo001_1 *demo001_1 = new Demo001_1;
        ui->switchShowPageUI->addWidget(demo001_1);
        ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_1));
    }
    if (lastSavedIndex == 2) {
        Demo001_2 *demo001_2 = new Demo001_2;
        ui->switchShowPageUI->addWidget(demo001_2);
        ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_2));
    }
    if (lastSavedIndex == 3) {
        Demo001_3 *demo001_3 = new Demo001_3;
        ui->switchShowPageUI->addWidget(demo001_3);
        ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_3));
    }
}

void OriginalWnd::updateTime() {
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString timeStr = currentDateTime.toString("yyyy-MM-dd HH:mm:ss");
    ui->label_time->setText(timeStr);

    QSettings settings("YourCompany_2", "YourAppName_2");
    QString userName = settings.value("userName", "???").toString();
    ui->label_username->setText(userName);

}


void OriginalWnd::on_ChartButton_clicked()
{
    Demo001_1 *demo001_1 = new Demo001_1;
    ui->switchShowPageUI->addWidget(demo001_1);
    ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_1));
    QSettings settings("OrganizationName__", "ApplicationName__");
    settings.setValue("lastIndex", 1);
}


void OriginalWnd::on_DoubleImageButton_clicked()
{
    Demo001_2 *demo001_2 = new Demo001_2;
    ui->switchShowPageUI->addWidget(demo001_2);
    ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_2));
    QSettings settings("OrganizationName__", "ApplicationName__");
    settings.setValue("lastIndex", 2);
}


void OriginalWnd::on_SingleImageButton_clicked()
{
    Demo001_3 *demo001_3 = new Demo001_3;
    ui->switchShowPageUI->addWidget(demo001_3);
    ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_3));
    QSettings settings("OrganizationName__", "ApplicationName__");
    settings.setValue("lastIndex", 3);
}



