#include "ctrlverifyclockpanel.h"
#include "base/waitingdlg.h"
#include "globaldefine.h"
#include "gutil/qgui.h"
#include "tools.h"
#include <QMessageBox>
#include <QLineEdit>
#include <QButtonGroup>

CtrlVerifyClockPanel::CtrlVerifyClockPanel(QWidget *parent) : QWidget(parent) {
    auto vBox = new VBox(this);
    auto hBox = new HBox(vBox);

    label = new QLabel;
    hBox->addWidget(label);

    fdTimezone = new QComboBox;
    fdTimezone->setMinimumWidth(200);
    fdTimezone->addItem("Africa/Brazzaville");//西部非洲标准时间 (布拉扎维)</timezone>
    fdTimezone->addItem("Africa/Cairo");//东欧标准时间 (开罗)</timezone>
    fdTimezone->addItem("Africa/Casablanca");//卡萨布兰卡</timezone>
    fdTimezone->addItem("Africa/Harare");//中部非洲标准时间 (哈拉雷)</timezone>
    fdTimezone->addItem("Africa/Nairobi");//东部非洲标准时间 (内罗毕)</timezone>
    fdTimezone->addItem("Africa/Windhoek");//温得和克</timezone>
    fdTimezone->addItem("America/Anchorage");//>安克雷奇</timezone>
    fdTimezone->addItem("America/Argentina/Buenos_Aires");//布宜诺斯艾利斯</timezone>
    fdTimezone->addItem("America/Barbados");//>大西洋时间 (巴巴多斯)</timezone>
    fdTimezone->addItem("America/Bogota");//>哥伦比亚时间 (波哥大)</timezone>
    fdTimezone->addItem("America/Caracas");//>委内瑞拉时间 (加拉加斯)</timezone>
    fdTimezone->addItem("America/Chicago");//>美国中部时间 (芝加哥)</timezone>
    fdTimezone->addItem("America/Chihuahua");//>奇瓦瓦</timezone>
    fdTimezone->addItem("America/Costa_Rica");//>美国中部时间 (哥斯达黎加)</timezone>
    fdTimezone->addItem("America/Denver");//>美国山区时间 (丹佛)</timezone>
    fdTimezone->addItem("America/Godthab");//戈特霍布</timezone>
    fdTimezone->addItem("America/Los_Angeles");//>美国太平洋时间 (洛杉矶)</timezone>
    fdTimezone->addItem("America/Manaus");//亚马逊标准时间 (马瑙斯)</timezone>
    fdTimezone->addItem("America/Mexico_City");//>美国中部时间 (墨西哥城)</timezone>
    fdTimezone->addItem("America/Montevideo");//乌拉圭时间 (蒙得维的亚)</timezone>
    fdTimezone->addItem("America/New_York");//>美国东部时间 (纽约)</timezone>
    fdTimezone->addItem("America/Phoenix");//>美国山区时间 (凤凰城)</timezone>
    fdTimezone->addItem("America/Regina");//>美国中部时间 (里贾纳)</timezone>
    fdTimezone->addItem("America/Santiago");//圣地亚哥</timezone>
    fdTimezone->addItem("America/Sao_Paulo");//圣保罗</timezone>
    fdTimezone->addItem("America/St_Johns");//纽芬兰时间 (圣约翰)</timezone>
    fdTimezone->addItem("America/Tijuana");//>美国太平洋时间 (提华纳)</timezone>
    fdTimezone->addItem("Asia/Almaty");//阿拉木图</timezone>
    fdTimezone->addItem("Asia/Amman");//东欧标准时间 (安曼)</timezone>
    fdTimezone->addItem("Asia/Baghdad");//巴格达</timezone>
    fdTimezone->addItem("Asia/Baku");//巴库</timezone>
    fdTimezone->addItem("Asia/Bangkok");//曼谷</timezone>
    fdTimezone->addItem("Asia/Beirut");//东欧标准时间 (贝鲁特)</timezone>
    fdTimezone->addItem("Asia/Calcutta");//加尔各答</timezone>
    fdTimezone->addItem("Asia/Colombo");//科伦坡</timezone>
    fdTimezone->addItem("Asia/Dubai");//迪拜</timezone>
    fdTimezone->addItem("Asia/Hong_Kong");//香港时间 (香港)</timezone>
    fdTimezone->addItem("Asia/Irkutsk");//伊尔库茨克时间 (伊尔库茨克)</timezone>
    fdTimezone->addItem("Asia/Jerusalem");//以色列时间 (耶路撒冷)</timezone>
    fdTimezone->addItem("Asia/Kabul");//阿富汗时间 (喀布尔)</timezone>
    fdTimezone->addItem("Asia/Karachi");//卡拉奇</timezone>
    fdTimezone->addItem("Asia/Katmandu");//尼泊尔时间 (加德满都)</timezone>
    fdTimezone->addItem("Asia/Krasnoyarsk");//克拉斯诺亚尔斯克</timezone>
    fdTimezone->addItem("Asia/Kuala_Lumpur");//吉隆坡</timezone>
    fdTimezone->addItem("Asia/Kuwait");//科威特</timezone>
    fdTimezone->addItem("Asia/Magadan");//马加丹时间 (马加丹)</timezone>
    fdTimezone->addItem("Asia/Oral");//乌拉尔</timezone>
    fdTimezone->addItem("Asia/Rangoon");//缅甸时间 (仰光)</timezone>
    fdTimezone->addItem("Asia/Seoul");//首尔</timezone>
    fdTimezone->addItem("Asia/Shanghai");//中国标准时间 (北京)</timezone>
    fdTimezone->addItem("Asia/Taipei");//台北时间 (台北)</timezone>
    fdTimezone->addItem("Asia/Tbilisi");//第比利斯</timezone>
    fdTimezone->addItem("Asia/Tehran");//伊朗标准时间 (德黑兰)</timezone>
    fdTimezone->addItem("Asia/Tokyo");//日本时间 (东京)</timezone>
    fdTimezone->addItem("Asia/Vladivostok");//海参崴时间 (符拉迪沃斯托克)</timezone>
    fdTimezone->addItem("Asia/Yakutsk");//雅库茨克时间 (雅库茨克)</timezone>
    fdTimezone->addItem("Asia/Yekaterinburg");//叶卡捷林堡</timezone>
    fdTimezone->addItem("Asia/Yerevan");//埃里温</timezone>
    fdTimezone->addItem("Atlantic/Azores");//亚述尔群岛</timezone>
    fdTimezone->addItem("Atlantic/Cape_Verde");//佛得角</timezone>
    fdTimezone->addItem("Atlantic/South_Georgia");//南乔治亚</timezone>
    fdTimezone->addItem("Australia/Adelaide");//阿德莱德</timezone>
    fdTimezone->addItem("Australia/Brisbane");//布里斯班</timezone>
    fdTimezone->addItem("Australia/Darwin");//达尔文</timezone>
    fdTimezone->addItem("Australia/Hobart");//霍巴特</timezone>
    fdTimezone->addItem("Australia/Perth");//佩思</timezone>
    fdTimezone->addItem("Australia/Sydney");//悉尼</timezone>
    fdTimezone->addItem("Europe/Amsterdam");//中欧标准时间 (阿姆斯特丹)</timezone>
    fdTimezone->addItem("Europe/Athens");//东欧标准时间 (雅典)</timezone>
    fdTimezone->addItem("Europe/Belgrade");//中欧标准时间 (贝尔格莱德)</timezone>
    fdTimezone->addItem("Europe/Brussels");//中欧标准时间 (布鲁塞尔)</timezone>
    fdTimezone->addItem("Europe/Helsinki");//东欧标准时间 (赫尔辛基)</timezone>
    fdTimezone->addItem("Europe/London");//格林尼治标准时间 (伦敦)</timezone>
    fdTimezone->addItem("Europe/Minsk");//明斯克</timezone>
    fdTimezone->addItem("Europe/Moscow");//莫斯科</timezone>
    fdTimezone->addItem("Europe/Sarajevo");//中欧标准时间 (萨拉热窝)</timezone>
    fdTimezone->addItem("Pacific/Auckland");//奥克兰</timezone>
    fdTimezone->addItem("Pacific/Fiji");//斐济</timezone>
    fdTimezone->addItem("Pacific/Guam");//关岛</timezone>
    fdTimezone->addItem("Pacific/Honolulu");
    fdTimezone->addItem("Pacific/Majuro");
    fdTimezone->addItem("Pacific/Midway");
    fdTimezone->addItem("Pacific/Tongatapu");//东加塔布</timezone>
    hBox->addWidget(fdTimezone);

    fdTimeZoneSet = new QPushButton;
    connect(fdTimeZoneSet, &QPushButton::clicked, this, [this] {
        if(gSelCards->isEmpty()) {
            QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
            return;
        }
        QJsonObject json;
        json.insert("_id", "SetTimezone");
        json.insert("_type", "SetTimezone");
        json.insert("timezone", fdTimezone->currentText());
        if(gSelCards->count() == 1) {
            auto waitingDlg = new WaitingDlg(this, tr("SetTimezone"));
            Def_CtrlReqPre
            connect(reply, &QNetworkReply::finished, this, [reply, waitingDlg] {
                Def_CtrlSetReqAfter
            });
        } else {
            foreach(auto card, *gSelCards) {
                Def_CtrlSetMulti(tr("SetTimezone"))
            }
        }
    });
    hBox->addWidget(fdTimeZoneSet);
    hBox->addSpacing(20);

    lbLang = new QLabel;
    hBox->addWidget(lbLang);

    fdIsEn = new QRadioButton("English");
    hBox->addWidget(fdIsEn);

    fdIsCn = new QRadioButton("中文");
    hBox->addWidget(fdIsCn);

    btnLangSet = new QPushButton;
    connect(btnLangSet, &QPushButton::clicked, this, [this] {
        if(gSelCards->isEmpty()) {
            QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
            return;
        }
        QJsonObject json;
        json.insert("_id", "SetLanguage");
        json.insert("_type", "SetLanguage");
        json.insert("language", fdIsCn->isChecked() ? 1 : 0);
        if(gSelCards->count() == 1) {
            auto waitingDlg = new WaitingDlg(this, "设置语言");
            Def_CtrlReqPre
            connect(reply, &QNetworkReply::finished, this, [reply, waitingDlg] {
                Def_CtrlSetReqAfter
            });
        } else {
            foreach(auto card, *gSelCards) {
                Def_CtrlSetMulti("设置语言")
            }
        }
    });
    hBox->addWidget(btnLangSet);

    btnLangGet = new QPushButton;
    connect(btnLangGet, &QPushButton::clicked, this, [this] {
        if(gSelCards->isEmpty()) {
            QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
            return;
        }
        QJsonObject json;
        json.insert("_id", "GetLanguage");
        json.insert("_type", "GetLanguage");
        if(gSelCards->count() == 1) {
            auto waitingDlg = new WaitingDlg(this, "获取语言");
            Def_CtrlReqPre
            connect(reply, &QNetworkReply::finished, this, [this, reply, waitingDlg] {
                Def_CtrlSingleGetReply
                waitingDlg->success();
                if(json["language"].toInt()==1) fdIsCn->setChecked(true);
                else fdIsEn->setChecked(true);
            });
        } else {
            foreach(auto card, *gSelCards) {
                auto reply = Tools::netManager().post(reqForJson("http://"+card->m_strCardIp+":2016/settings"), QJsonDocument{json}.toJson(QJsonDocument::Compact));
                auto cardId = card->m_strCardId;
                connect(reply, &QNetworkReply::finished, this, [reply, cardId] {
                    QJsonDocument json;
                    QString err = checkReplyForJson(reply, &json);
                    if(err.isEmpty()) err = json["language"].toInt()==1 ? "中文" : "英文";
                    gFdResInfo->append(cardId+" 获取语言 "+err);
                });
            }
        }
    });
    hBox->addWidget(btnLangGet);
    hBox->addStretch();


    labelVerifyClockConfiguration = new QLabel;
    labelVerifyClockConfiguration->setAlignment(Qt::AlignCenter);
    vBox->addWidget(labelVerifyClockConfiguration);

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

    hBox = new HBox(vBox);
    auto vvv = new VBox(hBox);

    btnSyncTime = new QPushButton(tr("Verify to Computer time"));
    btnSyncTime->setMinimumSize(QSize(60, 30));
    connect(btnSyncTime, &QPushButton::clicked, this, [this] {
        if(gSelCards->isEmpty()) {
            QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
            return;
        }
        QJsonObject json;
        json.insert("_id", "SyncTime");
        json.insert("_type", "SyncTime");
        json.insert("time", QDateTime::currentDateTime().toMSecsSinceEpoch());
        if(gSelCards->count() == 1) {
            auto waitingDlg = new WaitingDlg(this, tr("SyncTime"));
            Def_CtrlReqPre
            connect(reply, &QNetworkReply::finished, this, [reply, waitingDlg] {
                Def_CtrlSetReqAfter
            });
        } else {
            foreach(auto card, *gSelCards) {
                Def_CtrlSetMulti(tr("SyncTime"))
            }
        }
    });
    vvv->addWidget(btnSyncTime);

    groupBox_5 = new QGroupBox;
    vvv->addWidget(groupBox_5);

    auto vvvv = new QVBoxLayout(groupBox_5);
    vvvv->addStretch();

    btnDateGet = new QPushButton;
    btnDateGet->setMinimumSize(QSize(0, 30));
    connect(btnDateGet, &QPushButton::clicked, this, [this] {
        if(gSelCards->isEmpty()) {
            QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
            return;
        }
        QJsonObject json;
        json.insert("_id", "GetControllerDate");
        json.insert("_type", "GetControllerDate");
        if(gSelCards->count() == 1) {
            auto waitingDlg = new WaitingDlg(this, tr("GetControllerDate"));
            Def_CtrlReqPre
            connect(reply, &QNetworkReply::finished, this, [this, reply, waitingDlg] {
                Def_CtrlSingleGetReply
                waitingDlg->success();
                labelCurTime->setText(json["date"].toString());
            });
        } else {
            foreach(auto card, *gSelCards) {
                auto reply = Tools::netManager().post(reqForJson("http://"+card->m_strCardIp+":2016/settings"), QJsonDocument{json}.toJson(QJsonDocument::Compact));
                auto cardId = card->m_strCardId;
                connect(reply, &QNetworkReply::finished, this, [reply, cardId] {
                    QJsonDocument json;
                    QString err = checkReplyForJson(reply, &json);
                    if(err.isEmpty()) err = json["date"].toString();
                    gFdResInfo->append(cardId+" "+tr("GetControllerDate")+" "+err);
                });
            }
        }
    });
    vvvv->addWidget(btnDateGet);

    labelCurTime = new QLabel;
    vvvv->addWidget(labelCurTime);
    vvvv->addStretch();


    hBox->addSpacing(20);


    groupBox = new QGroupBox;
    groupBox->setCheckable(true);
    vvv = new VBox(groupBox);
    vvv->setSpacing(3);

    auto hhh = new HBox(vvv);
    fdIsLora = new QRadioButton("Lora");
    hhh->addWidget(fdIsLora);
    fdIsGPS = new QRadioButton("GPS");
    hhh->addWidget(fdIsGPS);
    fdIsNTP = new QRadioButton("NTP");
    hhh->addWidget(fdIsNTP);
    fdIsLan = new QRadioButton;
    hhh->addWidget(fdIsLan);

    horizontalLayout_5 = new QHBoxLayout();
    horizontalLayout_5->setContentsMargins(0, 0, 0, 0);

    labelSyncTimeInterval = new QLabel(groupBox);
    horizontalLayout_5->addWidget(labelSyncTimeInterval);

    lineEdit_3 = new QLineEdit(groupBox);
    lineEdit_3->setMinimumSize(QSize(160, 30));
    lineEdit_3->setMaximumSize(QSize(160, 16777215));
    horizontalLayout_5->addWidget(lineEdit_3);

    labelmintime = new QLabel(groupBox);

    horizontalLayout_5->addWidget(labelmintime);


    vvv->addLayout(horizontalLayout_5);

    horizontalLayout_9 = new QHBoxLayout();
    horizontalLayout_9->setContentsMargins(-1, 0, -1, -1);
    labelIdCode = new QLabel(groupBox);

    horizontalLayout_9->addWidget(labelIdCode);

    lineEditIdCode = new QLineEdit(groupBox);
    lineEditIdCode->setMinimumSize(QSize(0, 30));
    lineEditIdCode->setStyleSheet(QString::fromUtf8("background-color: #FFFFFF;"));

    horizontalLayout_9->addWidget(lineEditIdCode);

    vvv->addLayout(horizontalLayout_9);

    horizontalLayout_10 = new QHBoxLayout();
    horizontalLayout_10->setContentsMargins(-1, 0, -1, -1);
    labelTimeOffset = new QLabel(groupBox);

    horizontalLayout_10->addWidget(labelTimeOffset);

    lineEditTimeOffset = new QLineEdit(groupBox);
    lineEditTimeOffset->setMinimumSize(QSize(0, 30));
    lineEditTimeOffset->setStyleSheet(QString::fromUtf8("background-color: #FFFFFF;"));

    horizontalLayout_10->addWidget(lineEditTimeOffset);


    vvv->addLayout(horizontalLayout_10);

    horizontalLayout_13 = new QHBoxLayout();
    horizontalLayout_13->setContentsMargins(-1, 0, -1, -1);
    checkBoxBrightness = new QCheckBox(groupBox);

    horizontalLayout_13->addWidget(checkBoxBrightness);

    checkBoxVolume = new QCheckBox(groupBox);

    horizontalLayout_13->addWidget(checkBoxVolume);

    checkBoxScreenSwitch = new QCheckBox(groupBox);

    horizontalLayout_13->addWidget(checkBoxScreenSwitch);


    vvv->addLayout(horizontalLayout_13);

    horizontalLayout_7 = new QHBoxLayout();
    horizontalLayout_7->setContentsMargins(-1, 0, -1, -1);
    btnSyncSet = new QPushButton(groupBox);
    btnSyncSet->setMinimumSize(QSize(60, 30));
    connect(btnSyncSet, &QPushButton::clicked, this, [this] {
        if(gSelCards->isEmpty()) {
            QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
            return;
        }
        QJsonObject json;
        json.insert("_id", "SetingSyncMethod");
        json.insert("_type", "SetingSyncMethod");
        json.insert("ntpServer", labelNtpServerAddress->text());
        json.insert("delaySync", lineEditTimeOffset->text().toInt());
        json.insert("identificationCode",lineEditIdCode->text());
        json.insert("brightness", checkBoxBrightness->isChecked() ? "serial" : "none");
        json.insert("volume", checkBoxVolume->isChecked() ? "serial" : "none");
        json.insert("screenSwitch", checkBoxScreenSwitch->isChecked() ? "serial" : "none");
        if(fdIsLora->isChecked()) json.insert("time", "serial");
        else if(fdIsNTP->isChecked()) {
            json.insert("time", "ntp");
            json.insert("checkNtpTime", lineEdit_3->text().toInt());
        } else if(fdIsGPS->isChecked()) json.insert("time", "gps");
        else if(fdIsLan->isChecked()) json.insert("time", "Lan");
        if(gSelCards->count() == 1) {
            auto waitingDlg = new WaitingDlg(this, tr("SetingSyncMethod"));
            Def_CtrlReqPre
            connect(reply, &QNetworkReply::finished, this, [reply, waitingDlg] {
                Def_CtrlSetReqAfter
            });
        } else {
            foreach(auto card, *gSelCards) {
                Def_CtrlSetMulti(tr("SetingSyncMethod"))
            }
        }
    });
    horizontalLayout_7->addWidget(btnSyncSet);

    fdSyncGet = new QPushButton;
    fdSyncGet->setMinimumSize(QSize(60, 30));
    connect(fdSyncGet, &QPushButton::clicked, this, [this] {
        if(gSelCards->isEmpty()) {
            QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
            return;
        }
        QJsonObject json;
        json.insert("_id", "GetingSyncMethod");
        json.insert("_type", "GetingSyncMethod");
        if(gSelCards->count() == 1) {
            auto waitingDlg = new WaitingDlg(this, tr("GetingSyncMethod"));
            Def_CtrlReqPre
            connect(reply, &QNetworkReply::finished, this, [this, reply, waitingDlg] {
                Def_CtrlSingleGetReply
                waitingDlg->success();
                dealGetSync(json);
            });
        } else {
            foreach(auto card, *gSelCards) {
                auto reply = Tools::netManager().post(reqForJson("http://"+card->m_strCardIp+":2016/settings"), QJsonDocument{json}.toJson(QJsonDocument::Compact));
                auto cardId = card->m_strCardId;
                connect(reply, &QNetworkReply::finished, this, [reply, cardId] {
                    QJsonDocument json;
                    QByteArray data;
                    QString err = checkReplyForJson(reply, &json, &data);
                    if(err.isEmpty()) {
                        QString strOtherSyncItem = tr("screenSwitch") + ":" + (json["screenSwitch"].toBool() ? tr("YES") : tr("NO"))
                                                + " " + tr("volume") + ":" + (json["volume"].toBool() ? tr("YES") : tr("NO"))
                                                + " " + tr("brightness") + ":" + (json["brightness"].toBool() ? tr("YES") : tr("NO"));
                        QString lastSynchronousTime = QDateTime::fromMSecsSinceEpoch(json["lastSynchronousTime"].toDouble()).toString("yyyy-MM-dd HH:mm:ss");
                        QString time = json["time"].toString();
                        QString lower = time.toLower();
                        if(lower=="serial") {
                            err = "Lora\n    "+tr("identificationCode")+": "+json["identificationCode"].toString()+"\n    "
                                    +tr("delaySync")+": "+QString::number(json["delaySync"].toInt())+tr("msec")+"\n    "
                                    +tr("OtherSyncItem")+": "+strOtherSyncItem+"\n    "
                                    +tr("lastSynchronousTime")+": "+lastSynchronousTime;
                        } else if(lower=="ntp") {
                            err = time+"\n    "+tr("checkNtpTime")+": "+QString::number(json["checkNtpTime"].toInt())+"\n    "
                                    +tr("OtherSyncItem")+": "+strOtherSyncItem+"\n    "
                                    +tr("lastSynchronousTime")+": "+lastSynchronousTime;
                        } else if(lower=="gps") {
                            err = time+"\n    "+tr("OtherSyncItem")+": "+strOtherSyncItem+"\n    "+tr("lastSynchronousTime")+": "+lastSynchronousTime;
                        } else if(lower=="lan") {
                            err = tr("LAN")+"\n    "+tr("identificationCode")+": "+json["identificationCode"].toString()+"\n    "
                                    +tr("delaySync")+": "+QString::number(json["delaySync"].toInt())+tr("msec")+"\n    "
                                    +tr("OtherSyncItem")+": "+strOtherSyncItem+"\n    "
                                    +tr("lastSynchronousTime")+": "+lastSynchronousTime;
                        } else {
                            err = time+"\n    "+data;
                        }
                    }
                    gFdResInfo->append(cardId+" "+tr("GetingSyncMethod")+" "+err);
                });
            }
        }
    });
    horizontalLayout_7->addWidget(fdSyncGet);

    vvv->addLayout(horizontalLayout_7);
    vvv->addStretch();

    groupBox_4 = new QGroupBox(groupBox);
    groupBox_4->setMinimumSize(QSize(0, 20));
    horizontalLayout_11 = new QHBoxLayout(groupBox_4);
    horizontalLayout_11->setSpacing(6);
    horizontalLayout_11->setContentsMargins(0, 0, 0, 0);
    fdMaster = new QRadioButton(groupBox_4);

    buttonGroup_2 = new QButtonGroup(this);
    buttonGroup_2->addButton(fdMaster);
    fdMaster->setChecked(true);

    horizontalLayout_11->addWidget(fdMaster, 0, Qt::AlignHCenter);

    fdSlave = new QRadioButton(groupBox_4);
    buttonGroup_2->addButton(fdSlave);

    horizontalLayout_11->addWidget(fdSlave, 0, Qt::AlignHCenter);


    vvv->addWidget(groupBox_4);

    horizontalLayout_12 = new QHBoxLayout();
    horizontalLayout_12->setContentsMargins(-1, 0, -1, -1);
    btnLoraMasterSet = new QPushButton(groupBox);
    btnLoraMasterSet->setMinimumSize(QSize(0, 30));
    connect(btnLoraMasterSet, &QPushButton::clicked, this, [this] {
        if(gSelCards->isEmpty()) {
            QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
            return;
        }
        QJsonObject json;
        json.insert("_id", "MasterSlaveSwitch");
        json.insert("_type", "MasterSlaveSwitch");
        bool isMaster = buttonGroup_2->checkedId()==0;
        json.insert("master", isMaster);
        if(gSelCards->count() == 1) {
            auto waitingDlg = new WaitingDlg(this, isMaster ? tr("MasterSwitch ") : tr("SlaveSwitch "));
            Def_CtrlReqPre
            connect(reply, &QNetworkReply::finished, this, [reply, waitingDlg] {
                Def_CtrlSetReqAfter
            });
        } 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, isMaster] {
                    QString err = checkReplyForJson(reply);
                    gFdResInfo->append(card->m_strCardId+" "+(isMaster ? tr("MasterSwitch ") : tr("SlaveSwitch "))+" "+(err.isEmpty()?QCoreApplication::translate("Def","Success"):err));
                });
            }
        }
    });
    horizontalLayout_12->addWidget(btnLoraMasterSet);

    btnLoraMasterGet = new QPushButton(groupBox);
    btnLoraMasterGet->setMinimumSize(QSize(0, 30));
    connect(btnLoraMasterGet, &QPushButton::clicked, this, [this] {
        if(gSelCards->isEmpty()) {
            QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
            return;
        }
        QJsonObject json;
        json.insert("_id", "IsMasterSlave");
        json.insert("_type", "IsMasterSlave");
        if(gSelCards->count() == 1) {
            auto waitingDlg = new WaitingDlg(this, tr("IsMasterSlave"));
            Def_CtrlReqPre
            connect(reply, &QNetworkReply::finished, this, [this, reply, waitingDlg] {
                Def_CtrlSingleGetReply
                waitingDlg->success();
                bool isMaster = json["result"].toBool();
                fdMaster->setChecked(isMaster);
                fdSlave->setChecked(! isMaster);
            });
        } else {
            foreach(auto card, *gSelCards) {
                auto reply = Tools::netManager().post(reqForJson("http://"+card->m_strCardIp+":2016/settings"), QJsonDocument{json}.toJson(QJsonDocument::Compact));
                auto cardId = card->m_strCardId;
                connect(reply, &QNetworkReply::finished, this, [reply, cardId] {
                    QJsonDocument json;
                    QString err = checkReplyForJson(reply, &json);
                    if(err.isEmpty()) err = json["result"].toBool() ? tr("Master") : tr("Slave");
                    gFdResInfo->append(cardId+" "+tr("Lora identity")+" "+err);
                });
            }
        }
    });
    horizontalLayout_12->addWidget(btnLoraMasterGet);

    vvv->addLayout(horizontalLayout_12);

    hBox->addWidget(groupBox);

    hBox->addSpacing(20);

    groupNTP = new QGroupBox;
    vvv = new VBox(groupNTP);

    hhh = new HBox(vvv);
    labelNtpServerAddress = new QLabel(groupNTP);
    hhh->addWidget(labelNtpServerAddress);

    fdNtpServer = new QLineEdit(groupNTP);
    fdNtpServer->setMinimumSize(QSize(160, 30));
    fdNtpServer->setMaximumSize(QSize(160, 16777215));
    fdNtpServer->setStyleSheet(QString::fromUtf8("background-color: #FFFFFF;"));
    hhh->addWidget(fdNtpServer);
    hhh->addStretch();

    vvv->addSpacing(20);

    hhh = new HBox(vvv);
    btnNtpSet = new QPushButton;
    btnNtpSet->setMinimumSize(QSize(60, 30));
    connect(btnNtpSet, &QPushButton::clicked, this, [this] {
        if(gSelCards->isEmpty()) {
            QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
            return;
        }
        QJsonObject json;
        json.insert("_id", "SetNtpServer");
        json.insert("_type", "SetNtpServer");
        json.insert("ntpServer", fdNtpServer->text());
        if(gSelCards->count() == 1) {
            auto waitingDlg = new WaitingDlg(this, tr("SetNtpServer"));
            Def_CtrlReqPre
            connect(reply, &QNetworkReply::finished, this, [reply, waitingDlg] {
                Def_CtrlSetReqAfter
            });
        } else {
            foreach(auto card, *gSelCards) {
                Def_CtrlSetMulti(tr("SetNtpServer"))
            }
        }
    });
    hhh->addWidget(btnNtpSet);

    btnNtpGet = new QPushButton;
    btnNtpGet->setMinimumSize(QSize(60, 30));
    connect(btnNtpGet, &QPushButton::clicked, this, [this] {
        if(gSelCards->isEmpty()) {
            QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
            return;
        }
        QJsonObject json;
        json.insert("_id", "GetNtpServer");
        json.insert("_type", "GetNtpServer");
        if(gSelCards->count() == 1) {
            auto waitingDlg = new WaitingDlg(this, tr("GetNtpServer"));
            Def_CtrlReqPre
            connect(reply, &QNetworkReply::finished, this, [this, reply, waitingDlg] {
                Def_CtrlSingleGetReply
                waitingDlg->success();
                fdNtpServer->setText(json["ntpServer"].toString());
            });
        } else {
            foreach(auto card, *gSelCards) {
                auto reply = Tools::netManager().post(reqForJson("http://"+card->m_strCardIp+":2016/settings"), QJsonDocument{json}.toJson(QJsonDocument::Compact));
                auto cardId = card->m_strCardId;
                connect(reply, &QNetworkReply::finished, this, [reply, cardId] {
                    QJsonDocument json;
                    QString err = checkReplyForJson(reply, &json);
                    if(err.isEmpty()) err = json["ntpServer"].toString();
                    gFdResInfo->append(cardId+" "+tr("GetNtpServer")+" "+err);
                });
            }
        }
    });
    hhh->addWidget(btnNtpGet);

    vvv->addStretch();

    hBox->addWidget(groupNTP);
    hBox->addStretch();

    vBox->addStretch();

    btnNtpSet->setProperty("ssType", "progManageTool");
    btnSyncSet->setProperty("ssType", "progManageTool");
    btnSyncTime->setProperty("ssType", "progManageTool");
    fdSyncGet->setProperty("ssType", "progManageTool");
    btnNtpGet->setProperty("ssType", "progManageTool");

    btnLoraMasterSet->setProperty("ssType", "progManageTool");
    btnLoraMasterGet->setProperty("ssType", "progManageTool");
    btnDateGet->setProperty("ssType", "progManageTool");
    fdTimeZoneSet->setProperty("ssType", "progManageTool");
    buttonGroup_2->setId(fdMaster, 0);
    buttonGroup_2->setId(fdSlave, 1);

    fdIsLora->setChecked(true);
    labelIdCode->setVisible(true);
    lineEditIdCode->setVisible(true);
    labelTimeOffset->setVisible(true);
    lineEditTimeOffset->setVisible(true);
    lineEdit_3->setVisible(false);
    labelSyncTimeInterval->setVisible(false);
    labelmintime->setVisible(false);

    //按钮事件绑定
    connect(fdIsGPS,SIGNAL(clicked()),this,SLOT(OnRadioButton()));
    connect(fdIsLora,SIGNAL(clicked()),this,SLOT(OnRadioButton2()));
    connect(fdIsNTP,SIGNAL(clicked()),this,SLOT(OnRadioButton3()));
    connect(fdIsLan,SIGNAL(clicked()),this,SLOT(OnRadioButton6()));

    connect(gDevicePanel, &DevicePanel::sigSelectedDeviceList, this, [this] {
        if(isVisible()) init();
    });
    transUi();
}

