#include "PanelPolicy.h"
#include "app/Application.h"
#include "app/AppData.h"
#include "common/JsonN.h"
#include "common/Utils.h"

//const std::string QSS_COMBOX_1 =
//"QComboBox {border: 1px solid rgb(18, 251, 255); background-color: rgb(255, 89, 0); border-radius: 3px; color: rgb(30,30,30); font: bold 14px;}"
//"QComboBox QAbstractItemView { border: 1px solid gray; background-color: rgba(8, 54, 91); border-radius: 5px; color:white;}"
//"QComboBox::drop-down { border-radius: 5px; width: 30px; }"
//"QComboBox:disabled { color:rgb(150,150,150);}";
//
//const std::string QSS_COMBOX_2 =
//"QComboBox {border: 1px solid rgb(18, 251, 255); background-color: rgb(255, 255, 0); border-radius: 3px; color: rgb(30,30,30); font: bold 14px;}"
//"QComboBox QAbstractItemView { border: 1px solid gray; background-color: rgba(8, 54, 91); border-radius: 5px; color:white;}"
//"QComboBox::drop-down { border-radius: 5px; width: 30px; }"
//"QComboBox:disabled { color:rgb(150,150,150);}";
//
//const std::string QSS_COMBOX_3 =
//"QComboBox {border: 1px solid rgb(18, 251, 255); background-color: rgb(121, 191, 226); border-radius: 3px; color: rgb(30,30,30); font: bold 14px;}"
//"QComboBox QAbstractItemView { border: 1px solid gray; background-color: rgba(8, 54, 91); border-radius: 5px; color:white;}"
//"QComboBox::drop-down { border-radius: 5px; width: 30px; }"
//"QComboBox:disabled { color:rgb(150,150,150);}";
//
//const std::string QSS_COMBOX_4 =
//"QComboBox {border: 1px solid rgb(18, 251, 255); background-color: rgb(0, 255, 58); border-radius: 3px; color: rgb(30,30,30); font: bold 14px;}"
//"QComboBox QAbstractItemView { border: 1px solid gray; background-color: rgba(8, 54, 91); border-radius: 5px; color:white;}"
//"QComboBox::drop-down { border-radius: 5px; width: 30px; }"
//"QComboBox:disabled { color:rgb(150,150,150);}";

const std::string QSS_COMBOX_1 =
"QComboBox { background-color: transparent; border: 1px solid rgb(255, 89, 0); color: rgb(255, 89, 0); border-radius: 3px; font: bold 14px;}"
"QComboBox QAbstractItemView { border: 1px solid gray; background-color: rgba(8, 54, 91); border-radius: 3px; color:white;}"
"QComboBox::drop-down { border-radius: 5px; width: 30px; }"
"QComboBox:disabled {border: none; color: gray;}";

const std::string QSS_COMBOX_2 =
"QComboBox { background-color: transparent; border: 1px solid rgb(255, 255, 0); color: rgb(255, 255, 0); border-radius: 3px; font: bold 14px;}"
"QComboBox QAbstractItemView { border: 1px solid gray; background-color: rgba(8, 54, 91); border-radius: 3px; color:white;}"
"QComboBox::drop-down { border-radius: 5px; width: 30px; }"
"QComboBox:disabled { border: none; color: gray;}";

const std::string QSS_COMBOX_3 =
"QComboBox { background-color: transparent; border: 1px solid rgb(121, 191, 226); color: rgb(121, 191, 226); border-radius: 3px;  font: bold 14px;}"
"QComboBox QAbstractItemView { border: 1px solid gray; background-color: rgba(8, 54, 91); border-radius: 3px; color:white;}"
"QComboBox::drop-down { border-radius: 5px; width: 30px; }"
"QComboBox:disabled { border: none; color: gray;}";

const std::string QSS_COMBOX_4 =
"QComboBox { background-color: transparent; border: 1px solid rgb(0, 255, 58);color: rgb(0, 255, 58); border-radius: 3px; font: bold 14px;}"
"QComboBox QAbstractItemView { border: 1px solid gray; background-color: rgba(8, 54, 91); border-radius: 3px; color:white;}"
"QComboBox::drop-down { border-radius: 5px; width: 30px; }"
"QComboBox:disabled { border: none; color: gray;}";

