@extends('layouts.app')

@section('title', __('nx600Lab::lang.communication_logs'))

@section('content')
<section class="content-header">
    <h1><i class="fas fa-stream"></i> {{ __('nx600Lab::lang.communication_logs') }}</h1>
</section>

<section class="content">

    @if(session('status'))
        <div class="alert alert-{{ session('status')['success'] ? 'success' : 'danger' }}">
            {{ session('status')['msg'] }}
        </div>
    @endif

    <div class="row">
        <div class="col-md-12">
            @component('components.widget', ['class' => 'box-primary'])
                <div style="margin-bottom:12px;">
                    <form method="GET" class="form-inline" style="display:inline;">
                        <input type="text" name="search" value="{{ request('search') }}" class="form-control" placeholder="Message...">
                        <select name="level" class="form-control">
                            <option value="">All levels</option>
                            @foreach(['info','success','warning','error'] as $lvl)
                                <option value="{{ $lvl }}" {{ request('level') === $lvl ? 'selected' : '' }}>{{ ucfirst($lvl) }}</option>
                            @endforeach
                        </select>
                        <select name="category" class="form-control">
                            <option value="">All categories</option>
                            @foreach(['connection','worklist','result','error','system'] as $cat)
                                <option value="{{ $cat }}" {{ request('category') === $cat ? 'selected' : '' }}>{{ ucfirst($cat) }}</option>
                            @endforeach
                        </select>
                        <button class="btn btn-default"><i class="fa fa-search"></i> Filter</button>
                    </form>
                    <form method="POST" action="{{ route('nx.logs.clear') }}" style="display:inline;"
                          onsubmit="return confirm('Clear all communication logs?');">
                        @csrf @method('DELETE')
                        <button class="btn btn-danger pull-right"><i class="fa fa-trash"></i> Clear Logs</button>
                    </form>
                </div>

                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Time</th>
                            <th>Level</th>
                            <th>Category</th>
                            <th>Message</th>
                            <th>Device / Sample</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($logs as $log)
                            <tr>
                                <td style="white-space:nowrap;">{{ $log->created_at?->format('Y-m-d H:i:s') }}</td>
                                <td><span class="label {{ $log->level_badge }}">{{ strtoupper($log->level) }}</span></td>
                                <td><i class="{{ $log->category_icon }}"></i> {{ ucfirst($log->category) }}</td>
                                <td>{{ $log->message }}</td>
                                <td>
                                    <small class="text-muted">
                                        {{ $log->device_ip ?: '' }}
                                        @if($log->sample_no) &middot; {{ $log->sample_no }} @endif
                                    </small>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="5" class="text-center text-muted">No log entries.</td></tr>
                        @endforelse
                    </tbody>
                </table>

                {!! $logs->links() !!}
            @endcomponent
        </div>
    </div>
</section>
@endsection