void CtrlVerifyClockPanel::showEvent(QShowEvent *event) {
    QWidget::showEvent(event);
    init();
}
void CtrlVerifyClockPanel::init() {
    bool isSingle = gSelCards->count()==1;
    if(! isSingle) return;
    auto card = gSelCards->at(0);

    QJsonObject json;
    json.insert("_id", "GetingSyncMethod");
    json.insert("_type", "GetingSyncMethod");
    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()) return;
        dealGetSync(json);
    });

    json = QJsonObject();
    json.insert("_id", "GetTimezone");
    json.insert("_type", "GetTimezone");
    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()) return;
        fdTimezone->setCurrentText(json["timezone"].toString());
    });

    json = QJsonObject();
    json.insert("_id", "GetLanguage");
    json.insert("_type", "GetLanguage");
    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()) return;
        if(json["language"].toInt()==1) fdIsCn->setChecked(true);
        else fdIsEn->setChecked(true);
    });
}
void CtrlVerifyClockPanel::changeEvent(QEvent *event) {
    QWidget::changeEvent(event);
    if(event->type() == QEvent::LanguageChange) transUi();
}
void CtrlVerifyClockPanel::transUi() {
    btnDateGet->setText(tr("Readback"));
    btnLangGet->setText(tr("Readback"));
    btnLangSet->setText(tr("Set"));
    btnLoraMasterGet->setText(tr("Readback"));
    btnLoraMasterSet->setText(tr("Set"));
    btnNtpGet->setText(tr("Readback"));
    btnNtpSet->setText(tr("Set"));
    btnSyncTime->setText(tr("Verify to Computer time"));
    checkBoxBrightness->setText(tr("Brightness"));
    checkBoxScreenSwitch->setText(tr("Screen on/off"));
    checkBoxVolume->setText(tr("Volume"));
    fdIsLan->setText(tr("LAN"));
    fdNtpServer->setPlaceholderText(tr("NTP Server address"));
    fdSyncGet->setText(tr("Readback"));
    fdTimeZoneSet->setText(tr("Set"));
    groupBox->setTitle(tr("Enable Synchronous playing"));
    groupBox_5->setTitle(tr("Cur time of controller"));
    groupNTP->setTitle(tr("NTP Server"));
    label->setText(tr("TimeZone"));
    labelIdCode->setText(tr("Identification Code"));
    labelNtpServerAddress->setText(tr("NTP Server address"));
    labelSyncTimeInterval->setText(tr("Sync time interval"));
    labelTimeOffset->setText(tr("Time offset(msec)"));
    labelVerifyClockConfiguration->setText(tr("Verify Clock Configuration"));
    labelmintime->setText(tr("(min/time)"));
    lbLang->setText(tr("Language:"));
    lineEditIdCode->setPlaceholderText(tr("identification code"));
    lineEdit_3->setPlaceholderText(tr("Sync time interval"));
    btnSyncSet->setText(tr("Set"));
    fdMaster->setText(tr("Master"));
    fdSlave->setText(tr("Slave"));
}
void CtrlVerifyClockPanel::dealGetSync(QJsonDocument &json) {
    QString strType = json["time"].toString().toLower();
    if(strType=="serial" || strType=="lan") {
        if(strType=="serial") fdIsLora->setChecked(true);
        else fdIsLan->setChecked(true);
        lineEditIdCode->setText(json["identificationCode"].toString());
        lineEditTimeOffset->setText(QString::number(json["delaySync"].toInt()));
        labelIdCode->setVisible(true);
        lineEditIdCode->setVisible(true);
        labelTimeOffset->setVisible(true);
        lineEditTimeOffset->setVisible(true);
        lineEdit_3->setVisible(false);
        labelSyncTimeInterval->setVisible(false);
        labelmintime->setVisible(false);
        checkBoxScreenSwitch->setChecked(json["screenSwitch"].toString()!="NONE");
        checkBoxVolume->setChecked(json["volume"].toString()!="NONE");
        checkBoxBrightness->setChecked(json["brightness"].toString()!="NONE");
        checkBoxScreenSwitch->setVisible(true);
        checkBoxVolume->setVisible(true);
        checkBoxBrightness->setVisible(true);
        btnSyncSet->setVisible(true);
        fdSyncGet->setVisible(true);
    } else if(strType=="ntp") {
        fdIsNTP->setChecked(true);
        lineEdit_3->setText(QString::number(json["checkNtpTime"].toInt()));
        labelIdCode->setVisible(false);
        lineEditIdCode->setVisible(false);
        labelTimeOffset->setVisible(false);
        lineEditTimeOffset->setVisible(false);
        lineEdit_3->setVisible(true);
        labelSyncTimeInterval->setVisible(true);
        labelmintime->setVisible(true);
        groupBox_4->setVisible(false);
        fdMaster->setVisible(false);
        fdSlave->setVisible(false);
        btnLoraMasterSet->setVisible(false);
        btnLoraMasterGet->setVisible(false);
        checkBoxScreenSwitch->setVisible(false);
        checkBoxVolume->setVisible(false);
        checkBoxBrightness->setVisible(false);
        btnSyncSet->setVisible(true);
        fdSyncGet->setVisible(true);
    } else if(strType=="gps") {
        fdIsGPS->setChecked(true);
        labelIdCode->setVisible(false);
        lineEditIdCode->setVisible(false);
        labelTimeOffset->setVisible(false);
        lineEditTimeOffset->setVisible(false);
        labelSyncTimeInterval->setVisible(false);
        lineEdit_3->setVisible(false);
        labelmintime->setVisible(false);
        groupBox_4->setVisible(false);
        fdMaster->setVisible(false);
        fdSlave->setVisible(false);
        btnLoraMasterSet->setVisible(false);
        btnLoraMasterGet->setVisible(false);
        checkBoxScreenSwitch->setVisible(false);
        checkBoxVolume->setVisible(false);
        checkBoxBrightness->setVisible(false);
        btnSyncSet->setVisible(true);
        fdSyncGet->setVisible(true);
    }
}
void CtrlVerifyClockPanel::OnRadioButton()//gps
{
    labelIdCode->setVisible(false);
    lineEditIdCode->setVisible(false);
    labelTimeOffset->setVisible(false);
    lineEditTimeOffset->setVisible(false);
    labelSyncTimeInterval->setVisible(false);
    lineEdit_3->setVisible(false);
    labelmintime->setVisible(false);
    groupBox_4->setVisible(false);
    fdMaster->setVisible(false);
    fdSlave->setVisible(false);
    btnLoraMasterSet->setVisible(false);
    btnLoraMasterGet->setVisible(false);
    checkBoxScreenSwitch->setVisible(false);
    checkBoxVolume->setVisible(false);
    checkBoxBrightness->setVisible(false);
    btnSyncSet->setVisible(true);
    fdSyncGet->setVisible(true);
}
void CtrlVerifyClockPanel::OnRadioButton2()//lora
{
    labelIdCode->setVisible(true);
    lineEditIdCode->setVisible(true);
    labelTimeOffset->setVisible(true);
    lineEditTimeOffset->setVisible(true);
    lineEdit_3->setVisible(false);
    labelSyncTimeInterval->setVisible(false);
    labelmintime->setVisible(false);
    groupBox_4->setVisible(true);
    fdMaster->setVisible(true);
    fdSlave->setVisible(true);
    btnLoraMasterSet->setVisible(true);
    btnLoraMasterGet->setVisible(true);
    checkBoxScreenSwitch->setVisible(true);
    checkBoxVolume->setVisible(true);
    checkBoxBrightness->setVisible(true);
    btnSyncSet->setVisible(true);
    fdSyncGet->setVisible(true);
}

