/********************************************************************************
** Form generated from reading UI file 'FlangeAnalyticUILTsZHk.ui'
**
** Created by: Qt User Interface Compiler version 5.14.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef FLANGEANALYTICUILTSZHK_H
#define FLANGEANALYTICUILTSZHK_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QListWidget>
#include <QtWidgets/QListView>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include <QStandardItemModel>
#include <QDebug>
#include <QTimer>
#include <QAbstractItemView>
#include <QIcon>
#include <QModelIndex>
#include <QListWidgetItem>

#include "../HTTPClient/client.h"
#include "../MonitorUI/MonitorUI.h"
#include "FlangeItem.h"
#include "tools.h"
QT_BEGIN_NAMESPACE

class FlangeAnalyticWidget:public QWidget
{
public:
    QHBoxLayout *horizontalLayout;
    QListView *deviceList;
    QWidget *displayWidget;
    QGridLayout *gridLayout;
    QListWidget *flangeList;
    QSpacerItem *verticalSpacer;
    QSpacerItem *horizontalSpacer;
    QWidget *infoWidget;
    QVBoxLayout *verticalLayout;
    QHBoxLayout *horizontalLayout_2;
    QLabel *flangePiclabel;
    QLabel *infoLabel;
    QLabel *trendLabel;
    QStandardItemModel *deviceModel;
    QTimer* timer;

    QWidget *flangeWidget;
    QVBoxLayout *veticalLayout;

    MonitorWidget* monitor;
    int  curId;

    void setupUi(QWidget *FlangeAnalyticWidget)
    {
        if (FlangeAnalyticWidget->objectName().isEmpty())
            FlangeAnalyticWidget->setObjectName(QString::fromUtf8("FlangeAnalyticWidget"));
        FlangeAnalyticWidget->resize(1228, 773);
        horizontalLayout = new QHBoxLayout(FlangeAnalyticWidget);
        horizontalLayout->setSpacing(0);
        horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
        horizontalLayout->setContentsMargins(20, 20, 20, 20);

        deviceList = new QListView(FlangeAnalyticWidget);
        deviceList->setObjectName(QString::fromUtf8("deviceList"));
        deviceModel = new QStandardItemModel();
        deviceList->setModel(deviceModel);
        deviceList->setEditTriggers(QAbstractItemView::NoEditTriggers);

        horizontalLayout->addWidget(deviceList);

        flangeList = new QListWidget (FlangeAnalyticWidget);
        flangeList->setObjectName(QString::fromUtf8("flangeList"));
        flangeList->setEditTriggers(QAbstractItemView::NoEditTriggers);

        flangeWidget = new QWidget (FlangeAnalyticWidget);
        infoLabel = new QLabel (flangeWidget);
        infoLabel->setAlignment(Qt::AlignCenter);
        infoLabel->setObjectName("infoLabel");

        veticalLayout = new QVBoxLayout(flangeWidget);
        veticalLayout->addWidget(infoLabel);
        veticalLayout->addWidget(flangeList);

        horizontalLayout->addWidget(flangeWidget);

        horizontalLayout->setStretch(0, 1);
        horizontalLayout->setStretch(1, 5);

        QMetaObject::connectSlotsByName(FlangeAnalyticWidget);

        addDevice();
        addFlanges(deviceModel->data(deviceModel->index(0, 0)).toString());
        QModelIndex index = deviceList->model()->index(0, 0); // 获取第2行的索引
        deviceList->setCurrentIndex(index);
        timer = new QTimer();
        QObject::connect(timer, &QTimer::timeout, [&]() {
            // 计时器超时时更新标签
            addDevice();
            QModelIndex currentIndex = deviceList->currentIndex();
            QString fanCode = currentIndex.data().toString();
            addFlanges(fanCode);
        });
        timer->start(3000);

        connect(deviceList, &QListView::doubleClicked, [&](const QModelIndex& index) {
            if (index.isValid()) {
                QString fanCode = index.data().toString();
                curId = fans.allFans[fanCode].fanId;
                addFlanges(fanCode);
            }
        });

    } // setupUi



    void addDevice(){
//        httpClient.getFanList();
        map allFans = fans.states;
        if (!monitor->isInitState()){
            if (monitor->displayList.size() > deviceModel->rowCount()){
                for (int i = 0; i <= allFans.size() - deviceModel->rowCount() + 1; i++) {
                    QStandardItem* emptyItem = new QStandardItem("");
                    deviceModel->appendRow(emptyItem);
                }
            }else if (monitor->displayList.size() < deviceModel->rowCount()){
                for (int i = 0; i <= deviceModel->rowCount() - allFans.size() + 2; i++) {
                    deviceModel->removeRow(0);
                }
            }
        }else{
            if (allFans.size() > deviceModel->rowCount()){
                for (int i = 0; i <= allFans.size() - deviceModel->rowCount() + 1; i++) {
                    QStandardItem* emptyItem = new QStandardItem("");
                    deviceModel->appendRow(emptyItem);
                }
            }else if (allFans.size() < deviceModel->rowCount()){
                for (int i = 0; i <= deviceModel->rowCount() - allFans.size() + 2; i++) {
                    deviceModel->removeRow(0);
                }
            }
        }

        int count = 0;
        for (auto it = allFans.begin(); it != allFans.end(); ++it) {
            if (monitor->shouldDisplay(it->first)){
                QStandardItem* item = deviceModel->item(count);

                // 修改QStandardItem对象的文本和图标
                item->setText(it->first);
                item->setIcon(getDevIcon(it->second));
                count++;
            }
        }
    }

    void addFlanges(QString fanCode){
        int fanId = fans.allFans[fanCode].fanId;
        httpClient.getFlangeList(fanId);
        map flanges = fans.idMaps[fans.allFans[fanCode].fanId].flanges;
        flangeList->clear();
        int normalCount = 0;
        int errorCount = 0;
        for (auto flange = flanges.begin(); flange != flanges.end(); ++flange){
            FlangeItem *item = new FlangeItem(flange->second);
            QListWidgetItem* listItem = new QListWidgetItem();
            listItem->setSizeHint(QSize(200,120));
            flangeList->addItem(listItem);
            flangeList->setItemWidget(listItem, item);
            if (flange->second.flangeState == QString("正常")){
                normalCount++;
            }else if (flange->second.flangeState == QString("故障")){
                errorCount++;
            }
        }
        infoLabel->setText(QString("总览 (正常: %1/%2  报警: %3/%4)")
                               .arg(normalCount).arg(flanges.size()).arg(errorCount).arg(flanges.size()));
    }

    void addFeatures(QString flangeId){
        map flanges = fans.idMaps[curId].flanges;

    }

    FlangeAnalyticWidget(MonitorWidget* monitor) : monitor(monitor){
        setupUi(this);
    }


};


QT_END_NAMESPACE

#endif // FLANGEANALYTICUILTSZHK_H
