#include "cloeUploadFile.h"
#include "ui_cloeUploadFile.h"

#include "CAppInfo.h"
#include "awMsgBox.h"
#include "CCommonTools.h"
#include <QDesktopWidget>
#include <QFileDialog>


cloeUploadFile::cloeUploadFile(__int64 nExamRecordDataId, std::vector<QString> vFileTypes, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::cloeUploadFile), m_nExamRecordDataId(nExamRecordDataId)
{
    ui->setupUi(this);

    setStyleSheet(g_appInfoPtr->m_sQssStr);
    
    ui->rdoBtn_zip->setVisible(false);
    ui->rdoBtn_pdf->setVisible(false);
    ui->rdoBtn_picture->setVisible(false);
    for(QString sType : vFileTypes)
    {
        if(sType == "ZIP")
        {
            ui->rdoBtn_zip->setVisible(true);
        }

        if(sType == "PDF")
        {
            ui->rdoBtn_pdf->setVisible(true);
        }

        if(sType == "IMAGE")
        {
            ui->rdoBtn_picture->setVisible(true);
        }
    }

	initUI();

	qRegisterMetaType<CSubmitOfflinePaper>("CSubmitOfflinePaper");
    connect(g_httpBllPtr.get(), &CHttpBll::sgnSubmitOfflinePaper, this, &cloeUploadFile::onSubmitOfflinePaper);
}

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

void cloeUploadFile::initUI()
{
    QDesktopWidget *dekwiget = QApplication::desktop();
    setGeometry(0, 0, dekwiget->width(), dekwiget->height());
    ui->widget_mask->setGeometry(0, 0, dekwiget->width(), dekwiget->height());
    ui->widget_cloe_BG->setGeometry((width() - g_appInfoPtr->m_fRate*490)/2, (height() - g_appInfoPtr->m_fRate*295)/2,
                                   g_appInfoPtr->m_fRate*490, g_appInfoPtr->m_fRate*295);
    ui->label_fc_title->adjustSize();
    ui->label_fc_title->setGeometry(g_appInfoPtr->m_fRate*20, g_appInfoPtr->m_fRate*16,
                                    ui->label_fc_title->width(), ui->label_fc_title->height());
    ui->btn_fc_close->setGeometry(ui->widget_cloe_BG->width() - g_appInfoPtr->m_fRate*(20 + 16),
                                  g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*16);
    ui->label_HLine->setGeometry(0, ui->label_fc_title->y() + ui->label_fc_title->height() + g_appInfoPtr->m_fRate*16,
                                 ui->widget_cloe_BG->width(), g_appInfoPtr->m_fRate*1 < 1 ? 1 : g_appInfoPtr->m_fRate*1);
    ui->label_cloe_selFileType->adjustSize();
    ui->label_cloe_selFileType->setGeometry(g_appInfoPtr->m_fRate*30, ui->label_HLine->y() + ui->label_HLine->height() + g_appInfoPtr->m_fRate*30,
                                            ui->label_cloe_selFileType->width(), ui->label_cloe_selFileType->height());
    int nLeftRadio = ui->label_cloe_selFileType->x() + ui->label_cloe_selFileType->width() + g_appInfoPtr->m_fRate*26;
    ui->rdoBtn_zip->adjustSize();
    ui->rdoBtn_zip->setGeometry(nLeftRadio,
                                ui->label_cloe_selFileType->y() + (ui->label_cloe_selFileType->height() - ui->rdoBtn_zip->height())/2,
                                ui->rdoBtn_zip->width(), ui->rdoBtn_zip->height());
    nLeftRadio = ui->rdoBtn_zip->x() + ui->rdoBtn_zip->width() + g_appInfoPtr->m_fRate*20;

    ui->rdoBtn_pdf->adjustSize();
    ui->rdoBtn_pdf->setGeometry(nLeftRadio,
                                ui->rdoBtn_zip->y(), ui->rdoBtn_pdf->width(), ui->rdoBtn_pdf->height());
    nLeftRadio = ui->rdoBtn_pdf->x() + ui->rdoBtn_pdf->width() + g_appInfoPtr->m_fRate*30;    
    
    ui->rdoBtn_picture->adjustSize();
    ui->rdoBtn_picture->setGeometry(nLeftRadio,
                                    ui->rdoBtn_zip->y(), ui->rdoBtn_picture->width(), ui->rdoBtn_picture->height());

    ui->edt_cloe_file->setGeometry(ui->label_cloe_selFileType->x(), ui->label_cloe_selFileType->y() + ui->label_cloe_selFileType->height() + g_appInfoPtr->m_fRate*20,
                                   g_appInfoPtr->m_fRate*360, g_appInfoPtr->m_fRate*40);
    ui->btn_cloe_selFile->setGeometry(ui->edt_cloe_file->x() + ui->edt_cloe_file->width() + g_appInfoPtr->m_fRate*10,
                                      ui->edt_cloe_file->y() + (ui->edt_cloe_file->height() - g_appInfoPtr->m_fRate*36)/2,
                                      g_appInfoPtr->m_fRate*60, g_appInfoPtr->m_fRate*36);
    ui->btn_comfirmUpload->setGeometry(ui->widget_cloe_BG->width() - g_appInfoPtr->m_fRate*(30+120), ui->widget_cloe_BG->height() - g_appInfoPtr->m_fRate*(30+40),
                                       g_appInfoPtr->m_fRate*120, g_appInfoPtr->m_fRate*40);
    ui->btn_cancelUpload->setGeometry(ui->btn_comfirmUpload->x() - g_appInfoPtr->m_fRate*10 - ui->btn_comfirmUpload->width(),
                                      ui->btn_comfirmUpload->y(), ui->btn_comfirmUpload->width(), ui->btn_comfirmUpload->height());
}

