#include "PageSysmgr.h"


#include "app/Application.h"
#include "database/Dao.h"
#include "database/DataModelDef.h"
#include "common/Snowflake.h"
#include "pv/pages/PanelPolicy.h"

std::vector<std::string> g_comboxList_isOpen = {"启用", "禁用"};

///////////////////////////////////////////////////////////////////////////////////////////////////
// === PageUser ===
PageUser::PageUser(PARAM* p, EPvCode pvcode) :PageTable(p)
{
    auto& appdata = Application::data();
    table->addHead(DMUser::USER_ID, "用户编号", 180, {});
    table->addHead(DMUser::ACCOUNT, "用户名", 180, {});
    table->addHead(DMUser::NAME, "姓名", 180, {});
    table->addHead(DMUser::GENDER, "性别", 180, appdata.mapping.gender);
    table->addHead(DMUser::AGE, "年龄", 180, {});
    table->addHead(DMUser::PHONE, "手机号", 180, {});
    table->addHead(DMUser::EMAIL, "邮箱", 180, {});
    table->addHead(DMRole::ROLE_ID, "角色", 180, appdata.mapping.role);
    table->addHead(DMUser::LOGINTIME, "上次登录时间", 200, {});
    table->addOperate({"编辑"});

    int btnNew = PvApp::button(p, PV_ID_MAIN, 20, 160, 80, 35, POP_OPER_NEW);
    PvApp::bind(p, PvEvent::BUTTON_EVENT, btnNew, [=](std::string) { this->onOperate(-1, -1, POP_OPER_NEW); });

    int x = 50, y = 80, w = 350, h = 60;
    auto pop = this->addPop(700, 500, 180, "用户信息", {DMUser::USER_ID});
    pop->addParamLineEdit(DMUser::USER_ID, "编号", x, y, false);
    pop->addParamCombox(DMRole::ROLE_ID, "角色", x+w, y, appdata.getRoleNames());
    pop->addParamLineEdit(DMUser::ACCOUNT, "用户名", x, y += h);
    pop->addParamLineEdit(DMUser::NAME, "姓名", x+w, y);
    pop->addParamCombox(DMUser::GENDER, "性别", x, y += h, {"女", "男"});
    pop->addParamLineEdit(DMUser::AGE, "年龄", x+w, y);
    pop->addParamLineEdit(DMUser::PHONE, "手机号", x, y += h);
    pop->addParamLineEdit(DMUser::EMAIL, "邮箱", x+w, y);
}

void PageUser::onQueryTable(PageInfo& pageInfo, std::vector<Fields>& result)
{
    DAO::queryUserList(pageInfo, result);
}
void PageUser::onOperate(int irow, int icol, std::string oper)
{
    if (oper == POP_OPER_NEW)
    {
        Fields fields;
        fields.set(DMUser::USER_ID, Snowflake::instance().getIdStr());
        this->showPop(0, oper, fields);
    }
    else if (oper == POP_OPER_EDIT)
    {
        this->showPop(0, oper, table->row(irow));
    }
};
std::string PageUser::onValidation(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    if (fields.value(DMUser::ACCOUNT).empty()) { return "请输入用户名"; }
    return "";
};
std::string PageUser::onPopConfirm(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    if (pop->status == POP_OPER_NEW)
    {
        Errcode err = DAO::insertUser(fields);
        if (err == Errcode::OK) { return ""; }
        else if (err == Errcode::ERR_DB_DUPLICATE) { return "用户名已经存在"; }
        else { return "系统错误"; }
    }
    else if (pop->status == POP_OPER_EDIT)
    {
        Errcode err = DAO::updateUserById(fields);  
        if (err == Errcode::OK) { return ""; }
        else { return "系统错误"; }
    }
    return "";
};

