<%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:           newsletters_table.html
Version:            0.1

DOCUMENTATION :: END
</%doc>

<% from plexpy import newsletter_handler %>
<ul class="stacked-configs list-unstyled">
    % for newsletter in sorted(newsletters_list, key=lambda k: (k['agent_label'], k['friendly_name'], k['id'])):
    <li class="newsletter-agent pointer" data-id="${newsletter['id']}">
        <span>
            <span class="toggle-left trigger-tooltip ${'active' if newsletter['active'] else ''}" data-toggle="tooltip" data-placement="top" title="Newsletter ${'active' if newsletter['active'] else 'inactive'}"><i class="fa fa-lg fa-fw fa-newspaper-o"></i></span>
            % if newsletter['friendly_name']:
            ${newsletter['agent_label']} &nbsp;<span class="friendly_name">(${newsletter['id']} - ${newsletter['friendly_name']})</span>
            % else:
            ${newsletter['agent_label']} &nbsp;<span class="friendly_name">(${newsletter['id']})</span>
            % endif
            <span class="toggle-right friendly_name">
                % if newsletter_handler.NEWSLETTER_SCHED.get_job('newsletter-{}'.format(newsletter['id'])):
                <% job = newsletter_handler.NEWSLETTER_SCHED.get_job('newsletter-{}'.format(newsletter['id'])) %>
                <span id="newsletter-next_run-${newsletter['id']}">
                    <script>
                        $("#newsletter-next_run-${newsletter['id']}").text(
                            "Next: " + moment("${job.next_run_time}", "YYYY-MM-DD HH:mm:ssZ").fromNow() + " | ")
                    </script>
                </span>
                % endif
                % if newsletter['last_triggered']:
                <% icon, icon_tooltip = ('fa-check', 'Success') if newsletter['last_success'] else ('fa-times', 'Failed') %>
                <span id="newsletter-last_triggered-${newsletter['id']}">
                    <script>
                        $("#newsletter-last_triggered-${newsletter['id']}").html(
                            "Last: " + moment("${newsletter['last_triggered']}", "X").fromNow() + ' <i class="fa fa-lg fa-fw ${icon}" data-toggle="tooltip" data-placement="top" title="${icon_tooltip}"></i>'
                        )
                    </script>
                </span>
                % else:
                Last: never
                <i class="fa fa-lg fa-fw fa-minus"></i>
                % endif
            </span>
        </span>
    </li>
    % endfor
    <li class="add-newsletter-agent pointer" id="add-newsletter-agent" data-target="#add-newsletter-modal" data-toggle="modal">
        <span>
            <span class="toggle-left"><i class="fa fa-lg fa-fw fa-newspaper-o"></i></span> Add a new newsletter agent
            <span class="toggle-right"><i class="fa fa-lg fa-fw fa-plus"></i></span>
        </span>
    </li>
</ul>

<script>
    // Load newsletter config modal
    $(".newsletter-agent").click(function () {
        var newsletter_id = $(this).data('id');
        loadNewsletterConfig(newsletter_id);
    });

    $('.trigger-tooltip').tooltip();
</script>