<%# app/views/admin/auditoria_logs/index.html.erb %>
<% content_for :title, 'Auditoria' %>

<h1 class="text-2xl font-bold text-white mb-6 flex items-center gap-2"><%= icone :auditoria, espaco: false %> Auditoria</h1>

<%= form_with url: admin_auditoria_logs_path, method: :get,
    class: 'bg-[#1a1a1a] border border-[#2a2a2a] rounded-xl p-4 mb-6 flex flex-wrap gap-3' do %>
  <%= select_tag :user_id,
      options_from_collection_for_select(@usuarios, :id, :nome_display, params[:user_id]),
      include_blank: 'Todos os usuários',
      class: 'bg-[#0a0a0a] border border-[#2a2a2a] text-white rounded-lg px-3 py-2.5' %>
  <%= text_field_tag :acao, params[:acao], placeholder: 'Ação (ex: finalizar)',
      class: 'bg-[#0a0a0a] border border-[#2a2a2a] text-white rounded-lg px-3 py-2.5' %>
  <%= submit_tag 'Filtrar', class: 'bg-orange-500 hover:bg-orange-600 text-black font-bold px-5 rounded-lg cursor-pointer' %>
<% end %>

<div class="bg-[#1a1a1a] border border-[#2a2a2a] rounded-xl overflow-hidden">
  <table class="w-full text-sm">
    <thead class="bg-[#0a0a0a] text-gray-400">
      <tr>
        <th class="text-left px-4 py-3">Quando</th>
        <th class="text-left px-4 py-3">Usuário</th>
        <th class="text-left px-4 py-3">Ação</th>
        <th class="text-left px-4 py-3">Entidade</th>
        <th class="text-left px-4 py-3">Detalhes</th>
      </tr>
    </thead>
    <tbody class="divide-y divide-[#2a2a2a] text-gray-300">
      <% @logs.each do |log| %>
        <tr class="hover:bg-[#0a0a0a]">
          <td class="px-4 py-3 whitespace-nowrap"><%= l log.created_at, format: :short %></td>
          <td class="px-4 py-3"><%= User.find_by(id: log.user_id)&.nome_display || "##{log.user_id}" %></td>
          <td class="px-4 py-3"><span class="bg-orange-500/20 text-orange-400 px-2 py-1 rounded text-xs font-bold"><%= log.acao %></span></td>
          <td class="px-4 py-3"><%= log.entidade %><%= " ##{log.entidade_id}" if log.entidade_id %></td>
          <td class="px-4 py-3 text-gray-500 text-xs"><%= log.dados_novos['detalhes'] || log.dados_novos.to_s.first(80) %></td>
        </tr>
      <% end %>
    </tbody>
  </table>
  <% if @logs.empty? %>
    <p class="text-gray-500 text-center py-10">Nenhum registro de auditoria encontrado.</p>
  <% end %>
</div>
