@extends('layouts.app')

@section('title', __('labCore::lang.lab_core') . ' — ' . __('labCore::lang.agents'))

@section('content')
<section class="content-header">
    <h1><i class="fas fa-vials"></i> {{ __('labCore::lang.lab_core') }} <small>{{ __('labCore::lang.agents') }}</small></h1>
</section>

<section class="content">
    @include('labCore::partials._nav')

    <div class="row">
        <div class="col-md-4">
            <div class="box box-primary">
                <div class="box-header with-border"><h3 class="box-title">Add Bridge Agent</h3></div>
                <form method="POST" action="{{ route('lab.agents.store') }}">
                    @csrf
                    <div class="box-body">
                        <div class="form-group">
                            <label>Name</label>
                            <input type="text" name="name" class="form-control" placeholder="e.g. Main Clinic" value="{{ old('name') }}" required>
                        </div>
                        <div class="form-group">
                            <label>Code</label>
                            <input type="text" name="code" class="form-control" placeholder="e.g. main-clinic" value="{{ old('code') }}" required>
                            <small class="text-muted">Letters, numbers, dashes. Used by the agent at registration.</small>
                        </div>
                    </div>
                    <div class="box-footer">
                        <button type="submit" class="btn btn-primary"><i class="fa fa-plus"></i> Create Agent</button>
                    </div>
                </form>
            </div>
        </div>

        <div class="col-md-8">
            <div class="box box-solid">
                <div class="box-header with-border"><h3 class="box-title">Registered Agents</h3></div>
                <div class="box-body no-padding">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>Name</th><th>Code</th><th>Devices</th><th>Status</th>
                                <th>Connectivity</th><th>Last Heartbeat</th><th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                        @forelse($agents as $agent)
                            <tr>
                                <td>{{ $agent->name }}</td>
                                <td><code>{{ $agent->code }}</code></td>
                                <td>{{ $agent->devices_count }}</td>
                                <td><span class="label {{ $agent->status === 'active' ? 'label-success' : 'label-default' }}">{{ $agent->status }}</span></td>
                                <td>
                                    @if($agent->is_online)
                                        <span class="label label-success">Online</span>
                                    @else
                                        <span class="label label-default">Offline</span>
                                    @endif
                                </td>
                                <td>{{ $agent->last_heartbeat_at?->diffForHumans() ?? '—' }}</td>
                                <td>
                                    <form method="POST" action="{{ route('lab.agents.token', $agent->id) }}" style="display:inline;">
                                        @csrf
                                        <button class="btn btn-xs btn-default" title="Regenerate API token"><i class="fa fa-key"></i></button>
                                    </form>
                                    <form method="POST" action="{{ route('lab.agents.destroy', $agent->id) }}" style="display:inline;" onsubmit="return confirm('Delete this agent?');">
                                        @csrf @method('DELETE')
                                        <button class="btn btn-xs btn-danger"><i class="fa fa-trash"></i></button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="7" class="text-muted">{{ __('labCore::lang.no_data') }}</td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>
@endsection
