<%pre>

#include <epgsearch.h>
#include <tools.h>
#include <setup.h>
#include <epg_events.h>
#include <timers.h>
#include <recman.h>
#include <services.h>


using namespace vdrlive;

</%pre>
<%args>
  // input parameters
  std::string searchtimerid;
  std::string searchtimerquery;
  std::string searchplain;
</%args>
<%session scope="global">
bool logged_in(false);
</%session>
<%request scope="page">
  std::string searchterm="";
</%request>
<%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());
  }

  pageTitle = tr("Search results");
  SearchResults results;
  if (!searchtimerid.empty())
    results.GetByID(parse_int< int >(searchtimerid));
  if (!searchtimerquery.empty())
    results.GetByQuery(SearchResults::GetQuery(searchtimerquery));
  if (!searchplain.empty())
  {
    SearchTimer s;
    s.SetId(0);
    s.SetSearch(searchplain);
    s.SetSearchMode(0);
    s.SetUseTitle(true);
    s.SetUseSubtitle(false);
    s.SetUseDescription(false);
    results.GetByQuery(s.ToText());

    searchterm = searchplain;
  }
</%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 &>
    <& pageelems.create_html_js &>
  </head>
  <body onpagehide="saveScrollPosition('content')" onpageshow="restoreScrollPosition()">
    <& pageelems.logo &>
    <& menu active=("searchepg") component=("searchresults.searchresults_actions") &>
    <div id="content">
      <div class="spacebar"><# spacer with fade-out effect #></div>
%      if (results.size() == 0) {
        <$ tr("No search results") $>
<%cpp>
       } else {
        std::string current_day = "";
        bool display_pictures = !(LiveSetup().GetTvscraperImageDir().empty());
        int col_span = 4;
        if (display_pictures) col_span++;
        cToSvConcat<0> epg_item;
        epg_item.reserve(10000);
        epg_item.concat('[');
        for (SearchResults::iterator result = results.begin(); result != results.end(); ++result) {
          LOCK_TIMERS_READ;
          LOCK_CHANNELS_READ;
          const cChannel* channel = result->GetChannel(Channels);
          if (!channel) continue;
          LOCK_RECORDINGS_READ;  // required because appendEpgItemWithRecItem needs also lock on recordings
          LOCK_SCHEDULES_READ;
          const cEvent *event = result->GetEvent(channel, Schedules);
          if (!event || !event->StartTime() ) continue;
          current_day = appendEpgItemWithRecItem(epg_item, current_day, event, channel, true, Timers);
        }
        epg_item.concat("]]]");
        if (current_day.empty() ) {
</%cpp>
          <$ tr("No search results") $>
%       } else {
<script>
document.write(addEventListString(<$col_span$>, <$$ epg_item.c_str() $>))
</script>
%       } }
      <div class="spacebar"><# spacer with fade-out effect #></div>
    </div>
    <script>imgLoad()</script>
  </body>
</html>

<%include>page_exit.eh</%include>


<%def searchresults_actions>
<a href="edit_searchtimer.html?search=<$ searchterm $>"><$ tr("New search timer") $></a>
</%def>
