#include "controlnetconfigwidget.h"
#include "base/waitingdlg.h"
#include "globaldefine.h"
#include "tools.h"
#include <QJsonArray>
#include <QMessageBox>
#include <QBoxLayout>

ControlNetConfigWidget::ControlNetConfigWidget(QWidget *parent,QList<LedCard *> *list) : QWidget(parent) {
    auto vBox = new QVBoxLayout(this);

    lbLanCfg = new QLabel;
    QFont font = lbLanCfg->font();
    font.setPointSize(12);
    lbLanCfg->setFont(font);
    lbLanCfg->setAlignment(Qt::AlignCenter);
    vBox->addWidget(lbLanCfg);

    auto hBox = new QHBoxLayout();
    hBox->addStretch();

    fdDhcp = new QRadioButton;
    hBox->addWidget(fdDhcp, 0, Qt::AlignTop);

    hBox->addSpacing(40);

    vBoxSpecifyIp = new QVBoxLayout();
    vBoxSpecifyIp->setContentsMargins(0, 0, 0, 0);

    fdSpecifyIp = new QRadioButton;
    vBoxSpecifyIp->addWidget(fdSpecifyIp, 0, Qt::AlignCenter);

    gBoxSpecifyIp = new QGroupBox;
    gBoxSpecifyIp->setMaximumWidth(340);
    {
        auto vvv = new QVBoxLayout(gBoxSpecifyIp);
        auto hhh = new QHBoxLayout();

        labelIpAddress = new QLabel;
        hhh->addWidget(labelIpAddress);

        fdIP = new QIPLineEdit;
        fdIP->setFixedWidth(160);
        fdIP->setStyleSheet("background-color: #fff;");
        hhh->addWidget(fdIP);

        vvv->addLayout(hhh);
        hhh = new QHBoxLayout();

        labelMaskAddress = new QLabel;
        hhh->addWidget(labelMaskAddress);

        fdMask = new QIPLineEdit;
        fdMask->setFixedWidth(160);
        fdMask->setStyleSheet("background-color: #FFF;");
        hhh->addWidget(fdMask);

        vvv->addLayout(hhh);
        hhh = new QHBoxLayout();

        labelGateway = new QLabel;
        hhh->addWidget(labelGateway);

        fdGateWay = new QIPLineEdit;
        fdGateWay->setFixedWidth(160);
        fdGateWay->setStyleSheet("background-color: #fff;");
        hhh->addWidget(fdGateWay);

        vvv->addLayout(hhh);
        hhh = new QHBoxLayout();

        labelDnsAddress = new QLabel;
        hhh->addWidget(labelDnsAddress);

        fdDns = new QIPLineEdit;
        fdDns->setFixedWidth(160);
        fdDns->setStyleSheet("background-color: #FFF;");
        hhh->addWidget(fdDns);

        vvv->addLayout(hhh);
    }
    vBoxSpecifyIp->addWidget(gBoxSpecifyIp);

    hBox->addLayout(vBoxSpecifyIp);

    hBox->addStretch();
    vBox->addLayout(hBox);

    hBox = new QHBoxLayout();
    hBox->addStretch();

    btnLanSet = new QPushButton;
    btnLanSet->setMinimumSize(QSize(60, 30));
    connect(btnLanSet, &QPushButton::clicked, this, [this] {
        if(gSelCards->isEmpty()) {
            QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
            return;
        }
        QString ip = fdIP->text();
        QString mask = fdMask->text();
        QString gateWay = fdGateWay->text();
        QString dns = fdDns->text();
        if(fdDhcp->isChecked()) {
            if(ip=="...") ip="0.255.255.255";
            if(mask=="...") mask="0.255.255.255";
            if(gateWay=="...") gateWay="0.255.255.255";
            if(dns=="...") dns="0.255.255.255";
        } else {
            if(ip=="...") {
                QMessageBox::warning(gMainWin, tr("Attention"), tr("Please input IP address!"));
                fdIP->setFocus();
                return;
            }
            if(!isTextValid(ip)) {
                QMessageBox::warning(gMainWin, tr("Attention"), tr("Your IP Address is Invalid!")+"["+ip+"]");
                fdIP->setFocus();
                return;
            }
            if(mask=="...") {
               QMessageBox::warning(gMainWin, tr("Attention"), tr("Please input Mask address!"));
                fdMask->setFocus();
                return;
            }
            if(!isTextValid(mask)) {
                QMessageBox::warning(gMainWin, tr("Attention"), tr("Your Mask Address is Invalid!"));
                fdMask->setFocus();
                return;
            }
            if(gateWay=="...") {
               QMessageBox::warning(gMainWin, tr("Attention"), tr("Please input Gateway address!"));
                fdGateWay->setFocus();
                return;
            }
            if(!isTextValid(gateWay)) {
                QMessageBox::warning(gMainWin, tr("Attention"), tr("Your Gateway Address is Invalid!"));
                fdGateWay->setFocus();
                return;
            }
            if(dns=="...") {
               QMessageBox::warning(gMainWin, tr("Attention"), tr("Please input DNS address!"));
                fdDns->setFocus();
                return;
            }
            if (!isTextValid(dns)) {
                QMessageBox::warning(gMainWin, tr("Attention"), tr("Your DNS Address is Invalid!"));
                fdDns->setFocus();
                return;
            }
        }
        QJsonObject json;
        json.insert("_id", "SetEthernet");
        json.insert("_type", "SetEthernet");
        json.insert("dhcp", fdDhcp->isChecked());
        json.insert("ip", ip);
        json.insert("netMask", mask);
        json.insert("gateWay", gateWay);
        json.insert("dns", dns);
        if(gSelCards->count() == 1) {
            auto waitingDlg = new WaitingDlg(this, tr("SetEthernet"));
            Def_CtrlReqPre
            connect(reply, &QNetworkReply::finished, this, [reply, waitingDlg] {
                Def_CtrlSetReqAfter
            });
        } else {
            foreach(auto card, *gSelCards) {
                Def_CtrlSetMulti(tr("SetEthernet"))
            }
        }
    });
    hBox->addWidget(btnLanSet);

    hBox->addSpacing(100);

    btnLanGet = new QPushButton;
    btnLanGet->setMinimumSize(QSize(60, 30));
    connect(btnLanGet, &QPushButton::clicked, this, [this] {
        if(gSelCards->isEmpty()) {
            QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
            return;
        }
        QJsonObject json;
        json.insert("_id", "GetEthernet");
        json.insert("_type", "GetEthernet");
        if(gSelCards->count() == 1) {
            auto waitingDlg = new WaitingDlg(this, tr("GetEthernet"));
            Def_CtrlReqPre
            connect(reply, &QNetworkReply::finished, this, [this, reply, waitingDlg] {
                Def_CtrlSingleGetReply
                waitingDlg->success();
                if(json["dhcp"].toBool()) {
                    fdDhcp->setChecked(true);
                    emit sigGetDhcpOrSpecifyIp(0);
                } else {
                    fdSpecifyIp->setChecked(true);
                    emit sigGetDhcpOrSpecifyIp(1);
                }
                fdIP->setText(json["ipAddr"].toString());
                fdMask->setText(json["netMask"].toString());
                fdGateWay->setText(json["gateWay"].toString());
                fdDns->setText(json["dnsAddr"].toString());
            });
        } else {
            foreach(auto card, *gSelCards) {
                auto reply = Tools::netManager().post(reqForJson("http://"+card->m_strCardIp+":2016/settings"), QJsonDocument{json}.toJson(QJsonDocument::Compact));
                connect(reply, &QNetworkReply::finished, this, [reply, card] {
                    QJsonDocument json;
                    QString err = checkReplyForJson(reply, &json);
                    if(err.isEmpty()) err = json["dhcp"].toBool() ? tr("DHCP IP") : tr("STATIC IP");
                    gFdResInfo->append(card->m_strCardId+" "+tr("GetEthernet")+" "+err);
                    gFdResInfo->append("  IP: "+json["ipAddr"].toString());
                    gFdResInfo->append("  Mask: "+json["netMask"].toString());
                    gFdResInfo->append("  GateWay: "+json["gateWay"].toString());
                    gFdResInfo->append("  Dns: "+json["dnsAddr"].toString());
                });
            }
        }
    });
    hBox->addWidget(btnLanGet);

    hBox->addStretch();
    vBox->addLayout(hBox);

    auto line = new QFrame;
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Sunken);
    vBox->addWidget(line);

    label_5 = new QLabel;
    label_5->setFont(font);
    label_5->setAlignment(Qt::AlignCenter);

    vBox->addWidget(label_5);

    horizontalLayout_2 = new QHBoxLayout();
    horizontalLayout_2->setContentsMargins(-1, -1, 10, -1);
    verticalLayout_6 = new QVBoxLayout();
    verticalLayout_6->setContentsMargins(-1, -1, -1, 0);
    groupBox_3 = new QGroupBox;
    groupBox_3->setMinimumSize(QSize(0, 144));
    groupBox_3->setMaximumSize(QSize(460, 16777215));
    groupBox_3->setCheckable(true);
    verticalLayout_7 = new QVBoxLayout(groupBox_3);
    horizontalLayout_9 = new QHBoxLayout();
    horizontalLayout_9->setSpacing(6);
    horizontalLayout_9->setContentsMargins(0, 0, -1, -1);
    verticalLayout_11 = new QVBoxLayout();
    verticalLayout_11->setContentsMargins(-1, -1, 0, -1);
    labelWifiName = new QLabel(groupBox_3);
    labelWifiName->setMinimumSize(QSize(0, 30));

    verticalLayout_11->addWidget(labelWifiName);

    labelWifiPassword = new QLabel(groupBox_3);
    labelWifiPassword->setMinimumSize(QSize(0, 30));

    verticalLayout_11->addWidget(labelWifiPassword);


    horizontalLayout_9->addLayout(verticalLayout_11);

    verticalLayout_12 = new QVBoxLayout();
    verticalLayout_12->setContentsMargins(-1, -1, 0, -1);
    comboBox = new QComboBox(groupBox_3);
    comboBox->setMinimumSize(QSize(260, 30));
    comboBox->setMaximumSize(QSize(260, 16777215));
    comboBox->setStyleSheet(QString::fromUtf8("background-color: #FFFFFF;"));
    comboBox->setEditable(true);

    verticalLayout_12->addWidget(comboBox);

    lineEditWifiPassword = new QLineEdit(groupBox_3);
    lineEditWifiPassword->setMinimumSize(QSize(140, 30));
    lineEditWifiPassword->setMaximumSize(QSize(160, 16777215));
    lineEditWifiPassword->setStyleSheet(QString::fromUtf8("background-color: #FFFFFF;"));
    lineEditWifiPassword->setEchoMode(QLineEdit::PasswordEchoOnEdit);

    verticalLayout_12->addWidget(lineEditWifiPassword);


    horizontalLayout_9->addLayout(verticalLayout_12);

    verticalLayout_13 = new QVBoxLayout();
    verticalLayout_13->setContentsMargins(-1, -1, 0, -1);
    pushButtonScan = new QPushButton(groupBox_3);
    pushButtonScan->setMinimumSize(QSize(60, 30));
    pushButtonScan->setMaximumSize(QSize(100, 16777215));

    verticalLayout_13->addWidget(pushButtonScan);

    horizontalSpacer_10 = new QSpacerItem(40, 30, QSizePolicy::Expanding, QSizePolicy::Minimum);

    verticalLayout_13->addItem(horizontalSpacer_10);


    horizontalLayout_9->addLayout(verticalLayout_13);


    verticalLayout_7->addLayout(horizontalLayout_9);

    pushButtonWiFiModel = new QPushButton(groupBox_3);
    pushButtonWiFiModel->setMinimumSize(QSize(60, 30));

    verticalLayout_7->addWidget(pushButtonWiFiModel, 0, Qt::AlignHCenter);


    verticalLayout_6->addWidget(groupBox_3);


    horizontalLayout_2->addLayout(verticalLayout_6);

    pushButtonReadbackWiFiModel = new QPushButton;
    QSizePolicy sizePolicy3(QSizePolicy::Expanding, QSizePolicy::Fixed);
    sizePolicy3.setHorizontalStretch(0);
    sizePolicy3.setVerticalStretch(0);
    sizePolicy3.setHeightForWidth(pushButtonReadbackWiFiModel->sizePolicy().hasHeightForWidth());
    pushButtonReadbackWiFiModel->setSizePolicy(sizePolicy3);
    pushButtonReadbackWiFiModel->setMinimumSize(QSize(0, 30));
    pushButtonReadbackWiFiModel->setMaximumSize(QSize(100, 16777215));

    horizontalLayout_2->addWidget(pushButtonReadbackWiFiModel, 0, Qt::AlignBottom);

    verticalLayout_4 = new QVBoxLayout();
    verticalLayout_4->setContentsMargins(-1, 0, -1, -1);
    groupBox_4 = new QGroupBox;
    groupBox_4->setMinimumSize(QSize(0, 144));
    groupBox_4->setMaximumSize(QSize(300, 16777215));
    groupBox_4->setCheckable(true);
    verticalLayout_8 = new QVBoxLayout(groupBox_4);
    horizontalLayout_12 = new QHBoxLayout();
    horizontalLayout_12->setContentsMargins(0, -1, -1, -1);
    verticalLayout_14 = new QVBoxLayout();
    verticalLayout_14->setContentsMargins(0, -1, -1, -1);
    labelApName = new QLabel(groupBox_4);

    verticalLayout_14->addWidget(labelApName);

    labelApPassword = new QLabel(groupBox_4);

    verticalLayout_14->addWidget(labelApPassword);


    horizontalLayout_12->addLayout(verticalLayout_14);

    verticalLayout_15 = new QVBoxLayout();
    verticalLayout_15->setContentsMargins(0, -1, -1, -1);
    lineEditHotspotName = new QLineEdit(groupBox_4);
    lineEditHotspotName->setMinimumSize(QSize(120, 30));
    lineEditHotspotName->setMaximumSize(QSize(120, 16777215));
    lineEditHotspotName->setStyleSheet(QString::fromUtf8("background-color: #FFFFFF;"));

    verticalLayout_15->addWidget(lineEditHotspotName);

    lineEditApPassword = new QLineEdit(groupBox_4);
    lineEditApPassword->setMinimumSize(QSize(120, 30));
    lineEditApPassword->setMaximumSize(QSize(120, 16777215));
    lineEditApPassword->setStyleSheet(QString::fromUtf8("background-color: #FFFFFF;"));
    lineEditApPassword->setEchoMode(QLineEdit::PasswordEchoOnEdit);

    verticalLayout_15->addWidget(lineEditApPassword);


    horizontalLayout_12->addLayout(verticalLayout_15);


    verticalLayout_8->addLayout(horizontalLayout_12);

    pushButtonApSet = new QPushButton(groupBox_4);
    pushButtonApSet->setMinimumSize(QSize(60, 30));

    verticalLayout_8->addWidget(pushButtonApSet, 0, Qt::AlignHCenter);


    verticalLayout_4->addWidget(groupBox_4);


    horizontalLayout_2->addLayout(verticalLayout_4);

    horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    horizontalLayout_2->addItem(horizontalSpacer_2);


    vBox->addLayout(horizontalLayout_2);

    line_3 = new QFrame;
    line_3->setFrameShape(QFrame::HLine);
    line_3->setFrameShadow(QFrame::Sunken);

    vBox->addWidget(line_3);

    label = new QLabel;
    label->setFont(font);

    vBox->addWidget(label, 0, Qt::AlignHCenter);

    checkBox4g = new QCheckBox;

    vBox->addWidget(checkBox4g);

    horizontalLayout_13 = new QHBoxLayout();
    horizontalLayout_13->setContentsMargins(-1, 0, -1, -1);
    pushButtonReadback4G = new QPushButton;
    pushButtonReadback4G->setMinimumSize(QSize(60, 30));
    pushButtonReadback4G->setMaximumSize(QSize(200, 16777215));

    horizontalLayout_13->addWidget(pushButtonReadback4G);

    label_ApnInfo = new QLabel;
    label_ApnInfo->setMinimumSize(QSize(0, 0));

    horizontalLayout_13->addWidget(label_ApnInfo);

    lineEditApnInfo = new QLineEdit;

    horizontalLayout_13->addWidget(lineEditApnInfo);

    pushButtonRead4GStatus = new QPushButton;
    pushButtonRead4GStatus->setMinimumSize(QSize(0, 30));

    horizontalLayout_13->addWidget(pushButtonRead4GStatus);

    horizontalSpacer_6 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    horizontalLayout_13->addItem(horizontalSpacer_6);


    vBox->addLayout(horizontalLayout_13);

    verticalSpacer_2 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);

    vBox->addItem(verticalSpacer_2);

    groupBox_5 = new QGroupBox;
    groupBox_5->setMinimumSize(QSize(0, 80));
    groupBox_5->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
    groupBox_5->setFlat(false);
    groupBox_5->setCheckable(true);
    groupBox_5->setChecked(false);
    verticalLayout_16 = new QVBoxLayout(groupBox_5);
    label_8 = new QLabel(groupBox_5);
    label_8->setWordWrap(true);

    verticalLayout_16->addWidget(label_8);

    checkBoxCustom = new QCheckBox(groupBox_5);
    checkBoxCustom->setChecked(false);

    verticalLayout_16->addWidget(checkBoxCustom);

    horizontalLayout_10 = new QHBoxLayout();
    horizontalLayout_10->setContentsMargins(-1, 0, -1, -1);
    pushButtonGetMcc = new QPushButton(groupBox_5);
    pushButtonGetMcc->setMinimumSize(QSize(0, 30));

    horizontalLayout_10->addWidget(pushButtonGetMcc);

    label_9 = new QLabel(groupBox_5);

    horizontalLayout_10->addWidget(label_9);

    label_2 = new QLabel(groupBox_5);

    horizontalLayout_10->addWidget(label_2);

    comboBoxCountryID = new QComboBox(groupBox_5);
    comboBoxCountryID->setMinimumSize(QSize(80, 0));
    comboBoxCountryID->setStyleSheet(QString::fromUtf8("background-color: #FFFFFF"));
    comboBoxCountryID->setEditable(false);

    horizontalLayout_10->addWidget(comboBoxCountryID);

    label_11 = new QLabel(groupBox_5);

    horizontalLayout_10->addWidget(label_11);

    label_3 = new QLabel(groupBox_5);

    horizontalLayout_10->addWidget(label_3);

    comboBoxCarrierName = new QComboBox(groupBox_5);
    comboBoxCarrierName->setMinimumSize(QSize(200, 0));
    comboBoxCarrierName->setContextMenuPolicy(Qt::DefaultContextMenu);
    comboBoxCarrierName->setStyleSheet(QString::fromUtf8("background-color: #FFFFFF"));
    comboBoxCarrierName->setEditable(false);
    comboBoxCarrierName->setDuplicatesEnabled(false);

    horizontalLayout_10->addWidget(comboBoxCarrierName);

    label_12 = new QLabel(groupBox_5);

    horizontalLayout_10->addWidget(label_12);

    label_4 = new QLabel(groupBox_5);

    horizontalLayout_10->addWidget(label_4);

    lineEditAPN = new QLineEdit(groupBox_5);
    lineEditAPN->setMinimumSize(QSize(200, 0));
    lineEditAPN->setMaximumSize(QSize(200, 16777215));
    lineEditAPN->setStyleSheet(QString::fromUtf8("background-color: #FFFFFF"));
    lineEditAPN->setReadOnly(true);

    horizontalLayout_10->addWidget(lineEditAPN);

    horizontalSpacer_3 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    horizontalLayout_10->addItem(horizontalSpacer_3);


    verticalLayout_16->addLayout(horizontalLayout_10);

    horizontalLayout_11 = new QHBoxLayout();
    horizontalLayout_11->setContentsMargins(-1, 10, -1, -1);
    label_6 = new QLabel(groupBox_5);

    horizontalLayout_11->addWidget(label_6);

    lineEditUser = new QLineEdit(groupBox_5);
    lineEditUser->setMaximumSize(QSize(120, 16777215));
    lineEditUser->setStyleSheet(QString::fromUtf8("background-color: #FFFFFF"));
    lineEditUser->setReadOnly(true);

    horizontalLayout_11->addWidget(lineEditUser);

    label_7 = new QLabel(groupBox_5);

    horizontalLayout_11->addWidget(label_7);

    lineEditPassword = new QLineEdit(groupBox_5);
    lineEditPassword->setMaximumSize(QSize(120, 16777215));
    lineEditPassword->setStyleSheet(QString::fromUtf8("background-color: #FFFFFF"));
    lineEditPassword->setReadOnly(true);

    horizontalLayout_11->addWidget(lineEditPassword);

    horizontalSpacer_7 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    horizontalLayout_11->addItem(horizontalSpacer_7);


    verticalLayout_16->addLayout(horizontalLayout_11);

    horizontalLayout_8 = new QHBoxLayout();
    horizontalLayout_8->setContentsMargins(-1, 0, -1, -1);
    horizontalSpacer_5 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    horizontalLayout_8->addItem(horizontalSpacer_5);

    pushButtonSet4GInfo = new QPushButton(groupBox_5);
    pushButtonSet4GInfo->setMinimumSize(QSize(60, 30));

    horizontalLayout_8->addWidget(pushButtonSet4GInfo);

    pushButtonSetYuanHengApn = new QPushButton(groupBox_5);
    pushButtonSetYuanHengApn->setMinimumSize(QSize(0, 30));

    horizontalLayout_8->addWidget(pushButtonSetYuanHengApn);

    horizontalSpacer_4 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    horizontalLayout_8->addItem(horizontalSpacer_4);


    verticalLayout_16->addLayout(horizontalLayout_8);


    vBox->addWidget(groupBox_5);

    verticalSpacer_4 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);

    vBox->addItem(verticalSpacer_4);

    horizontalLayout_15 = new QHBoxLayout();
    horizontalLayout_15->setContentsMargins(-1, 0, 10, -1);
    label_10 = new QLabel;

    horizontalLayout_15->addWidget(label_10);

    widgetFightModel = new SwitchControl;
    widgetFightModel->setMinimumSize(QSize(80, 35));

    horizontalLayout_15->addWidget(widgetFightModel);

    pushButtonReadbackFlightModel = new QPushButton;
    pushButtonReadbackFlightModel->setMinimumSize(QSize(0, 30));

    horizontalLayout_15->addWidget(pushButtonReadbackFlightModel);

    horizontalSpacer_8 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    horizontalLayout_15->addItem(horizontalSpacer_8);


    vBox->addLayout(horizontalLayout_15);

    verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);

    vBox->addItem(verticalSpacer);


    fdDhcp->setText(QCoreApplication::translate("ControlNetConfigWidget", "DHCP", nullptr));
    fdSpecifyIp->setText(QCoreApplication::translate("ControlNetConfigWidget", "Specify IP", nullptr));
    labelIpAddress->setText(QCoreApplication::translate("ControlNetConfigWidget", "IP Address", nullptr));
    labelMaskAddress->setText(QCoreApplication::translate("ControlNetConfigWidget", "Mask Address", nullptr));
    labelGateway->setText(QCoreApplication::translate("ControlNetConfigWidget", "Gateway", nullptr));
    labelDnsAddress->setText(QCoreApplication::translate("ControlNetConfigWidget", "DNS Address", nullptr));
    label_5->setText(QCoreApplication::translate("ControlNetConfigWidget", "WIFI Configuration", nullptr));
    groupBox_3->setTitle(QCoreApplication::translate("ControlNetConfigWidget", "WiFi On/Off", nullptr));
    labelWifiName->setText(QCoreApplication::translate("ControlNetConfigWidget", "WiFi name", nullptr));
    labelWifiPassword->setText(QCoreApplication::translate("ControlNetConfigWidget", "Password", nullptr));
    lineEditWifiPassword->setPlaceholderText(QCoreApplication::translate("ControlNetConfigWidget", "Input password", nullptr));
    pushButtonScan->setText(QCoreApplication::translate("ControlNetConfigWidget", "Scan", nullptr));
    pushButtonWiFiModel->setText(QCoreApplication::translate("ControlNetConfigWidget", "Set", nullptr));
    pushButtonReadbackWiFiModel->setText(QCoreApplication::translate("ControlNetConfigWidget", "Readback", nullptr));
    groupBox_4->setTitle(QCoreApplication::translate("ControlNetConfigWidget", "Ap Mode", nullptr));
    labelApName->setText(QCoreApplication::translate("ControlNetConfigWidget", "Ap name", nullptr));
    labelApPassword->setText(QCoreApplication::translate("ControlNetConfigWidget", "Password", nullptr));
    lineEditHotspotName->setPlaceholderText(QCoreApplication::translate("ControlNetConfigWidget", "Input Ap name", nullptr));
    lineEditApPassword->setPlaceholderText(QCoreApplication::translate("ControlNetConfigWidget", "Input password", nullptr));
    pushButtonApSet->setText(QCoreApplication::translate("ControlNetConfigWidget", "Set", nullptr));
    label->setText(QCoreApplication::translate("ControlNetConfigWidget", "4G/5G Configuration", nullptr));
    checkBox4g->setText(QCoreApplication::translate("ControlNetConfigWidget", "Open 4G/5G", nullptr));
    pushButtonReadback4G->setText(QCoreApplication::translate("ControlNetConfigWidget", "Readback APN Info", nullptr));
    label_ApnInfo->setText(QCoreApplication::translate("ControlNetConfigWidget", " APN:", nullptr));
    pushButtonRead4GStatus->setText(QCoreApplication::translate("ControlNetConfigWidget", "Get cellular network status information", nullptr));
    groupBox_5->setTitle(QCoreApplication::translate("ControlNetConfigWidget", "Set Apn Info,Check SIM can automatically match to MCC, and then select carrier get apn param", nullptr));
    label_8->setText(QCoreApplication::translate("ControlNetConfigWidget", "Check SIM can automatically match to MCC, and then select carrier get apn param,if not found carrier,you can check the CUSTOM checkbox,then enter custom param.", nullptr));
    checkBoxCustom->setText(QCoreApplication::translate("ControlNetConfigWidget", "CUSTOM", nullptr));
    pushButtonGetMcc->setText(QCoreApplication::translate("ControlNetConfigWidget", "Check SIM", nullptr));
    label_9->setText(QCoreApplication::translate("ControlNetConfigWidget", "->", nullptr));
    label_2->setText(QCoreApplication::translate("ControlNetConfigWidget", "Country ID(mcc):", nullptr));
    label_11->setText(QCoreApplication::translate("ControlNetConfigWidget", "->", nullptr));
    label_3->setText(QCoreApplication::translate("ControlNetConfigWidget", "Carrier Name", nullptr));
    label_12->setText(QCoreApplication::translate("ControlNetConfigWidget", "->", nullptr));
    label_4->setText(QCoreApplication::translate("ControlNetConfigWidget", "APN:", nullptr));
    lineEditAPN->setPlaceholderText(QCoreApplication::translate("ControlNetConfigWidget", "APN(Required)", nullptr));
    label_6->setText(QCoreApplication::translate("ControlNetConfigWidget", "User:", nullptr));
    lineEditUser->setPlaceholderText(QCoreApplication::translate("ControlNetConfigWidget", "Not required", nullptr));
    label_7->setText(QCoreApplication::translate("ControlNetConfigWidget", "Password:", nullptr));
    lineEditPassword->setPlaceholderText(QCoreApplication::translate("ControlNetConfigWidget", "Not required", nullptr));
    pushButtonSet4GInfo->setText(QCoreApplication::translate("ControlNetConfigWidget", "Set", nullptr));
    pushButtonSetYuanHengApn->setText(QCoreApplication::translate("ControlNetConfigWidget", "Set Apn:iot.cargo", nullptr));
    label_10->setText(QCoreApplication::translate("ControlNetConfigWidget", "Flight Mode", nullptr));
    pushButtonReadbackFlightModel->setText(QCoreApplication::translate("ControlNetConfigWidget", "Readback", nullptr));


    btnLanGet->setProperty("ssType", "progManageTool");
    pushButtonReadbackWiFiModel->setProperty("ssType", "progManageTool");
    btnLanSet->setProperty("ssType", "progManageTool");
    pushButtonApSet->setProperty("ssType", "progManageTool");
    pushButtonWiFiModel->setProperty("ssType", "progManageTool");
    pushButtonScan->setProperty("ssType", "progManageTool");
    pushButtonReadback4G->setProperty("ssType", "progManageTool");
    pushButtonSet4GInfo->setProperty("ssType", "progManageTool");
    pushButtonGetMcc->setProperty("ssType", "progManageTool");
    pushButtonSetYuanHengApn->setProperty("ssType", "progManageTool");
    pushButtonReadbackFlightModel->setProperty("ssType", "progManageTool");
    pushButtonRead4GStatus->setProperty("ssType", "progManageTool");
    widgetFightModel->setSliderColor(QColor(0, 0, 0),QColor(0, 160, 230));
     widgetFightModel->setBgColor(QColor(200,200,200),QColor(0,200,0));
     widgetFightModel->setTextColor(QColor(100,100,100),QColor(0, 160, 230));
     widgetFightModel->setText(tr("OFF"),tr("ON"));

    m_buttonGroup = new QButtonGroup;
    m_buttonGroup->addButton(fdDhcp,0);
    m_buttonGroup->addButton(fdSpecifyIp,1);

    checkBox4g->setStyleSheet("QCheckBox::indicator {width: 16px; height: 16px;}");
    checkBoxCustom->setStyleSheet("QCheckBox::indicator {width: 16px; height: 16px;}");
    comboBoxCountryID->setStyleSheet("background:rgba(216,216,216,1);");
    comboBoxCarrierName->setStyleSheet("background:rgba(216,216,216,1);");
    lineEditAPN->setStyleSheet("background:rgba(216,216,216,1);");
    lineEditUser->setStyleSheet("background:rgba(216,216,216,1);");
    lineEditPassword->setStyleSheet("background:rgba(216,216,216,1);");
    checkBox4g->setChecked(true);
    fdDhcp->setChecked(true);

    connect(this,SIGNAL(sigGetDhcpOrSpecifyIp(int)),this,SLOT(OnDhcpOrSpecifyIp(int)));
    connect(m_buttonGroup,SIGNAL(buttonClicked(int)),this,SLOT(OnDhcpOrSpecifyIp(int)));

    connect(pushButtonWiFiModel, SIGNAL(clicked()), this, SLOT(onSetWiFiModel()));
    connect(pushButtonReadbackWiFiModel, SIGNAL(clicked()), this, SLOT(onReadbackWiFiModel()));
    connect(pushButtonApSet, SIGNAL(clicked()), this, SLOT(onSetAp()));
    connect(pushButtonScan,SIGNAL(clicked()),this,SLOT(OnScanWifi()));

    connect(pushButtonSet4GInfo, SIGNAL(clicked()), this, SLOT(onSet4GInfo()));
    connect(pushButtonReadback4G, SIGNAL(clicked()), this, SLOT(onReadback4G()));
    connect(pushButtonGetMcc, SIGNAL(clicked()), this, SLOT(onGetMcc()));
    connect(pushButtonRead4GStatus, SIGNAL(clicked()), this, SLOT(onGet4GStatus()));

    connect(comboBoxCountryID, SIGNAL(currentIndexChanged(int)), this, SLOT(OnComboMccChanged(int)));
    connect(comboBoxCarrierName, SIGNAL(currentIndexChanged(int)), this, SLOT(OnComboCarrierChanged(int)));
    connect(checkBoxCustom, SIGNAL(clicked(bool)), this, SLOT(OnCustom4G(bool)));
    connect(checkBox4g, SIGNAL(clicked(bool)), this, SLOT(OnOpne4G(bool)));
    connect(widgetFightModel, SIGNAL(checkedChanged(bool)), this, SLOT(onFightModelChecked(bool)));
    connect(pushButtonReadbackFlightModel, SIGNAL(clicked()), this, SLOT(onReadbackFlightModel()));

     MARCO_DEFINE_CONTROL_FUNC2(parent->parent(),parent)
    emit sigGetDhcpOrSpecifyIp(0);

     pushButtonSetYuanHengApn->setVisible(false);
     checkBoxCustom->setVisible(false);
    OnCustom4G(true);

    transUi();
}

