<%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
%>

<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, job_type in common.SCHEDULER_LIST.items():
        <%
            sched_job = plexpy.SCHED.get_job(job)
        %>
        % if sched_job:
        <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 - datetime.datetime.now(sched_job.next_run_time.tzinfo))}</td>
            <td>${sched_job.next_run_time.astimezone(plexpy.SYS_TIMEZONE).strftime('%Y-%m-%d %H:%M:%S')}</td>
        </tr>
        % elif job_type == 'websocket' and plexpy.WS_CONNECTED:
        <tr>
            % if job == 'Check for active sessions':
            <td><a class="queue-modal-link no-highlight" href="#" data-queue="active sessions">${job}</a></td>
            % elif job == 'Check for recently added items':
            <td><a class="queue-modal-link no-highlight" 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>
