#include "clObjectiveScore.h"
#include "ui_clObjectiveScore.h"

#include "CAppInfo.h"

#include <QListWidgetItem>
#include <QHBoxLayout>
#include <QLabel>
#include <QGraphicsDropShadowEffect>
#include "logproc.h"
#include "awMsgBox.h"

clObjectiveScore::clObjectiveScore(int nRow, QString sExamStudentId, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::clObjectiveScore), m_sExamStudentId(sExamStudentId), m_nRow(nRow)
{
    ui->setupUi(this);

    setStyleSheet(g_appInfoPtr->m_sQssStr);

	qRegisterMetaType<CQueryObjectiveScoreList>("CQueryObjectiveScoreList");
	connect(g_httpBllPtr.get(), &CHttpBll::sgnQueryObjectiveScoreList, this, &clObjectiveScore::onQueryObjectiveScoreList);

	CHttpRequestPackage hrp;
	hrp.sUri = "/api/ecs_oe_admin/client/exam/process/queryObjectiveScoreList";
	hrp.nRequestType = RequestType::rtQueryObjectiveScoreList;
	hrp.eParamType = HttpParamType::hptUrl;
	hrp.sCommonStr = sExamStudentId;
	hrp.sParamList.push_back(QString("examStudentId,%1").arg(m_sExamStudentId));
	g_httpBllPtr->post(hrp);
}

clObjectiveScore::~clObjectiveScore()
{
    awMsgBox::clear(this);
    delete ui;
}

void clObjectiveScore::setUI(const int nLeft, const int nTop, const int nWidth, const int nHeight)
{
    setGeometry(nLeft, nTop, nWidth, nHeight);
    ui->widget_objevtiveScore->setGeometry(g_appInfoPtr->m_fRate*5, g_appInfoPtr->m_fRate*5, width()-g_appInfoPtr->m_fRate*10, height()-g_appInfoPtr->m_fRate*10);

    ui->lw_objectiveScore->setGeometry(g_appInfoPtr->m_fRate*20, g_appInfoPtr->m_fRate*10,
                                       ui->widget_objevtiveScore->width() - g_appInfoPtr->m_fRate*20,
                                       ui->widget_objevtiveScore->height() - g_appInfoPtr->m_fRate*10*2);
   /*
	QListWidgetItem *pItem = new QListWidgetItem;
    QSize size = pItem->sizeHint();
    size.setHeight(g_appInfoPtr->m_fRate*46);
    pItem->setSizeHint(size);
    ui->lw_objectiveScore->addItem(pItem);
    QWidget *w = new QWidget;
    w->setStyleSheet(QString("QWidget{background:rgba(19, 187, 138, 1); border-radius:%1px}").arg((int)(g_appInfoPtr->m_fRate*10)));
    QHBoxLayout *l = new QHBoxLayout;
    QString sLabelStyle = QString(R"(QLabel{
                                  font-size:%1px;
                                  font-family:"Microsoft YaHei";
                                  font-weight:400;
                                  color:rgba(255,255,255,1);})").arg((int)(g_appInfoPtr->m_fRate*12));
    QLabel *label_times = new QLabel;
    label_times->setText(QString::fromLocal8Bit("次数"));
    label_times->setStyleSheet(sLabelStyle);
    label_times->adjustSize();
    QLabel *label_startTime = new QLabel;
    label_startTime->setText(QString::fromLocal8Bit("开始时间"));
    label_startTime->setStyleSheet(sLabelStyle);
    label_startTime->adjustSize();
    QLabel *label_handinPaperTime = new QLabel;
    label_handinPaperTime->setText(QString::fromLocal8Bit("交卷时间"));
    label_handinPaperTime->setStyleSheet(sLabelStyle);
    label_handinPaperTime->adjustSize();
    QLabel *label_score = new QLabel;
    label_score->setText(QString::fromLocal8Bit("客观分"));
    label_score->setStyleSheet(sLabelStyle);
    label_score->adjustSize();
    l->addSpacing(g_appInfoPtr->m_fRate*20);
    l->addWidget(label_times);
    l->addStretch();
    l->addWidget(label_startTime);
    l->addStretch();
    l->addWidget(label_handinPaperTime);
    l->addStretch();
    l->addWidget(label_score);
    l->addSpacing(g_appInfoPtr->m_fRate*20);

    w->setLayout(l);
    ui->lw_objectiveScore->setItemWidget(pItem, w);

    */

    QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
    effect->setOffset(g_appInfoPtr->m_fRate*4,g_appInfoPtr->m_fRate*4);
    effect->setColor(QColor(0,0,0,30));
    effect->setBlurRadius(g_appInfoPtr->m_fRate*16);
    ui->widget_objevtiveScore->setGraphicsEffect(effect);
}