ControlNetConfigWidget::~ControlNetConfigWidget()
{
    if(pHpptClient != nullptr)
    {
        qDebug() <<"delete pHpptClient in ~NetComm";
        delete pHpptClient;
        pHpptClient=nullptr;
    }
    if(pHpptClientAll != nullptr)
    {
        qDebug() <<"delete pHpptClientAll in ~NetComm";
        delete pHpptClientAll;
        pHpptClientAll=nullptr;
    }
}
void ControlNetConfigWidget::changeEvent(QEvent *event) {
    QWidget::changeEvent(event);
    if(event->type() == QEvent::LanguageChange) transUi();
}
void ControlNetConfigWidget::transUi() {
    lbLanCfg->setText(tr("Wire Enther(RJ45) Configuration"));
    fdDhcp->setText(tr("DHCP"));
    fdSpecifyIp->setText(tr("Specify IP"));
    labelApName->setText(tr("AP name"));
    labelGateway->setText(tr("Gateway"));
    labelWifiName->setText(tr("WiFi name"));
    labelIpAddress->setText(tr("IP Address"));
    labelApPassword->setText(tr("Password"));
    labelDnsAddress->setText(tr("DNS Address"));
    labelMaskAddress->setText(tr("Subnet mask"));
    labelWifiPassword->setText(tr("Password"));
    pushButtonScan->setText(tr("Scan"));
    pushButtonApSet->setText(tr("Set"));
    pushButtonWiFiModel->setText(tr("Set"));
    btnLanSet->setText(tr("Set"));
    pushButtonReadbackWiFiModel->setText(tr("Readback"));
    btnLanGet->setText(tr("Readback"));
    groupBox_3->setTitle(tr("WiFi On/Off"));
    groupBox_4->setTitle(tr("Ap Mode"));
    label_5->setText(tr("WIFI Configuration"));
    lineEditApPassword->setPlaceholderText(tr("Input password"));
    lineEditWifiPassword->setPlaceholderText(tr("Input password"));
    lineEditHotspotName->setPlaceholderText(tr("Input ap name"));
    label->setText(tr("4G/5G Configuration"));
    label_8->setText(tr("Check SIM can automatically match to MCC, and then select carrier get apn param,if not found carrier,you can check the CUSTOM checkbox,then enter custom param."));
    checkBox4g->setText(tr("Open 4G/5G"));


    groupBox_5->setTitle(tr("Set Apn Info,Check SIM can automatically match to MCC, and then select carrier get apn param"));
    checkBoxCustom->setText(tr("CUSTOM"));
    pushButtonGetMcc->setText(tr("Check SIM"));
    label_2->setText(tr("Country ID(mcc):"));
    label_3->setText(tr("Carrier Name"));
    label_4->setText(tr("APN:"));
    label_6->setText(tr("User:"));
    label_7->setText(tr("Password:"));
    pushButtonSet4GInfo->setText(tr("Set"));
    lineEditAPN->setPlaceholderText(tr("APN(Required)"));
    lineEditUser->setPlaceholderText(tr("Not required"));
    lineEditPassword->setPlaceholderText(tr("Not required"));
    pushButtonReadback4G->setText(tr("Readback APN Info"));
    pushButtonReadbackFlightModel->setText(tr("Readback"));
    pushButtonRead4GStatus->setText(tr("Get cellular network status information"));
    label_10->setText(tr("Flight Mode"));
}

