#import sickbeard
#import os.path
#import datetime
#import re
#from sickbeard import history
#from sickbeard import providers
#from sickbeard.providers import generic
#from sickbeard.common import *
#set global $title="History"

#set global $sbPath=".."

#set global $topmenu="history"#
#include $os.path.join($sickbeard.PROG_DIR, "data/interfaces/default/inc_top.tmpl")

<script>
<!--
\$(document).ready(function() {
    \$("#historyTable:has(tbody tr)").tablesorter({
        widgets: ['zebra', 'stickyHeaders', 'filter'],
        sortList: [[1,1]],
        sortLocaleCompare : true,
        headers: {
            1: { sorter: "text" }
        },
        textExtraction: {
            0: function(node) { if (\$("img[title]", node).length > 0) {
                                    return \$("img[title]", node).eq(0).attr("title");
                                } else {
                                    return \$(node).text();
                                }
                              },
            4: function(node) { if (\$("img[alt]", node).length > 0) {
                                    return \$("img[alt]", node).eq(0).attr("alt");
                                } else {
                                    return \$(node).text();
                                }
                              },
            5: function(node) { return \$(node).find("span").text().toLowerCase(); }
        },
        widgetOptions : {
            filter_useParsedData : true,
            filter_defaultFilter : { 2 : '/{query}/' }
        }
    });
    \$('#limit').change(function(){
        url = '$sbRoot/history/?limit=' + \$(this).val()
        window.location.href = url
    });
});
//-->
</script>

<div class="h2footer align-right"><b>Limit:</b>
    <select name="limit" id="limit">
        <option value="100" #if $limit == "100" then "selected=\"selected\"" else ""#>100</option>
        <option value="250" #if $limit == "250" then "selected=\"selected\"" else ""#>250</option>
        <option value="500" #if $limit == "500" then "selected=\"selected\"" else ""#>500</option>
        <option value="1000" #if $limit == "1000" then "selected=\"selected\"" else ""#>1000</option>
        <option value="0" #if $limit == "0" then "selected=\"selected\"" else ""#>All</option>
    </select>
</div><br/>

<table id="historyTable" class="tablesorter" cellspacing="1" border="0" cellpadding="0">
  <thead><tr><th class="col-src">Src</th><th class="col-time">Time</th><th>Episode</th><th class="col-action">Action</th><th>Provider</th><th>Quality</th></tr></thead>
  <tbody>
#for $hItem in $historyResults:
#set $curStatus, $curQuality = $Quality.splitCompositeStatus(int($hItem["action"]))
  <tr>
    <td class="col-src">
    #if $hItem["source"]:
        <img src="$sbRoot/images/${hItem["source"]}.png" width="16" height="16" alt="$hItem["source"]" title="$hItem["source"]" />
    #else:
        $hItem["source"]
    #end if
    </td>
    <td class="col-time">$datetime.datetime.strptime(str($hItem["date"]), $history.dateFormat)</td>
    <td><a href="$sbRoot/home/displayShow?show=$hItem["showid"]#season-$hItem["season"]">$hItem["show_name"] - <%=str(hItem["season"]) + "x" + "%02i" % int(hItem["episode"]) %></a></td>
    <td class="col-action">$statusStrings[$curStatus]</td>
    <td align="center">
    #if $curStatus == DOWNLOADED and $str($hItem["provider"]) == '-1':
        #set $match = $re.search("\-(\w+)\.\w{3}\Z", $os.path.basename($hItem["resource"]))
        #if $match:
            #if $match.group(1).upper() in ("X264", "720P"):
                #set $match = $re.search("(\w+)\-.*\-" + $match.group(1) + "\.\w{3}\Z", $os.path.basename($hItem["resource"]), re.IGNORECASE)
                #if $match:
                    $match.group(1).upper()
                #end if
            #else:
                $match.group(1).upper()
            #end if
        #end if
    #elif $curStatus == DOWNLOADED:
        $hItem["provider"]
    #else:
      #if $hItem["provider"] and $len($hItem["provider"]) > 0:
        #set provider_id = $generic.GenericProvider.makeID($hItem["provider"])
        #set $provider = $providers.getProviderClass($provider_id)
        #if $provider is not None:
            #set image_name = provider.imageName()
        #else:
            #if $hItem["provider"] in ["Bin-Req", "TVBinz", "NZBs.org Old", "NZBMatrix", "NZBs'R'US", "Newzbin", "nzbX"]:
                #set image_name = $provider_id + '.png'
            #else:
                #set image_name = 'missing.png'
            #end if
        #end if
        <img src="$sbRoot/images/providers/$image_name" width="16" height="16" alt="$hItem["provider"]" title="$hItem["provider"]"/>
      #end if
    #end if
    </td>
    <td align="center"><span class="quality $Quality.qualityStrings[$curQuality].replace("720p","HD720p").replace("1080p","HD1080p").replace("RawHD TV", "RawHD").replace("HD TV", "HD720p")">$Quality.qualityStrings[$curQuality]</span></td>
  </tr>
#end for
  </tbody>
</table>

#include $os.path.join($sickbeard.PROG_DIR, "data/interfaces/default/inc_bottom.tmpl")
