#include "uncertainweightpackform.h"
#include "ui_uncertainweightpackform.h"
#include <pqQtlib/utils/pqfileutils.h>


using namespace PQ;

UncertainWeightPackForm::UncertainWeightPackForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UncertainWeightPackForm),packServer(nullptr),Manger(nullptr)
{
    ui->setupUi(this);
    autoRead = true;
    QTimer *timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,&UncertainWeightPackForm::calcData);
    timer->start(500);
    ui->widget_6->setEnabled(false);
//    ui->tongshuguding->setEnabled(false);
//    //筒数限制，true限制
    istubeNumTure = false;
//    ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
//    ui->pushStart->hide();

    connect(glo,&GlobalInfo::dbConfigUpdated,this,&UncertainWeightPackForm::upconfig);
    connect(glo,&GlobalInfo::dengJiUped,this,&UncertainWeightPackForm::upDengjiInfo);
    connect(glo,&GlobalInfo::doInited,this,&UncertainWeightPackForm::changeSpecs);
    connect(glo,&GlobalInfo::selectproject,this,&UncertainWeightPackForm::changeSpecs);
    connect(glo,&GlobalInfo::wareUped,this,&UncertainWeightPackForm::upWare);
    connect(glo,&GlobalInfo::boxModelUped,this,&UncertainWeightPackForm::upBoxModelList);
    connect(glo,&GlobalInfo::packModelUpde,this,&UncertainWeightPackForm::upPackModelList);

    auto dt = CacheFile::readFile("budingInfo.cache");
    auto list = QString::fromUtf8(dt).split("[,]");
    if(list.size() == 2){
        ui->taitou->setText(list.at(0));
        ui->taiPhone->setText(list.at(1));
    }
    ui->autoSync->setChecked(true);
    ui->buding->setChecked(true);
//    ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    setShortcutConfig();
}

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

//从秤中获取毛重、皮重、净重、筒数------每100毫秒一次
void UncertainWeightPackForm::calcData()
{
    double pz = ui->pizhong->currentText().toDouble();
    double tz = ui->tongzhong->currentText().toDouble();
    double ts = ui->tongshu->text().toDouble();
    gross_weight = ui->grossWeight->text().toDouble();
    if(ui->autoRead->isChecked()){
        ui->grossWeight->setText(QString::number(GlobalInfo::this_()->currentWidght));
    }
//    qDebug()<<autoRead;
//    netNum = ui->grossWeight->text().toDouble()-ui->pizhong->currentText().toDouble()-ui->tongshu->text().toDouble()*ui->tongzhong->currentText().toDouble();
    if(ui->showMaoZhong->isChecked()){
        gross_weight = floor((ui->grossWeight->text().toDouble()+chengZhongJingDuModel_Num)*100)/100;
        netNum = floor(((ui->grossWeight->text().toDouble() - pz - tz * ts)+chengZhongJingDuModel_Num)*100)/100;

        f_gross_weight = float(floor((gross_weight+f_chengZhongJingDuModel_Num)*10))/10;
        f_netNum = float(floor((netNum+f_chengZhongJingDuModel_Num)*10))/10;
    } else {
        netNum = floor((ui->grossWeight->text().toDouble()+chengZhongJingDuModel_Num)*100)/100;
        gross_weight = floor(((ui->grossWeight->text().toDouble() + pz + tz*ts)+chengZhongJingDuModel_Num)*100)/100;

        f_gross_weight = float(floor((gross_weight+f_chengZhongJingDuModel_Num)*10))/10;
        f_netNum = float(floor((netNum+f_chengZhongJingDuModel_Num)*10))/10;
    }

    if(istubeNumTure){
        if(ui->tongshu->text().toInt()>tubeNumMax || ui->tongshu->text().toInt()<tubeNumMin){
            ui->tongshu->setValue(6);
        }
    }
//    ui->netWeight_2->setText(QString::number(netNum));
//    ui->tongshu->setText(QString::number(GlobalInfo::this_()->GetScaleData().tongshu));
}

