#include "modelselectform.h"
#include "ui_modelselectform.h"
#include "QDebug"

ModelSelectForm::ModelSelectForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModelSelectForm)
{
    ui->setupUi(this);
    connect(glo,&GlobalInfo::boxModelUped,this,&ModelSelectForm::changeToSelectBoxWidget);
    connect(glo,&GlobalInfo::packModelUpde,this,&ModelSelectForm::changeToSelectPackWidget);
//    changeToSelectWidget();
}

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

void ModelSelectForm::changeStackedWidget(int status)
{
    if(status == 1){
        changeToSelectBoxWidget();
        ui->stackedWidget->setCurrentWidget(ui->pageXiangDan);
    }else if(status == 2){
        changeToSelectPackWidget();
        ui->stackedWidget->setCurrentWidget(ui->pageMaDan);
    }
}

void ModelSelectForm::changeToSelectBoxWidget()
{
    boxModelList = glo->BoxModelList();
//    qDebug()<<"size:"<<boxModelList.size();
//    for(int i = 0;i<boxModelList.size();i++){
//        qDebug()<<boxModelList.at(i)->toObject();
//    }
    ui->boxModelSelectWidget->clear();
    selectBoxModelFoName("基础模板1");
}

void ModelSelectForm::changeToSelectPackWidget()
{
    packModelList = glo->PackModelList();
    ui->packModelSelectWidget->clear();
    selectPackModelFoName("基础模板1");
}

void ModelSelectForm::selectBoxModelFoName(QString baseModelName)
{
    auto item = new QTreeWidgetItem(QStringList()<<baseModelName);

    QWidget *tmp_widget = new QWidget();
    tmp_widget->setMaximumWidth(200);
    QHBoxLayout *tmp_layout = new QHBoxLayout(tmp_widget);
    auto addNew = new QPushButton("新建",tmp_widget);
    addNew->setMaximumWidth(40);
    auto modelView = new QPushButton("查看",tmp_widget);
    modelView->setMaximumWidth(40);
    tmp_layout->addWidget(addNew);
    tmp_layout->addWidget(modelView);
    tmp_layout->setSpacing(0);
    tmp_layout->setMargin(0);

    connect(addNew,&QPushButton::clicked,[baseModelName,this](){this->addNewBoxModel(baseModelName);});
    connect(modelView,&QPushButton::clicked,[baseModelName,this](){this->boxModelView(baseModelName);});
    ui->boxModelSelectWidget->addTopLevelItem(item);
    ui->boxModelSelectWidget->setItemWidget(item,1,tmp_widget);

    QStringList modelName;
    for(int i = 0;i<boxModelList.size();i++){
        if(boxModelList.at(i)->baseModel_Name!=baseModelName){
            continue;
        }
        modelName.append(boxModelList.at(i)->Model_Name);
    }

    for(int i = 0;i<modelName.size();i++){
        QWidget *tep_widget = new QWidget();
        tep_widget->setMaximumWidth(200);
        QHBoxLayout *tep_layout = new QHBoxLayout(tep_widget);
        QPushButton *edit = new QPushButton("编辑");
        edit->setMaximumWidth(40);
        QPushButton *del = new QPushButton("删除");
        del->setMaximumWidth(40);
        tep_layout->addWidget(edit);
        tep_layout->addWidget(del);
        tep_layout->setSpacing(0);
        tep_layout->setMargin(0);

        QTreeWidgetItem *childitem = new QTreeWidgetItem(QStringList()<<modelName.at(i));
        item->addChild(childitem);
        ui->boxModelSelectWidget->setItemWidget(childitem,1,tep_widget);
        QString ModelName = modelName.at(i);
        connect(edit,&QPushButton::clicked,[baseModelName,ModelName,this](){this->editBoxModel(baseModelName,ModelName);});
        connect(del,&QPushButton::clicked,[ModelName,this](){this->delBoxModel(ModelName);});
    }
}

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

void ModelSelectForm::addNewBoxModel(QString baseModelName)
{
    if(baseModelName == "基础模板1"){
        emit addBoxModel(baseModelName,1);
    }
}

