#include "MaskPageSysmgr.h"

#include <thread>
#include "common/Snowflake.h"
#include "pv/PvTable.h"
#include "pv/PvPopWidget.h"
#include "database/Dao.h"
#include "app/Application.h"
#include "PageSysmgr.h"

static void createPvTable(PARAM* p)
{
    int nRow = 20, nCol = 10;
    int id = PvApp::pvid(p);
    pvQTable(p, id, 0, nRow, nCol);
    pvSetGeometry(p, id, 10, 200, 1900, 700);

    //pvSetWhatsThis()
    //pvWhatsThisPrintf()
    //pvToolTip()
    //pvSetGeometry()
    //pvSetMinSize()
    //pvSetMaxSize()
    //pvMove();
    //pvResize();
    //pvHide();
    //pvShow();
    //pvSetStyleSheet()
    //pvPrintfStyleSheet();
    pvSetPaletteBackgroundColor(p, id, 1, 32, 54);
    pvSetPaletteForegroundColor(p, id, 0, 255, 0);
    //pvSetFontColor()
    //pvSetFont()
    //pvSetEnabled()
    //pvSetFocus()
    //pvCopyToClipboard()
    //pvSaveAsBmp()

    //pvTablePrintf()
    //pvClear();
    //pvSetColumnWidth()
    //pvSetRowHeight()
    //pvSetWordWrap()
    //pvSetTablePixmap()
    //pvEnsureCellVisible()
    pvSetEditable(p, id, 0);
    pvTableSetEnabled(p, id, 1, 0, 0);

    // 设置垂直（vertical）方向表头不可 resize
    for (int i = 0; i<nRow; ++i) { pvTableSetHeaderResizeEnabled(p, id, 0, 0, i); }
    // 设置水平（horizontal）方向表头不可 resize
    for (int i = 0; i<nCol; ++i) { pvTableSetHeaderResizeEnabled(p, id, 1, 0, i); }

    pvSetTableText(p, id, -1, -1, "A");
    //pvSetTableCheckBox()

    //pvSetTableLabel()
    pvSetTableButton(p, id, 1, 1, "HelloWorld");
    pvSetTableComboBox(p, id, 1, 2, 1, "choice1,#choice2,choice3");

    //pvSetNumRows()
    //pvSetNumCols()
    //pvInsertRows()
    //pvInsertColumns()
    //pvRemoveRow()
    //pvRemoveColumn()
    //pvSetTableTextAlignment();
    //pvSave()
    //pvMysqldump()
    //pvCSVcreate()
    //pvCSVdump()
    //pvCSV()
}


MaskPageSysmgr::MaskPageSysmgr(PARAM* p) : PvMask(p)
{
}

int MaskPageSysmgr::initUI(EPvCode pvcode)
{
    PvApp::label(p, PV_ID_MAIN, 10, 150, 1900, 790, "", QSS::LABEL_BKG_1);

    if (pvcode == EPvCode::MASK_SYSMGR) { pvcode = EPvCode::MASK_MGR_USER; }

    static std::vector<std::string> vecPageNames = {"用户管理", "角色管理", "权限管理", "场站管理", "设备管理", "策略管理", "系统日志", "告警日志"};
    for (int i = 0; i<vecPageNames.size(); ++i)
    {
        std::string& title = vecPageNames[i];
        bool isActive = (PvApp::getPvCode(title) == pvcode);
        int idPageBtn = PvApp::button(p, PV_ID_MAIN, 10+(i*110), 100, 100, 40, title, isActive ? QSS::QSS_BTN_MGR_ACTIVE : QSS::QSS_BTN_MGR);
        mapSubpage_[idPageBtn] = title;
    }

    switch (pvcode)
    {
    case EPvCode::MASK_MGR_USER: { subpage_ = std::make_shared<PageUser>(p, pvcode); }
        break;
    case EPvCode::MASK_MGR_ROLE: { subpage_ = std::make_shared<PageRole>(p, pvcode); }
        break;
    case EPvCode::MASK_MGR_PERMISSION: { subpage_ = std::make_shared<PagePermission>(p, pvcode); }
        break;
    case EPvCode::MASK_MGR_STATION: { subpage_ = std::make_shared<PageStation>(p, pvcode); }
        break;
    case EPvCode::MASK_MGR_DEVICE: { subpage_ = std::make_shared<PageDevice>(p, pvcode); }
        break;
    case EPvCode::MASK_MGR_POLICY: { subpage_ = std::make_shared<PagePolicy>(p, pvcode); }
        break;
    case EPvCode::MASK_MGR_SYSLOG: { subpage_ = std::make_shared<PageSyslog>(p, pvcode); }
        break;
    case EPvCode::MASK_MGR_ALERTLOG: { subpage_ = std::make_shared<PageAlertlog>(p, pvcode); }
        break;
    default: 
    { 
        subpage_ = std::make_shared<PageUser>(p, pvcode); 
    } break;
    }

    std::thread([=]() {
        if (subpage_) { subpage_->updateDataFromDB(); }
    }).detach();

    return 0;
}

EPvCode MaskPageSysmgr::onEventButton(int pvid)
{
    std::string title;
    auto iter = mapSubpage_.find(pvid);
    if (iter != mapSubpage_.end()) 
    { 
        title = iter->second; 
        return PvApp::getPvCode(title);
    }
    return EPvCode::NUL;
}