<%doc>
USAGE DOCUMENTATION :: PLEASE LEAVE THIS AT THE TOP OF THIS FILE

For Mako templating syntax documentation please visit: http://docs.makotemplates.org/en/latest/

Filename:           scheduler_table.html
Version:            0.1

DOCUMENTATION :: END
</%doc>

<%!
    import datetime
    import plexpy
    from plexpy import common, helpers

    scheduled_jobs = [j.id for j in plexpy.SCHED.get_jobs()]
%>

<table class="config-scheduler-table small-muted">
    <thead>
        <tr>
            <th>Scheduled Task</th>
            <th>State</th>
            <th>Interval</th>
            <th>Next Run In</th>
            <th>Next Run Time</th>
        </tr>
    </thead>
    <tbody>
        % for job in common.SCHEDULER_LIST:
        % if job in scheduled_jobs:
        <%
            sched_job = plexpy.SCHED.get_job(job)
            now = datetime.datetime.now(sched_job.next_run_time.tzinfo)
        %>
        <tr>
            <td>${sched_job.id}</td>
            <td><i class="fa fa-sm fa-fw fa-check"></i> Active</td>
            <td>${helpers.format_timedelta_Hms(sched_job.trigger.interval)}</td>
            <td>${helpers.format_timedelta_Hms(sched_job.next_run_time - now)}</td>
            <td>${sched_job.next_run_time.astimezone(plexpy.SYS_TIMEZONE).strftime('%Y-%m-%d %H:%M:%S')}</td>
        </tr>
        % elif job in ('Check for server response', 'Check for active sessions', 'Check for recently added items') and plexpy.WS_CONNECTED:
        <tr>
            % if job == 'Check for active sessions':
            <td><a class="queue-modal-link" href="#" data-queue="active sessions">${job}</a></td>
            % elif job == 'Check for recently added items':
            <td><a class="queue-modal-link" href="#" data-queue="recently added">${job}</a></td>
            % else:
            <td>${job}</td>
            % endif
            <td><i class="fa fa-sm fa-fw fa-check"></i> Websocket</td>
            <td>N/A</td>
            <td>N/A</td>
            <td>N/A</td>
        </tr>
        % else:
        <tr>
            <td>${job}</td>
            <td><i class="fa fa-sm fa-fw fa-times"></i> Inactive</td>
            <td>N/A</td>
            <td>N/A</td>
            <td>N/A</td>
        </tr>
        % endif
        % endfor
    </tbody>
</table>
<script>
    $('.queue-modal-link').on('click', function (e) {
        e.preventDefault();
        $.ajax({
            url: 'get_queue_modal',
            data: {
                queue: $(this).data('queue')
            },
            cache: false,
            async: true,
            complete: function(xhr, status) {
                $("#queue-modal").html(xhr.responseText);
                $('#queue-modal').modal();
            }
        });
    });
</script>
