@using NzbDrone.Web.Helpers
@model IEnumerable<NzbDrone.Web.Models.SeriesModel>
@{ViewBag.Title = "Series Editor";}

@section HeaderContent
{
    @Html.IncludeCss("Settings.css")
    @Html.IncludeCss("SeriesEditor.css")
}

@section ActionMenu
{
    <ul class="sub-menu">
        <li>@Ajax.ActionLink("Force Refresh", "ForceRefreshAll", "Command", null, null, new { Title = "Refresh episode and series information and scan for new episodes for all series" })</li>
        <li>@Ajax.ActionLink("Rename All Series", "RenameAllSeries", "Episode", null, new AjaxOptions { Confirm = "Are you sure you want to rename all series?" }, new { Title = "Rename all series monitored by NzbDrone" })</li>
    </ul>
}

@using (Html.BeginForm("Editor", "Series", FormMethod.Post, new { id = "SeriesEditor", name = "SeriesEditor" }))
{
    <table id="seriesEditorGrid" class="dataTable dataTablesGrid no-details">
        <thead>
            <tr>
                <th width="14px">@Html.CheckBox("editToggleMaster", false, new { @class = "editToggleMaster" })</th>
                <th>Title</th>
                <th width="125px;">Quality</th>
                <th width="90px">Monitored</th>
                <th width="110px">Season Folder</th>
                <th width="110px">Backlog Status</th>
                <th width="80px" title="Custom Start Date">Start Date</th>
                <th width="300px">Path</th>
                <th style="width: 10px"></th>
            </tr>
        </thead>
        
        <tbody>
            @foreach (var series in Model)
            {
                Html.RenderPartial("EditorItem", series);
            }
        </tbody>
        
        <tfoot>
            <tr>
                <th></th>
                <th class="editing-count"></th>
                <th>
                    @Html.DropDownList("masterQualitySelector", (SelectList)ViewData["MasterProfileSelectList"], new { @class = "footer-control-quality masterSelector master-quality", disabled = true })
                </th>
                <th>
                    @Html.DropDownList("masterMonitored", (SelectList)ViewData["BoolSelectList"], new { @class = "footer-control-boolean masterSelector master-monitored", disabled = true })
                </th>
                <th>
                    @Html.DropDownList("masterSeasonFolder", (SelectList)ViewData["BoolSelectList"], new { @class = "footer-control-boolean masterSelector master-season-folder", disabled = true })
                </th>
                <th>
                    @Html.DropDownList("masterBacklogSetting", (SelectList)ViewData["MasterBacklogSettingSelectList"], new { @class = "footer-control masterSelector master-backlog-setting", disabled = true })
                </th>
                <th>
                    @Html.TextBox("masterStartDate", "" , new { type = "date", @class = "footer-control-start-date masterSelector master-start-date jQuery-datepicker", disabled = true })
                </th>
                <th>
                    <button id="series-editor-save" type="submit" class="save_button" disabled="disabled" title="Commit the settings from your series above to the database">
                        Save Changes
                    </button>
                    
                    
                </th>
            </tr>
        </tfoot>
</table>
    
@Html.CheckBox("highlightEnded", true)
<label for="highlightEnded">Highlight Ended</label>
}

