@model String
@{ViewBag.Title = "History";}
@section ActionMenu{
    <ul class="sub-menu">
        <li>@Ajax.ActionLink("Trim History", "Trim", "History", null, new AjaxOptions{ OnSuccess = "redrawGrid", Confirm = "Delete history items older than 30 days?"}, new { Title = "Delete history items older than 30 days" })</li>
        <li>@Ajax.ActionLink("Purge History", "Purge", "History", null, new AjaxOptions{ OnSuccess = "redrawGrid", Confirm = "Purge all history items?" }, new { Title = "Delete all history items" })</li>
        <li>@Html.ActionLink("Search History", "Index", "SearchHistory", null, new { Title = "Review recent searches" })</li>
    </ul>
}

<div class="grid-container">
    <table id="historyGrid" class="dataTablesGrid hidden-grid">
        <thead>
            <tr>
                <th></th>
                <th>Series Title</th>
                <th>Episode</th>
                <th>Episode Title</th>
                <th>Quality</th>
                <th>Grabbed On</th>
        
                @*Commands Column*@
                <th>Actions</th>
            
                @*Details Column*@
                <th style="display: none;">Details</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>

@section Scripts{
    <script type="text/javascript">
        deleteHistoryRowUrl = '../History/Delete';
        redownloadUrl = '../History/Redownload';

        function reloadHistoryGrid() {
            var grid = $('#history').data('tGrid');
            grid.ajaxRequest();
        }

        $(document).ready(function() {
            $('#historyGrid').removeClass('hidden-grid');

            oTable = $('#historyGrid').dataTable({
                "sAjaxSource": "History/AjaxBinding",
                "bServerSide": true,
                "bProcessing": true,
                "bShowAll": false,
                "bPaginate": true,
                "bLengthChange": false,
                "bFilter": true,
                "bSort": true,
                "bInfo": true,
                "bAutoWidth": false,
                "iDisplayLength": 20,
                "sPaginationType": "four_button",
                "aoColumns": [
                                {
                                    sName: 'Icon', sWidth: '20px', "bSortable": false, "bSearchable": false, "mDataProp": function (source, type, val) {
                                        // 'display' and 'filter' use the image
                                        if (type === 'display' || type === 'filter') {
                                            if (source['Indexer'].indexOf("Newznab") === 0)
                                                return "<img src='/Content/Images/Indexers/" + source['Indexer'] + ".png' alt='" + source["Indexer"].replace('_', ' - ') + "' title='" + source["Indexer"].replace('_', ' - ') + "' onerror='this.src=\"/Content/Images/Indexers/Newznab.png\"'>";

                                            return "<img src='/Content/Images/Indexers/" + source['Indexer'] + ".png' alt='" + source["Indexer"] + "' title='" + source["Indexer"] + "'>";
                                        }
                                        // 'sort' and 'type' both just use the raw data
                                        return source["Indexer"];
                                    }
                                }, //Image
                                {
                                    sName: 'Series.Title', sWidth: 'auto', "mDataProp": function (source, type, val) {
                                        // 'display' and 'filter' use our fancy naming
                                        if (type === 'display' || type === 'filter') {
                                            return "<a href='/Series/Details?seriesId=" + source["SeriesId"] + "'>" + source["SeriesTitle"] + "</a>";
                                        }
                                        // 'sort' and 'type' both just use the raw data
                                        return source["SeriesTitleSorter"];
                                    }
                                }, //Series Title
                                { sName: 'EpisodeNumbering', sWidth: '80px', "mDataProp": "EpisodeNumbering", "bSortable": false, "bSearchable": false }, //EpisodeNumbering
                                { sName: 'Episodes.Title', sWidth: 'auto', "mDataProp": "EpisodeTitle", "bSortable": false }, //Episode Title
                                { sName: 'Quality', sWidth: '70px', "mDataProp": "Quality", "bSortable": false, "bSearchable": false }, //Quality
                                { sName: 'Date', sWidth: '150px', "bSearchable": false, "mDataProp": function (source, type, val) {
                                        // 'display' and 'filter' use our fancy naming
                                        if (type === 'display' || type === 'filter') {
                                            return source["Date"];
                                        }
                                        // 'sort' and 'type' both just use the raw data
                                        return source["DateSorter"];
                                    }
                                }, //Date
                                { sName: 'Actions', sWidth: '40px', "mDataProp": "HistoryId", "bSortable": false, "bSearchable": false, "fnRender": function (row) {
                                    var deleteImage = "<img src=\"../../Content/Images/close.png\" alt=\"Delete\" title=\"Delete from History\" class=\"gridAction\" onclick=\"deleteHistory(this.parentNode.parentNode, " + row.aData["HistoryId"] + ")\">";
                                    var redownloadImage = "<img src=\"../../Content/Images/redownload.png\" alt=\"Redownload\" title=\Redownload Episode\" class=\"gridAction\" onclick=\"redownloadHistory(this.parentNode.parentNode, " + row.aData["HistoryId"] + ", " + row.aData["EpisodeId"] + ")\">";

                                    return deleteImage + redownloadImage;
                                }
                                }, //Actions
                    {
                        sName: 'Details', sWidth: 'auto',
                        "mDataProp": "Details",
                        "bSortable": false,
                        "bSearchable": false,
                        "bVisible": false,
                        "fnRender": function(row) {
                            var result = "<b>Overview: </b>" + row.aData["EpisodeOverview"] + "<br/>" +
                                "<b>NZB Title: </b>" + row.aData["NzbTitle"] + "<br/>" +
                                    "<b>Proper: </b>" + row.aData["IsProper"] + "<br/>" +
                                    "<b>Indexer: </b>" + row.aData["Indexer"].replace('_', ' - ');
                            
                            if (row.aData["ReleaseGroup"] != null && row.aData["ReleaseGroup"] != "")
                                result += "<br/><b>Release Group: </b> " + row.aData["ReleaseGroup"];

                            if (row.aData["NzbInfoUrl"] != null && row.aData["NzbInfoUrl"] != "")
                                result += "<br/><b>Nzb Details: </b> <a href=\"" + row.aData["NzbInfoUrl"] + "\" target=\"_blank\">Details</a>";

                            return result;
                        }
                    } //Details
                ],
                "aaSorting": [[5, 'desc']],
                "oLanguage": {
                    "sEmptyTable": "Nothing has been downloaded, or results have been purged",
                    "sZeroRecords": "No items match the filter"
                }
            }).fnSetFilteringDelay(500);
        });

        function deleteHistory(row, historyId) {
            //Delete from DB
            $.ajax({
                type: "POST",
                url: deleteHistoryRowUrl,
                data: { historyId: historyId },
                success: function() {
                    oTable.fnDeleteRow(oTable.fnGetPosition(row));
                }
            });

        }

        function redownloadHistory(row, historyId, episodeId) {
            $.ajax({
                type: "POST",
                url: redownloadUrl,
                data: { historyId: historyId, episodeId: episodeId },
                success: function() {
                    oTable.fnDeleteRow(oTable.fnGetPosition(row));
                }
            });
        }
    </script>
}