static std::string GetPeriodQss(int v)
{
    if (v == 1) { return QSS_COMBOX_1; }
    else if (v == 2) { return QSS_COMBOX_2; }
    else if (v == 3) { return QSS_COMBOX_3; }
    else if (v == 4) { return QSS_COMBOX_4; }
    else { return QSS_COMBOX_1; }
}
static std::string GetPeriodQss(std::string text)
{
    if (text == "尖峰") { return QSS_COMBOX_1; }
    else if (text == "高峰") { return QSS_COMBOX_2; }
    else if (text == "平段") { return QSS_COMBOX_3; }
    else if (text == "低谷") { return QSS_COMBOX_4; }
    else { return QSS_COMBOX_1; }
}
static int GetPeriod(std::string text)
{
    if (text == "尖峰") { return 1; }
    else if (text == "高峰") { return 2; }
    else if (text == "平段") { return 3; }
    else if (text == "低谷") { return 4; }
    else { return 3; }
}
PanelPolicyPeak::PanelPolicyPeak(PARAM* p, int parent, int ix, int iy, int iw, int ih) : PvObject(p)
{
    data.vecPeriods.resize(12, std::vector<int>(24, 3));
    ui.parent = parent;
    pvid = PvApp::widget(p, parent, ix, iy, iw, ih);

    auto& appdata = Application::data();
    std::vector<std::string> vecPeriods = {"尖峰", "高峰", "平段", "低谷"};

    ui.vecComboxs.resize(12, std::vector<int>(24, PV_ID_NUL));

    int x = 0, y = 10, w = 64, h = 30;
    for (int row = 0; row<=12; row++)
    {
        for (int col = 0; col<=24; ++col)
        {
            std::string text;
            if (row == 0)
            {
                text = (col == 0) ? "月份" : std::to_string(col-1) + "-" + std::to_string(col) + "时";
            }
            else
            {
                text = (col == 0) ? std::to_string(row) : "";
            }
            if (row == 0 || col == 0)
            {
                std::string qssLabel = (row==0) ? QSS::label(14, "", "rgb(19, 93, 114)") : "";
                int label = PvApp::label(p, pvid, x+w*col, y+h*row, w, h, text, qssLabel);
                pvSetAlignment(p, label, AlignCenter);
            }
            else
            {
                int periodVal = data.vecPeriods[row-1][col-1];
                int combox = PvApp::combox(p, pvid, x+w*col, y+h*row+3, w-3, 24, vecPeriods, periodVal-1);
                pvSetStyleSheet(p, combox, GetPeriodQss(periodVal).c_str());
                ui.vecComboxs[row-1][col-1] = combox;
                PvApp::bind(p, PvEvent::TEXT_EVENT, combox, [=](std::string text) {
                    pvSetStyleSheet(p, combox, GetPeriodQss(text).c_str());
                    data.vecPeriods[row-1][col-1] = GetPeriod(text);
                });
            }

        }
    }

    const int H = 24;
    int labelPolicy = PvApp::label(p, pvid, 20, y = 420, 300, H, "充电策略");
    ui.radioPolicy1 = PvApp::radioButton(p, labelPolicy, 80, 0, 80, H, "一充一放");
    ui.radioPolicy2 = PvApp::radioButton(p, labelPolicy, 170, 0, 80, H, "两充两放");

    ui.label1 = PvApp::label(p, pvid, 10, y+=30, 610, 90, "第一次充放电过程", QSS::label(14, "", "", "1px solid rgb(49, 130, 141)"));
    pvSetAlignment(p, ui.label1, AlignLeft | AlignTop);
    {
        int y0 = 70;
        int labelChargeT = PvApp::label(p, ui.label1, 10, 30, 300, H, "充电时间");
        ui.attr1TimeCharge0 = PvApp::timeEdit(p, labelChargeT, y0, 0, 90, H);
        PvApp::label(p, labelChargeT, y0 += 100, 0, 10, H, "--");
        ui.attr1TimeCharge1 = PvApp::timeEdit(p, labelChargeT, y0 =+ 20, 0, 90, H);

        int labelDischargeT = PvApp::label(p, ui.label1, 320, 30, 300, H, "放电时间");
        ui.attr1TimeDischarge0 = PvApp::timeEdit(p, labelDischargeT, y0 = 70, 0, 90, H);
        PvApp::label(p, labelDischargeT, y0 += 100, 0, 10, H, "--");
        ui.attr1TimeDischarge1 = PvApp::timeEdit(p, labelDischargeT, y0 += 20, 0, 90, H);

        int labelPowerIn = PvApp::label(p, ui.label1, 10, 60, 300, H, "充电功率");
        ui.attr1RadioPowerInAuto = PvApp::radioButton(p, labelPowerIn, y0 = 70, 0, 60, H, "自动");
        ui.attr1RadioPowerIn = PvApp::radioButton(p, labelPowerIn, y0 += 60, 0, 64, H, "自定义");
        ui.arrt1LabelPowerIn = PvApp::textedit(p, labelPowerIn, y0 += 70, 0, 80, H, "");

        int labelPowerOut = PvApp::label(p, ui.label1, 320, 60, 300, H, "放电功率");
        ui.attr1RadioPowerOutAuto = PvApp::radioButton(p, labelPowerOut, y0 = 70, 0, 60, H, "自动");
        ui.attr1RadioPowerOut = PvApp::radioButton(p, labelPowerOut, y0 += 60, 0, 64, H, "自定义");
        ui.arrt1LabelPowerOut = PvApp::textedit(p, labelPowerOut, y0 += 70, 0, 80, H, "");
    }

    ui.label2 = PvApp::label(p, pvid, 630, y, 610, 90, "第二次充放电过程", QSS::label(14, "", "", "1px solid rgb(49, 130, 141)"));
    pvSetAlignment(p, ui.label2, AlignLeft | AlignTop);
    {
        int y0 = 70;
        int labelChargeT = PvApp::label(p, ui.label2, 10, 30, 300, H, "充电时间");
        XLOGD() << "labelChargeT=" << labelChargeT;
        ui.attr2TimeCharge0 = PvApp::timeEdit(p, labelChargeT, y0, 0, 60, H);
        PvApp::label(p, labelChargeT, 150, 0, 10, H, "--");
        ui.attr2TimeCharge1 = PvApp::timeEdit(p, labelChargeT, 170, 0, 60, H);

        int labelDischargeT = PvApp::label(p, ui.label2, 320, 30, 300, H, "放电时间");
        ui.attr2TimeDischarge0 = PvApp::timeEdit(p, labelDischargeT, y0 = 70, 0, 60, H);
        PvApp::label(p, labelDischargeT, y0 += 70, 0, 10, H, "--");
        ui.attr2TimeDischarge1 = PvApp::timeEdit(p, labelDischargeT, y0 += 20, 0, 60, H);

        int labelPowerIn = PvApp::label(p, ui.label2, 10, 60, 300, H, "充电功率");
        ui.attr2RadioPowerInAuto = PvApp::radioButton(p, labelPowerIn, y0 = 70, 0, 60, H, "自动");
        ui.attr2RadioPowerIn = PvApp::radioButton(p, labelPowerIn, y0 += 60, 0, 64, H, "自定义");
        ui.arrt2LabelPowerIn = PvApp::textedit(p, labelPowerIn, y0 += 70, 0, 80, H, "");

        int labelPowerOut = PvApp::label(p, ui.label2, 320, 60, 300, H, "放电功率");
        ui.attr2RadioPowerOutAuto = PvApp::radioButton(p, labelPowerOut, y0 = 70, 0, 60, H, "自动");
        ui.attr2RadioPowerOut = PvApp::radioButton(p, labelPowerOut, y0 += 60, 0, 64, H, "自定义");
        ui.arrt2LabelPowerOut = PvApp::textedit(p, labelPowerOut, y0 += 70, 0, 80, H, "");
    }


    PvApp::bind(p, PvEvent::RADIOBUTTON_EVENT, ui.radioPolicy1, [=](std::string s) {
        this->data.times = 1;
        pvHide(p, ui.label2);
        //setTimeText(ui.attr2TimeCharge0, "");
        //setTimeText(ui.attr2TimeCharge1, "");
        //setTimeText(ui.attr2TimeDischarge0, "");
        //setTimeText(ui.attr2TimeDischarge1, "");
        //pvSetChecked(p, ui.attr2RadioPowerInAuto, 1);
        //pvSetChecked(p, ui.attr2RadioPowerOutAuto, 1);
        //pvSetText(p, ui.arrt2LabelPowerOut, "");
        //data.attr2.chargePower = data.attr2.dischargePower = 0.0;
        //data.attr2.chargeTimeStart = data.attr2.chargeTimeEnd = data.attr2.dischargeTimeStart = data.attr2.dischargeTimeEnd = "";
    });

    PvApp::bind(p, PvEvent::RADIOBUTTON_EVENT, ui.radioPolicy2, [=](std::string s) {
        this->data.times = 2;
        pvShow(p, ui.label2);
    });

    PvApp::bind(p, PvEvent::TEXT_EVENT, ui.attr1TimeCharge0, [=](std::string s) {
        data.attr1.chargeTimeStart = s.substr(0, 8);
    });
    PvApp::bind(p, PvEvent::TEXT_EVENT, ui.attr1TimeCharge1, [=](std::string s) {
        data.attr1.chargeTimeEnd = s.substr(0, 8);
    });
    PvApp::bind(p, PvEvent::TEXT_EVENT, ui.attr1TimeDischarge0, [=](std::string s) {
        data.attr1.dischargeTimeStart = s.substr(0, 8);
    });
    PvApp::bind(p, PvEvent::TEXT_EVENT, ui.attr1TimeDischarge1, [=](std::string s) {
        data.attr1.dischargeTimeEnd = s.substr(0, 8);
    });
    PvApp::bind(p, PvEvent::TEXT_EVENT, ui.attr2TimeCharge0, [=](std::string s) {
        data.attr2.chargeTimeStart = s.substr(0, 8);
    });
    PvApp::bind(p, PvEvent::TEXT_EVENT, ui.attr2TimeCharge1, [=](std::string s) {
        data.attr2.chargeTimeEnd = s.substr(0, 8);
    });
    PvApp::bind(p, PvEvent::TEXT_EVENT, ui.attr2TimeDischarge0, [=](std::string s) {
        data.attr2.dischargeTimeStart = s.substr(0, 8);
    });
    PvApp::bind(p, PvEvent::TEXT_EVENT, ui.attr2TimeDischarge1, [=](std::string s) {
        data.attr2.dischargeTimeEnd = s.substr(0, 8);
    });

//    {
//        int btn = PvApp::button(p, pvid, 10, y+=100, 90, 30, "DUMP");
//        PvApp::bind(p, PvEvent::BUTTON_EVENT, btn, [=](std::string) {
//            XLOGD() << dumpAttr();
//        });
//    }
//    {
//        int btn = PvApp::button(p, pvid, 110, y, 90, 30, "PARSE1");
//        PvApp::bind(p, PvEvent::BUTTON_EVENT, btn, [=](std::string) {
//            std::string s = R"({"cycle":[{"charge_end":"","charge_power":0.0,"charge_start":"","discharge_end":"","discharge_power":0.0,"discharge_start":""}],
//"period":[[1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]],
//"times":1})";
//            parseAttr(s);
//        });
//    }
//    {
//        int btn = PvApp::button(p, pvid, 210, y, 90, 30, "PARSE2");
//        PvApp::bind(p, PvEvent::BUTTON_EVENT, btn, [=](std::string) {
//            std::string s = R"({"cycle":[
//{"charge_end":"09:00:00","charge_power":0.0,"charge_start":"00:00:00","discharge_end":"","discharge_power":0.0,"discharge_start":""},
//{"charge_end":"23:59:00","charge_power":0.0,"charge_start":"20:00:00","discharge_end":"13:00","discharge_power":0.0,"discharge_start":"09:00"}
//],
//"period":[[1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]],
//"times":2})";
//            parseAttr(s);
//        });
//    }
}