@section Scripts
{
    <script type="text/javascript">
        $(document).ready(function() {
            $('#seriesEditorGrid').removeClass('hidden-grid');

            oTable = $('.dataTablesGrid').dataTable({
                "bShowAll": false,
                "bPaginate": false,
                "bLengthChange": false,
                "bFilter": false,
                "bSort": true,
                "bInfo": false,
                "bAutoWidth": false,
                "aaSorting": [[1, 'asc']],
                "aoColumns": [
                    { "bSortable": false },
                    { "bSortable": true },
                    { "bSortable": false },
                    { "bSortable": false },
                    { "bSortable": false },
                    { "bSortable": false },
                    { "bSortable": false },
                    { "bSortable": false },
                    { "bSortable": true }
                ]
            });
            
            new FixedHeader(oTable, { "top": true, "left": false, "right": false, "bottom": true });
            
            $('.editToggle').enableCheckboxRangeSelection();
            
            $(document).ready(function () {
                var cookieValue = $.cookie("highlightEnded");

                if (cookieValue == "true") {
                    $('#highlightEnded').attr('checked', true);
                    toggleHighlightEnded(true);
                }

                else {
                    $('#highlightEnded').attr('checked', false);
                    toggleHighlightEnded(false);
                }

                $('#highlightEnded').button();
            });

            $('#highlightEnded').on('change', function () {
                var checked = $(this).attr('checked');
                toggleHighlightEnded(checked);
                toggleHighlightEndedCookie(checked);
            });
            
            function toggleHighlightEnded(highlight) {
                var ended = $('tr[data-status="Ended"]');

                ended.each(function () {
                    if (highlight) {
                        $(this).addClass('series-ended');
                    }

                    else {
                        $(this).removeClass('series-ended');
                    }
                });
            }

            function toggleHighlightEndedCookie(highlight) {
                if (highlight)
                    $.cookie("highlightEnded", true, { expires: 365 });

                else
                    $.cookie("highlightEnded", false, { expires: 365 });
            }
        });

        $(document).on('change', '.editToggleMaster', function () {
            var toggle = $(this).prop('checked');
            $('.editToggle').each(function () {
                $(this).prop('checked', toggle);
            });
        });

        $(document).on('change', '.editToggle, .editToggleMaster', function () {
            var selectedCount = $('.editToggle:checked');

            if (selectedCount.length > 0) {
                $('.masterSelector').each(function () {
                    $(this).attr("disabled", false);
                });

                $('.editing-count').text(selectedCount.length + ' series have been selected for editing');

                if (selectedCount.length === 1) {
                    $('.editing-count').text(selectedCount.length + ' series has been selected for editing');
                }
            }

            else {
                $('.masterSelector').each(function () {
                    $(this).attr("disabled", true);
                    $('.editing-count').text('');
                });
            }
        });
        
        $(document).on('change', '#masterQualitySelector', function () {
            var profileId = $(this).val();
            
            if (profileId === -10)
                return;

            var selected = $('.editToggle:checked');

            selected.each(function () {
                $(this).parent('td').parent('.seriesEditRow').find('.quality').val(profileId);         
            });
        });
        
        $(document).on('change', '#masterMonitored', function () {
            var monitored = $(this).val();

            if (monitored === -10)
                return;
            
            var monitoredBool = true;
            if (monitored != 1)
                monitoredBool = false;

            var selected = $('.editToggle:checked');

            selected.each(function () {
                $(this).parent('td').parent('.seriesEditRow').find('.monitored').prop('checked', monitoredBool);
            });
        });
        
        $(document).on('change', '#masterSeasonFolder', function () {
            var seasonFolder = $(this).val();

            if (seasonFolder === -10)
                return;

            var seasonFolderBool = true;
            if (seasonFolder != 1)
                seasonFolderBool = false;

            var selected = $('.editToggle:checked');

            selected.each(function () {
                $(this).parent('td').parent('.seriesEditRow').find('.seasonFolder').prop('checked', seasonFolderBool);
            });
        });
        
        $(document).on('change', '#masterBacklogSetting', function () {
            var backlogStatus = $(this).val();

            if (backlogStatus === -10)
                return;

            var selected = $('.editToggle:checked');

            selected.each(function () {
                $(this).parent('td').parent('.seriesEditRow').find('.backlogSetting').val(backlogStatus);
            });
        });
        
        $(document).on('change', '#masterStartDate', function () {
            var startDate = $(this).val();

            var selected = $('.editToggle:checked');

            selected.each(function () {
                $(this).parent('td').parent('.seriesEditRow').find('.start-date').val(startDate);
            });
        });
        
        $(document).on('click', '#series-editor-save', function () {
            $('#SeriesEditor').submit();
        });
    </script>
}