void UncertainWeightPackForm::save()
{
    QStringList list;
    list << ui->taitou->text() <<  ui->taiPhone->text();
    PQ::CacheFile::writeFile("budingInfo.cache",list.join("[,]").toUtf8());

    if(!packinfo.isNull()){
        Manger->savePackInfo(packinfo);
    }
}

void UncertainWeightPackForm::on_addPackList_clicked()
{
    if(packinfo.isNull()) return;
    if(!packinfo.isNull()&&packinfo->boxes().size()>0){
        packinfo->quantity = packinfo->boxes().size();
        packinfo->level_id = ui->dengji->currentData().toInt();
        packinfo->machine_no = ui->jitai->currentText();
        packinfo->carton_type = ui->zhixiang->currentText();
        packinfo->customer = ui->taitou->text();
        packinfo->telephone = ui->taiPhone->text();
    }
//    qDebug()<<packinfo->boxes().at(0)->tube_number;
    packinfo->warehouse_id = 0;
    if(ui->ruku->isChecked()){
        packinfo->warehouse_id = ui->wareBox->currentData().toInt();
    }

    if(ui->autoSync->isChecked() && !GlobalInfo::this_()->isNoLine())
        packServer->send(packinfo);
    Manger->savePackInfo(packinfo);
//    GlobalInfo::this_()->packInfoManger()->setPackInfoSync(packinfo->codeSingle(),packinfo->time(),true);
    if(ui->madanPrint->isChecked()){
        if(ui->madan->currentIndex() == 0){
            GlobalInfo::this_()->printerPack(packinfo);
        }else{
            GlobalInfo::this_()->printerPackModel(packinfo,ui->madan->currentIndex()-1);
        }
    }

    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(0);

//    ui->widget_2->setEnabled(true);

    packinfo.clear();
}

void UncertainWeightPackForm::on_addBoxList_clicked()
{
    if(ui->grossWeight->text().toDouble()<=0){
        QMessageBox::information(this,tr("注意"),tr("重量异常"));
        return;
    }
    if(ui->tongshu->text().toInt()<=0){
        QMessageBox::information(this,tr("注意"),tr("筒数异常"));
        return;
    }

    if(netNum<=0){
//        netNum = 0.00;
        QMessageBox::information(this,tr("注意"),tr("重量异常"));
        return;
    }

    checkPackInfo(); //检查packinfo isNull
    //当当前箱数大于等于最大箱数是启动on_addPackList_clicked();
    if(packinfo->boxes().size() >= ui->mashu->currentText().toInt()){
        on_addPackList_clicked();
//        checkPackInfo();
//        return;
    }
    checkPackInfo(); //检查packinfo isNull
    auto index = packinfo->boxInfoSize();
    auto box = packinfo->addBoxInfo();
    calcData();
    box->box_weight = int(ui->pizhong->currentText().toDouble()*1000);
    box->tube_number = ui->tongshu->text().toInt();
    box->bucket_weight = int(floor((ui->tongzhong->currentText().toDouble()*ui->tongshu->text().toDouble()+0.005)*100)*10);
    if(chengZhongJingDu == 2){
        box->gross_weight = QString::number(gross_weight*1000).toInt();
        box->net_weight = QString::number(netNum*1000).toInt();
    }else if(chengZhongJingDu == 1){
        box->gross_weight = QString::number(f_gross_weight*1000).toInt();
        box->net_weight = QString::number(f_netNum*1000).toInt();
    }
    box->Level = ui->dengji->currentText();
    box->twist_type = ui->nianxiang->currentText();
    box->bucket_color = ui->guanse->currentText();
    box->sort = "1";
//    box->remark = ui->beizhu->toPlainText();
    packinfo->level = ui->dengji->currentText();
    packinfo->level_id = ui->dengji->currentData().toInt();
//    packinfo->warehouse_id = ui->wareBox->currentData().toInt();
    packinfo->autoRuku = ui->ruku->isChecked();

    addTableWidget(box,index);


//    把box数据添加到packinfo的boxes数据表里；
    packinfo->addBoxInfoList(box);
    if(ui->xiangdanPrint->isChecked()){
        if(ui->xiangdan->currentText() == "默认箱单"){
            GlobalInfo::this_()->printerBox(box);
        }else if(ui->xiangdan->currentText() == "默认箱单2"){
            GlobalInfo::this_()->printerBox1(box);
        }else{
            GlobalInfo::this_()->printerBoxModel(box,ui->xiangdan->currentIndex()-1);
        }
    }
    upSum();
        //添加数据库
    //    aputils.insertDetailsSQL(box,packinfo->codeSingle());

}

