<%pre>

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

#include <vdr/recording.h>

using namespace std;
using namespace vdrlive;

</%pre>
<%args>
	// input parameters
	string recid;
	string async;
	// form parameters
	string name = "";
	string directory = "";
	string options[];
</%args>
<%session scope="global">
bool logged_in(false);
string edit_rec_referer;
</%session>
<%request scope="page">
const cRecording* recording;
</%request>
<%include>page_init.eh</%include>
<%cpp>
	if (!logged_in && LiveSetup().UseAuth()) return reply.redirect("login.html");

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

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

	string message;

	recording = NULL;
	if (!recid.empty()) {
		recording = LiveRecordingsManager()->GetByMd5Hash(recid);
		if (!recording)
			throw HtmlError(tr("Couldn't find recording. Maybe you mistyped your request?"));
	}

	if (request.getMethod() == "POST") {
		if (name.empty())
			message = tr("Please set a name for the recording!");
		else if (recording) {
			bool copy_only = false;
			for (options_type::const_iterator it = options.begin(); it != options.end(); ++it) {
				if (*it == "delresume")
					LiveRecordingsManager()->DeleteResume(recording);
				else if (*it == "delmarks")
					LiveRecordingsManager()->DeleteMarks(recording);
				else if (*it == "copy")
					copy_only = true;
			}
			options.clear();
			string filename =  directory.empty() ? name : StringReplace(directory, "/", "~") + "~" + name;
			if (LiveRecordingsManager()->MoveRecording(recording, FileSystemExchangeChars(filename, true), copy_only))
				return reply.redirect(!edit_rec_referer.empty() ? edit_rec_referer : "recordings.html");
			else
				message = tr("Cannot copy, rename or move the recording.");
		}
	}

	if (message.empty())
		edit_rec_referer = request.getHeader("Referer:", "recordings.html");

	if (recording) {
		string path = recording->Name();
		size_t found = path.find_last_of("~");

		if (found != string::npos) {
			directory = StringReplace(path.substr(0, found), "~", "/");
			name = path.substr(found + 1);
		}
		else {
			directory = "";
			name = path;
		}
	}
</%cpp>
<& pageelems.doc_type &>
<html>
	<head>
		<title>VDR Live - <$ tr("Edit recording") $></title>
<%cpp>
	if (!ajaxReq) {
</%cpp>
		<& pageelems.stylesheets &>
		<& pageelems.ajax_js &>
<%cpp>
	}
</%cpp>
	</head>
	<body>
<%cpp>
	if (!ajaxReq) {
</%cpp>
		<& pageelems.logo &>
		<& menu active=("recordings") &>
<%cpp>
	}
</%cpp>
		<div class="inhalt">
			<form method="post" name="edit_recording" id="<$ recid $>" action="edit_recording.ecpp">
				<input type="hidden" name="recid" value="<$ recid $>"/>
				<table class="formular" cellpadding="0" cellspacing="0">
					<tr class="head">
						<td class="toprow leftcol rightcol" colspan="2"><div class="boxheader"><div><div class="caption"><$ tr("Edit recording") $></div></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="80"/></td>
					</tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Directory") $>:</div></td>
						<td class="rightcol">
							<select name="directory" size="1" id="directory" style="margin-top: 5px">
<%cpp>
	DirectoryListPtr dirList(LiveRecordingsManager()->GetDirectoryList());
	DirectoryList::DirVecType::const_iterator dir;

	for (dir = dirList->begin(); dir != dirList->end(); ++dir) {
</%cpp>
								<option value="<$ *dir $>"<$ (*dir == directory) ? "selected=\"selected\"" : "" $>><$ *dir $></option>
<%cpp>
	}
</%cpp>
							</select>
						</td>
					</tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Delete resume information") $>:</div></td>
						<td class="rightcol"><input type="checkbox" name="options" 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="options" value="delmarks"/></td>
					</tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Copy only") $>:</div></td>
						<td class="rightcol"><input type="checkbox" name="options" value="copy"/></td>
					</tr>
<%cpp>
if (recording && recording->Info()->ShortText()) {
</%cpp>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Short description") $>:</div></td>
						<td class="rightcol"><$ recording->Info()->ShortText() $></td>
					</tr>
<%cpp>
}
if (recording && recording->Info()->Description()) {
</%cpp>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Description") $>:</div></td>
						<td class="rightcol"><$ recording->Info()->Description() $></td>
					</tr>
<%cpp>
}
if (recording && recording->Info()->Aux()) {
</%cpp>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Auxiliary info") $>:</div></td>
						<td class="rightcol"><$ recording->Info()->Aux() $></td>
					</tr>
<%cpp>
}
</%cpp>
					<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="history.back()"><$ tr("Cancel") $></button>
							</div>
						</td>
					</tr>
				</table>
			</form>
			<div style="color: red; margin: 0 auto;">
				<$ message $>
			</div>
		</div>
	</body>
</html>
<%include>page_exit.eh</%include>