#include "fixedweightpackform.h"
#include "editboxinfodialog.h"
#include "ui_fixedweightpackform.h"
#include <pqQtlib/utils/pqfileutils.h>

using namespace PQ;

FixedWeightPackForm::FixedWeightPackForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FixedWeightPackForm)
    , packServer(nullptr)
    , Manger(nullptr)
{
    ui->setupUi(this);
    //    QTimer *timer = new QTimer(this);
    //    connect(timer,&QTimer::timeout,this,&FixedWeightPackForm::calcData);
    //    timer->start(500);
    connect(glo, &GlobalInfo::dbConfigUpdated, this, &FixedWeightPackForm::upconfig);
    connect(glo, &GlobalInfo::dengJiUped, this, &FixedWeightPackForm::upDengjiInfo);
    connect(glo, &GlobalInfo::doInited, this, &FixedWeightPackForm::changeSpecs);
    connect(glo, &GlobalInfo::selectproject, this, &FixedWeightPackForm::changeSpecs);
    connect(glo, &GlobalInfo::wareUped, this, &FixedWeightPackForm::upWare);
    connect(glo, &GlobalInfo::boxModelUped, this, &FixedWeightPackForm::upBoxModelList);
    connect(glo, &GlobalInfo::packModelUpde, this, &FixedWeightPackForm::upPackModelList);

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

    //初始化快捷键
    setShortcutConfig();
}

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

//从秤中获取毛重、皮重、净重、筒数------每100毫秒一次
void FixedWeightPackForm::calcData()
{
    double pz = ui->pizhong->currentText().toDouble();
    double tz = ui->tongzhong->currentText().toDouble();
    double ts = ui->tongshu->text().toDouble();
    if (ui->showMaoZhong->isChecked()) {
        gross_weight = floor((ui->grossWeight_2->text().toDouble() + chengZhongJingDuModel_Num)
                             * 100)
                       / 100;
        netNum = floor(((ui->grossWeight_2->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_2->text().toDouble() + chengZhongJingDuModel_Num) * 100)
                 / 100;
        gross_weight = floor(((ui->grossWeight_2->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 (netNum < 0) {
        netNum = 0.00;
        f_netNum = 0.0;
    }
}

void FixedWeightPackForm::save()
{
    QStringList list;

    list << ui->taitou->text() << ui->taiPhone->text() << ui->lineEdit_address->text();
    PQ::CacheFile::writeFile("dingzhongInfo.cache", list.join("[,]").toUtf8());

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

void FixedWeightPackForm::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();
        packinfo->address = ui->lineEdit_address->text();
    }

    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 FixedWeightPackForm::on_addBoxList_clicked()
{
    if (ui->grossWeight_2->text().toDouble() <= 0) {
        QMessageBox::information(this, tr("注意"), tr("重量异常"));
        return;
    }
    if (ui->tongshu->text().toInt() <= 0) {
        QMessageBox::information(this, tr("注意"), tr("筒数异常"));
        return;
    }
    calcData();

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

    checkPackInfo(); //检查packinfo isNull
    //当当前箱数大于等于最大箱数是启动on_addPackList_clicked();
    if (packinfo->boxes().size() >= ui->comboBox->currentText().toInt()) {
        on_addPackList_clicked();
        //        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((ui->tongzhong->currentText().toDouble()*ui->tongshu->text().toDouble())*1000);
    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->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);
        }
    }

    //    if(ui->xiangdanPrint->isChecked()){
    //        if(ui->xiangdan->currentIndex()==0){
    //            GlobalInfo::this_()->printerBox(box);
    //        }
    //    }
    //    qDebug()<<ui->xiangdan->currentIndex();
    upSum();
    //添加数据库
    //    aputils.insertDetailsSQL(box,packinfo->codeSingle());
}

void FixedWeightPackForm::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));
    }
}

//检查packinfo isNull
void FixedWeightPackForm::checkPackInfo()
{
    if (packinfo.isNull()) {
        packinfo.reset(new PackInfo(_info));
        packinfo->operator_id = QString::number(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();
        packinfo->address = ui->lineEdit_address->text();

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

        restTableWidget();
    }
    if (packinfo->goodsID() < 0) {
        packinfo.clear();
    }
}

//往tablewidget里添加数据
void FixedWeightPackForm::addTableWidget(QSharedPointer<BoxInfo> box, int row)
{
    ui->nowCode->setText(packinfo->codeSingle());
    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();
    tmp_widget->setMaximumWidth(200);
    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->setSpacing(0);
    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->resizeColumnsToContents();
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); // x先自适应宽度
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(6, QHeaderView::ResizeToContents);
}