void UncertainWeightPackForm::upSum()
{
    if(packinfo.isNull()){
        ui->labelTongZHong->setText("0");
        ui->labelWight->setText("0");
        ui->labelJing->setText("0");
    } else {
        ui->labelTongZHong->setText(QString::number(packinfo->tube_number()));
        ui->labelWight->setText(getDecimalbit(packinfo->gross_weight() / 1000.00));
        ui->labelJing->setText(getDecimalbit(packinfo->net_weight() / 1000.00));
    }
}

//自动读数按钮
//void UncertainWeightPackForm::on_handleRead_2_clicked()
//{
//    if(autoRead==true){
//        autoRead = false;
//        ui->handleRead_2->setText("自动读数");
//        ui->label->setText("已开启手动");
//        return;
//    }
//    autoRead = true;
//    ui->handleRead_2->setText("手动读数");
//    ui->label->setText("已开启自动");
//}

//筒数是否固定
void UncertainWeightPackForm::on_tongshuguding_currentIndexChanged(const QString &arg1)
{
    if(arg1=="固定"){
        ui->widget_6->setEnabled(false);
        return;
    }
    ui->widget_6->setEnabled(true);
}

//筒数是否限制
void UncertainWeightPackForm::on_tongshuxianzhi_stateChanged(int arg1)
{
//    qDebug()<<arg1;

    if(arg1 == 2){
        istubeNumTure = true;
        return;
    }
    istubeNumTure = false;
}

//检查packinfo isNull
void UncertainWeightPackForm::checkPackInfo()
{
    if(packinfo.isNull()){
        packinfo.reset(new PackInfo(_info));
        packinfo->operator_id = GlobalInfo::this_()->user().userId;
        packinfo->packing_time = QDate::currentDate().toString("yyyy-MM-dd");
        packinfo->packing_type = _info->category;
        packinfo->packType = 0;

        packinfo->batch_no = _info->batch_no;
        packinfo->quantity = packinfo->boxes().size();
        packinfo->level_id = ui->dengji->currentData().toInt();
        packinfo->machine_no = ui->jitai->currentText();
        packinfo->carton_type = ui->zhixiang->currentText();
        packinfo->customer = ui->taitou->text();
        packinfo->telephone = ui->taiPhone->text();

        restTableWidget();
    }
}

