#include "clopResultList.h"
#include "ui_clopResultList.h"

#include "CAppInfo.h"
#include "clOperation.h"

#include <QScrollBar>
#include "awMsgBox.h"

clopResultList::clopResultList(CPracticeCourseInfo pci, QString sBatchName, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::clopResultList)
{
    ui->setupUi(this);

    setStyleSheet(g_appInfoPtr->m_sQssStr);

    qRegisterMetaType<CQueryPracticeRecordList>("CQueryPracticeRecordList");
    connect(g_httpBllPtr.get(), &CHttpBll::sgnQueryPracticeRecordList, this, &clopResultList::onQueryPracticeRecordList);

    ui->label_clop_batch->setText(QString::fromLocal8Bit("批次：%1").arg(sBatchName));
    ui->label_clop_course->setText(QString::fromLocal8Bit("科目：%1").arg(pci.sCourseName));
    ui->label_clop_avgCorrectRate->setText(QString::fromLocal8Bit("平均正确率：%1").arg(pci.fAveObjectiveAccuracy));
    ui->label_clop_maxCorrectRate->setText(QString::fromLocal8Bit("最高正确率：%1").arg(pci.fMaxObjectiveAccuracy));

    CHttpRequestPackage hrp;
    hrp.sUri = QString("/api/ecs_oe_admin/client/exam/process/queryPracticeRecordList");
    hrp.sParamList.push_back(QString("examStudentId,%1").arg(pci.nExamStudentId));
    hrp.nRequestType = RequestType::rtQueryPracticeRecordList;
    hrp.eParamType = HttpParamType::hptUrl;
    g_httpBllPtr->post(hrp);
}

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

void clopResultList::setUI(const int nLeft, const int nTop, const int nWidth, const int nHeight)
{
    setGeometry(nLeft, nTop, nWidth, nHeight);
    ui->widget_clop_BG->setGeometry(0, 0, width(), height());
    ui->widget_clop_top->setGeometry(0, 0, width(), g_appInfoPtr->m_fRate*56);
    ui->btn_clop_goback->setGeometry(ui->widget_clop_top->width() - g_appInfoPtr->m_fRate*(108 + 30), (ui->widget_clop_top->height() - g_appInfoPtr->m_fRate*32)/2, g_appInfoPtr->m_fRate*108, g_appInfoPtr->m_fRate*32);
    ui->btn_clop_goback->setIconSize(QSize(g_appInfoPtr->m_fRate*14, g_appInfoPtr->m_fRate*14));
    ui->btn_clop_goback->setIcon(QIcon(":/images/icon-goback-list.png"));
    ui->label_clop_batch->adjustSize();
    ui->label_clop_batch->setGeometry(g_appInfoPtr->m_fRate*30, (ui->widget_clop_top->height() - ui->label_clop_batch->height())/2,
                                 ui->label_clop_batch->width(), ui->label_clop_batch->height());
    ui->label_clop_maxCorrectRate->adjustSize();
    ui->label_clop_maxCorrectRate->setGeometry(ui->btn_clop_goback->x() - g_appInfoPtr->m_fRate*30 - ui->label_clop_maxCorrectRate->width(),
                                               ui->label_clop_batch->y(), ui->label_clop_maxCorrectRate->width(), ui->label_clop_maxCorrectRate->height());
    ui->label_clop_course->adjustSize();
    ui->label_clop_avgCorrectRate->adjustSize();
    int nLabelSpace = (ui->label_clop_maxCorrectRate->x() - ui->label_clop_batch->x() - ui->label_clop_batch->width() - ui->label_clop_course->width() - ui->label_clop_avgCorrectRate->width())/3;
    ui->label_clop_course->setGeometry(ui->label_clop_batch->x() + ui->label_clop_batch->width() + nLabelSpace,
                                       ui->label_clop_batch->y(), ui->label_clop_course->width(), ui->label_clop_course->height());
    ui->label_clop_avgCorrectRate->setGeometry(ui->label_clop_course->x() + ui->label_clop_course->width() + nLabelSpace,
                                               ui->label_clop_batch->y(), ui->label_clop_avgCorrectRate->width(), ui->label_clop_avgCorrectRate->height());
    ui->tablewt_examList->setGeometry(g_appInfoPtr->m_fRate*30, ui->widget_clop_top->height() + g_appInfoPtr->m_fRate*10,
                                      ui->widget_clop_BG->width() - g_appInfoPtr->m_fRate*30*2,
                                      ui->widget_clop_BG->height() - ui->widget_clop_top->height() - g_appInfoPtr->m_fRate*(10+78));
    ui->tablewt_examList->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //整行选中的方式
    ui->tablewt_examList->setSelectionBehavior(QAbstractItemView::SelectRows);
    //设置为只能选中一行
    ui->tablewt_examList->setSelectionMode(QAbstractItemView::SingleSelection);
    //隐藏列表头
    ui->tablewt_examList->verticalHeader()->setVisible(false);
    //隐藏边框
    ui->tablewt_examList->setShowGrid(false);
    //表头不高亮显示
    ui->tablewt_examList->horizontalHeader()->setHighlightSections(false);
    //设置行数
    ui->tablewt_examList->setRowCount(10);
    //设置列数
    ui->tablewt_examList->setColumnCount(10);

    ui->tablewt_examList->setHorizontalScrollMode(QTableWidget::ScrollPerPixel);
    ui->tablewt_examList->horizontalScrollBar()->setSingleStep(g_appInfoPtr->m_fRate*5);

    QStringList sHeadStr;
    sHeadStr << QString::fromLocal8Bit("练习编号") <<
                QString::fromLocal8Bit("开始日期") <<
                QString::fromLocal8Bit("结束日期") <<
                QString::fromLocal8Bit("练习时长") <<
                QString::fromLocal8Bit("总题量") <<
                QString::fromLocal8Bit("正确") <<
                QString::fromLocal8Bit("错误") <<
                QString::fromLocal8Bit("未答") <<
                QString::fromLocal8Bit("正确率") <<
                QString::fromLocal8Bit("操作");

    ui->tablewt_examList->setHorizontalHeaderLabels(sHeadStr);
    ui->tablewt_examList->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    ui->tablewt_examList->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);

    ui->tablewt_examList->verticalHeader()->setDefaultSectionSize(g_appInfoPtr->m_fRate*52);
    ui->tablewt_examList->setColumnWidth(0, g_appInfoPtr->m_fRate*92);
    ui->tablewt_examList->setColumnWidth(1, g_appInfoPtr->m_fRate*172);
    ui->tablewt_examList->setColumnWidth(2, g_appInfoPtr->m_fRate*172);
    ui->tablewt_examList->setColumnWidth(3, g_appInfoPtr->m_fRate*97);
    ui->tablewt_examList->setColumnWidth(4, g_appInfoPtr->m_fRate*90);
    ui->tablewt_examList->setColumnWidth(5, g_appInfoPtr->m_fRate*90);
    ui->tablewt_examList->setColumnWidth(6, g_appInfoPtr->m_fRate*90);
    ui->tablewt_examList->setColumnWidth(7, g_appInfoPtr->m_fRate*90);
    ui->tablewt_examList->setColumnWidth(8, g_appInfoPtr->m_fRate*90);
    ui->tablewt_examList->setColumnWidth(9, g_appInfoPtr->m_fRate*128);
    ui->tablewt_examList->horizontalHeader()->setStretchLastSection(true);
}

