#!/mod/bin/jimsh

package require cgi
source /mod/webif/lib/setup
require altrow rsv.class progressbar epg.class system.class

jqplugin tablesorter contextMenu form freezeheader
jscss script.js
header

set svcmap {}
lmap i \
    [$::channeldb query {select hSvc, usSvcid from TBL_SVC}] \
    {
	set svcmap([lindex $i 1]) [lindex $i 3]
    }
set svckeys [array names svcmap]

require epg_popup

puts {
<div id=output class=hidden></div>
<div id=sdialogue></div>
}

proc eventheader {{table TBL_RESERVATION} note} {
	puts {
		<fieldset style="display: inline; padding: 1em">
		<legend>
	}
	puts "<h3>$note</h3>"
	puts {
		</legend>
		<table class="borders tablesorter">
		<thead>
		<tr>
	}

	if {$table eq "pending" } { puts "<th>Action</th>" }

	puts {
			<th></th>
			<th>&nbsp;</th><th>Channel</th>
			<th>Programme</th>
			<th>Start Time</th>
			<th>Duration</th>
			<th>Mode</th>
		</tr>
		</thead>
		<tbody>
	}
}

proc eventrow {event {table TBL_RESERVATION} {pending 0}} {
	global svcmap svckeys

	set name [$event name]

	set attrs "table=$table sid=[$event get ulslot] \
	    reckind=[$event get ucRecKind] rsvtype=[$event get ersvtype] \
	    ar=[expr ! [$event padded]]"
	if {$pending} {
		append attrs " class=\"blueshade strike\""
	}
	altrow $attrs

	if {$table eq "pending"} {
		puts "<td align=center>[$event pendingicon]</td>"
	}

	puts "<td>[$event get ulslot]</td>"

	if {[$event get usLcn] ne ""} {
		puts "
		<td>
			[epg channelicon [$event channel_name] 50]
		</td>
		<td nowrap>[$event get usLcn]<br>
			[$event channel_name]</td>
		"
	} else {
		puts "<td>&nbsp;<br><br></td><td>&nbsp;</td>"
	}

	puts "<td nowrap>
		<a slot=[$event get ulslot] table=$table
		href=# class=schedule>$name</a>"
	if {
	    ([$event get ucRecKind] == 4 && $name ne [$event folder]) ||
	    ([$event get ucRecKind] == 1 && [$event folder] ne "")
	} {
		puts "<br><span class=also>&nbsp;(Folder: [$event folder])</span>"
	}

	puts "</td>"

	set s [$event get nsttime]
	set d [$event get nduration]
	set e $($s + $d)
	set n [clock seconds]

	if {$n > $e && [$event get ersvtype] <= 3} {
		set ds "[clock format $s -format {%a %d %b %Y %H:%M}]"
		puts "<td nowrap class=va>
			--- -- --- ---- --:--<br>
			<span class=footnote>(Last: $ds)</span>"
		puts "</td><td>--:--:--</td>"
		incr ::ended
	} else {
		set elist [$event aul]
		set ds "[clock format $s -format {%a %d %b %Y %H:%M}]"
		puts "<td nowrap class=\"va"
		if {$n > $e} { puts " blood" }
		puts "\">"

		if {[llength $elist] > 0} {
			lassign [lindex $elist 0] service_id start end event_id
			if {$start == $s && $service_id in $svckeys} {
				puts "<a class=event href=#
				    xs=$svcmap($service_id) xe=$event_id>
				    $ds</a>"
			} else {
				puts $ds
			}
		} else {
			puts $ds
		}

		if {$d > 0 && $n > $s && $n < $e} {
			puts "<br>"
			set perc [expr [expr $n - $s] * 100 / $d]
			puts "<img class=va
			    src=/images/745_1_11_Video_1REC.png>"
			puts [progressbar $perc]
		}

		if {[llength $elist] > 1} {
			puts "<div class=also style=\"margin-top: 0.5em\">"
			foreach e $elist {
				lassign $e service_id start end event_id
				if {$start == $s} continue
				if {$service_id in $svckeys} {
				  puts "<a class=event href=#
				    xs=$svcmap($service_id) xe=$event_id>
				    [clock format $start \
				    -format {%a %d %b %Y %H:%M}]</a>"
				  if {$service_id != [$event get hsvc]} {
					set ch [system strip [\
					    get_channel_attr_byhsvc \
					    $service_id szSvcName]]
					puts "<img width=15 src=\"[\
					    epg channeliconpath $ch]\""
					puts "title=\"$ch\" alt=\"$ch\">"
				  }
				  puts "<br>"
				} else {
				  puts "[clock format $start \
				    -format {%a %d %b %Y %H:%M}]
				    ($service_id)<br>"
				}
			}
			puts "</div>";
		}

		puts "</td><td align=center>"
		set xndur [$event get nduration]
		if {$xndur > 0} {
			puts "[clock format $xndur -format %H:%M]"
		} else {
			puts "--:--:--"
		}
		if {[llength $elist] > 1} {
			puts "<div class=also style=\"margin-top: 0.5em\">"
			foreach e $elist {
				lassign $e service_id start end event_id
				if {$start == $s} continue
				puts "[clock format $($end - $start) \
				    -format {%H:%M}]<br>"
			}
			puts "</div>";
		}

		puts "</td>"
	}

	puts "<td nowrap>[join [$event iconset] ""]</td>"

	puts "<td><a href=# class=smenu sid=[$event get ulslot]>
	    <img border=0 src=/img/more_btn.gif></a></td>"
	puts "</tr>"
}

proc eventfooter {} {
	puts "</tbody></table></fieldset>"
}

proc ekey {event} {
	set ret ""
	foreach key {ersvtype nsttime nduration hsvc usevtid} {
		append ret ".[$event get $key]"
	}
	return $ret
}

set pmap {}
set events [rsv list pending]
set ended 0
if {[llength $events] > 0} {
	eventheader pending "Pending Scheduled Events"
	foreach event $events {
		eventrow $event pending
		lappend pmap [ekey $event]
	}
	eventfooter
	puts {
		<small>
		<button onclick="window.location='/cgi-bin/db.jim?db=rsvp.db&tab=pending';">
		Raw database view
		</button>
		</small>
	}
}

set events [rsv list]
set ended 0
eventheader "live" "Scheduled Events"
foreach event $events {
	if {[ekey $event] in $pmap} {
		eventrow $event TBL_RESERVATION 1
	} else {
		eventrow $event
	}
}
eventfooter

puts {
	<br>
	<small>
	<button onclick="window.location='/backup/index.jim';">
		Backup/Restore Scheduled Recordings/Events
	</button>
}

if {$ended} {
	puts { <button id=schedule_cleanup>Cleanup old recordings</button> }
}

puts {
	<button onclick="window.location='/cgi-bin/db.jim?db=rsv.db&tab=TBL_RESERVATION';">
		Raw database view
	</button>
	<script type=text/javascript>
		$('button').button();
	</script>
}

source assets.jim

footer