void UncertainWeightPackForm::addTableWidget(QSharedPointer<BoxInfo> box, int row)
{
    ui->tableWidget->insertRow(row);

    ui->tableWidget->setItem(row,0,new QTableWidgetItem(box->caseNumber()));
    ui->tableWidget->setItem(row,1,new QTableWidgetItem(QString::number(box->tube_number)));
    ui->tableWidget->setItem(row,2,new QTableWidgetItem(QString::number(box->gross_weight / 1000.00)));
    ui->tableWidget->setItem(row,3,new QTableWidgetItem(ui->tongzhong->currentText()));
    ui->tableWidget->setItem(row,4,new QTableWidgetItem(box->bucket_color));
    ui->tableWidget->setItem(row,5,new QTableWidgetItem(QString::number(box->net_weight / 1000.00)));
    ui->tableWidget->item(row,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    ui->tableWidget->item(row,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    ui->tableWidget->item(row,2)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    ui->tableWidget->item(row,3)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    ui->tableWidget->item(row,4)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    ui->tableWidget->item(row,5)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);

    QWidget *tmp_widget = new QWidget();
    QHBoxLayout *tmp_layout = new QHBoxLayout(tmp_widget);
    auto btn_1 = new QPushButton("删除",tmp_widget);
    btn_1->setMaximumWidth(40);
    auto btn_2 = new QPushButton("修改",tmp_widget);
    btn_2->setMaximumWidth(40);
    auto btn_3 = new QPushButton("补打",tmp_widget);
    btn_3->setMaximumWidth(40);
    tmp_layout->addWidget(btn_1);
    tmp_layout->addWidget(btn_2);
    tmp_layout->addWidget(btn_3);
    tmp_layout->setMargin(0);
    int i = row;
    connect(btn_1,&QPushButton::clicked,[i,this](){this->del_clicked(i);});
    connect(btn_2,&QPushButton::clicked,[i,this](){this->edit_clicked(i);});
    connect(btn_3,&QPushButton::clicked,[i,this](){this->Makeup_clicked(i);});
    ui->tableWidget->setCellWidget(row,6,tmp_widget);

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);    //x先自适应宽度
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(6, QHeaderView::ResizeToContents);
}

void UncertainWeightPackForm::init()
{
    if(packServer == nullptr){
        Manger = GlobalInfo::this_()->packInfoManger();
        packServer = new PackInfoSeed2Server(Manger,this,true);
    }
    ui->pizhong->setCurrentText(QString(_info->box_weight));
    ui->guanse->setCurrentText(_info->bucket_color);
    ui->zhixiang->setCurrentText(_info->carton_type);
    ui->tongzhong->setCurrentText(_info->bucket_weight);
    ui->nianxiang->setCurrentText(_info->twist_type);

    if(!_info->customer.isEmpty()){
        ui->taitou->setText(_info->customer);
    }
    if(!_info->tel.isEmpty()){
        ui->taiPhone->setText(_info->tel);
    }
    restTableWidget();
}

void UncertainWeightPackForm::upconfig()
{
    // TODO：打包配置更新
    DaBaoConfig config = glo->dbConfig();

    ui->autoRead->setChecked(config.ziDongDushu);

    //称重精度
    chengZhongJingDu = config.chengZhongJIngDu;

    //称重精度方式//4:四舍五入 5：二舍三入 6：一舍二入
    chengZhongJingDuModel = config.chengZhongJingDuMode;
    switch (chengZhongJingDuModel) {
        case 4:
            chengZhongJingDuModel_Num = 0.005;
            f_chengZhongJingDuModel_Num = 0.05;
            break;
        case 5:
            chengZhongJingDuModel_Num = 0.007;
            f_chengZhongJingDuModel_Num = 0.07;
            break;
        case 6:
            chengZhongJingDuModel_Num = 0.008;
            f_chengZhongJingDuModel_Num = 0.08;
            break;
    }
//    qDebug()<<"称重精度数据"<<chengZhongJingDuModel_Num;

    if(config.morenTongZhongGUDing){
        ui->tongshuguding->setCurrentText("固定");
    }
    // 打包及入库
    ui->ruku->setChecked(config.daBaoRuku);
    // 码数
    ui->mashu->clear();
    for(int i = 0; i < config.mashu.size(); ++i){
        ui->mashu->addItem(QString::number(config.mashu.at(i)));
    }
    //机台
    ui->jitai->clear();
    ui->jitai->addItems(config.jitai);

    //打包工班次
    ui->dabaobanci->clear();
    ui->dabaobanci->addItems(config.daBaoGongBanCi);

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

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

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

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

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

    tubeNumMin = config.buDingZhongTongShuFanWei.first;
    tubeNumMax = config.buDingZhongTongShuFanWei.second;

}
void UncertainWeightPackForm::upDengjiInfo()
{
// TODO： 等级信息更新
    QList<DengJiConfigItem> dengJis = glo->dengJiList();
    ui->dengji->clear();
    for(int i = 0;i<dengJis.size();i++){
        if(!dengJis.at(i).isEnable){
            ui->dengji->addItem(dengJis.at(i).value,dengJis.at(i).id);
        }
    }
}


