#include "MenuTree.hpp"

#include <sqlpp11/postgresql/connection.h>
#include <eedb/db/connection.hpp>
#include <sqlpp11/sqlpp11.h>
#include <eedb/model/auth_info.h>
#include <eedb/model/category.h>


void eedb::MenuTree::createTree()
{
    using namespace eedb::db;
    using namespace sqlpp;

    Wt::WTree *tree = new Wt::WTree();

    tree->setSelectionMode(Wt::ExtendedSelection);

    Wt::WIconPair *folderIcon
        = new Wt::WIconPair("icons/yellow-folder-closed.png",
                            "icons/yellow-folder-open.png", false);

    Wt::WTreeNode *node = new Wt::WTreeNode("Categories", folderIcon);

    tree->setTreeRoot(node);
    node->label()->setTextFormat(Wt::PlainText);
    node->setLoadPolicy(Wt::WTreeNode::NextLevelLoading);

    const eedb::category cat;

    auto r = _db(sqlpp::select( sqlpp::all_of(cat) ).from(cat).where(cat.parent_path < 'root') );

    for (const auto & row: r){\
      node->addChildNode(new Wt::WTreeNode(row.Name));
    }

//    if (_db.is_open()){
//        auto sql = "SELECT * FROM category WHERE parent_path <@ 'root'";
//    nontransaction N(C);
//    result R( N.exec( sql ));

//    for (auto r = R.begin(); r<R.end(); r++)
//        node->addChildNode(new Wt::WTreeNode(r[1].as<std::string>()));

}
