#ifndef BLASTOPEPAGE_H
#define BLASTOPEPAGE_H

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLayout>
#include <QMessageBox>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QProgressBar>
#include <QPushButton>
#include <QSet>
#include <QStandardItemModel>
#include <QWebEnginePage>
#include <QWebEngineView>
#include <QWidget>

#include "./serial/serialtool.h"
#include "PageWidget.h"
#include "countdownwidget.h"
#include "fireWidget/firingwidget.h"  // 包含 firingWidget 头文件
#include "hprojectdao.h"

// 自定义结构体
struct ProgressBarTriple {
    QProgressBar *bar1;
    QProgressBar *bar2;
    QProgressBar *bar3;

    // 构造函数
    ProgressBarTriple(QProgressBar *b1, QProgressBar *b2, QProgressBar *b3)
        : bar1(b1), bar2(b2), bar3(b3) {}
};
namespace Ui {
class BlastOpePage;
}

class BlastOpePage : public QWidget {
    Q_OBJECT

   public:
    explicit BlastOpePage(QWidget *parent = nullptr);
    ~BlastOpePage();
   private slots:
    void PageChanged(int page);
    void onComboBoxIndexChanged(int index);
    void updateProgressBar(int value, int row);
    void onUpdateBlastStatus(int status, int row);
    void changeButByMqtt(int status, int row);
    void showDownWidget(QString uuid, const QString &topic, const QString &message);
    void updateProject(QString uuid);
    void destroyFiringWidget(const QString &uuid);
    void on_btnSelect_clicked();
    void onItemCheckboxChanged(QStandardItem *item);
    void handleSelect(QString uuid);
    void showDownWidgetSelect();
    void triggerBlastSelected();
    void destroyFiringWidgetSelect(const QString &uuid);

   private:
    void handleFeaturePermission(QWebEnginePage *page, const QUrl &securityOrigin,
                                 QWebEnginePage::Feature feature);
    void RefreshData();
    void loadDataFromSource(int currentPage, int pageSize);
    void InitFace();
    void closeWebViewAndRestoreUI();
    void parseJsonString(const QJsonDocument &jsonDoc);
    void onUrlChanged(const QUrl &newUrl);
    void initPagination();
    QJsonObject sendPostRequest(const QUrl &url, const QJsonObject &data);
    QJsonObject getMetaInfo();
    void handleButtonClick(int row, QPushButton *button);
    bool checkUuidsSame();

   private:
    Ui::BlastOpePage *ui;
    QList<HeaderInfo> headers;
    QStandardItemModel *model;
    QMap<QString, firingWidget *> uuidWidgetMap;
    QWebEngineView *view;
    QVBoxLayout *layout;
    QString certifyId;
    PageWidget *pageWidget;
    QJsonArray dataArray;
    HProjectDao dao;
    QVector<ProgressBarTriple> progressBars;  // 用于保存每行的两个进度条指针
    QMap<int, QString> uuidMap;               // 用于存储行号和对应的 uuid
    QMap<QString, firingWidget *> uuidWidgetSMap;
    SerialTool *serialTool;
    QSet<QString> selectedUuids;
    QMetaObject::Connection connectionItem;
    int pageSize;     // 每页显示的记录数量
    int currentPage;  // 当前页面
    int totalCount;
    QString stopBlastButtonTxt = "中止起爆流程";
    QString startBlastButtonTxt = "开启起爆流程";
};

#endif  // BLASTOPEPAGE_H