void PanelPolicyPeak::updateUI()
{
}

void PanelPolicyPeak::setTimeText(int id, std::string s)
{
    int hour {0}, minute {0}, second {0};
    sscanf_s(s.c_str(), "%d:%d:%d", &hour, &minute, &second);
    pvSetTime(p, id, hour, minute, second);
}

void PanelPolicyPeak::parseAttr(std::string str)
{
    njson jsonroot;
    bool ret = JSON::parse(str, jsonroot);
    if (!ret)
    {
        return;
    }

    JSON::read(jsonroot, "times", data.times);
    // 更新UI (充电策略)
    if (data.times == 2) 
    {
        pvSetChecked(p, ui.radioPolicy2, 1);
        pvShow(p, ui.label2);
    } 
    else
    {
        pvSetChecked(p, ui.radioPolicy1, 1);
        pvHide(p, ui.label2);
    }

    std::vector<std::vector<int>> vecTmp;
    JSON::read<std::vector<std::vector<int>>>(jsonroot, "period", vecTmp);
    for (int row = 0; row < data.vecPeriods.size(); ++row)
    {
        auto& vecRows = data.vecPeriods[row];
        for (int col = 0; col < vecRows.size(); ++col)
        {
            auto& periodVal = vecRows[col];
            int tmpVal {};
            if (row < vecTmp.size())
            {
                tmpVal = (col < vecTmp[row].size() ? vecTmp[row][col] : 3);
            }
            else
            {
                tmpVal = 3;
            }
            if (periodVal != tmpVal)
            {
                periodVal = tmpVal;
                // 更新UI (时段下拉列表)
                auto combox = ui.vecComboxs[row][col];
                pvSetCurrentItem(p, combox, periodVal-1);
                pvSetStyleSheet(p, combox, GetPeriodQss(periodVal).c_str());
            }
        }
    }
    if (jsonroot.contains("cycle"))
    {
        auto& jsonCycle = jsonroot["cycle"];
        if (jsonCycle.is_array())
        {
            auto size = jsonCycle.size();
            if (size >= 1)
            {
                data.attr1.chargeTimeStart = jsonCycle[0]["charge_start"];
                data.attr1.chargeTimeEnd = jsonCycle[0]["charge_end"];
                data.attr1.chargePower = jsonCycle[0]["charge_power"];
                data.attr1.dischargeTimeStart = jsonCycle[0]["discharge_start"];
                data.attr1.dischargeTimeEnd = jsonCycle[0]["discharge_end"];
                data.attr1.dischargePower = jsonCycle[0]["discharge_power"];
                // 更新UI (一放一充)
                setTimeText(ui.attr1TimeCharge0, data.attr1.chargeTimeStart);
                setTimeText(ui.attr1TimeCharge1, data.attr1.chargeTimeEnd);
                //setTimeText(ui.attr1TimeCharge1, data.attr1.chargeTimeEnd);     
                setTimeText(ui.attr1TimeDischarge0, data.attr1.dischargeTimeStart);
                setTimeText(ui.attr1TimeDischarge1, data.attr1.dischargeTimeEnd);
                //setTimeText(ui.attr1TimeCharge1, data.attr1.chargeTimeEnd);
            }
            if (size >= 2)
            {
                data.attr2.chargeTimeStart = jsonCycle[1]["charge_start"];
                data.attr2.chargeTimeEnd = jsonCycle[1]["charge_end"];
                data.attr2.chargePower = jsonCycle[1]["charge_power"];
                data.attr2.dischargeTimeStart = jsonCycle[1]["discharge_start"];
                data.attr2.dischargeTimeEnd = jsonCycle[1]["discharge_end"];
                data.attr2.dischargePower = jsonCycle[1]["discharge_power"];
                // 更新UI (两放两充)
                setTimeText(ui.attr2TimeCharge0, data.attr2.chargeTimeStart);
                setTimeText(ui.attr2TimeCharge1, data.attr2.chargeTimeEnd);
                //setTimeText(ui.attr2TimeCharge1, data.attr2.chargeTimeEnd);     
                setTimeText(ui.attr2TimeDischarge0, data.attr2.dischargeTimeStart);
                setTimeText(ui.attr2TimeDischarge1, data.attr2.dischargeTimeEnd);
                //setTimeText(ui.attr2TimeCharge1, data.attr2.chargeTimeEnd);
            }
        }
    }
    // 更新UI

}

