#include "sloaddlg.h"
#include <QMessageBox>
#include <QVBoxLayout>
#include <QPushButton>

#include "mainwindow.h"
#include "stable.h"

SLoadDlg::SLoadDlg()
{
    setWidgets();
}

SLoadDlg::~SLoadDlg()
{

}

void SLoadDlg::setWidgets()
{
    setWindowTitle("Load");
    QVBoxLayout *vlayout = new QVBoxLayout;
    m_plwData = new QListWidget;
    vlayout->addWidget(m_plwData);
    m_plwData->setSelectionMode(QAbstractItemView::SingleSelection);
    QPushButton *ppbRefresh = new QPushButton("Refresh");
    vlayout->addWidget(ppbRefresh);
    connect(ppbRefresh, SIGNAL(released()),this, SLOT(Refresh()));
    setLayout(vlayout);    
    connect(m_plwData,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(ItemChanged(QListWidgetItem*)));
}

void SLoadDlg::Refresh()
{
    m_plwData->clear();
    QSqlQuery query("select id,name,count from datagroup");
    while (query.next())
    {
        QString str = query.value(1).toString();
        str += " ( ";
        str += query.value(0).toString();
        str += " , ";
        str += query.value(2).toString();
        str += " )";
        QListWidgetItem *pItem = new QListWidgetItem(str,m_plwData);
        pItem->setData(Qt::UserRole, QVariant(query.value(0)));
    }
    GetMainWindow()->InsertLog("Start");
}

void SLoadDlg::ItemChanged( QListWidgetItem *item)
{
    QMessageBox msg;
    msg.setText("Please choose...");
    msg.setModal(true);
    QPushButton *pbAll = msg.addButton("All",QMessageBox::ActionRole);
    QPushButton *pbBody = msg.addButton("Body",QMessageBox::ActionRole);
    QPushButton *pbReply = msg.addButton("Reply",QMessageBox::ActionRole);
    msg.exec();
    if (msg.clickedButton() == pbAll) GetMainWindow()->m_pDataDlg->DataReload("data_" + item->data(Qt::UserRole).toString(),STable::E_ARTICLE_ALL);
    else if (msg.clickedButton() == pbBody) GetMainWindow()->m_pDataDlg->DataReload("data_" + item->data(Qt::UserRole).toString(),STable::E_ARTICLE_BODY);
    else if (msg.clickedButton() == pbReply) GetMainWindow()->m_pDataDlg->DataReload("data_" + item->data(Qt::UserRole).toString(),STable::E_ARTICLE_REPLY);
}