void UncertainWeightPackForm::upWare()
{
    QList<WarehouseConfig> ware = glo->wareList();
    for(int i = 0;i<ware.size();i++){
        ui->wareBox->addItem(ware.at(i).name,ware.at(i).warehouse_id);
    }
}

void UncertainWeightPackForm::upBoxModelList()
{
    ui->xiangdan->clear();
    ui->xiangdan->addItem("默认箱单");
    ui->xiangdan->addItem("默认箱单2");
    QList<QSharedPointer<BoxModel>> boxList = glo->BoxModelList();
    for(int i = 0;i<boxList.size();i++){
        ui->xiangdan->addItem(boxList.at(i)->Model_Name);
    }
}

void UncertainWeightPackForm::upPackModelList()
{
    ui->madan->clear();
    ui->madan->addItem("默认码单");
    QList<QSharedPointer<PackModel>> packList = glo->PackModelList();
    for(int i = 0;i<packList.size();i++){
        ui->madan->addItem(packList.at(i)->Model_Name);
    }
}

//获取品目信息
void UncertainWeightPackForm::on_searchButton_clicked()
{
    auto str = ui->lineEdit->text();
    if(str.isEmpty()){
        updateTableWidget(prolist);
    } else {
        auto list = GlobalInfo::this_()->searchProject(str);
        updateTableWidget(list);
    }
}

void UncertainWeightPackForm::changeSpecs()
{
    prolist = GlobalInfo::this_()->project().values();
    updateTableWidget(prolist);
}

void UncertainWeightPackForm::updateTableWidget(QList<QSharedPointer<ProjectInfo>> & list)
{
    ui->tableWidget_2->clearContents();
    ui->tableWidget_2->setRowCount(0);


    ui->tableWidget_2->setColumnWidth(0,40);
    ui->tableWidget_2->setColumnWidth(1,80);
    ui->tableWidget_2->setColumnWidth(2,80);
//    ui->tableWidget_2->setColumnWidth(3,70);
    ui->tableWidget_2->setColumnWidth(4,40);
    ui->tableWidget_2->horizontalHeader()->setStretchLastSection(true);


    int currt = -1;
    for(int i = 0; i < list.size(); ++i){
        auto & v = list.at(i);
        if(v->goods_id<0){
            continue;
        }
//        ui->tableWidget_2->insertRow(i);
        ui->tableWidget_2->setRowCount(ui->tableWidget_2->rowCount()+1);
        auto xuanze_btn =  new QRadioButton();
        xuanze_btn->setMaximumWidth(20);
        QString batchno = prolist.at(i)->batch_no;
        connect(xuanze_btn,&QPushButton::clicked,[batchno,this](){this->xuanZe_clicked(batchno);});
        int row = ui->tableWidget_2->rowCount()-1;
        ui->tableWidget_2->setCellWidget(row,0,xuanze_btn);
        ui->tableWidget_2->setItem(row,1,new QTableWidgetItem(v->specs));
        ui->tableWidget_2->setItem(row,2,new QTableWidgetItem(v->category));
        ui->tableWidget_2->setItem(row,3,new QTableWidgetItem(v->batch_no));
        ui->tableWidget_2->setItem(row,4,new QTableWidgetItem(v->color));
        if(!_info.isNull() && _info->batch_no == v->batch_no){
            xuanze_btn->setChecked(true);
            currt = i;
        }
    }
    if(currt >= 0)
        ui->tableWidget_2->setCurrentCell(currt,0);
}