std::string PanelPolicyPeak::dumpAttr()
{
    njson jsonroot;
    jsonroot["period"] = data.vecPeriods;
    jsonroot["times"] = data.times;

    njson nodeCycle = njson::array();

    njson nodeAttr1;
    nodeAttr1["charge_start"] = data.attr1.chargeTimeStart;
    nodeAttr1["charge_end"] = data.attr1.chargeTimeEnd;
    nodeAttr1["charge_power"] = data.attr1.chargePower;
    nodeAttr1["discharge_start"] = data.attr1.dischargeTimeStart;
    nodeAttr1["discharge_end"] = data.attr1.dischargeTimeEnd;
    nodeAttr1["discharge_power"] = data.attr1.dischargePower;
    nodeCycle.push_back(nodeAttr1);
    if (data.times > 1)
    {
        njson nodeAttr2;
        nodeAttr2["charge_start"] = data.attr2.chargeTimeStart;
        nodeAttr2["charge_end"] = data.attr2.chargeTimeEnd;
        nodeAttr2["charge_power"] = data.attr2.chargePower;
        nodeAttr2["discharge_start"] = data.attr2.dischargeTimeStart;
        nodeAttr2["discharge_end"] = data.attr2.dischargeTimeEnd;
        nodeAttr2["discharge_power"] = data.attr2.dischargePower;
        nodeCycle.push_back(nodeAttr2);
    }
    jsonroot["cycle"] = nodeCycle;
    return jsonroot.dump();
}


