#include "packdetailform.h"
#include "ui_packdetailform.h"
#include "struct_/packinfo.h"
#include <pqQtlib/utils/pqfileutils.h>

PackDetailForm::PackDetailForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PackDetailForm)
{
    ui->setupUi(this);
    ui->treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->treeWidget->header()->setStretchLastSection( true);
    nowPageNum = 0;
    limits = 5;
    connect(glo,&GlobalInfo::dengJiUped,this,&PackDetailForm::upconfig);
    ui->denierLine->hide();
    ui->dtexLine->hide();
    ui->fiberLine->hide();
    ui->startTimeEdit->setCalendarPopup(true);
    ui->startTimeEdit->setDisplayFormat("yyyy-MM-dd");
    ui->startTimeEdit->setDate(QDate::currentDate());
    ui->endTimeEdit->setCalendarPopup(true);
    ui->endTimeEdit->setDisplayFormat("yyyy-MM-dd");
    ui->endTimeEdit->setDate(QDate::currentDate());
    ui->startTimeEdit->setMaximumDate(QDate::currentDate());
    ui->startTimeEdit->setMinimumDate(QDate::currentDate().addYears(-1));
}

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

void PackDetailForm::on_pushBack_clicked()
{
    emit back();
}

void PackDetailForm::on_pushButton_clicked()
{

    nowPageNum = 0;


    int start = ui->startTimeEdit->date().toString("yyyyMMdd").toInt();
    int end = ui->endTimeEdit->date().toString("yyyyMMdd").toInt();
    if(start>end){
        QMessageBox::information(this,tr("注意"),tr("请输入正确的起始时间和结束时间"));
        return;
    }
    QDate tm = ui->startTimeEdit->date();
    for(int i = 0;i<10;i++){
        if(tm.addDays(i).toString("yyyyMMdd").toInt() >= end){
            days = i;
            break;
        }
    }

    QList<PackSaveInfo> save;
    QList<PackSaveInfo> saveList;

        for(int i = 0;i<=days;i++){
            QDate tm = ui->endTimeEdit->date().addDays(-i);
            QList<PackSaveInfo> saveinfo;
            //issync未同步按钮是否点击
            if(!ui->issync->isChecked()){
                saveinfo = GlobalInfo::this_()->packInfoManger()->getPackInfo(tm);
            }else{
                saveinfo = GlobalInfo::this_()->packInfoManger()->getPackInfoSync(tm);
            }
            for(int i = saveinfo.size()-1;i>=0;i--){
                save.append(saveinfo.at(i));
            }
    //        saveList.append(save);
        }

//    qDebug()<<save.size();
    setSaveInfo(save);
    if(!ui->codeSingleLine->text().isEmpty()||!ui->batch_noLine->text().isEmpty()){
        select();
    }
    dengjiselect();
    changeTreeWidget(0,limits);
}

//筛选
void PackDetailForm::select()
{
    QList<PackSaveInfo> saveList;
    int num =saveInfo.size();
    for(int i = 0;i<num;i++){
        if(!ui->codeSingleLine->text().isEmpty()){
            if(saveInfo.at(i).packInfo->codeSingle()==ui->codeSingleLine->text()){
                saveList.append(saveInfo.at(i));
                break;
            }
        }

        if(!ui->batch_noLine->text().isEmpty()){
            if(saveInfo.at(i).packInfo->batch_no==ui->batch_noLine->text()){
                saveList.append(saveInfo.at(i));
                continue;
            }
        }
    }
    setSaveInfo(saveList);
}

void PackDetailForm::dengjiselect()
{
    if(ui->levelBox->currentText()!="等级"){
        QList<PackSaveInfo> save;
        for(int i = 0;i<saveInfo.size();i++){
            if(saveInfo.at(i).packInfo->level == ui->levelBox->currentText()){
                save.append(saveInfo.at(i));
            }
        }
        setSaveInfo(save);
        return;
    }
}

