@extends('layouts.app')

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

@section('content')
<section class="content-header">
    <h1>
        <i class="fas fa-plug"></i> {{ __('nx600Lab::lang.device_monitor') }}
        @if($activeCount > 0)
            <small><span class="label label-success">{{ $activeCount }} live connection(s)</span></small>
        @endif
    </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-default', 'title' => 'Listener Configuration'])
                <p class="text-muted" style="margin-bottom:6px;">
                    Run these console commands on the machine the analyzer connects to:
                </p>
                <code>php artisan nx600lab:tcp-server --host={{ $settings['tcp_host'] }} --port={{ $settings['tcp_port'] }} --type={{ $settings['comm_type'] }}</code><br>
                <code>php artisan nx600lab:hl7-server --port={{ $settings['hl7_port'] }}</code><br>
                <code>php artisan nx600lab:simulate</code> <span class="text-muted">— test without hardware</span>
            @endcomponent
        </div>
    </div>

    <div class="row">
        <div class="col-md-6">
            @component('components.widget', ['class' => 'box-primary', 'title' => 'Connection History'])
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Device</th>
                            <th>Comm</th>
                            <th>Status</th>
                            <th>Connected</th>
                            <th>Msgs / Results</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($connections as $c)
                            <tr>
                                <td>{{ $c->device_ip }}:{{ $c->device_port }}</td>
                                <td>{{ $c->comm_type }}</td>
                                <td>
                                    <span class="label label-{{ $c->status === 'connected' ? 'success' : ($c->status === 'error' ? 'danger' : 'default') }}">
                                        {{ ucfirst($c->status) }}
                                    </span>
                                </td>
                                <td>{{ $c->connected_at?->format('Y-m-d H:i') }}</td>
                                <td>{{ $c->messages_received }} / {{ $c->results_stored }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="5" class="text-center text-muted">No connections recorded.</td></tr>
                        @endforelse
                    </tbody>
                </table>
                {!! $connections->links() !!}
            @endcomponent
        </div>
        <div class="col-md-6">
            @component('components.widget', ['class' => 'box-warning', 'title' => 'Device Errors'])
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Code</th>
                            <th>Date / Time</th>
                            <th>Info</th>
                            <th>{{ __('nx600Lab::lang.actions') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($errors as $err)
                            <tr class="{{ $err->acknowledged ? '' : 'warning' }}">
                                <td><strong>{{ $err->error_number }}</strong></td>
                                <td>{{ $err->error_date?->format('Y-m-d') }} {{ $err->error_time }}</td>
                                <td>
                                    {{ !empty($err->added_info) ? implode(', ', $err->added_info) : '—' }}
                                </td>
                                <td>
                                    @if($err->acknowledged)
                                        <span class="label label-default">Acknowledged</span>
                                    @else
                                        <form method="POST" action="{{ route('nx.devices.errors.ack', $err->id) }}">
                                            @csrf @method('PATCH')
                                            <button class="btn btn-xs btn-default">Acknowledge</button>
                                        </form>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="text-center text-muted">No device errors. Good.</td></tr>
                        @endforelse
                    </tbody>
                </table>
                {!! $errors->links() !!}
            @endcomponent
        </div>
    </div>
</section>
@endsection
