<%pre>

#include <epgsearch.h>
#include <tools.h>
#include <setup.h>
#include <epg_events.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>
	if (!logged_in && LiveSetup().UseAuth()) return reply.redirect("login.html");

	pageTitle = tr("Search results");
	SearchResults results;
	if (!searchtimerid.empty())
		results.GetByID(lexical_cast< int >(searchtimerid));
	if (!searchtimerquery.empty())
		results.GetByQuery(SearchResults::PopQuery(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>
		<title>VDR-Live - <$ pageTitle $></title>
		<& pageelems.stylesheets &>
		<& pageelems.ajax_js &>
	</head>
	<body onload="adjustHeader()" onresize="adjustHeader()">
		<& pageelems.logo &>
		<& menu active=("searchepg") component=("searchresults.searchresults_actions") &>
		<div class="inhalt">
%			if (results.size() == 0) {
				<$ tr("No search results") $>
%			}
			<table class="listing" cellspacing="0" cellpadding="0">
<%cpp>
				std::string current_day = "";

				for (SearchResults::iterator result = results.begin(); result != results.end(); ++result) {
#if VDRVERSNUM >= 20301
                                       	cStateKey StateKey;
                                      	if (const cChannels *Channels = cChannels::GetChannelsRead(StateKey)) {
				#ifdef DEBUG_LOCK
		                		dsyslog("live: pages/searchresults.ecpp LOCK_CHANNELS_READ");
				#endif
						cChannel* channel = (cChannel *)Channels->GetByChannelID(result->Channel());
#else
					cChannel* channel = Channels.GetByChannelID(result->Channel());
#endif
						if (!channel) {
         						StateKey.Remove();
							continue;
						}
						std::string channelname = channel->Name();
						int channelnr = channel->Number();
						std::string start(result->StartTime() ? FormatDateTime(tr("%I:%M %p"), result->StartTime()) : "");
						std::string end(result->StopTime() ? FormatDateTime(tr("%I:%M %p"), result->StopTime()) : "");
						std::string day(result->StartTime() ? FormatDateTime(tr("%A, %b %d %Y"), result->StartTime()) : "");
						std::string description = result->Description();
						std::string epgid = EpgEvents::EncodeDomId(result->Channel(), result->EventId());

						bool truncated = false;

						bool bottom = false;
						SearchResults::iterator nextResult = result; ++nextResult;
						if (nextResult == results.end())
							bottom = true;
						else {
							std::string nextDay(nextResult->StartTime() ? FormatDateTime(tr("%A, %b %d %Y"), nextResult->StartTime()) : "");
							bottom = (day != nextDay);
						}

						if (current_day != day) {
							if (current_day != "") {
</%cpp>
						<tr class="spacer">
							<td colspan="4"/>
						</tr>
%							}
						<tr class="head">
							<td colspan="4"><div class="boxheader"><div><div><$ day $></div></div></div></td>
						</tr>
%							current_day = day;
%						}
%					        StateKey.Remove();  // release channels read lock before calling event_timer which make a timers read lock
							<tr>
							<td class="action leftcol <? bottom ? "bottomrow"?>"><& pageelems.event_timer epgid=(epgid) &></td>
							<td class="topaligned <? bottom ? "bottomrow"?>"><div class="withmargin"><a href="schedule.html?channel=<$ channelnr $>"><$ channelname $></a></div></td>
							<td class="topaligned <? bottom ? "bottomrow"?>"><div class="withmargin nowrap"><$ start $> - <$ end $></div></td>
							<td class="topaligned rightcol <? bottom ? "bottomrow"?>"><div class="more withmargin"><a <& tooltip.hint text=(StringEscapeAndBreak(StringWordTruncate(description, 300, truncated)) + "<br />" + tr("Click to view details.")) &><& tooltip.display domId=(epgid) &>><span class="title"><$ result->Title() $></span><br /><span class="short"><%cpp>if (result->ShortText().empty()) { </%cpp>&nbsp;<%cpp> } </%cpp><$ result->ShortText() $></span></a></div></td>
						</tr>
%					}
%				}
			</table>
		</div>
	</body>
</html>

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


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