void cloeUploadFile::showEvent(QShowEvent *)
{
	int nLeftRadio = ui->label_cloe_selFileType->x() + ui->label_cloe_selFileType->width() + g_appInfoPtr->m_fRate * 26;	
	if (ui->rdoBtn_zip->isVisible())
	{
		nLeftRadio = ui->rdoBtn_zip->x() + ui->rdoBtn_zip->width() + g_appInfoPtr->m_fRate * 20;
	}

	if (ui->rdoBtn_pdf->isVisible())
	{	
		ui->rdoBtn_pdf->setGeometry(nLeftRadio,
			ui->rdoBtn_zip->y(), ui->rdoBtn_pdf->width(), ui->rdoBtn_pdf->height());
		nLeftRadio = ui->rdoBtn_pdf->x() + ui->rdoBtn_pdf->width() + g_appInfoPtr->m_fRate * 30;
	}

	if (ui->rdoBtn_picture->isVisible())
	{	
		ui->rdoBtn_picture->setGeometry(nLeftRadio,
			ui->rdoBtn_zip->y(), ui->rdoBtn_picture->width(), ui->rdoBtn_picture->height());
	}
}

void cloeUploadFile::on_btn_cloe_selFile_clicked()
{
    QString sTitle = "";
    QString sExt = "";
    if(ui->rdoBtn_zip->isChecked())
    {
        sTitle = QString::fromLocal8Bit("选择ZIP文件");
        sExt = QString::fromLocal8Bit("WinRAR ZIP 压缩文件(*.zip)");
    }
    else if(ui->rdoBtn_pdf->isChecked())
    {
        sTitle = QString::fromLocal8Bit("选择PDF文件");
        sExt = QString::fromLocal8Bit("PDF文件(*.pdf)");
    }
    else if(ui->rdoBtn_picture->isChecked())
    {
        sTitle = QString::fromLocal8Bit("选择图片文件");
        sExt = QString::fromLocal8Bit("图片文件(*.jpeg;*.jpg;*.png)");
    }
	else
	{		
        ShowMsg(QString::fromLocal8Bit("请先选择文件类型"), this, MSG_ICON_TYPE::mit_error);
		return;
	}
	
	if (ui->rdoBtn_picture->isChecked())
	{
		QStringList fileNames = QFileDialog::getOpenFileNames(this,
			sTitle,
			".",
			sExt);
		ui->edt_cloe_file->setText(fileNames.join(";"));
	}
	else
	{ 
		QString fileName = QFileDialog::getOpenFileName(this,
			sTitle,
			".",
			sExt);
		ui->edt_cloe_file->setText(fileName);
	}
    
}

void cloeUploadFile::on_btn_cancelUpload_clicked()
{
    emit cancel();
}

void cloeUploadFile::on_btn_comfirmUpload_clicked()
{
	if (ui->edt_cloe_file->text().isEmpty())
	{
        ShowMsg(QString::fromLocal8Bit("请先选择文件，再上传"), this, MSG_ICON_TYPE::mit_error);
		return;
	}
	
	QString sType = "";
	if (ui->rdoBtn_zip->isChecked())
	{
		sType = "ZIP";		
	}
	else if (ui->rdoBtn_pdf->isChecked())
	{
		sType = "PDF";
	}
	else if (ui->rdoBtn_picture->isChecked())
	{
		sType = "IMAGE";
	}
	QString sFileNames = ui->edt_cloe_file->text();
	QStringList sFileList = sFileNames.split(";");
	CHttpRequestPackage hrp;
	hrp.sUri = "/api/ecs_oe_admin/client/exam/process/batchSubmitPaper";
	hrp.nRequestType = RequestType::rtSubmitOfflinePaper;
	hrp.sParamList.push_back(QString("examRecordDataId,%1").arg(m_nExamRecordDataId));
	for (QString sFile : sFileList)
	{
        hrp.sParamList.push_back(QString("formdataFileType,fileArray,%1").arg(sFile));
		hrp.sParamList.push_back(QString("fileMd5Array,%1").arg(CCommonTools::fileMd5(sFile)));
	}
	
	hrp.sParamList.push_back(QString("fileType,%1").arg(sType));
	hrp.eParamType = HttpParamType::hptFormdata;
	g_httpBllPtr->post(hrp);
}

void cloeUploadFile::onSubmitOfflinePaper(CSubmitOfflinePaper submitOfflinePaper)
{
	if (submitOfflinePaper.nCode == 200)
	{
        ShowMsg(QString::fromLocal8Bit("上传答案完成"), (QWidget*)this->parent(), MSG_ICON_TYPE::mit_succeed);
		emit uploadSucceed();
	}
	else
	{
        if(submitOfflinePaper.sMessage.isEmpty())
        {
            ShowMsg(QString::fromLocal8Bit("上传答案失败"), (QWidget*)(this->parent()), MSG_ICON_TYPE::mit_error);
        }
        else
        {
            ShowMsg(submitOfflinePaper.sMessage, (QWidget*)(this->parent()), MSG_ICON_TYPE::mit_error);
        }
	}
}

void cloeUploadFile::on_btn_fc_close_clicked()
{
    emit cancel();
}
