#include "settingsform.h"
#include "ui_settingsform.h"
#include <QPrinterInfo>

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

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

void SettingsForm::Init()
{
    //串口名称
    foreach(const QPrinterInfo &info,QPrinterInfo::availablePrinters())
    {
       ui->printerName->addItem(info.printerName());
//               qDebug()<<info.portName();
    }

    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
       ui->portName->addItem(info.portName());
//               qDebug()<<info.portName();
    }

    //串口波特率
    QStringList Baudlist;
    Baudlist<<"300"<<"600"<<"2400"<<"4800"<<"9600"<<"19200"<<"56000"<<"57600"<<"115200"<<"128000"<<"256000"<<"921600";
    ui->portBaudRate->addItems(Baudlist);

    auto config = GlobalInfo::this_()->config();

    ui->baseUrlLine->setText(config->baseUrl);
    ui->printerName->setCurrentText(config->printerName);
    ui->portName->setCurrentText(config->portName);
    ui->portBaudRate->setCurrentText(config->portBaudRate);
    ui->comboBox_2->setCurrentIndex(config->portType);

}

void SettingsForm::on_back_btn_clicked()
{
    emit back();
}

void SettingsForm::on_save_btn_clicked()
{
    QJsonObject obj;
    obj.insert("baseUrl",ui->baseUrlLine->text());
    obj.insert("printerName",ui->printerName->currentText());
    obj.insert("portName",ui->portName->currentText());
    obj.insert("portBaudRate",ui->portBaudRate->currentText());
    obj.insert("portType",ui->comboBox_2->currentIndex());
    PQ::ConfigFile::writeObject("Config.json",obj);
    QMessageBox::information(this,"提示","请重新启动程序，以使用新配置！");
}
