<%pre>
#include <memory>
#include <vdr/channels.h>
#include <vdr/config.h>
#include <vdr/epg.h>
#include <vdr/i18n.h>
#include "exception.h"
#include "tools.h"
#include "timers.h"
#include "setup.h"

using namespace std;
using namespace vdrlive;

</%pre>
<%args>
	// input parameters
	string timerid;
	tChannelID channelid;
	tEventID eventid = 0;
	// form parameters
	tChannelID channel;
	bool active = true;
	string title = "";
	string day = "";
	bool wday_mon = false;
	bool wday_tue = false;
	bool wday_wed = false;
	bool wday_thu = false;
	bool wday_fri = false;
	bool wday_sat = false;
	bool wday_sun = false;
	int start_h = 0;
	int start_m = 0;
	int end_h = 0;
	int end_m = 0;
	bool vps = 0;
	int priority = 0;
	int lifetime = 0;
	string aux = "";
</%args>
<%session scope="global">
bool logged_in(false);
</%session>
<%request scope="page">
cTimer* timer;
</%request>
<%include>page_init.eh</%include>
<{
if (!logged_in && LiveSetup().UseAuth()) return reply.redirect("login.html");
}>
<%cpp>
	cMutexLock timersLock( &LiveTimerManager() );
	SortedTimers& timers = LiveTimerManager().GetTimers();

	timer = 0;
	if ( !timerid.empty() ) {
		timer = timers.GetByTimerId( timerid );
		if ( timer == 0 )
			throw HtmlError( tr("Couldn't find timer. Maybe you mistyped your request?") );
	}

	if ( request.getMethod() == "POST" ) {
		uint flags = ( active ? tfActive : 0 ) | ( vps ? tfVps : 0 );
		string weekdays = string( wday_mon ? "M" : "-" ) + ( wday_tue ? "T" : "-" ) + ( wday_wed ? "W" : "-" )
							  + ( wday_thu ? "T" : "-" ) + ( wday_fri ? "F" : "-" ) + ( wday_sat ? "S" : "-" )
							  + ( wday_sun ? "S" : "-" );
		int start = start_h * 100 + start_m;
		int stop = end_h * 100 + end_m;

		LiveTimerManager().UpdateTimer( timer, flags, channel, weekdays, day, start, stop, priority, lifetime, title, aux );

		return reply.redirect("timers.html");
	}

	auto_ptr< cTimer > eventTimer;
	if ( timer == 0 ) {
		if ( channelid.Valid() && eventid != 0 ) {
			cerr << "grabbing event" << endl << endl;
			cSchedulesLock schedLock;
			cSchedules const* schedules = cSchedules::Schedules( schedLock );
			cSchedule const* schedule = schedules->GetSchedule( channelid );

			eventTimer.reset( new cTimer( schedule->GetEvent( eventid ) ) );
		} else {
			cerr << "grabbing new timer" << endl << endl;
			eventTimer.reset( new cTimer() );
			eventTimer->SetFlags( tfActive );
		}
		timer = eventTimer.get();
	}

	if ( timer != 0 ) {
		active = timer->Flags() & tfActive;
		channel = timer->Channel()->GetChannelID();
		title = timer->File() ? timer->File() : "";
		day = timer->Day() ? FormatDateTime( "%d", timer->Day() ) : "";
		wday_mon = timer->WeekDays() & 0x01;
		wday_tue = timer->WeekDays() & 0x02;
		wday_wed = timer->WeekDays() & 0x04;
		wday_thu = timer->WeekDays() & 0x08;
		wday_fri = timer->WeekDays() & 0x10;
		wday_sat = timer->WeekDays() & 0x20;
		wday_sun = timer->WeekDays() & 0x40;
		start_h = ( timer->Start() / 100 ) % 100;
		start_m = timer->Start() % 100;
		end_h = ( timer->Stop() / 100 ) % 100;
		end_m = timer->Stop() % 100;
		vps = timer->Flags() & tfVps;
		priority = timer->Priority();
		lifetime = timer->Lifetime();
		aux = timer->Aux() ? timer->Aux() : "";
	}