//往treewidget添加数据
void PackDetailForm::changeTreeWidget(/*QList<QMap<QString,QString>> productList*/int page,int limit)
{
        if(saveInfo.isEmpty()){
            QMessageBox::information(this,tr("提示"),tr("数据为空"));
            ui->treeWidget->clear();
            return;
        }

        ui->treeWidget->clear();
//        product = productList;


/******************************************************************/
        //循环添加数据
        for(int i = 0;i<limit;i++){

            if(page*limit+i>=saveInfo.size()){
                break;
            }
            auto & v = saveInfo.at(page*limit+i);

            QStringList list;
            list.append(v.isSync() ? "已同步" : "未同步");
//            if(productList.at(i).value("tongbu") == "0"){
//            }
            list.append(v.packInfo->codeSingle());
//            list.append(saveInfo.at(i).packInfo.batch_no);
            list.append(getDecimalbit(v.packInfo->gross_weight()/1000.00));
            list.append(getDecimalbit(v.packInfo->net_weight()/1000.00));
            list.append(QString::number(v.packInfo->quantity));


            auto item = new QTreeWidgetItem(list);
            auto tongbu_btn =  new QPushButton("同步");
            connect(tongbu_btn,&QPushButton::clicked,[item,i,this](){this->tongbu_clicked(item,i);});
    //        connect(tongbu_btn, SIGNAL(clicked(bool)), this, SLOT(tongbu_clicked()));
            auto code_single_btn = new QPushButton("打印码单");
            connect(code_single_btn,&QPushButton::clicked,[i,this](){this->code_single_clicked(i);});
//            auto case_nember_btn = new QPushButton("打印箱单");
//            connect(case_nember_btn,SIGNAL(clicked(bool)),this,SLOT(case_number_clicked()));

            ui->treeWidget->addTopLevelItem(item);
            ui->treeWidget->setItemWidget(item,6,tongbu_btn);
            ui->treeWidget->setItemWidget(item,7,code_single_btn);
//            ui->treeWidget->setItemWidget(item,9,case_nember_btn);

//            details = database.GetValues("details",detailSqlList," where code_single = "+product.at(i).value("code_single"));

/*************************子项目****************************/
            //treewidget子项目标题
            QStringList columItemList;
            QTreeWidgetItem *child;
            columItemList<<"箱号"<<"毛重(千克/kg)"<<"净重(千克/kg)";
            child = new QTreeWidgetItem(columItemList);
            item->addChild(child);
            ui->treeWidget->setCurrentItem(child);

            //treewidget子项目数据循环添加
            QList<QSharedPointer<BoxInfo>> boxes = v.packInfo->boxes();
            for(int j=0; j<boxes.size(); ++j)
                {
                    QStringList columItemList;
                    QTreeWidgetItem *child;
                    columItemList<<boxes.at(j)->caseNumber()<<QString::number(boxes.at(j)->gross_weight/1000.00)<<QString::number(boxes.at(j)->net_weight/1000.00);
//                    qDebug()<<"test"<<boxes.at(j)->caseNumber()<<QString::number(boxes.at(j)->gross_weight/1000.00)<<QString::number(boxes.at(j)->net_weight/1000.00);
                    child = new QTreeWidgetItem(columItemList);
                    item->addChild(child);
//                    ui->treeWidget->setCurrentItem(child);
                }
        }
/**************************************************************/
        pageNumAll = saveInfo.size() / limit;
        if(saveInfo.size()%limit>0){
            pageNumAll+=1;
        }
//        ui->pageDataNum->setText("本页数据:"+QString::number(product.length()));
        ui->pagesNum->setText(QString("%1/%2").arg(nowPageNum+1).arg(pageNumAll));

}

//同步按钮
void PackDetailForm::tongbu_clicked(QTreeWidgetItem * item,int row)
{
    if(item->text(0) != "未同步"){
        QMessageBox::information(this,tr("提示"),tr("请刷新"));
        return;
    }
    Manger = GlobalInfo::this_()->packInfoManger();
    packServer = new PackInfoSeed2Server(Manger,this,true);
    QSharedPointer<PackInfo> pack = saveInfo.at(limits*nowPageNum+row).packInfo;
    connect(packServer,&PackInfoSeed2Server::syncEnd,this,&PackDetailForm::SynReturn);
    packServer->send(pack);
    item->setText(0,"同步中");
}

