<%= content_for :sublayout_title, "Request Log" %>
<%= action_button_to 'Purge', admin_service_purge_logs_path, :method => :delete, :class => 'delete', :remote => true unless @log_requests.empty? %>

<p><%= h @service.name %>: Latest API requests</p>

<div id="log-requests-container">
  <% if @log_requests.empty? %>
    <%= render :partial => 'no_log_requests' %>
  <% else %>

      <% @log_requests.each do |log| %>
        <%  request_status = case log[:code].to_i
                              when 200..299 then 'success'
                              when 500..599 then 'error'
                             end
         %>
        <div class="log-requests-transaction <%= request_status %>">
          <table class="log-request-transaction-table">
            <tbody>

            <tr><th>Time:</th>
              <td><%= log[:timestamp] %></td>
            </tr>
            <tr>
              <th>Application:</th>
              <td>
              <%=
                if @appid2app[log[:app_id]].nil?
                  "(Application missing)"
                else
                  link_to @appid2app[log[:app_id]].name, admin_buyers_application_path(@appid2app[log[:app_id]].id)
                end
              %></td></tr>
            <tr><th>Account:</th><td>
              <%=
                if (@appid2app[log[:app_id]].nil? || @appid2app[log[:app_id]].account.nil?)
                  "(Account missing)"
                else
                  link_to @appid2app[log[:app_id]].account.name, admin_buyers_account_path(@appid2app[log[:app_id]].account.id)
                end
              %></td></tr>
            <tr>
              <th>Code:</th>
              <td class="<%= request_status %>">
                <%= log[:code] %>
              </td>
            </tr>
            <tr>
              <th>Request:</th>
              <td><%= log[:request] %></td>
            </tr>
            <tr>
              <th>Response:</th>
              <td><%= log[:response] %></td>
            </tr>
            <tr>
              <th>Associated usage:</th>
              <td><%= log[:usage] %></td>
            </tr>
         </tbody>
        </table>
      </div>
    <% end %>
  <% end %>
</div>