</%cpp>
<& pageelems.doc_type &>
<html>
	<head>
		<title>VDR Live - <$ timer ? tr("Edit timer") : tr("New timer") $></title>
		<link rel="stylesheet" type="text/css" href="styles.css" />
		<& tooltip.javascript var=("domTT_styleClass") value=("domTTepg") &>
		<& pageelems.ajax_js &>
	</head>
	<body onload="<& pageelems.infobox_start_update &>">
		<& pageelems.logo &>
		<& menu active=("timers") &>
		<div class="inhalt">
			<form method="POST" name="edit_timer" action="edit_timer.ecpp">
				<input type="hidden" name="timerid" value="<$ timerid $>"/>
				<input type="hidden" name="aux" value="<$ aux $>"/>
				<table class="edit" cellpadding="0" cellspacing="0">
					<tr>
						<td class="head" colspan="4"><div class="boxheader"><div><div><$ timer ? tr("Edit timer") : tr("New timer") $></div></div></div></td>
					</tr>
					<tr>
						<td class="border" style="border-left: 1px solid black"></td>
						<td class="label"><$ tr("Active") $>:</td>
						<td>
							<input name="active" value="1" <{ reply.out() << ( active ? "checked=\"checked\"" : "" ); }> type="radio"><$ tr("Yes") $>
							<input name="active" value="0" <{ reply.out() << ( !active ? "checked=\"checked\"" : "" ); }> type="radio"><$ tr("No") $>
						</td>
						<td class="border" style="border-right: 1px solid black"></td>
					</tr>
					<tr>
						<td class="border" style="border-left: 1px solid black"></td>
						<td class="label"><$ tr("Channel") $>:</td>
						<td><& channels_widget name=("channel") channelid=(true) selected=(channel) &></td>
						<td class="border" style="border-right: 1px solid black"></td>
					</tr>

					<tr>
						<td class="border" style="border-left: 1px solid black"></td>
						<td class="label"><$ tr("Title" ) $>:</td>
						<td><input type="text" name="title" value="<$ title $>" size="80" /></td>
						<td class="border" style="border-right: 1px solid black"></td>
					</tr>


					<tr>
						<td class="border" style="border-left: 1px solid black"></td>
						<td class="label"><$ tr("Day") $>:</td>
						<td><input type="text" name="day" value="<$ day $>" size="2" maxlength="2" /></td>
						<td class="border" style="border-right: 1px solid black"></td>
					</tr>

					<tr>
						<td class="border" style="border-left: 1px solid black"></td>
						<td class="label"><$ tr("Weekday") $>:</td>
						<td>
							<input type="checkbox" name="wday_mon" value="1" <{ reply.out() << ( wday_mon ? "checked=\"checked\"" : "" ); }>/> <$ tr("Monday") $>
							<input type="checkbox" name="wday_tue" value="1" <{ reply.out() << ( wday_tue ? "checked=\"checked\"" : "" ); }>/> <$ tr("Tuesday") $>
							<input type="checkbox" name="wday_wed" value="1" <{ reply.out() << ( wday_wed ? "checked=\"checked\"" : "" ); }>/> <$ tr("Wednesday") $>
							<input type="checkbox" name="wday_thu" value="1" <{ reply.out() << ( wday_thu ? "checked=\"checked\"" : "" ); }>/> <$ tr("Thursday") $>
							<input type="checkbox" name="wday_fri" value="1" <{ reply.out() << ( wday_fri ? "checked=\"checked\"" : "" ); }>/> <$ tr("Friday") $>
							<input type="checkbox" name="wday_sat" value="1" <{ reply.out() << ( wday_sat ? "checked=\"checked\"" : "" ); }>/> <$ tr("Saturday") $>
							<input type="checkbox" name="wday_sun" value="1" <{ reply.out() << ( wday_sun ? "checked=\"checked\"" : "" ); }>/> <$ tr("Sunday") $>
						<td class="border" style="border-right: 1px solid black"></td>
						</td>
					</tr>

					<tr>
						<td class="border" style="border-left: 1px solid black"></td>
						<td class="label"><$ tr("Start") $>:</td>
						<td><input type="text" size="2" maxlength="2" name="start_h" value="<$ ZeroPad(start_h) $>" /> : <input type="text" size="2" maxlength="2" name="start_m" value="<$ ZeroPad(start_m) $>" /></td>
						<td class="border" style="border-right: 1px solid black"></td>
					</tr>

					<tr>
						<td class="border" style="border-left: 1px solid black"></td>
						<td class="label"><$ tr("Stop") $>:</td>
						<td><input type="text" size="2" maxlength="2" name="end_h" value="<$ ZeroPad(end_h) $>" /> : <input type="text" size="2" maxlength="2" name="end_m" value="<$ ZeroPad(end_m) $>" /></td>
						<td class="border" style="border-right: 1px solid black"></td>
					</tr>

					<tr>
						<td class="border" style="border-left: 1px solid black"></td>
						<td class="label"><$ tr("Use VPS") $>:</td>
						<td><input type="checkbox" name="vps" value="1" <{ reply.out() << ( vps ? "checked=\"checked\"" : "" ); }>/></td>
						<td class="border" style="border-right: 1px solid black"></td>
					</tr>

					<tr>
						<td class="border" style="border-left: 1px solid black"></td>
						<td class="label"><$ tr("Priority") $>:</td>
						<td><input type="text" size="2" maxlength="2" name="priority" value="<$ priority $>" /></td>
						<td class="border" style="border-right: 1px solid black"></td>
					</tr>

					<tr>
						<td class="border" style="border-left: 1px solid black"></td>
						<td class="label"><$ tr("Lifetime") $>:</td>
						<td><input type="text" size="2" maxlength="2" name="lifetime" value="<$ lifetime $>" /></td>
						<td class="border" style="border-right: 1px solid black"></td>
					</tr>

					<tr>
						<td class="border" style="border-left: 1px solid black; border-bottom: 1px solid black"></td>
						<td class="buttonpanel" colspan="2" style="border-bottom: 1px solid black">
							<button class="green" type="submit"><$ tr("Save") $></button>
							<button type="button" class="red" onclick="location.href='timers.html';"><$ tr("Cancel") $></button>
						</td>
						<td class="border" style="border-right: 1px solid black; border-bottom: 1px solid black"></td>
					</tr>
				</table>
			</form>
		</div>
	</body>
</html>
<%include>page_exit.eh</%include>