PanelPolicyRequire::PanelPolicyRequire(PARAM* p, int parent, int ix, int iy, int iw, int ih) : PvObject(p)
{
    pvid = PvApp::widget(p, parent, ix, iy, iw, ih);

    int y = 10;
    const int H = 24;
    ui.label1 = PvApp::label(p, pvid, 10, y, 600, 80, "响应削峰指令", QSS::label(14, "", "", "1px solid rgb(49, 130, 141)"));
    pvSetAlignment(p, ui.label1, AlignLeft | AlignTop);
    {
        ui.dischargeSoc = PvApp::lineTextedit(p, ui.label1, PvRect(10, 40, 270, H), 100, "电池SOC阈值", "");
        ui.dischargePower = PvApp::lineTextedit(p, ui.label1, PvRect(300, 40, 270, H), 70, "放电功率", "");
    }

    ui.label2 = PvApp::label(p, pvid, 10, y += 90, 600, 90, "响应填谷指令", QSS::label(14, "", "", "1px solid rgb(49, 130, 141)"));
    pvSetAlignment(p, ui.label2, AlignLeft | AlignTop);
    {
        ui.chargeSoc = PvApp::lineTextedit(p, ui.label2, PvRect(10, 40, 270, H), 100, "电池SOC阈值", "");
        ui.chargePower = PvApp::lineTextedit(p, ui.label2, PvRect(300, 40, 270, H), 70, "充电功率", "");
    }
    PvApp::bind(p, PvEvent::TEXT_EVENT, ui.dischargeSoc, [=](std::string s) {
        data.dischargeSoc = Utils::toInt(s);
    });
    PvApp::bind(p, PvEvent::TEXT_EVENT, ui.dischargePower, [=](std::string s) {
        data.dischargePower = Utils::toInt(s);
    });
    PvApp::bind(p, PvEvent::TEXT_EVENT, ui.chargeSoc, [=](std::string s) {
        data.chargeSoc = Utils::toInt(s);
    });
    PvApp::bind(p, PvEvent::TEXT_EVENT, ui.chargePower, [=](std::string s) {
        data.chargePower = Utils::toInt(s);
    });
}

