#include "showxiangdanlabelform.h"
#include "ui_showxiangdanlabelform.h"
#include "QDebug"

ShowXiangDanLabelForm::ShowXiangDanLabelForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ShowXiangDanLabelForm)
{
    ui->setupUi(this);
    ui->showWidget->setAutoFillBackground(true);
//    addComboBox();
//    controlHide();

    _dataDir = PQ::AppPath::this_()->dataDir();
    if(!_dataDir.exists("BoxModel")){
        _dataDir.mkdir("BoxModel");
    }
    _dataDir.cd("BoxModel");

//    connect(glo,&GlobalInfo::boxModelUped,this,&ShowXiangDanLabelForm::upBoxModel);
    addComboBox();
}

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

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

void ShowXiangDanLabelForm::init(QString baseModelName,int status,QString modelName)
{
    ui->saveButton->hide();
    ui->upModel->hide();
    BaseModel = baseModelName;
    Status = status;

    QPalette palette;
    _boxPrinter.reset(new BoxTemlateOne());
    palette.setBrush(ui->showWidget->backgroundRole(), QBrush(_boxPrinter->Boxmodel().scaled(// 缩放背景图.
        ui->showWidget->size(),
        Qt::IgnoreAspectRatio,
        Qt::SmoothTransformation)));
    ui->showWidget->setPalette(palette);

    if(status == 1){
        ui->modelName->clear();
        ui->saveButton->show();
        controlShow();
    }else if(status == 2){
        controlHide();
        showBaseModel();
    }else if(status == 3){
        ui->upModel->show();
        ModelName = modelName;
        controlShow();
        ui->modelName->setText(ModelName);
        for(int i = 0;i<glo->BoxModelList().size();i++){
            if(ModelName == glo->BoxModelList().at(i)->Model_Name){
                upData(glo->BoxModelList().at(i));
                ui->modelName->setText(ModelName);
                break;
            }
        }
    }
}

void ShowXiangDanLabelForm::upData(QSharedPointer<BoxModel> boxModelData)
{
    ui->text1->setText(boxModelData->Type_Name.at(0).toString());
    ui->text2->setText(boxModelData->Type_Name.at(1).toString());
    ui->text3->setText(boxModelData->Type_Name.at(2).toString());
    ui->text4->setText(boxModelData->Type_Name.at(3).toString());
    ui->text5->setText(boxModelData->Type_Name.at(4).toString());
    ui->text1_2->setText(boxModelData->Type_Name.at(5).toString());
    ui->text2_2->setText(boxModelData->Type_Name.at(6).toString());
    ui->text3_2->setText(boxModelData->Type_Name.at(7).toString());
    ui->text4_2->setText(boxModelData->Type_Name.at(8).toString());
    ui->text5_2->setText(boxModelData->Type_Name.at(9).toString());

    ui->data1->setCurrentText(boxModelData->Type_Data.at(0).toString());
    ui->data2->setCurrentText(boxModelData->Type_Data.at(1).toString());
    ui->data3->setCurrentText(boxModelData->Type_Data.at(2).toString());
    ui->data4->setCurrentText(boxModelData->Type_Data.at(3).toString());
    ui->data5->setCurrentText(boxModelData->Type_Data.at(4).toString());
    ui->data1_2->setCurrentText(boxModelData->Type_Data.at(5).toString());
    ui->data2_2->setCurrentText(boxModelData->Type_Data.at(6).toString());
    ui->data3_2->setCurrentText(boxModelData->Type_Data.at(7).toString());
    ui->data4_2->setCurrentText(boxModelData->Type_Data.at(8).toString());
    ui->data5_2->setCurrentText(boxModelData->Type_Data.at(9).toString());
}

void ShowXiangDanLabelForm::showBaseModel()
{
            QImage image;
            image.load(":/images/moren.bmp");
            QPalette palette;
    //        _boxPrinter.reset(new BoxTemlateOne());
            palette.setBrush(ui->showWidget->backgroundRole(), QBrush(image.scaled(
                ui->showWidget->size(),
                Qt::IgnoreAspectRatio,
                Qt::SmoothTransformation)));
            ui->showWidget->setPalette(palette);
}




void ShowXiangDanLabelForm::controlHide()
{
    ui->text1->hide();//第一行从上而下
    ui->text2->hide();
    ui->text3->hide();
    ui->text4->hide();
    ui->text5->hide();
    ui->text1_2->hide();//第二行从上而下
    ui->text2_2->hide();
    ui->text3_2->hide();
    ui->text4_2->hide();
    ui->text5_2->hide();

    ui->data1->hide();
    ui->data2->hide();
    ui->data3->hide();
    ui->data4->hide();
    ui->data5->hide();
    ui->data1_2->hide();
    ui->data2_2->hide();
    ui->data3_2->hide();
    ui->data4_2->hide();
    ui->data5_2->hide();

}