void ControlNetConfigWidget::OnDhcpOrSpecifyIp(int iIndex)
{
    if(iIndex==0)
    {
        gBoxSpecifyIp->setEnabled(false);
        fdDns->setStyleSheet("QLineEdit{background:rgba(216,216,216,1);}");
        fdIP->setStyleSheet("QLineEdit{background:rgba(216,216,216,1);}");
        fdMask->setStyleSheet("QLineEdit{background:rgba(216,216,216,1);}");
        fdGateWay->setStyleSheet("QLineEdit{background:rgba(216,216,216,1);}");
        fdDns->MySetColor(QColor::fromRgb(216,216,216));
        fdIP->MySetColor(QColor::fromRgb(216,216,216));
        fdMask->MySetColor(QColor::fromRgb(216,216,216));
        fdGateWay->MySetColor(QColor::fromRgb(216,216,216));
    }
    else {
        gBoxSpecifyIp->setEnabled(true);
        fdDns->setStyleSheet("QLineEdit{background:rgba(255,255,255,1);}");
        fdIP->setStyleSheet("QLineEdit{background:rgba(255,255,255,1);}" );
        fdMask->setStyleSheet("QLineEdit{background:rgba(255,255,255,1);}" );
        fdGateWay->setStyleSheet("QLineEdit{background:rgba(255,255,255,1);}");
        fdDns->MySetColor(QColor::fromRgb(255,255,255));
        fdIP->MySetColor(QColor::fromRgb(255,255,255));
        fdMask->MySetColor(QColor::fromRgb(255,255,255));
        fdGateWay->MySetColor(QColor::fromRgb(255,255,255));
    }

}
void ControlNetConfigWidget::onSelectedDeviceList(QList<LedCard*> *list)
{
    m_pLedlist=list;
    if(m_pLedlist==nullptr)
        return;
    if(m_pLedlist->count()==1)
    {
         if(groupBox_3->isChecked())
            pushButtonScan->setEnabled(true);
     }
    else {
        if(groupBox_3->isChecked())
            pushButtonScan->setEnabled(false);
   }
    //MACRO_DEFINE_CONTROL_FUNC3
            if(m_pLedlist->count()==1)
            {
                 m_pLedCard=list->at(0);
                 m_strUrl="http://"+m_pLedCard->m_strCardIp+":2016/settings";
                 if(m_oldLedlist!=m_pLedCard&&m_bSelected==true)
                 {
                     m_oldLedlist=m_pLedCard;
                    onReadbackAllThisPage();
                 }
            }
}
void  ControlNetConfigWidget::onReadbackAllThisPage()
{
    if(m_pLedlist==nullptr)
        return;
    if(m_pLedlist->count()!=1)
        return;

    QJsonObject json;
    json.insert("_id", "GetEthernet");
    json.insert("_type", "GetEthernet");
    if(gSelCards->count() == 1) {
        auto card = gSelCards->at(0);
        auto reply = Tools::netManager().post(reqForJson("http://"+card->m_strCardIp+":2016/settings"), QJsonDocument{json}.toJson(QJsonDocument::Compact));
        connect(reply, &QNetworkReply::finished, this, [this, reply] {
            QJsonDocument json;
            QString err = checkReplyForJson(reply, &json);
            if(! err.isEmpty()) {
                QMessageBox::critical(gMainWin, tr("Error"), err);
                return;
            }
            if(json["dhcp"].toBool()) {
                fdDhcp->setChecked(true);
                emit sigGetDhcpOrSpecifyIp(0);
            } else {
                fdSpecifyIp->setChecked(true);
                emit sigGetDhcpOrSpecifyIp(1);
            }
            fdIP->setText(json["ipAddr"].toString());
            fdGateWay->setText(json["gateWay"].toString());
            fdMask->setText(json["netMask"].toString());
            fdDns->setText(json["dnsAddr"].toString());
        });
    }

    QJsonObject json1;
    json1.insert("_id", getRandomString(10));
    json1.insert("_type", "GetWifiList");
    HttpPostByTypeJsonObject(pHpptClientAll,m_strUrl,json1);
    comboBox->clear();
    QJsonObject json55;
    json55.insert("_id", getRandomString(10));
    json55.insert("_type", "GetSwitchSimData");
    HttpPostByTypeJsonObject(pHpptClientAll,m_strUrl,json55);
    QJsonObject json2;
    json2.insert("_id", getRandomString(10));
    json2.insert("_type", "IsPortableHotSpot");
    HttpPostByTypeJsonObject(pHpptClientAll,m_strUrl,json2);
    QJsonObject json66;
    json66.insert("_id", getRandomString(10));
    json66.insert("_type", "GetSwitchWiFi");
    HttpPostByTypeJsonObject(pHpptClientAll,m_strUrl,json66);
    QJsonObject json3;
    json3.insert("_id", getRandomString(10));
    json3.insert("_type", "GetAPNList");
    HttpPostByTypeJsonObject(pHpptClientAll,m_strUrl,json3);

    QJsonObject json4;
    json4.insert("_id", getRandomString(10));
    json4.insert("_type", "GetCurrentAPN");
    HttpPostByTypeJsonObject(pHpptClientAll,m_strUrl,json4);

    QJsonObject json5;
    json5.insert("_id", getRandomString(10));
    json5.insert("_type", "GetTimezone");
    HttpPostByTypeJsonObject(pHpptClientAll,m_strUrl,json5);

    QJsonObject json6;
    json6.insert("_id", getRandomString(10));
    json6.insert("_type", "GetFlightModeState");
    HttpPostByTypeJsonObject(pHpptClientAll,m_strUrl,json6);
    MACRO_DEFINE_TIPDLG_ALL_FUCN(tr("Readback"),tr("Success"),tr("failed"))
}
void ControlNetConfigWidget::onSetWiFiModel()
{
    CHECK_CARD_SELECTED

    if(groupBox_3->isChecked())
    {
        comboBox->setStyleSheet("background:rgba(216,216,216,1);");
        lineEditWifiPassword->setStyleSheet("background:rgba(216,216,216,1);");

    }
    else {
        comboBox->setStyleSheet("background:rgba(216,216,216,1);");
        lineEditWifiPassword->setStyleSheet("background:rgba(216,216,216,1);");

    }

    if(m_pLedlist->count()==1)
    {
        if(groupBox_3->isChecked())
        {
            comboBox->setStyleSheet("background:rgba(255,255,255,1);");
            lineEditWifiPassword->setStyleSheet("background:rgba(255,255,255,1);");
                QJsonObject json1;
                json1.insert("_id", getRandomString(10));
                json1.insert("_type", "SetSwitchWiFi");
                json1.insert("enable", true);
                HttpPostByTypeJsonObject(pHpptClient,m_strUrl,json1);
                QJsonObject json;
                json.insert("_id", getRandomString(10));
                json.insert("_type", "ConfigurationWiFi");
                QString strSsid=comboBox->currentText();
                if(strSsid.contains(tr("SignalPower")))
                {
                    int itempOffset=strSsid.indexOf(tr("SignalPower"));
                    strSsid=strSsid.left(itempOffset-4);
                }
                qDebug()<<"strSsid="<<strSsid;
                json.insert("ssid", strSsid);
                json.insert("password", lineEditWifiPassword->text());
                HttpPostByTypeJsonObject(pHpptClient,m_strUrl,json);
                MACRO_DEFINE_TIPDLG_FUCN(tr("ConfigurationWiFi"),tr("Success"),tr("failed"))
        }

    }
    else {
        if(groupBox_3->isChecked())
        {
            QJsonObject json;
            json.insert("_id", getRandomString(10));
            json.insert("_type", "ConfigurationWiFi");
            QString strSsid=comboBox->currentText();
            if(strSsid.contains(tr("SignalPower")))
            {
                int itempOffset=strSsid.indexOf(tr("SignalPower"));
                strSsid=strSsid.left(itempOffset-4);
            }
            qDebug()<<"strSsid="<<strSsid;

            json.insert("ssid", strSsid);
            json.insert("password", lineEditWifiPassword->text());
            emit sigSend(json,tr("ConfigurationWiFi"));
        }
   }
}