void PackDetailForm::code_single_clicked(int row)
{
    QSharedPointer<PackInfo> pack = saveInfo.at(row).packInfo;
    GlobalInfo::this_()->printerPack(pack);
}

void PackDetailForm::case_number_clicked()
{
    qDebug()<<"打印箱单";//暂时不知道打哪个
}

//上一页
void PackDetailForm::on_prevoiusPage_clicked()
{
    if(nowPageNum<=0){
        return;
    }
    nowPageNum--;
    changeTreeWidget(nowPageNum,limits);
}

//下一页
void PackDetailForm::on_nextPage_clicked()
{
    if(nowPageNum>=pageNumAll-1){
        return;
    }
    nowPageNum++;
    changeTreeWidget(nowPageNum,limits);
}

//跳转
void PackDetailForm::on_jump_clicked()
{
    //判断jumpPage是否为数字，如果不是return
    if(ui->jumpPage->text().toInt() == false){
        return;
    }
    //判断jumpPage页数是否大于总页数，如果是return
    if(ui->jumpPage->text().toInt()>pageNumAll){
        return;
    }
    nowPageNum = ui->jumpPage->text().toInt()-1;
    changeTreeWidget(nowPageNum,limits);
}

void PackDetailForm::SynReturn()
{
    int pageNum = nowPageNum;
//    QDate tm = QDate::currentDate().addDays(-ui->startTime->currentData().toInt());
//    QList<PackSaveInfo> saveinfo = GlobalInfo::this_()->packInfoManger()->getPackInfo(tm);
//    setSaveInfo(saveinfo);
//    changeTreeWidget(pageNum,limits);

    QList<PackSaveInfo> save;
    QList<PackSaveInfo> saveList;

    for(int i = 0;i<=days;i++){
        QDate tm = QDate::currentDate().addDays(-i);
        QList<PackSaveInfo> saveinfo;
        if(!ui->issync->isChecked()){
            saveinfo = GlobalInfo::this_()->packInfoManger()->getPackInfo(tm);
        }else{
            saveinfo = GlobalInfo::this_()->packInfoManger()->getPackInfoSync(tm);
        }
        for(int i = saveinfo.size()-1;i>=0;i--){
            save.append(saveinfo.at(i));
        }
//        saveList.append(save);
    }
//    qDebug()<<save.size();
    setSaveInfo(save);
    if(!ui->codeSingleLine->text().isEmpty()||!ui->batch_noLine->text().isEmpty()||ui->levelBox->currentText()!="等级"){
        select();
    }
    changeTreeWidget(pageNum,limits);
}

void PackDetailForm::upconfig()
{

    DaBaoConfig config = glo->dbConfig();

    QList<DengJiConfigItem> dengJis = glo->dengJiList();

    for(int i = 0;i<dengJis.size();i++){
        if(!dengJis.at(i).isEnable){
            ui->levelBox->addItem(dengJis.at(i).value,dengJis.at(i).id);
        }
    }
}

void PackDetailForm::on_showMao_clicked()
{
    int start = ui->startTimeEdit->date().toString("yyyyMMdd").toInt();
    int end = ui->endTimeEdit->date().toString("yyyyMMdd").toInt();
    if(start>end){
        QMessageBox::information(this,tr("注意"),tr("请输入正确的起始时间和结束时间"));
        return;
    }
    QDate tm = ui->startTimeEdit->date();
    int days = 0;
    for(int i = 0;i<10;i++){
        if(tm.addDays(i).toString("yyyyMMdd").toInt() >= end){
            days = i;
            break;
        }
    }

}


