<tal:block metal:use-macro="python:here.getFormsTool().getForm('site_admin_template').macros['page']">
    <h1 metal:fill-slot="title" i18n:translate="">Interest Group Logger</h1>

    <tal:block metal:fill-slot="section" define="
        current_month python:here.REQUEST.form.get('current_month', 'current'); 
        site_logger python:here.get_site_logger_content(month=current_month); 
        logged_months here/get_logged_months; 
        plain_text_lines python: site_logger['plain_text_lines']; 
        lines python: site_logger['lines']; 
        site_url python: here.getSite().absolute_url();">
        <script type="text/javascript" src="/++resource++Products.Naaya/lib/datatables/jquery.dataTables.min.js"></script>
        <script tal:content="string: var WWW_URL='${site_url}';"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                if ($('#actions-on-content-list').length){
                    $('#actions-on-content-list').dataTable({
                        'aaSorting': [[0, "desc"]],
                        'sPaginationType': 'full_numbers',
                        "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
                    });
                }

                $('#month_selector').change(function(){
                    $('#month_selector_form').submit();
                });

                //$('#clear-log-files').click(function(){
                //    clearLogFiles();
                //});
                //
                //var clearLogFiles = function(){
                //    $('#dialog-clear-confirm').dialog({
                //        open: function() {
                //            $(this).siblings(".ui-dialog-buttonpane").find("button:eq(1)").focus();
                //        },
                //        resizable: false,
                //        height: 200,
                //        width: 500,
                //        modal: true,
                //        draggable: false,
                //        buttons: {
                //                "Delete log entries": function() {
                //                    $.ajax({
                //                        type: "POST",
                //                        url: WWW_URL + '/clear_log_files',
                //                        success: function(message){
                //                            if ( message == 'SUCCESS' ) {
                //                                window.location.reload();
                //                            }else {
                //                                $( this ).dialog('close');
                //                                alert('The log file could not be cleared.');
                //                            }
                //                        }
                //                    });
                //                },
                //                "Cancel": function() {
                //                        $( this ).dialog( "close" );
                //                }
                //        }
                //    });
                //}

                $('#plain-text-switch').click(function(){
                    var button = $(this);
                    $('#log-table-holder').fadeOut('fast', function(){
                        $('#log-plain-text-holder').fadeIn('fast');
                        button.fadeOut('fast', function(){
                            $('#table-switch').fadeIn('fast');
                        });
                    });
                });

                $('#table-switch').click(function(){
                    var button = $(this);
                    $('#log-plain-text-holder').fadeOut('fast', function(){
                        $('#log-table-holder').fadeIn('fast');
                        button.fadeOut('fast', function(){
                            $('#plain-text-switch').fadeIn('fast');
                        });
                    });
                });
            });
        </script>

        <style type="text/css">
        #log-plain-text-holder,
        #table-switch {
            display: none;
        }

        #plain-text-box {
            border: 1px solid #EBEBEB;
            -webkit-border-radius: 5px;
               -moz-border-radius: 5px;
                    border-radius: 5px;
            padding: 15px;
            height: 395px;
            overflow: auto;
            margin-bottom: 15px;
            line-height: 1.5;
            font-size: 12px;
            background: #eee;
        }
        #log-download-button {
            float: right;
        }
        #log-enabled {
            color: green;
        }
        #log-enabled img {
            vertical-align: -3px;
        }
        #log-disabled {
            color: red;
        }
        </style>

        <link rel="stylesheet" type="text/css" href="/++resource++Products.Naaya/lib/datatables/jquery.dataTables.css" />
        <link rel="stylesheet" type="text/css" href="/++resource++Products.Naaya/js/css/jquery-ui.css" />
        <link rel="stylesheet" href="/++resource++naaya.updater/updater.css" type="text/css" media="screen" charset="utf-8" />

        <p i18n:translate="">
            Here you can browse logged events in this Interest Group. The event types that are currently logged are:
        </p>
        <ul>
            <li>Access or download of HTML Documents, Files, Events, News, Stories and Contacts.</li>
            <li>User Management changes: Role assignments and unassignments.</li>
        </ul>

        <div class="message-error" tal:condition="python: not site_logger['writeable']">
            <fieldset><legend i18n:translate="">Notification</legend>
                <div class="fieldset_div">
                    <p i18n:translate="">
                        Site log file is not writeable or there is no path configured.
                    </p>
                </div>
            </fieldset>
        </div>
        <br />

        <!--<p id="log-enabled" tal:condition="here/content_action_logging">
            <span i18n:translate="">
                <img src="/++resource++Products.Naaya/checked.png" title="Logging activated" alt="" i18n:name="checked_icon" /> Logging actions on content is enabled.
            </span>
            <a tal:attributes="href string:${site_url}/admin_set_log_content?enabled=false">Disable</a>
        </p>

        <p id="log-disabled" tal:condition="python: not here.content_action_logging">
            <span i18n:translate="">
                Logging actions on content is disabled.
            </span>
            <a i18n:translate="" tal:attributes="href string:${site_url}/admin_set_log_content?enabled=true">Enable</a>
        </p>-->

        <form method="GET" action="" id="month_selector_form">
            <div style="float:right">
                <a href="javascript:void(0); " title="View logs as plain text" i18n:translate="" i18n:attributes="title" id="plain-text-switch">View as plain text</a>
                <a href="javascript:void(0); " title="View logs as plain text" i18n:translate="" i18n:attributes="title" id="table-switch">View as table</a>
            </div>
            <p>Select month for the logs:
                <select id="month_selector" name="current_month">
                    <option tal:repeat="month logged_months" 
                        tal:attributes="value month; selected python:month == current_month"
                        tal:content="python:'20' + month.replace('-', ' / ')"></option>
                </select>
            </p>
        </form>

        <div id="log-table-holder" style="margin-top:30px">
            <table cellpadding="0" cellspacing="0" class="datatable display listing" id="actions-on-content-list">
                <thead>
                    <tr class="list-header">
                        <th i18n:translate="" style="width: 14%">
                            Date
                        </th>
                        <th i18n:translate="">
                            User (Who)
                        </th>
                        <th i18n:translate="">
                            Event Type
                        </th>
                        <th i18n:translate="">
                            Content
                        </th>
                        <th i18n:translate="">
                            Event Message
                        </th>
                    </tr>
                </thead>
                <tbody>
                <tal:block repeat="line lines">
                    <tr>
                        <td>
                            <span tal:replace="python:here.utShowFullDateTime(line['date'])" />
                        </td>

                        <td>
                            <a title="User Profile"
                               tal:attributes="href string:https://www.eionet.europa.eu/users/${line/who};"
                               tal:condition="python: line['who'] != 'Anonymous User'"
                               i18n:attributes="title"
                               target="_blank">
                                <span tal:replace="line/who" />
                            </a>

                            <span tal:replace="line/who" tal:condition="python: line['who'] == 'Anonymous User'" />
                        </td>

                        <td>
                            <span tal:replace="line/type" />
                        </td>

                        <td>
                            <a tal:define="ob python:here.aq_parent.restrictedTraverse(line['content_path'])"
                               tal:attributes="href line/content_path; title ob/pretty_path" tal:on-error="string: "
                            class="tipsy-title">
                                <span tal:replace="python:ob.title_or_id()"  tal:on-error="string: " />
                            </a>
                        </td>

                        <td>
                            <span tal:replace="structure line/readable_message" />
                        </td>
                    </tr>
                </tal:block>
                </tbody>
                <tfoot>
                    <tr class="list-header">
                        <th i18n:translate="" style="width: 14%">
                            Date
                        </th>
                        <th i18n:translate="">
                            User (Who)
                        </th>
                        <th i18n:translate="">
                            Event Type
                        </th>
                        <th i18n:translate="">
                            Content
                        </th>
                        <th i18n:translate="">
                            Event Message
                        </th>
                    </tr>
                </tfoot>
            </table>
        </div>

        <div id="log-plain-text-holder" >
            <tal:block condition="python: plain_text_lines or not lines">
                <div id="plain-text-box"><tal:block repeat="line plain_text_lines"><span tal:replace="line" /><br /></tal:block></div>

                <a tal:attributes="href string:${site_url}/admin_download_log_file"
                   title="View logs as plain text" i18n:translate=""
                   tal:condition="plain_text_lines"
                   i18n:attributes="title" id="log-download-button">Download log file</a>
            </tal:block>

            <p tal:condition="python: not plain_text_lines and lines">
                The log file is too large to be displayed. Try <a tal:attributes="href string:${site_url}/admin_download_log_file"
                                                                      title="View logs as plain text" i18n:translate=""
                                                                      i18n:attributes="title">downloading</a> the file instead.
            </p>
        </div>

        <!--<button type="submit" id="clear-log-files" i18n:translate="" tal:condition="lines">Clear log file</button>

        <div id="dialog-clear-confirm" title="Delete log file content?" style="display: none" i18n:attributes="title">
            <p>
                <span class="ui-icon ui-icon-alert" style="float:left; margin:3px 7px 20px 0;"></span>
                <span i18n:translate="">
                    All content from the log file will be permanently deleted and may not be recovered.
                    <br i18n:name="break_line" />
                    Are you sure?
                </span>
            </p>
        </div>-->
    </tal:block>
</tal:block>
