<%pre>
#include <exception.h>
#include <setup.h>
#include <recman.h>
#include <tools.h>
#include <stringhelpers.h>

using namespace vdrlive;

</%pre>
<%args>
  name = "recid";
  selected;
  onchange;
</%args>
<%session scope="global">
  bool logged_in(false);
  std::string prev_recording;
  std::string next_recording;
</%session>
<%cpp>
  if (!logged_in && LiveSetup().UseAuth()) {
    cToSvConcat targetUrl = "/login.html?redirect=";
    targetUrl.appendUrlEscaped(request.getQuery());
    return reply.redirect(targetUrl.data());
  }
</%cpp>
<select name="<$ name $>" id="<$ name $>" <{ reply.out() << ( !onchange.empty() ? "onchange=\""+onchange+"\"" : "" ); }>>
<%cpp>
  XXH128_hash_t selected_rec_hash = {0, 0};
  if (selected.length() == 42 && selected.compare(0, 10, "recording_") == 0)
    selected_rec_hash = parse_hex_128(selected.substr(10));

  bool prev_selected = false;
  cToSvConcat<8 + 128/4> recId; // 32 hash bytes (4 bits per hex digit) plus some spare bytes
  RecordingsTreePtr recordingsTree(RecordingsManager::GetRecordingsTree());
  const std::vector<RecordingsItemRec*>* allRecordings = recordingsTree->allRecordings(vdrlive::RecordingsManager::eSortOrder::name);
  for (const RecordingsItemRec* rPtr : *allRecordings ) {
    bool isSelected = XXH128_isEqual(rPtr->IdHash(), selected_rec_hash);
    if (isSelected) prev_recording = cSv(recId);  // this is the recId from last loop

    recId.clear(); recId.appendHex(rPtr->IdHash());

    if (prev_selected) next_recording = cSv(recId);
    prev_selected = isSelected;
</%cpp>
    <option value="recording_<$$ recId $>"<? isSelected ? " selected=\"selected\"" ?>><$ rPtr->Name() $>
%     if (!rPtr->Folder().empty() ) {
      (<$ rPtr->Folder() $>)
%    }
    </option>
% }
</select>
