#include "equipmentpage.h"
#include "ui_equipmentpage.h"
#include "../global.h"

EquipmentPage::EquipmentPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::EquipmentPage)
{
    ui->setupUi(this);
    m_pPageWidget = new PageWidget;
    connect(m_pPageWidget, &PageWidget::currentPageChanged, this, &EquipmentPage::PageChanged);
    connect(m_pPageWidget->getComboBox(), QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EquipmentPage::onComboBoxIndexChanged);
    m_httpClient = new HttpClient(this);
    pageSize = 10;
    currentPage = 1;
    ui->verticalLayout_4->addWidget(m_pPageWidget);
    loadAndRefreshData();
}


void EquipmentPage::loadAndRefreshData() {
    loadDataFromSource(currentPage,pageSize);
}

void EquipmentPage::loadDataFromSource(int currentPage,int pageSize)
{
    QUrl localUrl("equipment/pc");

    QUrlQuery query;
    // 添加新的参数到 QUrlQuery
    query.addQueryItem("pageIndex", QString::number(currentPage));
    query.addQueryItem("pageSize", QString::number(pageSize));

    // 再次将查询参数设置到 QUrl 对象中
    localUrl.setQuery(query);

    qDebug() << localUrl.toString();
    QUrl fullUrl = g_url.resolved(localUrl);
    m_httpClient->sendHttpGetRequest(fullUrl);
    // // 连接信号到槽，以便处理请求结果或错误
    connect(m_httpClient, &HttpClient::httpGetFinished, [this](const QJsonDocument &data) {
        this->dataTable(data);

    });
}

void EquipmentPage::PageChanged(int page)
{
    currentPage = page;
    loadDataFromSource(currentPage,pageSize);
}


void EquipmentPage::onComboBoxIndexChanged(int index) {
    // 获取当前选中的文本
     QVariant variant = m_pPageWidget->getComboBox()->itemData(index);
     int value = variant.toInt();
     pageSize = value;
    loadDataFromSource(currentPage,pageSize);

}


void EquipmentPage::dataTable(const QJsonDocument &jsonDoc)
{
    parseJsonString(jsonDoc);
    model = new QStandardItemModel(this);
    model->setHorizontalHeaderLabels({"名称", "设备编号", "设备类型","设备状态","在线状态","创建时间"});

    // 遍历 JSON 数组，将其转换为 QStandardItemModel
    for (const QJsonValue& jsonValue : dataArray) {
        QJsonObject jsonObj = jsonValue.toObject();
        QList<QStandardItem*> items = jsonToItem(jsonObj);
        model->appendRow(items);
    }

    // // 设置 QTableView 的 model
    ui->tableView->setModel(model);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableView->setAlternatingRowColors(true);  // 启用交替行颜色

}

QList<QStandardItem *> EquipmentPage::jsonToItem(const QJsonObject &jsonObj)
{

    // 创建一个 QStandardItem
      QStandardItem* nameItem = new QStandardItem(jsonObj["name"].toString());
      nameItem->setTextAlignment(Qt::AlignCenter);
      QStandardItem* equipItem = new QStandardItem(jsonObj["equipmentSn"].toString());
      QStandardItem* typeItem = new QStandardItem(jsonObj["equipmentTypeName"].toString());
      QStandardItem* statusItem = new QStandardItem(jsonObj["statusName"].toString());
      QStandardItem* onlineItem = new QStandardItem(jsonObj["onlineName"].toString());
      // 处理 createdAt 字段
      QString createdAtStr = jsonObj["createdAt"].toString();
      QDateTime createdAt = QDateTime::fromString(createdAtStr, Qt::ISODateWithMs);
      QString formattedCreatedAt = createdAt.toString("yyyy-MM-dd HH:mm:ss");
      QStandardItem* createdItem = new QStandardItem(formattedCreatedAt);

      return QList<QStandardItem*>() << nameItem << equipItem << typeItem<<statusItem<<onlineItem<<createdItem;

}


void EquipmentPage::parseJsonString(const QJsonDocument &jsonDoc)
{
    QJsonObject jsonObj = jsonDoc.object();
    QJsonObject dataObj = jsonObj["data"].toObject();
    dataArray = dataObj["list"].toArray();
    totalCount = dataObj["count"].toInt();
    m_pPageWidget->setMaxPage(ceil(static_cast<double>(totalCount)/pageSize));
    // qDebug()<<"Row data"<<dataObj["count"].toInt();
}


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