void ControlNetConfigWidget:: onReadbackWiFiModel()
{

    CHECK_CARD_SELECTED
            QJsonObject json66;
            json66.insert("_id", getRandomString(10));
            json66.insert("_type", "GetSwitchWiFi");
            HttpPostByTypeJsonObject(pHpptClientAll,m_strUrl,json66);
    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "IsPortableHotSpot");

    MACRO_DEFINE_TIPDLG_FUCN(tr("IsPortableHotSpot"),tr("Success"),tr("failed"))
}
void ControlNetConfigWidget:: OnScanWifi()
{
    CHECK_CARD_SELECTED

    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "GetWifiList");
    comboBox->clear();
    MACRO_DEFINE_TIPDLG_FUCN(tr("GetWifiList"),tr("Success"),tr("failed"))
}
void ControlNetConfigWidget:: onSetAp()
{
    CHECK_CARD_SELECTED
    if(groupBox_4->isChecked())
    {
        QJsonObject json;
        json.insert("_id", getRandomString(10));
        json.insert("_type", "ConfigurationHotSpot");
        json.insert("apName", lineEditHotspotName->text());
        json.insert("password", lineEditApPassword->text());
        MACRO_DEFINE_TIPDLG_FUCN(tr("ConfigurationHotSpot"),tr("Success"),tr("failed"))
    }
}