void ModelSelectForm::boxModelView(QString baseModelName)
{
    emit showBaseBoxModel(baseModelName,2);
}

void ModelSelectForm::editBoxModel(QString baseModelName,QString modelName)
{
    emit editBoxModeled(baseModelName,3,modelName);
}

void ModelSelectForm::delBoxModel(QString ModelName)
{
    _dataDir = PQ::AppPath::this_()->dataDir();
    if(!_dataDir.exists("BoxModel")){
        _dataDir.mkdir("BoxModel");
    }
    _dataDir.cd("BoxModel");
    QString fileName = ModelName+"_Model.json";
    if(_dataDir.remove(fileName)){
        QMessageBox::information(this,tr("提示"),tr("删除成功"));
        glo->selectBoxModel();
    }
}

//以下是码单
void ModelSelectForm::selectPackModelFoName(QString baseModelName)
{
    auto item = new QTreeWidgetItem(QStringList()<<baseModelName);

    QWidget *tmp_widget = new QWidget();
    tmp_widget->setMaximumWidth(200);
    QHBoxLayout *tmp_layout = new QHBoxLayout(tmp_widget);
    auto addNew = new QPushButton("新建",tmp_widget);
    addNew->setMaximumWidth(40);
    auto modelView = new QPushButton("查看",tmp_widget);
    modelView->setMaximumWidth(40);
    tmp_layout->addWidget(addNew);
    tmp_layout->addWidget(modelView);
    tmp_layout->setSpacing(0);
    tmp_layout->setMargin(0);

    connect(addNew,&QPushButton::clicked,[baseModelName,this](){this->addNewPackModel(baseModelName);});
    connect(modelView,&QPushButton::clicked,[baseModelName,this](){this->packModelView(baseModelName);});
    ui->packModelSelectWidget->addTopLevelItem(item);
    ui->packModelSelectWidget->setItemWidget(item,1,tmp_widget);

    QStringList modelName;
    for(int i = 0;i<packModelList.size();i++){
        if(packModelList.at(i)->BaseModel_Name!=baseModelName){
            continue;
        }
        modelName.append(packModelList.at(i)->Model_Name);
    }


    for(int i = 0;i<modelName.size();i++){

        QWidget *tep_widget = new QWidget();
        tep_widget->setMaximumWidth(200);
        QHBoxLayout *tep_layout = new QHBoxLayout(tep_widget);
        QPushButton *edit = new QPushButton("编辑");
        edit->setMaximumWidth(40);
        QPushButton *del = new QPushButton("删除");
        del->setMaximumWidth(40);
        tep_layout->addWidget(edit);
        tep_layout->addWidget(del);
        tep_layout->setSpacing(0);
        tep_layout->setMargin(0);

        QTreeWidgetItem *childitem = new QTreeWidgetItem(QStringList()<<modelName.at(i));
        item->addChild(childitem);
        ui->packModelSelectWidget->setItemWidget(childitem,1,tep_widget);

        QString ModelName = modelName.at(i);
        connect(edit,&QPushButton::clicked,[baseModelName,ModelName,this](){this->editPackModel(baseModelName,ModelName);});
        connect(del,&QPushButton::clicked,[ModelName,this](){this->delPackModel(ModelName);});
    }
}

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

void ModelSelectForm::addNewPackModel(QString baseModelName)
{
    if(baseModelName == "基础模板1"){
        emit addPackModel(baseModelName,1);
    }
}

void ModelSelectForm::packModelView(QString baseModelName)
{
    emit showBasePackModel(baseModelName,2);
}

void ModelSelectForm::editPackModel(QString baseModelName , QString ModelName)
{
    emit editPackModeled(baseModelName,3,ModelName);
}

void ModelSelectForm::delPackModel(QString ModelName)
{
    _dataDir = PQ::AppPath::this_()->dataDir();
    if(!_dataDir.exists("PackModel")){
        _dataDir.mkdir("PackModel");
    }
    _dataDir.cd("PackModel");
    QString fileName = ModelName+"_Model.json";
    if(_dataDir.remove(fileName)){
        QMessageBox::information(this,tr("提示"),tr("删除成功"));
        glo->selectPackModel();
    }
}
