<div class="system-changes-report">
    <x-title>
        System Changes
        <x-slot:buttons>
            <button wire:click="$refresh" class="btn btn-primary btn-sm" title="Refresh">
                <i class="mdi mdi-sync"></i>
            </button>
        </x-slot:buttons>
    </x-title>

    <div class="row">
        {{-- MAIN --}}
        <div class="col-12 col-md-9">
            <div class="card">
                <div class="card-body">

                    {{-- CONTROLS --}}
                    <div class="row g-2 align-items-end mb-3">
                        <div class="col-6 col-md-2">
                            <label class="form-label">Per Page</label>
                            <select class="form-select form-select-sm" wire:model.live="perPage">
                                <option value="25">25</option>
                                <option value="50">50</option>
                                <option value="100">100</option>
                                <option value="200">200</option>
                            </select>
                        </div>

                        <div class="col-6 col-md-3">
                            <label class="form-label">Category</label>
                            <select class="form-select form-select-sm" wire:model.live="category">
                                <option value="">All</option>
                                @foreach($categories as $key => $cat)
                                    <option value="{{ $key }}">{{ $cat['label'] }}</option>
                                @endforeach
                            </select>
                        </div>

                        <div class="col-12 col-md-3">
                            <label class="form-label">User</label>
                            <select class="form-select form-select-sm" wire:model.live="userId">
                                <option value="">Any user</option>
                                @foreach($users as $user)
                                    <option value="{{ $user->id }}">
                                        {{ $user->name_first }} {{ $user->name_last }}
                                    </option>
                                @endforeach
                            </select>
                        </div>

                        <div class="col-6 col-md-2">
                            <label class="form-label">From</label>
                            <input type="date" class="form-control form-control-sm" wire:model.live="from">
                        </div>

                        <div class="col-6 col-md-2">
                            <label class="form-label">To</label>
                            <input type="date" class="form-control form-control-sm" wire:model.live="to">
                        </div>

                        <div class="col-12">
                            <label class="form-label">Search</label>
                            <input class="form-control form-control-sm"
                                   wire:model.live.debounce.300ms="search"
                                   placeholder="Search actions…">
                        </div>
                    </div>

                    {{-- TABLE --}}
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="table-light">
                            <tr>
                                <th style="width: 170px">When</th>
                                <th style="width: 200px">By</th>
                                <th>Action</th>
                            </tr>
                            </thead>
                            <tbody>
                            @forelse($entries as $entry)
                                <tr>
                                    <td class="text-muted small">
                                        <span title="{{ $entry->created_at }}">
                                            {{ $entry->created_at->diffForHumans() }}
                                        </span>
                                        <div class="text-muted small">
                                            {{ $entry->created_at->format('d M Y H:i') }}
                                        </div>
                                    </td>
                                    <td>
                                        @if($entry->user)
                                            <span class="fw-semibold">
                                                {{ $entry->user->name_first }} {{ $entry->user->name_last }}
                                            </span>
                                            <div class="text-muted small">
                                                {{ $entry->user->email_address }}
                                            </div>
                                        @else
                                            <span class="fst-italic text-muted">System</span>
                                        @endif
                                    </td>
                                    <td>
                                        {{ $entry->action }}
                                        @if(!empty($entry->context))
                                            <details class="mt-1">
                                                <summary class="small text-muted" style="cursor:pointer">Details</summary>
                                                <pre class="small text-muted mb-0 mt-1"
                                                     style="white-space:pre-wrap">{{ json_encode($entry->context, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) }}</pre>
                                            </details>
                                        @endif
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="3" class="text-center text-muted py-5">
                                        <i class="mdi mdi-shield-check-outline d-block mb-2 fs-2"></i>
                                        No administrative changes in this range.
                                    </td>
                                </tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-3">
                        {{ $entries->links() }}
                    </div>
                </div>
            </div>
        </div>

        {{-- SIDEBAR --}}
        <div class="col-12 col-md-3">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">About</h4>
                    <hr>
                    <p class="text-muted small">
                        Chronological log of changes made to users, roles,
                        permissions, tags, statuses, key dates, document
                        types and other administrative settings.
                    </p>
                    <p class="text-muted small mb-0">
                        Each entry records who made the change and when.
                        Expand <em>Details</em> to see the recorded context.
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
