#include "selectvalueform.h"
#include "ui_selectvalueform.h"
#include <QJsonArray>

SelectValueForm::SelectValueForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SelectValueForm),nowPage(0),pageListNum(20),totlePage(1),totleNum(0)
{
    ui->setupUi(this);
    connect(&_request,&HttpRequest::result,this,&SelectValueForm::result);
    nowPage = 0;
    connect(glo,&GlobalInfo::doInited,this,&SelectValueForm::Start);
    connect(glo,&GlobalInfo::selectproject,this,&SelectValueForm::Start);
    ui->denier->hide();
    ui->dtex->hide();
    ui->fiber->hide();
    ui->category->hide();
    ui->machine_no->hide();
//    ui->specs->hide();
    ui->color->hide();
    ui->pushButton_2->setEnabled(false);
    Manger = GlobalInfo::this_()->packInfoManger();
    proinfo = new ProjectInfoManger(Manger,this);
    connect(proinfo,&ProjectInfoManger::sync,this,&SelectValueForm::on_RefreshButton_clicked);
//    ui->RefreshButton->hide();
}

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

void SelectValueForm::Start()
{
    ui->pushButton_2->setEnabled(true);
    prolist = GlobalInfo::this_()->project().values();

//    if(prolist.isEmpty()){
//        return;
//    }
    totleNum = prolist.size();
    refPage(nowPage);
    ui->labelTotle->setText(QString::number(totleNum));
    ui->back_Button->hide();
    ui->backMainButton->hide();

    if(status == 0){
        ui->backMainButton->show();
        return;
    }
    ui->back_Button->show();
}

void SelectValueForm::result(int code, const QJsonObject & body)
{
    if(body.value("code").toInt(404) != 200){
        nowPage = 0;
        pageListNum = 20;
        totlePage = 0;
        totleNum = 0;
        return;
    }
//    currtInfo.clear();
    totleNum = body.value("total").toInt();
    QJsonArray ary = body.value("data").toArray();
    for(int i =0; i < ary.size(); ++i){
        parseObject(ary.at(i).toObject());
    }
//    refPage();
}


void SelectValueForm::parseObject(const QJsonObject & obj)
{
    ProjectInfo info;
    info.dtex = obj.value("detx").toString();

    info.goods_id = obj.value("goods_id").toInt();
    info.batch_no = obj.value("batch_no").toString();
    info.category = obj.value("category").toString();
    info.product_type_code = obj.value("product_type_code").toString();
    info.spec_role = obj.value("spec_role").toString();
    info.denier = obj.value("denier").toString();
//    info.dtex = obj.value("detx").toString();
    info.fiber = obj.value("fiber").toString();
    info.specs = obj.value("specs").toString();
    info.color = obj.value("color").toString();
    info.twist_type = obj.value("twist_type").toString();
    info.bucket_color = obj.value("bucket_color").toString();
    info.box_weight = obj.value("box_weight").toString();
    info.bucket_weight = obj.value("bucket_weight").toString();
    info.machine_no = obj.value("machine_no").toString();
    info.carton_type = obj.value("carton_type").toString();
    info.limit_number = obj.value("limit_number").toString();
    info.bucket_number = obj.value("bucket_number").toString();
    info.cake_float = obj.value("cake_float").toString();
    info.box_float = obj.value("box_float").toString();
    info.box_rule = obj.value("box_rule").toString();
    info.customer = obj.value("customer").toString();
    info.remark = obj.value("remark").toString();
    info.is_disable  = obj.value("is_disable").toBool();
    info.create_time = obj.value("create_time").toInt();
    info.modified_time = obj.value("modified_time").toInt();

//    currtInfo.append(info);

}

void SelectValueForm::refPage(int page, int pageListNum)
{
    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(0);
    for(int i = 0; i < pageListNum; ++i){
        if(page*20+i>=prolist.size()){
            break;
        }
        auto & v = prolist.at(page*20+i);
        ui->tableWidget->insertRow(i);
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(v->batch_no));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(v->specs));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(v->product_type_code));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(v->category));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(v->machine_no));
        ui->tableWidget->setItem(i,5,new QTableWidgetItem(v->bucket_color));
        ui->tableWidget->setItem(i,6,new QTableWidgetItem(v->twist_type));
        ui->tableWidget->setItem(i,7,new QTableWidgetItem(v->color));
        ui->tableWidget->setItem(i,8,new QTableWidgetItem(v->goods_id>=0?"已同步":"未同步"));
        if(v->goods_id<0){
            ui->tableWidget->item(i,8)->setBackground(QBrush(QColor(255,85,0,100)));
        }else{
            ui->tableWidget->item(i,8)->setBackground(QBrush(QColor(85,255,0,100)));
        }
    }
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);    //根据tablewidget宽度平均分配宽度
//    ui->tableWidget->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);//根据内容适应宽度

    totlePage = totleNum / pageListNum;
    if(totleNum % pageListNum > 0)
        totlePage ++;
    this->ui->labelPages->setText(QString("%1/%2").arg(nowPage+1).arg(totlePage));
}

void SelectValueForm::on_pushPrev_clicked()
{
    if(nowPage > 0) {
        nowPage --;
        refPage(nowPage);
    }

}

void SelectValueForm::on_pushNext_clicked()
{
    if(nowPage < totlePage-1){
        nowPage ++;
        refPage(nowPage);
    }
}

void SelectValueForm::on_pushButton_2_clicked()
{
    int now = ui->tableWidget->currentRow();
    if(now<0){
        QMessageBox::information(this,tr("注意"),tr("请先选择一个数据"));
        return;
    }
    if(ui->tableWidget->item(now,8)->text()=="未同步"){
        QMessageBox::information(this,tr("注意"),tr("请先同步批号"));
        return;
    }
        emit selectProject(ui->tableWidget->item(now,0)->text());

}

void SelectValueForm::on_searchButton_clicked()
{
//    UserInfo user = GlobalInfo::this_()->user();
//    bool isNoLine = GlobalInfo::this_()->isNoLine();
//    GlobalInfo::this_()->start(user,isNoLine);
//    qDebug()<<user.userName<<user.accountId<<user.acessToken;
    auto batch = ui->batch_no->text();
    auto specs = ui->specs->text();
    if(batch.isEmpty()&&specs.isEmpty()){
//        updateTableWidget(prolist);
        prolist = GlobalInfo::this_()->project().values();
        totleNum = prolist.size();
        refPage(nowPage);
        ui->labelTotle->setText(QString::number(totleNum));
    } else {
        auto list = GlobalInfo::this_()->searchProject(batch,specs);
//        updateTableWidget(list);
        prolist = list;
        totleNum = prolist.size();
        refPage(nowPage);
        ui->labelTotle->setText(QString::number(totleNum));
    }
}

void SelectValueForm::on_backMainButton_clicked()
{
    emit back();
}

void SelectValueForm::on_back_Button_clicked()
{
    emit backFandU();
}

void SelectValueForm::on_closeButton_clicked()
{
    ui->batch_no->clear();
    ui->specs->clear();
    on_searchButton_clicked();
}

void SelectValueForm::on_RefreshButton_clicked()
{
    GlobalInfo::this_()->selectProject(false);
}

//同步按钮
void SelectValueForm::on_synButtong_clicked()
{
    for(int i = 0;i<prolist.size();i++){
        if(prolist.at(i)->goods_id<0){
            proinfo->addNewProject(prolist.at(i));
        }
    }
}