void ControlNetConfigWidget::onReadback4G()
{
    CHECK_CARD_SELECTED
    lineEditApnInfo->setText("");
    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "GetCurrentAPN");
    MACRO_DEFINE_TIPDLG_FUCN(tr("GetCurrentAPN"),tr("Success"),tr("failed"))
}
void ControlNetConfigWidget::onGetMcc()
{
    m_buttonFlagClick=1;
    CHECK_CARD_SELECTED

    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "GetSIMStatus");
    MACRO_DEFINE_TIPDLG_FUCN(tr("GetSIMStatus"),tr("Success"),tr("failed"))
}
void ControlNetConfigWidget::onGet4GStatus()
{
    m_buttonFlagClick=0;
    CHECK_CARD_SELECTED

    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "GetSIMStatus");
    MACRO_DEFINE_TIPDLG_FUCN(tr("GetSIMStatus"),tr("Success"),tr("failed"))
}
void ControlNetConfigWidget::onSet4GInfo()
{
    CHECK_CARD_SELECTED

    QJsonObject json;
    if(checkBoxCustom->isChecked())
    {
        json.insert("_id", getRandomString(10));
        json.insert("_type", "SetAPN");
        json.insert("carrier", comboBoxCarrierName->currentText());
        json.insert("apn",lineEditAPN->text());
        json.insert("user", lineEditUser->text());
        json.insert("password", lineEditPassword->text());
        json.insert("type","");
        json.insert("server", "");
        json.insert("proxy", "");
        json.insert("port","");
        json.insert("mmsc","");
        json.insert("mmsproxy","");
        json.insert("mmsport","");
    }
    else {
        json.insert("_id", getRandomString(10));
        json.insert("_type", "SetAPN");
        json.insert("carrier", m_4gObj["carrier"].toString());
        json.insert("apn",lineEditAPN->text());
        json.insert("user", lineEditUser->text());
        json.insert("password", lineEditPassword->text());
        json.insert("type", m_4gObj["type"].toString());
        json.insert("server",  m_4gObj["server"].toString());
        json.insert("proxy", m_4gObj["proxy"].toString());
        json.insert("port", m_4gObj["port"].toString());
        json.insert("mmsc", m_4gObj["mmsc"].toString());
        json.insert("mmsproxy", m_4gObj["mmsproxy"].toString());
        json.insert("mmsport", m_4gObj["mmsport"].toString());
    }
     MACRO_DEFINE_TIPDLG_FUCN(tr("SetAPN"),tr("Success"),tr("failed"))
}
//获取到httpPost的应答，如果参数变化需要出发修改控件item的内容
void ControlNetConfigWidget::OnProHttpResponse(QString url, QString postMD5, QByteArray data) {
    Q_UNUSED(postMD5)
    //qDebug()<< url + "\r\n"+m_strUrl;
    if(url == m_strUrl)
    {
        QJsonParseError parseJsonErr;
        QJsonDocument document = QJsonDocument::fromJson(data,&parseJsonErr);
        if(!(parseJsonErr.error == QJsonParseError::NoError))
        {
            if(data.size()==0)
                qDebug()<<"json is empty";
            else
                qDebug()<<tr("DeviceItem OnProHttpResponse 解析json文件错误！");
            return;
        }
        QJsonObject jsonObject = document.object();
        QString strType=jsonObject["_type"].toString();
        if(jsonObject["success"].toBool())
        {
            if(strType == "GetSwitchWiFi")
            {
                groupBox_3->setChecked(jsonObject["enable"].toBool());
            }
            else if(strType == "GetWifiList")
            {
                //QVariant iTemp=jsonObject["wifiList"].toVariant();
                QJsonValue value = jsonObject.take("wifiList");
                QJsonArray array= value.toArray();
                int iSize=array.size();
                QList<wifiInfo> wifInfoiList;
                for(int i=0;i<iSize;i++)
                {
                    QJsonObject obj=array.at(i).toObject();
                   wifiInfo tempwifiInfo;
                   tempwifiInfo.encryptionMethod=obj["encryptionMethod"].toString();
                   tempwifiInfo.signal=obj["signal"].toInt();
                   tempwifiInfo.ssid=obj["ssid"].toString();
                   wifInfoiList.append(tempwifiInfo);
                }
//                qSort(wifInfoiList.begin(),wifInfoiList.end(),[](const wifiInfo &infoA,const wifiInfo &infoB){
//                              return infoA.signal > infoB.signal;});
               for(int j=0;j<iSize;j++)
                   {

                   //QString strSignalPower=QString("%1").arg(wifInfoiList.at(j).signal, 4, 10, QChar('0'));
                   QString strSignalPower=QString::number(wifInfoiList.at(j).signal);
                   comboBox->addItem(wifInfoiList.at(j).ssid+"...["+tr("SignalPower")+":"+strSignalPower+"]");

                    }
            }
            else if(strType == "IsPortableHotSpot")
            {
                QString iTemp=jsonObject["wifi"].toString();
//                if(iTemp.isEmpty())
//                    groupBox_3->setChecked(false);
//                else
//                {
//                    groupBox_3->setChecked(true);
//                }
                QString strKong="";
                comboBox->setCurrentText(iTemp+strKong);
                QString iTempHotSpot=jsonObject["hotSpots"].toString();
                if(iTempHotSpot.isEmpty())
                    groupBox_4->setChecked(false);
                else
                    groupBox_4->setChecked(true);
                lineEditHotspotName->setText(iTempHotSpot);
            }
            else if(strType == "SetAPN")
            {
                onReadback4G();
            }
            else if(strType == "GetSIMStatus") {
                QString str4GStatus="";
                if(m_buttonFlagClick==0) {
                    str4GStatus = tr("状态:");
                    switch(jsonObject["state"].toInt()) {
                    case 0:
                    case 1:
                        str4GStatus+=tr("未知");
                        break;
                    case 2:
                        str4GStatus+=tr("锁定状态，需要用户的PIN码解锁");
                        break;
                    case 3:
                        str4GStatus+=tr("锁定状态，需要用户的PUK码解锁");
                        break;
                    case 4:
                        str4GStatus+=tr("锁定状态，需要网络的PIN码解锁");
                        break;
                    case 5:
                        str4GStatus+=tr("就绪");
                        break;
                    }
                }
                QString strImsi=jsonObject["imsi"].toString();
                if(strImsi.isEmpty()) {
                    str4GStatus+="\r\n"+tr("no checked sim card");
                    QMessageBox::information(gMainWin, tr("Tip"),str4GStatus);
                } else {
                    str4GStatus+="\r\nAPN: "+apn+"\r\nIMSI: "+strImsi+"\r\n";
                    mcc = strImsi.left(3);
                    //comboBoxCountryID->setCurrentText(m_mcc);
                    comboBoxCountryID->setCurrentText("");
                    comboBoxCarrierName->setCurrentText("");
                    lineEditAPN->setText("");
                    lineEditUser->setText("");
                    lineEditPassword->setText("");

                     //SetMcc(m_mcc);
                    str4GStatus+=tr("国家码:");
                    str4GStatus+=jsonObject["countryCode"].toString()+"("+mcc+")\r\n";
                    str4GStatus+=tr("号码:");
                    str4GStatus+=jsonObject["phoneNumber"].toString()+"\r\n";
                    if(m_buttonFlagClick==0)
                    {
                        str4GStatus+=tr("用户:");
                        str4GStatus+=jsonObject["user"].toString()+"\r\n";
                        str4GStatus+=tr("信号:");
                        switch(jsonObject["serviceState"].toInt())
                        {
                            case 0:                    str4GStatus+=tr("信号正常")+"\r\n";                    break;
                            case 1:                    str4GStatus+=tr("不在服务区")+"\r\n";                    break;
                            case 2:                    str4GStatus+=tr("仅限紧急呼叫")+"\r\n";                    break;
                            case 3:                    str4GStatus+=tr("射频已经关闭")+"\r\n";                    break;
                        }
                        str4GStatus+=tr("网络:");
                        switch(jsonObject["networkType"].toInt())
                        {
                            case 0:                    str4GStatus+=tr("网络类型未知")+"\r\n";                    break;
                            case 1:                    str4GStatus+=tr("GPRS网络")+"\r\n";                    break;
                            case 2:                    str4GStatus+=tr("EDGE网络")+"\r\n";                    break;
                            case 3:                    str4GStatus+=tr("UMTS网络")+"\r\n";                    break;
                            case 4:                    str4GStatus+=tr("CDMA网络,IS95A 或 IS95B.")+"\r\n";                    break;
                            case 5:                    str4GStatus+=tr("EVDO网络, revision 0.")+"\r\n";                    break;
                            case 6:                    str4GStatus+=tr("EVDO网络, revision A.")+"\r\n";                    break;
                            case 7:                    str4GStatus+=tr("1xRTT网络")+"\r\n";                    break;
                            case 8:                    str4GStatus+=tr("HSDPA网络")+"\r\n";                    break;
                            case 9:                    str4GStatus+=tr("HSUPA网络")+"\r\n";                    break;
                            case 10:                    str4GStatus+=tr("HSPA网络")+"\r\n";                    break;
                        }
                        str4GStatus+=tr("漫游:");

                        // 是否为漫游
                        if(jsonObject["roaming"].toBool())
                        {
                            str4GStatus+=tr("Yes")+"\r\n";
                        }
                        else
                        {
                            str4GStatus+=tr("No")+"\r\n";
                        }
                        str4GStatus+=tr("数据连接状态:");
                        switch(jsonObject["dataState"].toInt())
                        {
                            case 0:                    str4GStatus+=tr("断开")+"\r\n";                    break;
                            case 1:                    str4GStatus+=tr("正在连接")+"\r\n";                    break;
                            case 2:                    str4GStatus+=tr("已连接")+"\r\n";                    break;
                            case 3:                    str4GStatus+=tr("暂停")+"\r\n";                    break;
                        }
                        str4GStatus+=tr("数据活动休眠状态:");
                        switch(jsonObject["dataActivity"].toInt())
                        {
                            case 0:                    str4GStatus+=tr("活动，但无数据发送和接收")+"\r\n";                    break;
                            case 1:                    str4GStatus+=tr("活动，正在接收数据")+"\r\n";                    break;
                            case 2:                    str4GStatus+=tr("活动，正在发送数据")+"\r\n";                    break;
                            case 3:                    str4GStatus+=tr("活动，正在接收和发送数据")+"\r\n";                    break;
                            case 4:                    str4GStatus+=tr("休眠状态")+"\r\n";                    break;
                        }
                        str4GStatus+=tr("信号强度:");
                        QString strSignalStrength=QString::number(jsonObject["signalStrength"].toInt());
                        str4GStatus+=strSignalStrength+"\r\n";
                    }
                    QMessageBox::information(gMainWin, tr("Tip"),str4GStatus);
                }
            }
            else if(strType == "GetCurrentAPN"){
//                lineEditAPN->setText(jsonObject["apn"].toString());
//                lineEditUser->setText(jsonObject["user"].toString());
//                lineEditPassword->setText(jsonObject["password"].toString());
                apn = jsonObject["apn"].toString();
                if(jsonObject["user"].toString().isEmpty()) lineEditApnInfo->setText(apn);
                else lineEditApnInfo->setText(apn+",USER:"+jsonObject["user"].toString()+",PASSWORD:"+jsonObject["password"].toString());
            }
            else if(strType == "SetSwitchSimData"){
            }
            else if(strType=="ContrFlightMode"){
            }
            else if(strType=="GetFlightModeState"){
                //widgetFightModel->setChecked(jsonObject["result"].toBool());
                widgetFightModel->setCheckedStatus(jsonObject["result"].toBool());
                widgetFightModel->update();
            }
            else if(strType == "GetTimezone"){
                QString strtemp=jsonObject["timezone"].toString();
               // strtemp=strtemp.right(strtemp.length()-strtemp.indexOf("/")-1);
                //comboBox_Timezone->setCurrentText(strtemp);
            }
            else if(strType == "SetTimezone"){

            }


            if(strType!="SetSwitchWiFi")
            {
                if(iEnableFlag==true)
                {
                    iEnableFlag=false;
                    MACRO_ASKTIME_STOP
                    MACRO_POSTING_DLG_UNLOCK

                }

            }

        }
    }
}