void FixedWeightPackForm::init()
{
    if (packServer == nullptr) {
        Manger = GlobalInfo::this_()->packInfoManger();
        packServer = new PackInfoSeed2Server(Manger, this, true);
    }
    ui->jitai->setCurrentText(_info->machine_no);
    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);
    }
    if (!_info->address.isEmpty()) {
        ui->lineEdit_address->setText(_info->address);
    }
    restTableWidget();
}

void FixedWeightPackForm::upconfig()
{

    DaBaoConfig config = glo->dbConfig();

    //称重精度
    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;
    }
    // 打包及入库
    ui->ruku->setChecked(config.daBaoRuku);
    // 码数
    ui->comboBox->clear();
    for (int i = 0; i < config.mashu.size(); ++i) {
        ui->comboBox->addItem(QString::number(config.mashu.at(i)));
    }
    //机台
    ui->jitai->clear();
    ui->jitai->addItems(config.jitai);

    //打包工班次
    ui->comboBox_2->clear();
    ui->comboBox_2->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)));
    }
}

void FixedWeightPackForm::upDengjiInfo()
{
    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 FixedWeightPackForm::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 FixedWeightPackForm::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);
    }
    //    ui->xiangdan->setCurrentText("默认箱单2");
}

void FixedWeightPackForm::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 FixedWeightPackForm::on_pushPiLiang_clicked()
{
    //    int tsize = 0;
    //    if(!packinfo.isNull()) tsize = packinfo->boxes().size();
    auto size = ui->spinBox->value();
    for (int i = 0; i < size; ++i) {
        on_addBoxList_clicked();
    }
}

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

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

void FixedWeightPackForm::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);
    //    ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    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();
        ui->tableWidget_2->setRowCount(ui->tableWidget_2->rowCount() + 1);
        auto xuanze_btn = new QRadioButton();
        QString batchno = prolist.at(i)->batch_no;
        //        qDebug()<<ui->tableWidget_2->rowCount()<<batchno;
        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 FixedWeightPackForm::setProInfo(const QSharedPointer<ProjectInfo> &info)
{
    _info = info;
    if (!packinfo.isNull()) {
        packinfo->upInfo(_info);
        packinfo->packing_type = _info->category;
    }
    init();
    EditBatch();
}

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

void FixedWeightPackForm::del_clicked(int row)
{
    if (packinfo.isNull())
        return;

    packinfo->delBoxInfoAt(row);
    restTableWidget();
}

void FixedWeightPackForm::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);
    //    ui->tableWidget->setColumnWidth(0,130);
    //    ui->tableWidget->setColumnWidth(1,45);
    //    ui->tableWidget->setColumnWidth(2,45);
    //    ui->tableWidget->setColumnWidth(3,45);
    //    ui->tableWidget->setColumnWidth(4,45);
    //    ui->tableWidget->setColumnWidth(5,45);
    //    ui->tableWidget->resizeColumnToContents(45);
    //    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    //    ui->tableWidget->resizeColumnToContents(10);
    //    ui->tableWidget->resizeColumnsToContents();
    //    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
    //    ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
    if (!packinfo.isNull()) {
        for (int i = 0; i < packinfo->boxInfoSize(); ++i) {
            addTableWidget(packinfo->boxInfoAt(i), i);
        }
    }
    upSum();
}

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

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

void FixedWeightPackForm::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->comboBox->currentText().toInt()) {
            pInfo = v.packInfo;
            break;
        }
    }
    if (pInfo.isNull()) {
        QMessageBox::warning(this, tr("无可接续码单"), tr("无可接续码单"));
    }
    packinfo = pInfo;
    setProInfo(packinfo->info());
}

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

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

void FixedWeightPackForm::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()*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();
        ////            qDebug()<<"boxNet"<<box->net_weight;
        ////            qDebug()<<"net"<<netNum*1000;
        //        }else if(chengZhongJingDu == 1){
        //            box->gross_weight = QString::number(f_gross_weight*1000).toInt();
        //            box->net_weight = QString::number(f_netNum*1000).toInt();
        ////            qDebug()<<"f_boxNet"<<box->net_weight;
        ////            qDebug()<<"f_net"<<f_netNum*1000;
        //        }
        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";
        //        box->remark = ui->beizhu->toPlainText();
    }
    packinfo->level = ui->dengji->currentText();
    packinfo->level_id = ui->dengji->currentData().toInt();
    restTableWidget();
}

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

void FixedWeightPackForm::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 FixedWeightPackForm::on_buding_clicked()
{
    emit buding();
    ui->dingzhong->setChecked(true);
}

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

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

void FixedWeightPackForm::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;
        }
        //        qDebug()<<"单精度"<<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->address = ui->lineEdit_address->text();
    packinfo->autoRuku = ui->ruku->isChecked();
    restTableWidget();
}

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

    ui->addBoxList->setShortcut(QKeySequence(key1.toString()));
    ui->addPackList->setShortcut(QKeySequence(key2.toString()));
    ui->pushPiLiang->setShortcut(QKeySequence(key3.toString()));
}