void UncertainWeightPackForm::setProInfo(const QSharedPointer<ProjectInfo> & info)
{
    _info = info;
    if(!packinfo.isNull()){
        packinfo->upInfo(_info);
        packinfo->packing_type = _info->category;
    }
    init();
    EditBatch();
}

void UncertainWeightPackForm::xuanZe_clicked(QString batchno)
{
    save();
    packinfo.clear();
    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(0);
//    ui->tableWidget->clear();
    setProInfo(GlobalInfo::this_()->project().value(batchno));
}

void UncertainWeightPackForm::del_clicked(int row)
{
    if(packinfo.isNull()) return;
//    qDebug()<<row;
    packinfo->delBoxInfoAt(row);
    restTableWidget();
}

void UncertainWeightPackForm::edit_clicked(int row)
{
    if(packinfo.isNull()) return;
    auto box =  packinfo->boxInfoAt(row);
    EditBoxInfoDialog dig(box,this);
    dig.exec();
    restTableWidget();
}

void UncertainWeightPackForm::Makeup_clicked(int row)
{
    if(packinfo.isNull()) return;
    auto box =  packinfo->boxInfoAt(row);
    GlobalInfo::this_()->printerBox(box);
}

void UncertainWeightPackForm::restTableWidget()
{
    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(0);
//    ui->tableWidget->setColumnWidth(0,200);
//    ui->tableWidget->setColumnWidth(1,75);
//    ui->tableWidget->setColumnWidth(2,75);
//    ui->tableWidget->setColumnWidth(3,75);
//    ui->tableWidget->setColumnWidth(4,75);
//    ui->tableWidget->setColumnWidth(5,75);

//    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);

    if(!packinfo.isNull()){
        for(int i =  0; i < packinfo->boxInfoSize(); ++i){
            addTableWidget(packinfo->boxInfoAt(i),i);
        }
    }
    upSum();
}

void UncertainWeightPackForm::on_pushContinue_clicked()
{
    auto dt = QDate::currentDate();
    auto list = Manger->getPackInfoSync(dt);
    if(list.isEmpty()){
        dt = dt.addDays(-1);
        list = Manger->getPackInfoSync(dt);
    }
    if(list.isEmpty()){
        QMessageBox::warning(this,tr("无可接续码单"),tr("无可接续码单"));
        return;
    }
    QList<PackSaveInfo>Plist;
    for(int i = 0;i<list.size();i++){
        if(list.at(i).packInfo->batch_no==_info->batch_no){
            Plist.append(list.at(i));
        }
    }
    std::sort(Plist.begin(),Plist.end(),[](const PackSaveInfo & item1, const PackSaveInfo & item2){
        return item1.packInfo->time() < item2.packInfo->time();
    });
    if(Plist.isEmpty()){
        QMessageBox::warning(this,tr("无可接续码单"),tr("无可接续码单"));
        return;
    }
    QSharedPointer<PackInfo> pInfo;
    for (int i = Plist.size() - 1;i >= 0 ; --i) {
        auto & v = Plist.at(i);
        if(v.packInfo->packType != 0) continue;
        if(v.packInfo->boxInfoSize() < ui->mashu->currentText().toInt()){
            pInfo = v.packInfo;
            break;
        }
    }
    if(pInfo.isNull()){
        QMessageBox::warning(this,tr("无可接续码单"),tr("无可接续码单"));
    }
    packinfo = pInfo;
    setProInfo(packinfo->info());
}

void UncertainWeightPackForm::on_showMaoZhong_stateChanged(int)
{
    if(ui->showMaoZhong->isChecked()){
        ui->labelWidget->setText(tr("毛重"));
    } else {
        ui->labelWidget->setText(tr("净重"));
    }
}

void UncertainWeightPackForm::on_pushEditBatch_clicked()
{
    emit backup(1);
}