void ShowXiangDanLabelForm::controlShow()
{
    ui->text1->show();//第一行从上而下
    ui->text2->show();
    ui->text3->show();
    ui->text4->show();
    ui->text5->show();
    ui->text1_2->show();//第二行从上而下
    ui->text2_2->show();
    ui->text3_2->show();
    ui->text4_2->show();
    ui->text5_2->show();

    ui->data1->show();
    ui->data2->show();
    ui->data3->show();
    ui->data4->show();
    ui->data5->show();
    ui->data1_2->show();
    ui->data2_2->show();
    ui->data3_2->show();
    ui->data4_2->show();
    ui->data5_2->show();
}

void ShowXiangDanLabelForm::addComboBox()
{

    QList<QString> text;
    text.append("空");
    text.append("品名");
    text.append("批号");
    text.append("规格");
    text.append("等级");
//    text.append("箱号");
    text.append("毛重");
    text.append("净重");
    text.append("筒数");
    text.append("捻向");
    text.append("类型");
    text.append("旦数");
    text.append("分特");
    text.append("孔数");
    text.append("颜色");
    text.append("皮重");
    text.append("筒重");
    text.append("管色");

    ui->data1->addItems(text);
    ui->data2->addItems(text);
    ui->data3->addItems(text);
    ui->data4->addItems(text);
    ui->data5->addItems(text);
    ui->data1_2->addItems(text);
    ui->data2_2->addItems(text);
    ui->data3_2->addItems(text);
    ui->data4_2->addItems(text);
    ui->data5_2->addItems(text);
}

void ShowXiangDanLabelForm::on_delModel_clicked()
{
    pack->getBoxModel();
}

void ShowXiangDanLabelForm::on_saveButton_clicked()
{

    if(ui->modelName->text().isEmpty()){
        QMessageBox::information(this,tr("注意"),tr("请输入模板名称"));
        return;
    }

    boxModel.reset(new BoxModel);

    boxModel->baseModel_Name = BaseModel;
    boxModel->Model_Name = ui->modelName->text();
    addBoxData();

    QString name = boxModel->Model_Name;
    name += "_Model.json";
    QString ret = _dataDir.absoluteFilePath(name);

    if(PQ::writeAbsoluteFile(ret,QJsonDocument(boxModel->toObject()).toJson())){
        QMessageBox::information(this,tr("提示"),tr("保存成功"));
//        ui->addNewModel->setText("新建模板");
        glo->selectBoxModel();
        return;
    }
        QMessageBox::information(this,tr("提示"),tr("保存失败"));
}

void ShowXiangDanLabelForm::addBoxData()
{
    boxModel->Type_Name.append(ui->text1->text());
    boxModel->Type_Name.append(ui->text2->text());
    boxModel->Type_Name.append(ui->text3->text());
    boxModel->Type_Name.append(ui->text4->text());
    boxModel->Type_Name.append(ui->text5->text());
    boxModel->Type_Name.append(ui->text1_2->text());
    boxModel->Type_Name.append(ui->text2_2->text());
    boxModel->Type_Name.append(ui->text3_2->text());
    boxModel->Type_Name.append(ui->text4_2->text());
    boxModel->Type_Name.append(ui->text5_2->text());

    boxModel->Type_Data.append(ui->data1->currentText());
    boxModel->Type_Data.append(ui->data2->currentText());
    boxModel->Type_Data.append(ui->data3->currentText());
    boxModel->Type_Data.append(ui->data4->currentText());
    boxModel->Type_Data.append(ui->data5->currentText());
    boxModel->Type_Data.append(ui->data1_2->currentText());
    boxModel->Type_Data.append(ui->data2_2->currentText());
    boxModel->Type_Data.append(ui->data3_2->currentText());
    boxModel->Type_Data.append(ui->data4_2->currentText());
    boxModel->Type_Data.append(ui->data5_2->currentText());
}

void ShowXiangDanLabelForm::on_upModel_clicked()
{
//    QList<QSharedPointer<BoxModel>> boxModelList = glo->BoxModelList();
//    for(int i = 0;i<boxModelList.size();i++){

//    }
    boxModel.reset(new BoxModel);
    boxModel->baseModel_Name = BaseModel;
    boxModel->Model_Name = ModelName;
    addBoxData();

    QString name = boxModel->Model_Name;
    name += "_Model.json";
    QString ret = _dataDir.absoluteFilePath(name);

    if(PQ::writeAbsoluteFile(ret,QJsonDocument(boxModel->toObject()).toJson())){
        QMessageBox::information(this,tr("提示"),tr("更新成功"));
//        ui->addNewModel->setText("新建模板");
        glo->selectBoxModel();
        return;
    }
        QMessageBox::information(this,tr("提示"),tr("更新失败"));

}