////往QTableWidget里添加数据
//void PackDetailForm::changeTable(QList<QMap<QString,QString>> productList)
//{
////    qDebug()<<productList.at(0).value("goods_id");
//    if(productList.isEmpty()){
//        QMessageBox::information(this,tr("提示"),tr("数据为空"));
//        return;
//    }
//    product = productList;
//    ui->tableWidget->setColumnCount(11);
//    for(int i = 0;i<productList.size();i++){
//        ui->tableWidget->insertRow(i);
//        ui->tableWidget->setItem(i,0,new QTableWidgetItem(productList.at(i).value("tongbu") == "0" ? "未同步" : "同步"));
//        if(ui->tableWidget->item(i,0)->text()=="未同步"){
//            ui->tableWidget->item(i,0)->setBackground(QBrush(QColor(255, 0, 0,180)));
//        }else{
//            ui->tableWidget->item(i,0)->setBackground(QBrush(QColor(0, 255, 255)));
//        }
//        ui->tableWidget->setItem(i,1,new QTableWidgetItem(productList.at(i).value("code_single")));
//        ui->tableWidget->setItem(i,2,new QTableWidgetItem(productList.at(i).value("batch_no")));
//        ui->tableWidget->setItem(i,3,new QTableWidgetItem(productList.at(i).value("gross_weight")));
//        ui->tableWidget->setItem(i,4,new QTableWidgetItem(productList.at(i).value("net_weight")));
//        ui->tableWidget->setItem(i,5,new QTableWidgetItem(productList.at(i).value("quantity")));
//        ui->tableWidget->setItem(i,6,new QTableWidgetItem(productList.at(i).value("packing_time")));
//        auto tongbu_btn =  new QPushButton("同步");
//        connect(tongbu_btn,&QPushButton::clicked,[i,this](){this->tongbu_clicked(i);});
////        connect(tongbu_btn, SIGNAL(clicked(bool)), this, SLOT(tongbu_clicked()));
//        auto code_single_btn = new QPushButton("打印码单");
//        connect(code_single_btn,SIGNAL(clicked(bool)),this,SLOT(code_single_clicked()));
//        auto case_nember_btn = new QPushButton("打印箱单");
//        connect(case_nember_btn,SIGNAL(clicked(bool)),this,SLOT(case_number_clicked()));
//        auto details = new QTreeWidget(this);
////        auto details_btn = new QPushButton("");
//        ui->tableWidget->setCellWidget(i,7,tongbu_btn);
//        ui->tableWidget->setCellWidget(i,8,code_single_btn);
//        ui->tableWidget->setCellWidget(i,9,case_nember_btn);
//        ui->tableWidget->setCellWidget(i,10,details);

//        details->setWindowTitle("tree");
//        QTreeWidgetItem* detail = new QTreeWidgetItem(QStringList()<<"详情");
////        detail->setCheckState(0, Qt::Checked);
////        details->setSortingEnabled(true);
//        details->header()->setSortIndicator(0,Qt::AscendingOrder);
//        details->addTopLevelItem(detail);
//        details->header()->setVisible(false);
//        for(int i=0; i<10; ++i)
//            {

//                QStringList columItemList;
//                QTreeWidgetItem *child;
//                columItemList<<"list";
//                child = new QTreeWidgetItem(columItemList);
//                detail->addChild(child);
//                details->setItemWidget(child,1,new QSpinBox);
//            }
//    }
//}

//同步
//void PackDetailForm::tongbu_clicked(int row)
//{
//    qDebug()<<"行数:" << row;
//    if(ui->tableWidget->item(row,0)->text()!="未同步"){
//        QMessageBox::information(this,tr("提示"),tr("请刷新"));
//        return;
//    }
//    QString condition = " where code_single="+product.at(row).value("code_single");
//    details = database.GetValues("details",detailSqlList,condition);
//    autoPack.doSend(autoPack.toJsonMap(product.at(row),details));
//    ui->tableWidget->setItem(row,0,new QTableWidgetItem("请刷新"));
//    ui->tableWidget->item(row,0)->setBackground(QBrush(QColor(255,255,0)));
//}