void PanelPolicyRequire::parseAttr(std::string str)
{
    njson jsonroot;
    bool ret = JSON::parse(str, jsonroot);
    if (!ret)
    {
        return;
    }

    JSON::read(jsonroot, "charge_soc", data.chargeSoc);
    JSON::read(jsonroot, "charge_power", data.chargePower);
    JSON::read(jsonroot, "discharge_soc", data.dischargeSoc);
    JSON::read(jsonroot, "discharge_power", data.dischargePower);

    // 更新UI
    pvSetText(p, ui.chargeSoc, std::to_string(data.chargeSoc).c_str());
    pvSetText(p, ui.chargePower, std::to_string(data.chargePower).c_str());
    pvSetText(p, ui.dischargeSoc, std::to_string(data.dischargeSoc).c_str());
    pvSetText(p, ui.dischargePower, std::to_string(data.dischargePower).c_str());
}

std::string PanelPolicyRequire::dumpAttr()
{
    njson jsonroot;
    jsonroot["discharge_soc"] = data.dischargeSoc;
    jsonroot["discharge_power"] = data.dischargePower;
    jsonroot["charge_soc"] = data.chargeSoc;
    jsonroot["charge_power"] = data.chargePower;
    return jsonroot.dump();
}

PanelPolicySelf::PanelPolicySelf(PARAM* p, int parent, int ix, int iy, int iw, int ih) : PvObject(p)
{
    pvid = PvApp::widget(p, parent, ix, iy, iw, ih);

    int y = 10;
    const int H = 24;
    ui.label1 = PvApp::label(p, pvid, 10, y, 600, 120, "响应削峰指令", QSS::label(14, "", "", "1px solid rgb(49, 130, 141)"));
    pvSetAlignment(p, ui.label1, AlignLeft | AlignTop);
    {
        ui.usedSoc = PvApp::lineTextedit(p, ui.label1, PvRect(10, 40, 270, H), 100, "自用电量比值", "");
        ui.gridSoc = PvApp::lineTextedit(p, ui.label1, PvRect(10, 80, 270, H), 100, "上网电量比值", "");

        ui.chargePower = PvApp::lineTextedit(p, ui.label1, PvRect(320, 40, 250, H), 70, "充电功率", "");
        ui.dischargePower = PvApp::lineTextedit(p, ui.label1, PvRect(320, 80, 250, H), 70, "放电功率", "");
    }
}

void PanelPolicySelf::parseAttr(std::string str)
{
    njson jsonroot;
    bool ret = JSON::parse(str, jsonroot);
    if (!ret)
    {
        return;
    }

    JSON::read(jsonroot, "used_soc", data.usedSoc);
    JSON::read(jsonroot, "grid_soc", data.gridSoc);
    JSON::read(jsonroot, "charge_power", data.chargePower);
    JSON::read(jsonroot, "discharge_power", data.dischargePower);

    // 更新UI
    pvSetText(p, ui.usedSoc, std::to_string(data.usedSoc).c_str());
    pvSetText(p, ui.gridSoc, std::to_string(data.gridSoc).c_str());
    pvSetText(p, ui.chargePower, std::to_string(data.chargePower).c_str());
    pvSetText(p, ui.dischargePower, std::to_string(data.dischargePower).c_str());
}

std::string PanelPolicySelf::dumpAttr()
{
    njson jsonroot;
    jsonroot["used_soc"] = data.usedSoc;
    jsonroot["grid_soc"] = data.gridSoc;
    jsonroot["charge_power"] = data.chargePower;
    jsonroot["discharge_power"] = data.dischargePower;
    return jsonroot.dump();
}