void ControlNetConfigWidget::OnProHttpResponseAll(QString url, QString postMD5, QByteArray data)
{
     Q_UNUSED(postMD5)
    //qDebug()<< url + "\r\n"+m_strUrl;
    if(url == m_strUrl)
    {
        QJsonParseError parseJsonErr;
        QJsonDocument document = QJsonDocument::fromJson(data,&parseJsonErr);
        if(!(parseJsonErr.error == QJsonParseError::NoError))
        {
            if(data.size()==0)
                qDebug()<<"json is empty";
            else
                qDebug()<<tr("DeviceItem OnProHttpResponse 解析json文件错误！");
            return;
        }
        QJsonObject jsonObject = document.object();
        QString strType=jsonObject["_type"].toString();
        if(jsonObject["success"].toBool()) {
            if(strType == "GetSwitchWiFi") groupBox_3->setChecked(jsonObject["enable"].toBool());
            else if(strType == "GetWifiList") {
                QJsonValue value = jsonObject.take("wifiList");
                QJsonArray array= value.toArray();
                int iSize=array.size();
                QList<wifiInfo> wifInfoiList;
                for(int i=0;i<iSize;i++)
                {
                    QJsonObject obj=array.at(i).toObject();
                    wifiInfo tempwifiInfo;
                    tempwifiInfo.encryptionMethod=obj["encryptionMethod"].toString();
                    tempwifiInfo.signal=obj["signal"].toInt();
                    tempwifiInfo.ssid=obj["ssid"].toString();
                    wifInfoiList.append(tempwifiInfo);
                }
                for(int j=0;j<iSize;j++)
                {
                    QString strSignalPower=QString::number(wifInfoiList.at(j).signal);
                    comboBox->addItem(wifInfoiList.at(j).ssid+"...["+tr("SignalPower")+":"+strSignalPower+"]");
                }

            }
            else if(strType == "IsPortableHotSpot")
            {
                QString iTemp=jsonObject["wifi"].toString();
                QString strKong="";
                comboBox->setCurrentText(iTemp+strKong);
                QString iTempHotSpot=jsonObject["hotSpots"].toString();
                if(iTempHotSpot.isEmpty())
                    groupBox_4->setChecked(false);
                else
                    groupBox_4->setChecked(true);
                lineEditHotspotName->setText(iTempHotSpot);
                if(iEnableFlag==true)
                {
                    iEnableFlag=false;
                    MACRO_ASKTIME_STOP
                            MACRO_POSTING_DLG_UNLOCK
                }
            }
            else if(strType == "GetAPNList")
            {
                qDebug()<<"GetAPNList";
                if(m_ApnInfoiList.count()<=0)
                {
//                if(m_ApnInfoiList.count()>0)
//                    m_ApnInfoiList.clear();
//                if(comboBoxCountryID->count()>0)
//                    comboBoxCountryID->clear();
//                if(comboBoxCarrierName->count()>0)
//                    comboBoxCarrierName->clear();
                foreach(QJsonValue value, jsonObject["apns"].toArray()) {
                    QJsonObject oApn = value.toObject();
                    QString strtempmcc=oApn["mcc"].toString();
                    bool itempHave=false;
                    for (int i=0;i<comboBoxCountryID->count();i++) {
                        if(strtempmcc==comboBoxCountryID->itemText(i))
                        {
                            itempHave=true;
                            break;
                        }

                    }
                    qDebug()<<strtempmcc;

                    if(!itempHave)
                        comboBoxCountryID->addItem(strtempmcc);
                    m_ApnInfoiList.append(oApn);
                }
                }
                comboBoxCountryID->setCurrentText("");


            }
            else if(strType=="GetFlightModeState")
            {
                //widgetFightModel->setChecked(jsonObject["result"].toBool());
                widgetFightModel->setCheckedStatus(jsonObject["result"].toBool());
                widgetFightModel->update();
            }
            else if(strType == "GetCurrentAPN") {
                apn = jsonObject["apn"].toString();
//                lineEditAPN->setText(strApn);
//                lineEditUser->setText(jsonObject["user"].toString());
//                lineEditPassword->setText(jsonObject["password"].toString());
                if(jsonObject["user"].toString().isEmpty()) lineEditApnInfo->setText(apn);
                else lineEditApnInfo->setText(apn+",USER:"+jsonObject["user"].toString()+",PASSWORD:"+jsonObject["password"].toString());
            }
            else if(strType == "GetTimezone"){
                QString strtemp=jsonObject["timezone"].toString();
                //comboBox_Timezone->setCurrentText(strtemp);
            }
            else if(strType == "SetTimezone"){

            }
            else if(strType == "GetSwitchSimData"){
                bool b=jsonObject["enable"].toBool();
                checkBox4g->setChecked(b);
                pushButtonSet4GInfo->setVisible(b);
                pushButtonReadback4G->setVisible(b);
                pushButtonGetMcc->setVisible(b);
#ifndef MACRO_YUANHENG_VERSION
                comboBoxCountryID->setVisible(b);
                comboBoxCarrierName->setVisible(b);
                //checkBoxCustom->setVisible(b);
                lineEditAPN->setVisible(b);
                lineEditUser->setVisible(b);
                lineEditPassword->setVisible(b);
                label_2->setVisible(b);
                label_3->setVisible(b);
                label_4->setVisible(b);
                label_6->setVisible(b);
                label_7->setVisible(b);
                label_8->setVisible(b);
#else
                lineEditAPN->setVisible(b);

#endif
            }
        }
    }
}
void ControlNetConfigWidget::OnControlTypeSwitchIndexChanged(int index)
{
    if(index==Setting_NetCfg)
    {
        m_bSelected=true;
        if(m_pLedCard!=nullptr)
            onReadbackAllThisPage();
    }
    else {
        m_bSelected=false;
    }
}
void ControlNetConfigWidget::OnComboMccChanged(int index)
{
    Q_UNUSED(index);
    SetMcc(comboBoxCountryID->currentText());
}

