<%pre>

#include <epgsearch.h>
#include <timerconflict.h>
#include <setup.h>
#include <users.h>
#include <tools.h>

using namespace vdrlive;

</%pre>
<%args>
  // input parameters
  std::string searchtimerid;
  std::string action;
</%args>
<%session scope="global">
bool logged_in(false);
TimerConflictNotifier timerNotifier;
</%session>
<%include>page_init.eh</%include>
<%cpp>
reply.setHeader (tnt::httpheader::cacheControl, "no-store, no-cache, max-age=0, must-revalidate, proxy-revalidate", true);
if (!logged_in && LiveSetup().UseAuth()) {
  cToSvConcat targetUrl = "/login.html?redirect=";
  targetUrl.appendUrlEscaped(request.getQuery());
  return reply.redirect(targetUrl.data());
}
</%cpp>
<%cpp>
  pageTitle = tr("Search timers");
  SearchTimers timers;
  if (!action.empty() ) {
    if (!searchtimerid.empty() ) {
      if (action == "toggle")
        timers.ToggleActive(searchtimerid);
      if (action == "delete") {
        if (!cUser::CurrentUserHasRightTo(UR_DELSTIMERS))
          throw HtmlError( tr("Sorry, no permission. Please contact your administrator!") );
        timers.Delete(searchtimerid);
      }
    }
    if (action == "update") {
      timers.TriggerUpdate();
      timerNotifier.SetTimerModification();
    }
</%cpp>
<!DOCTYPE html>
<html>
  <script>
    history.go(-1);
  </script>
</html>
<%cpp>
  }  else { // endif (!action.empty()
</%cpp>
<& pageelems.doc_type &>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>VDR-Live - <$ pageTitle $></title>
    <& pageelems.stylesheets &>
    <& pageelems.ajax_js &>
  </head>
  <body onpagehide="saveScrollPosition('content')" onpageshow="restoreScrollPosition()">
    <& pageelems.logo &>
    <& menu active=("searchtimers") component=("searchtimers.searchevent_actions")>
    <div id="content">
      <div class="spacebar"><# spacer with fade-out effect #></div>
      <table class="listing searchtimers" cellspacing="0" cellpadding="0">
        <tr class="head">
          <td colspan="8">
            <div class="boxheader"><div class="caption"><$ pageTitle $></div></div>
          </td>
        </tr>
        <tr class="description">
          <td class="action leftcol">&nbsp;</td>
          <td><div class="withmargin"><$ tr("Expression") $></div></td>
          <td><div class="withmargin"><$ tr("Channel") $></div></td>
          <td><div class="withmargin"><$ tr("Starts between") $></div></td>
          <td class="rightcol" colspan="4"/>
        </tr>
<%cpp>
        for (SearchTimers::iterator timer = timers.begin(); timer != timers.end(); ++timer) {
          SearchTimers::iterator nextTimer = timer; ++nextTimer;
          bool bottom = (nextTimer == timers.end());
          int i = std::abs(timer->Id() );
          char timerID_b[21]; // unsigned int 64: max. 20. (18446744073709551615) signed int64: max. 19 (+ sign)
          char *timerID = timerID_b+20;
          *(timerID) = 0;
          if (i < 10) *(--timerID) = '0' + i;
          else for (; i; i /= 10) *(--timerID) = '0' + (i % 10);
</%cpp>
        <tr class="<$ timer->UseAsSearchTimer() ? "active" : "inactive" $>">
          <td class="action leftcol <? bottom ? "bottomrow" ?>"><div class="state">
%         if(timer->UseAsSearchTimer()) {
            <img class="icon" src="<$ LiveSetup().GetThemedLink("img", "arrow.png") $>" alt="" />
%         }
          </div></td>
          <td class="<? bottom ? "bottomrow" ?>"><div class="searchtimer withmargin"><$ timer->Search() $></div></td>
          <td class="<? bottom ? "bottomrow" ?>"><div class="withmargin"><$ timer->ChannelText() $></div></td>
          <td class="<? bottom ? "bottomrow" ?>"><div class="withmargin"><? timer->UseTime() ? timer->StartTimeFormatted()+tr(" - ")+timer->StopTimeFormatted() ?></div></td>
          <td class="action rightcol <? bottom ? "bottomrow" ?>">
            <div class="event_actions">
%             constexpr auto regexBreaksAtLeft = "{[(|";
%             constexpr auto regexBreaksAtRight = "|)]}";
              <a href="searchtimers.html?searchtimerid=<$ timerID $>&action=toggle"><img class="icon" src="<$ LiveSetup().GetThemedLink("img", timer->UseAsSearchTimer() ? "active.png" : "inactive.png") $>" alt="<$ tr("Toggle search timer actions (in)active") $>" <& tooltip.hint label=(timer->Search()) className=("click-action") breaksAtLeft=(regexBreaksAtLeft) breaksAtRight=(regexBreaksAtRight) text=(tr("Toggle search timer actions (in)active")) &> /></a>
              <a href="searchresults.html?searchtimerid=<$ timerID $>"><img class="icon" src="<$ LiveSetup().GetThemedLink("img", "search.png") $>" alt="<$ tr("Browse search timer results") $>" <& tooltip.hint label=(timer->Search()) className=("click-action") breaksAtLeft=(regexBreaksAtLeft) breaksAtRight=(regexBreaksAtRight) text=(tr("Browse search timer results")) &> /></a>
              <a href="edit_searchtimer.html?searchtimerid=<$ timerID $>"><img class="icon" src="<$ LiveSetup().GetThemedLink("img", "edit.png") $>" alt="<$ tr("Edit search timer") $>" <& tooltip.hint label=(timer->Search()) className=("click-action") breaksbreaksAtLeft=(regexBreaksAtLeft) breaksAtRight=(regexBreaksAtRight) text=(tr("Edit search timer")) &> /></a>
              <a href="searchtimers.html?searchtimerid=<$ timerID $>&action=delete" onclick="return confirm('<$ timer->Search() $>\\n\\n<$ tr("Delete this search timer?") $>')"><img class="icon" src="<$ LiveSetup().GetThemedLink("img", "del.png") $>" alt="<$ tr("Delete search timer") $>" <& tooltip.hint label=(timer->Search()) className=("click-action") breaksAtLeft=(regexBreaksAtLeft) breaksAtRight=(regexBreaksAtRight) text=(tr("Delete search timer")) &> /></a>
              <div class="notpresent xs-visible-4 xs-columns-2"></div>
            </div>
          </td>
        </tr>
<%cpp>
        }
        if (timers.size() == 0) {
</%cpp>
          <# add the message in a table row beneath the header, because the message alone looks weird #>
          <tr>
            <td class="message leftcol rightcol bottomrow" colspan="8">
              <div><$ tr("No searchtimers found") $></div>
            </td>
          </tr>
<%cpp>
        }
</%cpp>
        <tr class="spacer">
          <td colspan="8"/>
        </tr>
      </table>
      <div class="spacebar"><# spacer with fade-out effect #></div>
    </div>
  </body>
</html>
% }
<%include>page_exit.eh</%include>

<%def searchevent_actions>
<a href="edit_searchtimer.html"><$ tr("New search timer") $></a>
<span class="sep">|</span>
<a href="searchtimers.html?action=update"><$ tr("Trigger search timer update") $></a>
</%def>
