#include "addnewspecsform.h"
#include "ui_addnewspecsform.h"
#include "QMessageBox"

AddNewSpecsForm::AddNewSpecsForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddNewSpecsForm)
{
    ui->setupUi(this);
    connect(glo,&GlobalInfo::dbConfigUpdated,this,&AddNewSpecsForm::init);
    connect(&request,&HttpRequest::result,this,&AddNewSpecsForm::result);
    ui->tiJiao_text->hide();
    ui->label_18->hide();
    ui->bucket_number->hide();
}

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

void AddNewSpecsForm::init()
{
    DaBaoConfig config = glo->dbConfig();

    ui->product_type_code->clear();
    ui->product_type_code->addItems(config.pinZhong);
    //机台
    ui->machine_no->clear();
    ui->machine_no->addItems(config.jitai);

    //皮重
    ui->box_weight->clear();
    for(int i = 0;i<config.xiangzhong.size();i++){
        ui->box_weight->addItem(QString::number(config.xiangzhong.at(i)));
    }

    //管色
    ui->bucket_color->clear();
    ui->bucket_color->addItems(config.guanse);

    //捻向
    ui->twist_type->clear();
    ui->twist_type->addItems(config.nianxiang);

    //纸箱
    ui->carton_type->clear();
    ui->carton_type->addItems(config.zhixiang);

    //筒重
    ui->bucket_weight->clear();
    for(int i = 0;i<config.tongZhong.size();i++){
        ui->bucket_weight->addItem(QString::number(config.tongZhong.at(i)));
    }
}

void AddNewSpecsForm::result(int code, const QJsonObject & body)
{
    ui->tiJiao_text->hide();
    qDebug()<<body;
    ui->StartButton->setEnabled(true);
    if(code == 200 && body.value("message") == ""){
        QMessageBox::information(this,tr("提示"),tr("保存成功"));
        return;
    }
    QMessageBox::information(this,tr("提示"),tr("保存失败"));
}

void AddNewSpecsForm::projectData(){
    project.reset(new ProjectInfo);
    project->batch_no = ui->batch_no->text();
    project->category = ui->category->text();
    project->product_type_code = ui->product_type_code->currentText();
    project->spec_role = ui->spec_role->text();
    project->denier = ui->denier->text();
    project->dtex = ui->dtex->text();
    project->fiber = ui->fiber->text();
    project->specs = ui->specs->text();
    project->color = ui->color->text();
    project->twist_type = ui->twist_type->currentText();
    project->bucket_color = ui->bucket_color->currentText();
    project->box_weight = ui->box_weight->currentText();
    project->bucket_weight = ui->bucket_weight->currentText();
    project->machine_no = ui->machine_no->currentText();
    project->carton_type = ui->carton_type->currentText();
    project->bucket_number = ui->bucket_number->text();
    project->customer = ui->customer->text();
    project->remark = ui->remark->toPlainText();
    project->tel = ui->tel->text();
}

void AddNewSpecsForm::on_StartButton_clicked()
{

    if(ui->batch_no->text().isEmpty()){
        QMessageBox::information(this,tr("注意"),tr("批号为空"));
        ui->StartButton->setEnabled(true);
        return;
    }

    if(ui->category->text().isEmpty()){
        QMessageBox::information(this,tr("注意"),tr("类型为空"));
        ui->StartButton->setEnabled(true);
        return;
    }

    if(ui->denier->text().isEmpty()){
        QMessageBox::information(this,tr("注意"),tr("旦数为空"));
        ui->StartButton->setEnabled(true);
        return;
    }

    if(ui->dtex->text().isEmpty()){
        QMessageBox::information(this,tr("注意"),tr("分特为空"));
        ui->StartButton->setEnabled(true);
        return;
    }

    ui->tiJiao_text->show();
    ui->StartButton->setEnabled(false);
    projectData();
    if(GlobalInfo::this_()->isNoLine()){
        if(GlobalInfo::this_()->addCacheInfo(project)){
            QMessageBox::information(this,tr("成功"),tr("离线保存成功"));
            ui->StartButton->setEnabled(true);
            ui->tiJiao_text->hide();
            return;
        }
        QMessageBox::information(this,tr("失败"),tr("离线保存失败"));
        ui->StartButton->setEnabled(true);
        ui->tiJiao_text->hide();
        return;
    }

    QString url = GlobalInfo::this_()->config()->baseUrl+"/v2/goods/add";
    request.setUserToken(QString::number(GlobalInfo::this_()->user().accountId),GlobalInfo::this_()->user().acessToken);
    request.post(url,project->toObject());
}

void AddNewSpecsForm::on_backButton_clicked()
{
    emit back();
}

void AddNewSpecsForm::on_backButton_2_clicked()
{
    emit back();
}