void ControlNetConfigWidget::SetMcc(QString strMcc)
{
    qDebug()<<"SetMcc:"<<strMcc;
    if(strMcc.isEmpty())
        return;
    m_MccCarrierInfoiList.clear();
    comboBoxCarrierName->clear();
    foreach(QJsonObject obj, m_ApnInfoiList)
    {
        if(obj["mcc"].toString()==strMcc)
        {
            m_MccCarrierInfoiList.append(obj);
           comboBoxCarrierName->addItem(obj["carrier"].toString());
           // comboBoxCarrierName->insertItem(0,obj["carrier"].toString());
        }
    }
    comboBoxCarrierName->clearEditText();
//    if(comboBoxCarrierName->isEditable()==false)
//    {
//        comboBoxCarrierName->setEditable(true);
//        comboBoxCarrierName->setCurrentText("");
//        comboBoxCarrierName->setEditable(false);
//    }


}
void ControlNetConfigWidget::OnComboCarrierChanged(int index)
{
    if(m_MccCarrierInfoiList.count()>0 && index<m_MccCarrierInfoiList.count())
    {
        m_4gObj=m_MccCarrierInfoiList.at(index);
        lineEditAPN->setText(m_4gObj["apn"].toString());
        lineEditUser->setText(m_4gObj["user"].toString());
        lineEditPassword->setText(m_4gObj["password"].toString());
    }
}
void ControlNetConfigWidget::OnCustom4G(bool b)
{
    //pushButtonGetMcc->setEnabled(b);
    comboBoxCountryID->setEditable(b);
    comboBoxCarrierName->setEditable(b);

    lineEditUser->setReadOnly(!b);
    lineEditPassword->setReadOnly(!b);
    if(b)
    {
        comboBoxCountryID->setStyleSheet("background:rgba(255,255,255,1);");
        comboBoxCarrierName->setStyleSheet("background:rgba(255,255,255,1);");
        lineEditAPN->setStyleSheet("background:rgba(255,255,255,1);");
        lineEditUser->setStyleSheet("background:rgba(255,255,255,1);");
        lineEditPassword->setStyleSheet("background:rgba(255,255,255,1);");

    }
    else {
        comboBoxCountryID->setStyleSheet("background:rgba(216,216,216,1);");
        comboBoxCarrierName->setStyleSheet("background:rgba(216,216,216,1);");
        lineEditAPN->setStyleSheet("background:rgba(216,216,216,1);");
        lineEditUser->setStyleSheet("background:rgba(216,216,216,1);");
        lineEditPassword->setStyleSheet("background:rgba(216,216,216,1);");

    }
#ifndef   MACRO_YUANHENG_VERSION
   lineEditAPN->setReadOnly(false);
#else
   lineEditAPN->setReadOnly(!b);
   lineEditAPN->setStyleSheet("background:rgba(255,255,255,1);");

#endif
}
void ControlNetConfigWidget::OnOpne4G(bool b)
{
    CHECK_CARD_SELECTED
    pushButtonSet4GInfo->setVisible(b);
    pushButtonReadback4G->setVisible(b);
    pushButtonGetMcc->setVisible(b);
    groupBox_5->setVisible(b);
    comboBoxCountryID->setVisible(b);
    comboBoxCarrierName->setVisible(b);
    lineEditAPN->setVisible(b);
    lineEditUser->setVisible(b);
    lineEditPassword->setVisible(b);
    label_2->setVisible(b);
    label_3->setVisible(b);
    label_4->setVisible(b);
    label_6->setVisible(b);
    label_7->setVisible(b);
    label_8->setVisible(b);

    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "SetSwitchSimData");
    json.insert("enable", b);
    MACRO_DEFINE_TIPDLG_FUCN(tr("SetSwitchSimData"),tr("Success"),tr("failed"))
}

void ControlNetConfigWidget::DeletePostingDlg()
{
    qDebug()<<"ControlNetConfigWidget::DeletePostingDlg";
    if(m_PostingDlg!=nullptr)
    {
        delete m_PostingDlg;
        m_PostingDlg=nullptr;
        iEnableFlag=true;
    }
}
void ControlNetConfigWidget::onFightModelChecked(bool b)
{
    CHECK_CARD_SELECTED

    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "ContrFlightMode");
    json.insert("state", b);
    MACRO_DEFINE_TIPDLG_FUCN(tr("ContrFlightMode"),tr("Success"),tr("failed"))
}
void ControlNetConfigWidget::onReadbackFlightModel()
{
    CHECK_CARD_SELECTED

    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "GetFlightModeState");
    MACRO_DEFINE_TIPDLG_FUCN(tr("GetFlightModeState"),tr("Success"),tr("failed"))
}