void clopResultList::onQueryPracticeRecordList(CQueryPracticeRecordList queryPracticeRecordList)
{
    if(queryPracticeRecordList.nCode == 200)
    {        
        int nSize = queryPracticeRecordList.vPracticeRecordList.size();
        ui->tablewt_examList->setRowCount(nSize);
        for(int i = 0; i < nSize; ++i)
        {
            CPracticeRecord pr = queryPracticeRecordList.vPracticeRecordList[i];
            ui->tablewt_examList->setItem(i, 0, new QTableWidgetItem(QString::number(pr.nId)));
            ui->tablewt_examList->setItem(i, 1, new QTableWidgetItem(pr.sStartTime));
            ui->tablewt_examList->setItem(i, 2, new QTableWidgetItem(pr.sEndTime));
            ui->tablewt_examList->setItem(i, 3, new QTableWidgetItem(QString("%1:%2:%3")
                                                                     .arg(pr.nUsedExamTime/1000/60/60, 2, 10, QChar('0'))
                                                                     .arg(pr.nUsedExamTime/1000/60%60, 2, 10, QChar('0'))
                                                                     .arg(pr.nUsedExamTime/1000%60, 2, 10, QChar('0'))));
            ui->tablewt_examList->setItem(i, 4, new QTableWidgetItem(QString::number(pr.nTotalQuestionCount)));
            ui->tablewt_examList->setItem(i, 5, new QTableWidgetItem(QString::number(pr.nSuccQuestionNum)));
            ui->tablewt_examList->setItem(i, 6, new QTableWidgetItem(QString::number(pr.nFailQuestionNum)));
            ui->tablewt_examList->setItem(i, 7, new QTableWidgetItem(QString::number(pr.nNotAnsweredCount)));
            ui->tablewt_examList->setItem(i, 8, new QTableWidgetItem(QString::fromLocal8Bit("%1%").arg(pr.fObjectiveAccuracy)));

            clOperation *clo = new clOperation(i, ui->tablewt_examList);
            connect(clo, &clOperation::viewPracticePaper, this, [&](int nRow){
                if(m_pReport == nullptr)
                {
                    m_pReport = std::make_shared<clopReport>(m_vPracticeRecordList[nRow], false, this);
                    connect(m_pReport.get(), &clopReport::goback, this, [&](){
                        m_pReport.reset();
                    });
					m_pReport->setUI(0, 0, width(), height());
                }
				m_pReport->show();
            });
            clo->setUI(ui->tablewt_examList->columnWidth(9), ui->tablewt_examList->rowHeight(i), CL_OPERATION_TYPE::cot_online_practice_result);
            ui->tablewt_examList->setCellWidget(i, 9, clo);
        }
        m_vPracticeRecordList.swap(queryPracticeRecordList.vPracticeRecordList);
    }
    else
    {
        if(queryPracticeRecordList.sMessage.isEmpty())
        {
            ShowMsg(QString::fromLocal8Bit("获取练习列表信息失败"), (QWidget*)(this->parent()), MSG_ICON_TYPE::mit_error);
        }
        else
        {
            ShowMsg(queryPracticeRecordList.sMessage, (QWidget*)(this->parent()), MSG_ICON_TYPE::mit_error);
        }
    }
}

void clopResultList::on_btn_clop_goback_clicked()
{
    emit goback();
}
