@extends('admin.layouts.app')
@section('title', 'Audit Logs')
@section('page-title', 'Audit Logs')

@section('content')
<div x-data="logsManager()" x-init="init()" class="space-y-4">

    {{-- Search bar --}}
    <div class="flex items-center gap-3">
        <div class="relative flex-1 max-w-sm">
            <svg class="absolute left-3 top-2.5 w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
            </svg>
            <input type="text" x-model="search" x-on:input="onSearch()"
                   placeholder="Search action, user, IP, user agent..."
                   class="pl-9 w-full text-sm border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 bg-white dark:bg-gray-700 text-gray-900 dark:text-white">
        </div>
        <button type="button" x-show="search" x-on:click="search=''; fetch(1)"
                class="text-sm text-gray-500 hover:text-gray-700 dark:text-gray-400" style="display:none">Clear</button>
        <p class="text-sm text-gray-500 dark:text-gray-400 ml-auto">
            <span x-text="meta.total"></span> entries
        </p>
    </div>

    {{-- Table --}}
    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm overflow-x-auto">
        <table class="w-full text-sm">
            <thead class="bg-gray-50 dark:bg-gray-700/50">
                <tr>
                    <th class="text-left px-5 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Action</th>
                    <th class="text-left px-5 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">User</th>
                    <th class="text-left px-5 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Model</th>
                    <th class="text-left px-5 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">IP</th>
                    <th class="text-left px-5 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">User Agent</th>
                    <th class="text-left px-5 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Time</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-200 dark:divide-gray-700">
                <template x-if="loading">
                    <tr>
                        <td colspan="6" class="px-6 py-10 text-center text-gray-400">
                            <svg class="w-5 h-5 animate-spin inline-block mr-2" fill="none" viewBox="0 0 24 24">
                                <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>
                                <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/>
                            </svg>
                            Loading...
                        </td>
                    </tr>
                </template>
                <template x-if="!loading && items.length === 0">
                    <tr><td colspan="6" class="px-6 py-8 text-center text-gray-400">No logs found.</td></tr>
                </template>
                <template x-for="log in items" :key="log.time_full + log.action">
                    <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/30">
                        <td class="px-5 py-3">
                            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium"
                                  :class="actionColor(log.action)" x-text="log.action"></span>
                        </td>
                        <td class="px-5 py-3 text-gray-700 dark:text-gray-300 font-medium text-xs" x-text="log.user"></td>
                        <td class="px-5 py-3 text-gray-500 dark:text-gray-400 text-xs" x-text="log.model ?? '—'"></td>
                        <td class="px-5 py-3 text-gray-500 dark:text-gray-400 font-mono text-xs whitespace-nowrap" x-text="log.ip ?? '—'"></td>
                        <td class="px-5 py-3 text-gray-400 dark:text-gray-500 text-xs" style="max-width:240px;">
                            <span class="block truncate" :title="log.user_agent" x-text="log.user_agent ?? '—'"></span>
                        </td>
                        <td class="px-5 py-3 text-gray-500 dark:text-gray-400 text-xs whitespace-nowrap"
                            :title="log.time_full" x-text="log.time"></td>
                    </tr>
                </template>
            </tbody>
        </table>

        {{-- Pagination --}}
        <div x-show="meta.last_page > 1" class="px-6 py-4 border-t border-gray-200 dark:border-gray-700 flex items-center justify-between gap-2">
            <button x-on:click="fetch(meta.current_page - 1)" :disabled="meta.current_page === 1"
                    class="px-3 py-1.5 text-sm rounded border border-gray-300 dark:border-gray-600 disabled:opacity-40 hover:bg-gray-50 dark:hover:bg-gray-700">←</button>
            <div class="flex items-center gap-1">
                <template x-for="p in pages()" :key="p">
                    <button x-on:click="fetch(p)"
                            :class="p === meta.current_page ? 'bg-indigo-600 text-white border-indigo-600' : 'border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-300'"
                            class="px-3 py-1.5 text-sm rounded border" x-text="p"></button>
                </template>
            </div>
            <button x-on:click="fetch(meta.current_page + 1)" :disabled="meta.current_page === meta.last_page"
                    class="px-3 py-1.5 text-sm rounded border border-gray-300 dark:border-gray-600 disabled:opacity-40 hover:bg-gray-50 dark:hover:bg-gray-700">→</button>
        </div>
    </div>
</div>
@endsection