void clObjectiveScore::onQueryObjectiveScoreList(CQueryObjectiveScoreList queryObjectiveScoreList)
{
    if (queryObjectiveScoreList.nCode == 200)
    {
        if(m_sExamStudentId == queryObjectiveScoreList.sExamStudentId)
        {
            ui->lw_objectiveScore->clear();
            QListWidgetItem *pItem = new QListWidgetItem;
            QSize size = pItem->sizeHint();
            size.setHeight(g_appInfoPtr->m_fRate * 46);
            pItem->setSizeHint(size);
            ui->lw_objectiveScore->addItem(pItem);
            QWidget *w = new QWidget;
            w->setStyleSheet(QString("QWidget{background:rgba(19, 187, 138, 1); border-radius:%1px}").arg((int)(g_appInfoPtr->m_fRate * 10)));
            QHBoxLayout *l = new QHBoxLayout;
            QString sLabelStyle = QString(R"(QLabel{
                                      font-size:%1px;
                                      font-family:"Microsoft YaHei";
                                      font-weight:400;
                                      color:rgba(255,255,255,1);})").arg((int)(g_appInfoPtr->m_fRate * 12));
            QLabel *label_times = new QLabel;
            label_times->setText(QString::fromLocal8Bit("次数"));
            label_times->setStyleSheet(sLabelStyle);
            label_times->adjustSize();
            QLabel *label_startTime = new QLabel;
            label_startTime->setText(QString::fromLocal8Bit("开始时间"));
            label_startTime->setStyleSheet(sLabelStyle);
            label_startTime->adjustSize();
            QLabel *label_handinPaperTime = new QLabel;
            label_handinPaperTime->setText(QString::fromLocal8Bit("交卷时间"));
            label_handinPaperTime->setStyleSheet(sLabelStyle);
            label_handinPaperTime->adjustSize();
            QLabel *label_score = new QLabel;
            label_score->setText(QString::fromLocal8Bit("客观分"));
            label_score->setStyleSheet(sLabelStyle);
            label_score->adjustSize();
            l->addSpacing(g_appInfoPtr->m_fRate * 20);
            l->addWidget(label_times);
            l->addStretch();
            l->addWidget(label_startTime);
            l->addStretch();
            l->addWidget(label_handinPaperTime);
            l->addStretch();
            l->addWidget(label_score);
            l->addSpacing(g_appInfoPtr->m_fRate * 20);

            w->setLayout(l);
            ui->lw_objectiveScore->setItemWidget(pItem, w);

            int nSzie = queryObjectiveScoreList.vObjectiveScore.size();

            for (int i = nSzie - 1; i >= 0; --i)
            {
                if(i == nSzie - 11)
                {
                    break;
                }
                CObjectiveScore os = queryObjectiveScoreList.vObjectiveScore[i];
                QListWidgetItem *pItem = new QListWidgetItem;
                QSize size = pItem->sizeHint();
                size.setHeight(g_appInfoPtr->m_fRate * 46);
                pItem->setSizeHint(size);
                ui->lw_objectiveScore->addItem(pItem);
                QWidget *w = new QWidget;
                w->setStyleSheet(QString("QWidget{background:rgba(255, 255, 255, 1); border-radius:%1px}").arg((int)(g_appInfoPtr->m_fRate * 10)));
                QHBoxLayout *l = new QHBoxLayout;
                QString sLabelStyle = QString(R"(QLabel{
                                          font-size:%1px;
                                          font-family:"Microsoft YaHei";
                                          font-weight:400;
                                          color:rgba(51,51,51,1);})").arg((int)(g_appInfoPtr->m_fRate * 12));
                QString sLabelScoreStyle = QString(R"(QLabel{
                                          font-size:%1px;
                                          font-family:"Microsoft YaHei";
                                          font-weight:500;
                                          color:rgba(51,51,51,1);})").arg((int)(g_appInfoPtr->m_fRate * 12));

                QLabel *label_times = new QLabel;
                label_times->setText(QString::number(os.nExamOrder));
                label_times->setStyleSheet(sLabelStyle);
                label_times->adjustSize();
                QLabel *label_startTime = new QLabel;
                label_startTime->setText(os.sStartTime);
                label_startTime->setStyleSheet(sLabelStyle);
                label_startTime->adjustSize();
                QLabel *label_handinPaperTime = new QLabel;
                label_handinPaperTime->setText(os.sEndTime);
                label_handinPaperTime->setStyleSheet(sLabelStyle);
                label_handinPaperTime->adjustSize();
                if (label_handinPaperTime->width() < label_startTime->width())
                {
                    label_handinPaperTime->setFixedWidth(label_startTime->width());
                }
                QLabel *label_score = new QLabel;
                QString sScoreText = QString::number(os.fObjectiveScore);
                if (os.bIsAuditing)
                {
                    sScoreText = QString::fromLocal8Bit("审核中");
                }
                if (os.bIsIllegality)
                {
                    sScoreText = QString::fromLocal8Bit("违纪");
                }
                label_score->setText(sScoreText);
                label_score->setStyleSheet(sLabelScoreStyle);
                label_score->adjustSize();
                l->addSpacing(g_appInfoPtr->m_fRate * 20);
                l->addWidget(label_times);
                l->addStretch();
                l->addWidget(label_startTime);
                l->addStretch();
                l->addWidget(label_handinPaperTime);
                l->addStretch();
                l->addWidget(label_score);
                l->addSpacing(g_appInfoPtr->m_fRate * 20);

                w->setLayout(l);
                ui->lw_objectiveScore->setItemWidget(pItem, w);
            }
			nSzie = nSzie > 10 ? 10 : nSzie;
            int nHeight = g_appInfoPtr->m_fRate * 46*(nSzie+1) + g_appInfoPtr->m_fRate * (10 + 5) + g_appInfoPtr->m_fRate * (20 + 5);
            emit objectiveScoreHeight(m_nRow, nHeight);
        }
	}
	else
	{
        if(queryObjectiveScoreList.sMessage.isEmpty())
        {
            ShowMsg(QString::fromLocal8Bit("获取客观题成绩失败"), (QWidget*)(this->parent()), MSG_ICON_TYPE::mit_error);
        }
        else
        {
            ShowMsg(queryObjectiveScoreList.sMessage, (QWidget*)(this->parent()), MSG_ICON_TYPE::mit_error);
        }
	}
}