void CtrlVerifyClockPanel::OnRadioButton3()//ntp
{
    labelIdCode->setVisible(false);
    lineEditIdCode->setVisible(false);
    labelTimeOffset->setVisible(false);
    lineEditTimeOffset->setVisible(false);
    lineEdit_3->setVisible(true);
    labelSyncTimeInterval->setVisible(true);
    labelmintime->setVisible(true);
    groupBox_4->setVisible(false);
    fdMaster->setVisible(false);
    fdSlave->setVisible(false);
    btnLoraMasterSet->setVisible(false);
    btnLoraMasterGet->setVisible(false);
    checkBoxScreenSwitch->setVisible(false);
    checkBoxVolume->setVisible(false);
    checkBoxBrightness->setVisible(false);
    btnSyncSet->setVisible(true);
    fdSyncGet->setVisible(true);
}
void CtrlVerifyClockPanel::OnRadioButton6() {
    labelIdCode->setVisible(true);
    lineEditIdCode->setVisible(true);
    labelTimeOffset->setVisible(true);
    lineEditTimeOffset->setVisible(true);
    lineEdit_3->setVisible(false);
    labelSyncTimeInterval->setVisible(false);
    labelmintime->setVisible(false);
    groupBox_4->setVisible(true);
    fdMaster->setVisible(true);
    fdSlave->setVisible(true);
    btnLoraMasterSet->setVisible(true);
    btnLoraMasterGet->setVisible(true);
    checkBoxScreenSwitch->setVisible(true);
    checkBoxVolume->setVisible(true);
    checkBoxBrightness->setVisible(true);
    btnSyncSet->setVisible(true);
    fdSyncGet->setVisible(true);
}