void UncertainWeightPackForm::EditBatch()
{
    if(packinfo.isNull()) return;
    for(int i =  0; i < packinfo->boxInfoSize(); ++i){
        auto box = packinfo->boxInfoAt(i);
        calcData();
        box->box_weight = int(ui->pizhong->currentText().toDouble()*1000);
        box->tube_number = ui->tongshu->text().toInt();
        box->bucket_weight = int(floor((ui->tongzhong->currentText().toDouble()*box->tube_number+0.005)*100)*10);
        int net = box->gross_weight-(ui->pizhong->currentText().toDouble()+box->tube_number*ui->tongzhong->currentText().toDouble())*1000;
//        qDebug()<<box->gross_weight;
        if(chengZhongJingDu==2){
            box->net_weight = net;
        }else if(chengZhongJingDu == 1){
            box->net_weight = floor(((net/1000.00)+0.05)*10)*100;
        }
        box->Level = ui->dengji->currentText();
        box->twist_type = ui->nianxiang->currentText();
        box->bucket_color = ui->guanse->currentText();
        box->sort = "1";
//        box->remark = ui->beizhu->toPlainText();
    }
    packinfo->level = ui->dengji->currentText();
    packinfo->level_id = ui->dengji->currentData().toInt();
    restTableWidget();
}

void UncertainWeightPackForm::on_pushNoSyncList_clicked()
{
    emit NoSyncList();
}

void UncertainWeightPackForm::on_pushSync_clicked()
{
    if(GlobalInfo::this_()->isNoLine()){
        QMessageBox::information(this,tr("注意"),tr("离线模式不予同步"));
        return;
    }
    QDate tm = QDate::currentDate();
    GlobalInfo::this_()->doSync(tm);
    QMessageBox::information(this,tr("注意"),tr("同步成功"));
}

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

void UncertainWeightPackForm::on_dingzhong_clicked()
{
    emit dingzhong();
    ui->buding->setChecked(true);
}

void UncertainWeightPackForm::on_ruku_stateChanged(int arg1)
{
    if(arg1 == 2){
        isRuku = true;
        return;
    }
    isRuku = false;
}

void UncertainWeightPackForm::on_useNewDataButton_clicked()
{
    if(packinfo.isNull()) return;
    for(int i =  0; i < packinfo->boxInfoSize(); ++i){
        QSharedPointer<BoxInfo> box = packinfo->boxInfoAt(i);
        calcData();
        box->box_weight = int(ui->pizhong->currentText().toDouble()*1000);
        box->tube_number = ui->tongshu->text().toInt();
        box->bucket_weight = int(floor((ui->tongzhong->currentText().toDouble()*box->tube_number+0.005)*100)*10);
        int net = box->gross_weight-(ui->pizhong->currentText().toDouble()+box->tube_number*ui->tongzhong->currentText().toDouble())*1000;
        if(chengZhongJingDu==2){
            box->net_weight = net;
        }else if(chengZhongJingDu == 1){
            box->net_weight = floor(((net/1000.00)+0.05)*10)*100;
        }
        box->Level = ui->dengji->currentText();
        box->twist_type = ui->nianxiang->currentText();
        box->bucket_color = ui->guanse->currentText();
        box->sort = "1";
    }
    packinfo->level = ui->dengji->currentText();
    packinfo->level_id = ui->dengji->currentData().toInt();
    packinfo->customer = ui->taitou->text();
    packinfo->telephone = ui->taiPhone->text();
    packinfo->autoRuku = ui->ruku->isChecked();
//    packinfo->warehouse_id = ui->wareBox->currentData().toInt();
    restTableWidget();
}


void UncertainWeightPackForm::setShortcutConfig()
{
    QSettings configIniWrite("jcxclient.ini", QSettings::IniFormat);
    QVariant key4 = configIniWrite.value("Shortcut/key1", "F1");
    QVariant key5 = configIniWrite.value("Shortcut/key2", "F2");

    ui->addBoxList->setShortcut(QKeySequence(key4.toString()));
    ui->addPackList->setShortcut(QKeySequence(key5.toString()));
}