///////////////////////////////////////////////////////////////////////////////////////////////////
// === PageRole ===
PageRole::PageRole(PARAM* p, EPvCode pvcode) : PageTable(p)
{
    auto& appdata = Application::data();

    table->addHead(DMRole::ROLE_ID, "角色编号", 180, {});
    table->addHead(DMRole::NAME, "角色名称", 200, {});
    table->addHead(DMRole::DESCRIBE, "角色描述", 900, {});
    table->addHead(DMRole::IS_OPEN, "是否启用", 200, appdata.mapping.isopen);
    table->addOperate({"编辑"});

    int btnNew = PvApp::button(p, PV_ID_MAIN, 20, 160, 80, 35, POP_OPER_NEW);
    PvApp::bind(p, PvEvent::BUTTON_EVENT, btnNew, [=](std::string) { this->onOperate(-1, -1, POP_OPER_NEW); });

    int x = 80, y = 80, h = 60;
    auto pop = this->addPop(500, 600, 180, "角色信息", {DMUser::USER_ID});
    pop->addParamLineEdit(DMRole::ROLE_ID, "编号", x, y, false);
    pop->addParamLineEdit(DMRole::NAME, "名称", x, y += h);
    pop->addParamCombox(DMUser::IS_OPEN, "是否启用", x, y += h, g_comboxList_isOpen);
    pop->addParamTextEdit(DMRole::DESCRIBE, "描述", x, y += h);

}
void PageRole::onQueryTable(PageInfo& pageInfo, std::vector<Fields>& result)
{
    DAO::queryRoleList(pageInfo, result);
}
void PageRole::onOperate(int irow, int icol, std::string oper)
{
    if (oper == POP_OPER_NEW)
    {
        Fields fields;
        fields.set(DMUser::USER_ID, Snowflake::instance().getIdStr());
        this->showPop(0, oper, fields);
    }
    else if (oper == POP_OPER_EDIT)
    {
        this->showPop(0, oper, table->row(irow));
    }
}
std::string PageRole::onValidation(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    return "";
};
std::string PageRole::onPopConfirm(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{

    return "";
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// === PagePermission ===
PagePermission::PagePermission(PARAM* p, EPvCode pvcode) : PageTable(p)
{
    auto& appdata = Application::data();
    table->addHead(DMPermission::PERMISSION_ID, "权限编号", 180, {});
    table->addHead(DMPermission::NAME, "权限名称", 200, {});
    table->addHead(DMPermission::DESCRIBE, "权限描述", 900, {});
    table->addHead(DMPermission::IS_OPEN, "是否启用", 200, appdata.mapping.isopen);
    table->addOperate({"编辑"});

    int btnNew = PvApp::button(p, PV_ID_MAIN, 20, 160, 80, 35, POP_OPER_NEW);
    PvApp::bind(p, PvEvent::BUTTON_EVENT, btnNew, [=](std::string) { this->onOperate(-1, -1, POP_OPER_NEW); });

    int x = 80, y = 80, h = 60; 
    auto pop = this->addPop(500, 600, 240, "角色信息", {DMPermission::PERMISSION_ID});
    pop->addParamLineEdit(DMPermission::PERMISSION_ID, "编号", x, y, false);
    pop->addParamLineEdit(DMPermission::NAME, "名称", x, y += h);
    pop->addParamCombox(DMPermission::IS_OPEN, "是否启用", x, y += h, g_comboxList_isOpen);
    pop->addParamTextEdit(DMPermission::DESCRIBE, "描述", x, y += h);
}
void PagePermission::onQueryTable(PageInfo& pageInfo, std::vector<Fields>& result)
{
    DAO::queryPermissionList(pageInfo, result);
}
void PagePermission::onOperate(int irow, int icol, std::string oper)
{
    if (oper == POP_OPER_NEW)
    {
        Fields fields;
        fields.set(DMUser::USER_ID, Snowflake::instance().getIdStr());
        this->showPop(0, oper, fields);
    }
    else if (oper == POP_OPER_EDIT)
    {
        this->showPop(0, oper, table->row(irow));
    }
}
std::string PagePermission::onValidation(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    return "";
};
std::string PagePermission::onPopConfirm(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    return "";
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// === PageStation ===
PageStation::PageStation(PARAM* p, EPvCode pvcode) : PageTable(p)
{
    auto& appdata = Application::data();
    table->addHead(DMStation::STATION_ID, "场站编号", 180, {});
    table->addHead(DMStation::NAME, "场站名称", 200, {});
    table->addHead(DMStation::ADDRESS, "地址", 200, {});
    table->addHead(DMStation::LONGITUDE, "经度", 200, {});
    table->addHead(DMStation::LATITUDE, "维度", 200, {});
    table->addHead(DMStation::TEL, "电话", 200, {});
    table->addHead(DMStation::STATUS, "状态", 200, appdata.mapping.isopen);
    table->addOperate({"编辑"});

    int btnNew = PvApp::button(p, PV_ID_MAIN, 20, 160, 80, 35, POP_OPER_NEW);
    PvApp::bind(p, PvEvent::BUTTON_EVENT, btnNew, [=](std::string) { this->onOperate(-1, -1, POP_OPER_NEW); });

    int x = 80, y = 80, h = 60;
    auto pop = this->addPop(500, 600, 240, "场站信息", {DMStation::STATION_ID});
    pop->addParamLineEdit(DMStation::STATION_ID, "编号", x, y, false);
    pop->addParamLineEdit(DMStation::NAME, "名称", x, y += h);
    pop->addParamLineEdit(DMStation::ADDRESS, "地址", x, y += h);
    pop->addParamLineEdit(DMStation::LONGITUDE, "经度", x, y += h);
    pop->addParamLineEdit(DMStation::LATITUDE, "维度", x, y += h);
    pop->addParamLineEdit(DMStation::TEL, "电话", x, y += h);
    pop->addParamCombox(DMStation::STATUS, "状态", x, y += h, g_comboxList_isOpen);
}
void PageStation::onQueryTable(PageInfo& pageInfo, std::vector<Fields>& result)
{
    DAO::queryStationList(pageInfo, result);
}
void PageStation::onOperate(int irow, int icol, std::string oper)
{
    if (oper == POP_OPER_NEW)
    {
        Fields fields;
        fields.set(DMUser::USER_ID, Snowflake::instance().getIdStr());
        this->showPop(0, oper, fields);
    }
    else if (oper == POP_OPER_EDIT)
    {
        this->showPop(0, oper, table->row(irow));
    }
}
std::string PageStation::onValidation(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    return "";
};
std::string PageStation::onPopConfirm(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    if (pop->status == POP_OPER_NEW)
    {
        Errcode err = DAO::insertStation(fields);
        if (err == Errcode::OK) { return ""; }
        else { return "系统错误"; }
    }
    else if (pop->status == POP_OPER_EDIT)
    {
        Errcode err = DAO::updateStationById(fields);
        if (err == Errcode::OK) { return ""; }
        else { return "系统错误"; }
    }
    return "";
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// === PageDevice ===
PageDevice::PageDevice(PARAM* p, EPvCode pvcode) : PageTable(p)
{
    auto& appdata = Application::data();
    table->addHead(DMDevice::DEVICE_ID, "设备编号", 120, {});
    table->addHead(DMDevice::STATION_ID, "所属场站", 120, appdata.mapping.stationName);
    table->addHead(DMDevice::TYPE, "设备类型", 120, appdata.mapping.deviceType);
    table->addHead(DMDevice::NAME, "设备名称", 180, {});
    table->addHead(DMDevice::CODE, "设备编码", 160, {});
    table->addHead(DMDevice::MODEL, "设备型号", 160, {});
    table->addHead(DMDevice::FACTORY, "厂家", 160, {});
    table->addHead(DMDevice::TEL, "厂家电话", 160, {});
    table->addHead(DMDevice::ATTRS, "设备参数", 460, {});
    table->addHead(DMDevice::IS_OPEN, "是否启用", 120, appdata.mapping.isopen);
    table->addOperate({"编辑"});

    int btnNew = PvApp::button(p, PV_ID_MAIN, 20, 160, 80, 35, POP_OPER_NEW);
    PvApp::bind(p, PvEvent::BUTTON_EVENT, btnNew, [=](std::string) { this->onOperate(-1, -1, POP_OPER_NEW); });

    int x = 50, y = 80, w = 350, h = 60;
    auto pop = this->addPop(700, 520, 180, "设备信息", {DMDevice::DEVICE_ID});
    pop->addParamLineEdit(DMDevice::DEVICE_ID, "设备编号", x, y, false);
    pop->addParamCombox(DMDevice::STATION_ID, "所属场站", x+w, y, appdata.getStationNames());
    pop->addParamCombox(DMDevice::TYPE, "类型", x, y += h, appdata.getDeviceTypeNames());
    pop->addParamLineEdit(DMDevice::NAME, "设备名称", x+w, y);
    pop->addParamLineEdit(DMDevice::CODE, "设备编码", x, y += h); 
    pop->addParamLineEdit(DMDevice::MODEL, "设备型号", x+w, y);
    pop->addParamLineEdit(DMDevice::FACTORY, "厂家", x, y += h); 
    pop->addParamLineEdit(DMDevice::TEL, "厂家电话", x+w, y);
    pop->addParamCombox(DMDevice::IS_OPEN, "是否启用", x, y += h, g_comboxList_isOpen);
    pop->addParamLineEdit(DMDevice::ATTRS, "设备参数", x + w, y);
}
void PageDevice::onQueryTable(PageInfo& pageInfo, std::vector<Fields>& result)
{
    DAO::queryDeviceList(pageInfo, result);
}
void PageDevice::onOperate(int irow, int icol, std::string oper)
{
    if (oper == POP_OPER_NEW)
    {
        Fields fields;
        fields.set(DMUser::USER_ID, Snowflake::instance().getIdStr());
        this->showPop(0, oper, fields);
    }
    else if (oper == POP_OPER_EDIT)
    {
        this->showPop(0, oper, table->row(irow));
    }
}
std::string PageDevice::onValidation(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    return "";
};
std::string PageDevice::onPopConfirm(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    if (pop->status == POP_OPER_NEW)
    {
        Errcode err = DAO::insertDevice(fields);
        if (err == Errcode::OK) { return ""; }
        else { return "系统错误"; }
    }
    else if (pop->status == POP_OPER_EDIT)
    {
        Errcode err = DAO::updateDeviceById(fields);
        if (err == Errcode::OK) { return ""; }
        else { return "系统错误"; }
    }
    return "";
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// === PagePolicy ===


PagePolicy::PagePolicy(PARAM* p, EPvCode pvcode) : PageTable(p)
{
    auto& appdata = Application::data();
    table->addHead(DMPolicy::POLICY_ID, "策略编号", 120, {});
    table->addHead(DMPolicy::TYPE, "策略类型", 120, appdata.mapping.policyType);
    table->addHead(DMPolicy::NAME, "策略名称", 160, {});
    table->addHead(DMPolicy::DESCRIBE, "策略描述", 300, {});
    table->addHead(DMPolicy::VALUE, "策略参数", 900, {});
    table->addHead(DMPolicy::IS_OPEN, "是否启用", 80, appdata.mapping.isopen);
    table->addOperate({ "编辑"});

    int btnNew = PvApp::button(p, PV_ID_MAIN, 20, 160, 80, 35, POP_OPER_NEW);
    PvApp::bind(p, PvEvent::BUTTON_EVENT, btnNew, [=](std::string) { this->onOperate(-1, -1, POP_OPER_NEW); });

    int x = 50, y = 80, w = 280;
    auto pop = this->addPop(1620, 800, 180, "策略信息", {DMPolicy::POLICY_ID});
    //pop->addParamLineEdit(DMPolicy::POLICY_ID, "编号", x, y, false);
    {
        pop->addParamLineEdit(DMPolicy::NAME, "策略名称", x, y);
        pop->addParamCombox(DMPolicy::TYPE, "策略类型", x+w, y, Application::data().getPolicyTypeNames());
        pop->addParamCombox(DMPolicy::IS_OPEN, "是否启用", x, y+=40, g_comboxList_isOpen),
        //pop->addParamLineEdit("3", "尖峰电价", x, y += 50);
        //pop->addParamLineEdit("4", "高峰电价", x+w, y);
        //pop->addParamLineEdit("5", "平段电价", x+w*2, y);
        //pop->addParamLineEdit("6", "低谷电价", x+w*3, y);

        pop->setCallbackTextEvent([=](std::string k, std::string text) { 
            if (k == DMPolicy::TYPE)
            {
                int policyTypeId = Application::data().getPolicyTypeId(text);
                this->setPanel(policyTypeId, "{}");
            }
        });

        panelPeak = std::make_shared<PanelPolicyPeak>(p, pop->widget(), 10, y+=40, 1600, 540);
        panelRequire  = std::make_shared<PanelPolicyRequire>(p, pop->widget(), 10, y, 620, 220);
        panelSelf = std::make_shared<PanelPolicySelf>(p, pop->widget(), 10, y, 620, 220);
    }
}
void PagePolicy::setPanel(int policyTypeId, std::string attrVal)
{
    auto pop = vecPop[0];
    panelPeak->show(0);
    panelRequire->show(0);
    panelSelf->show(0);
    if (policyTypeId == 1)
    {
        panelPeak->parseAttr(attrVal);
        panelPeak->show(1); 
        pop->resize(1620, 800); 
    }
    else if (policyTypeId == 2)
    { 
        panelRequire->parseAttr(attrVal);
        panelRequire->show(1); 
        pop->resize(640, 600); 
    }
    else if (policyTypeId == 3)
    { 
        panelSelf->parseAttr(attrVal);
        panelSelf->show(1); 
        pop->resize(640, 600); 
    }
}
std::string PagePolicy::getPanelAttr(int policyTypeId)
{
    std::string attrVal = "{}";
    if (policyTypeId == 1) { attrVal = panelPeak->dumpAttr(); }
    else if (policyTypeId == 2) { attrVal = panelRequire->dumpAttr(); }
    else if (policyTypeId == 3) { attrVal = panelSelf->dumpAttr(); }
    return attrVal;
}
void PagePolicy::onQueryTable(PageInfo& pageInfo, std::vector<Fields>& result)
{
    DAO::queryPolicyList(pageInfo, result);
}
void PagePolicy::onOperate(int irow, int icol, std::string oper)
{
    if (oper == POP_OPER_NEW)
    {
        this->showPop(0, oper, Fields());
        this->setPanel(1, {});
    }
    else if (oper == POP_OPER_EDIT)
    {
        int policyTypeId = curRowData.get<int>(DMPolicy::TYPE);
        std::string attrVal = curRowData.value(DMPolicy::VALUE);
        
        this->setPanel(policyTypeId, attrVal);
        this->showPop(0, oper, curRowData);
    }
}
std::string PagePolicy::onValidation(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    return "";
};
std::string PagePolicy::onPopConfirm(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    if (pop->status == POP_OPER_NEW)
    {
        int policyTypeId = fields.get<int>(DMPolicy::TYPE);
        fields.set(DMPolicy::VALUE, this->getPanelAttr(policyTypeId));
        Errcode err = DAO::insertPolicy(fields);
        if (err == Errcode::OK) { return ""; }
        else { return "系统错误"; }
    }
    else if (pop->status == POP_OPER_EDIT)
    {
        int policyTypeId = curRowData.get<int>(DMPolicy::TYPE);
        fields.set(DMPolicy::POLICY_ID, policyTypeId);
        fields.set(DMPolicy::VALUE, this->getPanelAttr(policyTypeId));
        Errcode err = DAO::updatePolicyById(fields);
        if (err == Errcode::OK) { return ""; }
        else { return "系统错误"; }
    }
    return "";
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// === PageSyslog ===
PageSyslog::PageSyslog(PARAM* p, EPvCode pvcode) : PageTable(p)
{
    table->addHead(DMLogSystem::LOG_ID, "日志编号", 160, {});
    table->addHead(DMLogSystem::TYPE, "日志类型", 160, {});
    table->addHead(DMLogSystem::USER_ACCOUNT, "用户", 160, {});
    table->addHead(DMLogSystem::CONTENT, "日志详情", 800, {});
    table->addHead(DMLogSystem::STATUS, "状态", 160, {});
    table->addHead(DMLogSystem::CREATE_TIME, "记录时间", 200, {});
    table->addOperate({"查看"});
}
void PageSyslog::onQueryTable(PageInfo& pageInfo, std::vector<Fields>& result)
{
    DAO::querySystemLogList(pageInfo, result);
}
void PageSyslog::onOperate(int irow, int icol, std::string oper)
{
    if (oper == POP_OPER_NEW)
    {
        Fields fields;
        fields.set(DMUser::USER_ID, Snowflake::instance().getIdStr());
        this->showPop(0, oper, fields);
    }
    else if (oper == POP_OPER_EDIT)
    {
        this->showPop(0, oper, table->row(irow));
    }
}
std::string PageSyslog::onValidation(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    return "";
};
std::string PageSyslog::onPopConfirm(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    return "";
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// === PageAlertlog ===
PageAlertlog::PageAlertlog(PARAM* p, EPvCode pvcode) : PageTable(p)
{
    table->addHead(DMLogAlert::LOG_ID, "日志编号", 160, {});
    table->addHead(DMLogAlert::TYPE, "日志类型", 160, {});
    table->addHead(DMLogAlert::DEVICE_ID, "设备ID", 160, {});
    table->addHead(DMLogAlert::CONTENT, "日志详情", 800, {});
    table->addHead(DMLogAlert::STATUS, "状态", 160, {});
    table->addHead(DMLogAlert::CREATE_TIME, "记录时间", 200, {});
    table->addOperate({"查看"});
}
void PageAlertlog::onQueryTable(PageInfo& pageInfo, std::vector<Fields>& result)
{
    //DAO::queryAlertLogList(pageInfo, result);
}
void PageAlertlog::onOperate(int irow, int icol, std::string oper)
{
    if (oper == POP_OPER_NEW)
    {
        Fields fields;
        fields.set(DMUser::USER_ID, Snowflake::instance().getIdStr());
        this->showPop(0, oper, fields);
    }
    else if (oper == POP_OPER_EDIT)
    {
        this->showPop(0, oper, table->row(irow));
    }
}
std::string PageAlertlog::onValidation(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    return "";
};
std::string PageAlertlog::onPopConfirm(std::shared_ptr<PvPopWidget> pop, Fields& fields)
{
    return "";
}