<%pre>

#include <recman.h>
#include <setup.h>
#include <users.h>

#include <vdr/recording.h>

using namespace vdrlive;

</%pre>
<%args>
  // input parameters
  std::string recid;
  std::string async;
  int history_num_back;  //number of pages to go back to the recordings page
      // call with 1 -> chancel will go back one page in history
  // form parameters
  std::string name = "";
  std::string directory = "";
  std::string newdir = "";
  std::string title = "";
  std::string shorttext = "";
  std::string description = "";
  std::string delresume = "";
  std::string delmarks = "";
  std::string copy = "";
  std::string sort;
  std::string filter;
  std::string flat;
</%args>
<%session scope="global">
bool logged_in(false);
std::string active_r;
</%session>
<%include>page_init.eh</%include>
<%cpp>
  if (!logged_in && LiveSetup().UseAuth()) {
    cToSvConcat targetUrl = "/login.html?redirect=";
    targetUrl.appendUrlEscaped(request.getQuery());
    return reply.redirect(targetUrl.data());
  }

  if (!cUser::CurrentUserHasRightTo(UR_EDITRECS))
    throw HtmlError( tr("Sorry, no permission. Please contact your administrator!") );

  bool ajaxReq = !async.empty() && (parse_int<int>(async) != 0);

  std::string message;

  bool returnToReferer = false;
  if (request.getMethod() == "POST") {
    ++history_num_back;
    if (name.empty())
      message = tr("Please set a name for the recording!");
    else  {
      {
        LOCK_RECORDINGS_READ;
        const cRecording* recording = RecordingsManager::GetByHash(recid, Recordings);
        if (!recording)
          throw HtmlError(tr("Couldn't find recording. Maybe you mistyped your request?"));
        if (delresume == "delresume") RecordingsManager::DeleteResume(recording);
        if (delmarks == "delmarks") RecordingsManager::DeleteMarks(recording);
      }
      bool copy_only = false;
      if (copy == "copy") copy_only = true;
      if (newdir != ".") directory = newdir;
      if (RecordingsManager::UpdateRecording(recid, directory, name, copy_only, title, shorttext, description))
        returnToReferer = true;
      else
        message = tr("Cannot copy, rename or move the recording.");
    }
  }
  if (returnToReferer) {
</%cpp>
<!DOCTYPE html>
<html>
  <script type="text/javascript" src="js/live/createHtml.js"></script>
  <script>
    back_depending_referrer(2, <$ history_num_back $>);
  </script>
</html>
<%cpp>
  } else {
    cToSvConcat aux_data;  // display only data
    bool search_timer = false;
    {
      LOCK_RECORDINGS_READ;
      const cRecording* recording = RecordingsManager::GetByHash(recid, Recordings);
      if (recording && recording->Info()) {
        const cRecordingInfo *info = recording->Info();
        name = recording->BaseName();
//      directory = std::string(cToSvReplace(*recording->Folder(), "~", "/"));
        directory = cSv(*recording->Folder());
        title = cSv(info->Title());
        shorttext = cSv(info->ShortText());
        description = cSv(info->Description());
        if (info->Aux()) {
          cGetAutoTimerReason getAutoTimerReason;
          getAutoTimerReason.recording_in = recording;
          getAutoTimerReason.requestRecording = false;
          if (getAutoTimerReason.call(LiveSetup().GetPluginTvscraper()) && getAutoTimerReason.createdByTvscraper) {
            AppendHtmlEscapedAndCorrectNonUTF8(aux_data, getAutoTimerReason.reason);
            search_timer = true;
          } else {
            cSv epgsearchinfo = partInXmlTag(info->Aux(), "epgsearch");
            if (!epgsearchinfo.empty()) {
              AppendHtmlEscapedAndCorrectNonUTF8(aux_data, partInXmlTag(epgsearchinfo, "searchtimer") );
              search_timer = true;
            }
          }
          if (!search_timer) AppendHtmlEscapedAndCorrectNonUTF8(aux_data, info->Aux() );
        }
      }
    }
</%cpp>
<& pageelems.doc_type &>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>VDR Live - <$ tr("Edit recording") $></title>
<%cpp>
  if (!ajaxReq) {
</%cpp>
    <& pageelems.stylesheets &>
    <& pageelems.create_html_js &>
    <& pageelems.ajax_js &>
<%cpp>
  }
</%cpp>
  <script type="text/javascript">
    function new_dir() {
      var dir = document.getElementById("directory");
      dir.style.display = "none";
      document.getElementById("new_dir_button").style.display = "none";
      var newdir = document.getElementById("newdir");
      newdir.value = dir.value;
      newdir.style.display = "";
    }
  </script>
  </head>
  <body onpagehide="saveScrollPosition('content')" onpageshow="restoreScrollPosition()">
<%cpp>
  if (!ajaxReq) {
</%cpp>
    <& pageelems.logo &>
    <& menu active=(active_r) &>
<%cpp>
  }
</%cpp>
    <div id="content">
      <div class="spacebar"><# spacer with fade-out effect #></div>
      <form method="post" name="edit_recording" id="<$ recid $>" action="edit_recording.ecpp">
        <input type="hidden" name="recid" value="<$ recid $>"/>
        <input type="hidden" name="history_num_back" value="<$ history_num_back $>"/>
        <table class="form" cellpadding="0" cellspacing="0">
          <tr class="head">
            <td class="toprow leftcol rightcol" colspan="2"><div class="boxheader"><div class="caption"><$ tr("Edit recording") $></div></div></td>
          </tr>
          <tr>
            <td class="label leftcol"><div class="withmargin"><$ tr("Name") $>:</div></td>
            <td class="rightcol"><input type="text" name="name" value="<$ name $>" size="55" class="width99" /></td>
          </tr>
          <tr>
            <td class="label leftcol"><div class="withmargin"><$ tr("Directory") $>:</div></td>
            <td class="rightcol">
              <select name="directory" size="1" id="directory">
<%cpp>
  RecordingsTreePtr recordingsTree(RecordingsManager::GetRecordingsTree());
  for (const auto &dir: recordingsTree->getAllDirs() ) {
</%cpp>
                <option value="<$ dir $>"<$ (dir == directory) ? "selected=\"selected\"" : "" $>><$ dir $></option>
<%cpp>
  }
</%cpp>
              </select>
              <img id="new_dir_button" class="icon button new-dir" src="<$ LiveSetup().GetThemedLink("img", "icon_overlay_plus.png") $>" title="<$tr("Create new directory")$>" onclick="new_dir()">
              <input type="text" name="newdir" id="newdir" value="." size="55" class="width99" style="display: none" />
            </td>
          </tr>
          <tr>
            <td class="label leftcol"><div class="withmargin"><$ tr("Delete resume information") $>:</div></td>
            <td class="rightcol"><input type="checkbox" name="delresume" value="delresume"/></td>
          </tr>
          <tr>
            <td class="label leftcol"><div class="withmargin"><$ tr("Delete marks information") $>:</div></td>
            <td class="rightcol"><input type="checkbox" name="delmarks" value="delmarks"/></td>
          </tr>
          <tr>
            <td class="label leftcol"><div class="withmargin"><$ tr("Copy only") $>:</div></td>
            <td class="rightcol"><input type="checkbox" name="copy" value="copy"/></td>
          </tr>
          <tr>
            <td class="label leftcol"><div class="withmargin"><$ tr("Title") $>:</div></td>
%#if VDRVERSNUM >= 20502
            <td class="rightcol"><input type="text" name="title" value="<$ title $>" size="55" class="width99" /></td>
%#else
            <td class="rightcol"><$ title $></td>
%#endif
          </tr>
          <tr>
            <td class="label leftcol"><div class="withmargin"><$ tr("Short description") $>:</div></td>
%#if VDRVERSNUM >= 20502
            <td class="rightcol"><input type="text" name="shorttext" value="<$ shorttext $>" size="55" class="width99" /></td>
%#else
            <td class="rightcol"><$ shorttext $></td>
%#endif
          </tr>
          <tr>
            <td class="label leftcol"><div class="withmargin"><$ tr("Description") $>:</div></td>
%#if VDRVERSNUM >= 20502
            <td class="rightcol"><textarea name="description" rows="8" cols="55" class="width99"><$ description $></textarea></td>
%#else
            <td class="rightcol"><$ description $></td>
%#endif
          </tr>
% if (!aux_data.empty()) {
          <tr>
            <td class="label leftcol"><div class="withmargin"><$ search_timer?tr("Search timer"):tr("Auxiliary info") $>:</div></td>
            <td class="rightcol"><$$ aux_data $></td>
          </tr>
% }
          <tr>
            <td class="buttonpanel leftcol rightcol bottomrow" colspan="2">
              <div class="withmargin">
              <button class="green" type="submit"><$ tr("Save") $></button>
              <button class="red" type="button" onclick="back_depending_referrer(1, <$ history_num_back $>)"><$ tr("Cancel") $></button>
              </div>
            </td>
          </tr>
        </table>
      </form>
      <div style="color: red; margin: 0 auto;">
        <$ message $>
      </div>
      <div class="spacebar"><# spacer with fade-out effect #></div>
    </div>
  </body>
</html>
% }
<%include>page_exit.eh</%include>
