#include "QWStatistics.h"

#include "app/Application.h"
#include "app/AppData.h"
#include "app/Station.h"
#include "database/DAO.h"

QWStatistics::QWStatistics(QWidget* parent) : MyWidget(parent)
{
    comboxStation = make_shared<QComboBox>(this);
    comboxStation->setGeometry(10, 10, 200, 28);
    comboxStation->setStyleSheet(QSS_BTN_COMBOX.c_str());

    QStringList listStation;
    listStation << "所有";
    for (auto item: Application::data().mapStation)
    {
        listStation << item.second->name.c_str();
    }
    comboxStation->addItems(listStation);
 

    QStringList headerTextList;
    headerTextList << "日期" << "日充电\n电量" << "日放电\n电量"
        << "日充电\n电量(尖)"  << "日充电\n电量(峰)" << "日充电\n电量(平)" << "日充电\n电量(谷)"
        << "日放电\n电量(尖)"  << "日放电\n电量(峰)" << "日放电\n电量(平)" << "日放电\n电量(谷)"
        << "日充电\n费用" << "日放电\n费用" << "日收益" << "--";
        //<< "总充电\n电量" << "总放电\n电量"
        //<< "总充电\n电量(尖)"  << "总充电\n电量(峰)" << "总充电\n电量(平)" << "总充电\n电量(谷)"
        //<< "总放电\n电量(尖)"  << "总放电\n电量(峰)" << "总放电\n电量(平)" << "总放电\n电量(谷)";

    table = MyQUI::TableWidget(this, 10, 50, 1350, 800);
    table->horizontalHeader()->setDefaultSectionSize(80);
    // 设置为水平表头
    table->setColumnCount(headerTextList.size());
    table->setHorizontalHeaderLabels(headerTextList);
    table->setColumnWidth(0, 100);

    btnRefresh = make_shared<QPushButton>("刷新", this);
    btnRefresh->setGeometry(220, 10, 60, 30);
    //btnRefresh->setStyleSheet();

    connect(comboxStation.get(), &QComboBox::currentIndexChanged, this, &QWStatistics::onCurrentIndexChanged);
    connect(btnRefresh.get(), &QPushButton::clicked, this, &QWStatistics::onCurrentIndexChanged);
}

QWStatistics::~QWStatistics()
{
}

void QWStatistics::onCurrentIndexChanged(int index)
{
    auto& appdata = Application::data();

    string stationName = comboxStation->itemText(index).toStdString();
    if (stationName == "所有")
    {

    }
    else
    {
        auto station = appdata.getStationByName(stationName);
        if (station)
        {
            string sql = "SELECT * FROM stat_total_day std WHERE station_id='" + std::to_string(station->stationId) 
                + "' ORDER BY dt DESC LIMIT 100;";
            vector<Fields> result;
            DAO::exec(NULL, sql, result);
            table->clearContents();
            table->setRowCount(0);
            for (int i = 0; i<result.size(); ++i)
            {
                auto& fields = result[i];
                MyQUI::setTableCellText(table, i, 0, fields.value("dt"));
                MyQUI::setTableCellText(table, i, 1, fields.value("E_in"));
                MyQUI::setTableCellText(table, i, 2, fields.value("E_in_J"));
                MyQUI::setTableCellText(table, i, 3, fields.value("E_in_F"));
                MyQUI::setTableCellText(table, i, 4, fields.value("E_in_P"));
                MyQUI::setTableCellText(table, i, 5, fields.value("E_in_G"));
                MyQUI::setTableCellText(table, i, 6, fields.value("E_out"));
                MyQUI::setTableCellText(table, i, 7, fields.value("E_out_J"));
                MyQUI::setTableCellText(table, i, 8, fields.value("E_out_F"));
                MyQUI::setTableCellText(table, i, 9, fields.value("E_out_P"));
                MyQUI::setTableCellText(table, i, 10, fields.value("E_out_G"));
                MyQUI::setTableCellText(table, i, 11, fields.value("fee_in"));
                MyQUI::setTableCellText(table, i, 12, fields.value("fee_out"));
                MyQUI::setTableCellText(table, i, 13, fields.value("income"));
            }
        }
    }
}