#include "showmadanlabelform.h"
#include "ui_showmadanlabelform.h"
#include "QPainter"

ShowMaDanLabelForm::ShowMaDanLabelForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ShowMaDanLabelForm)
{
    ui->setupUi(this);

    ui->showWidget->setAutoFillBackground(true);

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

//    QImage image;
//    image.load(":/images/Pmoren.bmp");
//    QPalette palette;
//    _packPrinter.reset(new PackTemlateOne());
//    palette.setBrush(ui->showWidget->backgroundRole(), QBrush(_packPrinter->Packmodel().scaled(
//        ui->showWidget->size(),
//        Qt::IgnoreAspectRatio,
//        Qt::SmoothTransformation)));
//    ui->showWidget->setPalette(palette);

    addComboBox();
}

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

    QPalette palette;
    _packPrinter.reset(new PackTemlateOne());
    palette.setBrush(ui->showWidget->backgroundRole(), QBrush(_packPrinter->Packmodel().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){
        controlShow();
        ui->upModel->show();
        ModelName = modelName;
        for(int i = 0;i<glo->PackModelList().size();i++){
            if(ModelName == glo->PackModelList().at(i)->Model_Name){
                upData(glo->PackModelList().at(i));
                ui->modelName->setText(ModelName);
                break;
            }
        }
    }
}

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

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

void ShowMaDanLabelForm::upData(QSharedPointer<PackModel> puckModelData)
{
    ui->text1->setText(puckModelData->Type_Name.at(0).toString());
    ui->text1_2->setText(puckModelData->Type_Name.at(1).toString());
    ui->text2->setText(puckModelData->Type_Name.at(2).toString());
    ui->text2_2->setText(puckModelData->Type_Name.at(3).toString());
    ui->text4->setText(puckModelData->Type_Name.at(4).toString());
    ui->text4_2->setText(puckModelData->Type_Name.at(5).toString());
    ui->text4_3->setText(puckModelData->Type_Name.at(6).toString());

    ui->data1->setCurrentText(puckModelData->Type_Data.at(0).toString());
    ui->data1_2->setCurrentText(puckModelData->Type_Data.at(1).toString());
    ui->data2->setCurrentText(puckModelData->Type_Data.at(2).toString());
    ui->data2_2->setCurrentText(puckModelData->Type_Data.at(3).toString());
    ui->data3->setCurrentText(puckModelData->Type_Data.at(4).toString());
    ui->data4->setCurrentText(puckModelData->Type_Data.at(5).toString());
    ui->data4_2->setCurrentText(puckModelData->Type_Data.at(6).toString());
    ui->data4_3->setCurrentText(puckModelData->Type_Data.at(7).toString());
}

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

void ShowMaDanLabelForm::on_saveButton_clicked()
{

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

    packModel.reset(new PackModel);

    packModel->BaseModel_Name = BaseModel;

    packModel->Model_Name = ui->modelName->text();

    addPackData();

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

    if(PQ::writeAbsoluteFile(ret,QJsonDocument(packModel->toObject()).toJson())){
        QMessageBox::information(this,tr("提示"),tr("保存成功"));
        glo->selectPackModel();
        return;
    }
        QMessageBox::information(this,tr("提示"),tr("保存失败"));
}

void ShowMaDanLabelForm::addPackData()
{
    packModel->Type_Name.append(ui->text1->text());
    packModel->Type_Name.append(ui->text1_2->text());
    packModel->Type_Name.append(ui->text2->text());
    packModel->Type_Name.append(ui->text2_2->text());
    packModel->Type_Name.append(ui->text4->text());
    packModel->Type_Name.append(ui->text4_2->text());
    packModel->Type_Name.append(ui->text4_3->text());

    packModel->Type_Data.append(ui->data1->currentText());
    packModel->Type_Data.append(ui->data1_2->currentText());
    packModel->Type_Data.append(ui->data2->currentText());
    packModel->Type_Data.append(ui->data2_2->currentText());
    packModel->Type_Data.append(ui->data3->currentText());
    packModel->Type_Data.append(ui->data4->currentText());
    packModel->Type_Data.append(ui->data4_2->currentText());
    packModel->Type_Data.append(ui->data4_3->currentText());
}

void ShowMaDanLabelForm::addComboBox()
{
    QStringList list;
    list.append("空");
    list.append("批号");
    list.append("类型");
    list.append("品种");
    list.append("旦数");
    list.append("分特");
    list.append("等级");
    list.append("孔数");
    list.append("规格");
    list.append("颜色");
    list.append("捻向");
    list.append("管色");
    list.append("皮重");
    list.append("机台");
    list.append("纸箱");
    list.append("总筒数");
    list.append("总毛重");
    list.append("总净重");

    QStringList list20;
    list20.append("空");
    list20.append("毛重");
    list20.append("净重");
    list20.append("筒数");
    list20.append("捻向");
    list20.append("管色");
    list20.append("总筒重");

    ui->data1->addItems(list);
    ui->data1_2->addItems(list);
    ui->data2->addItems(list);
    ui->data2_2->addItems(list);
    ui->data3->addItems(list20);
    ui->data4->addItems(list);
    ui->data4_2->addItems(list);
    ui->data4_3->addItems(list);

}

void ShowMaDanLabelForm::controlHide()
{
    ui->text1->hide();
    ui->text1_2->hide();
    ui->text2->hide();
    ui->text2_2->hide();
    ui->label_4->hide();
    ui->text4->hide();
    ui->text4_2->hide();
    ui->text4_3->hide();

    ui->data1->hide();
    ui->data1_2->hide();
    ui->data2->hide();
    ui->data2_2->hide();
    ui->data3->hide();
    ui->data4->hide();
    ui->data4_2->hide();
    ui->data4_3->hide();
}


void ShowMaDanLabelForm::controlShow()
{
    ui->text1->show();
    ui->text1_2->show();
    ui->text2->show();
    ui->text2_2->show();
    ui->label_4->show();
    ui->text4->show();
    ui->text4_2->show();
    ui->text4_3->show();

    ui->data1->show();
    ui->data1_2->show();
    ui->data2->show();
    ui->data2_2->show();
    ui->data3->show();
    ui->data4->show();
    ui->data4_2->show();
    ui->data4_3->show();
}

void ShowMaDanLabelForm::on_upModel_clicked()
{
    packModel.reset(new PackModel);

    packModel->BaseModel_Name = BaseModel;

    packModel->Model_Name =ModelName;

    addPackData();

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

    if(PQ::writeAbsoluteFile(ret,QJsonDocument(packModel->toObject()).toJson())){
        QMessageBox::information(this,tr("提示"),tr("更新成功"));
        glo->selectPackModel();
        return;
    }
        QMessageBox::information(this,tr("提示"),tr("更新失败"));
}
