<!-- $o=System::class -->
@extends('layouts.app')
@section('htmlheader_title')
	Systems
@endsection

@section('content')
	<div class="row">
		<div class="col-12">
			<h2>{{ $o->name }}</h2>

			<table class="table monotable">
				<tbody>
				<tr>
					<th>System Name</th>
					<td>{{ $o->name }} @can('update',$o)<span class="btn btn-success btn-sm float-end"><a href="{{ url('ftn/system/addedit',$o->id) }}">Edit</a></span>@endcan</td>
				</tr>
				<tr>
					<th>Sysop</th>
					<td>{{ $o->sysop }}</td>
				</tr>
				<tr>
					<th>Location</th>
					<td>{{ $o->location }}</td>
				</tr>
				<tr>
					<th>Address</th>
					<td>{{ $o->access_method }}</td>
				</tr>
				<tr>
					<th>Mailer</th>
					<td>{{ $o->access_mailer }}</td>
				</tr>
				@if($o->phone)
					<tr>
						<th>Phone</th>
						<td>{{ $o->phone }}</td>
					</tr>
				@endif
				<tr>
					<th>Last Seen</th>
					<td>{{ $o->logs->count() ? $o->logs->last()->created_at : '-' }}</td>
				</tr>
				@if($o->addresses->count())
					<tr>
						<th>FTN Addresses</th>
						<td>
							<table class="table noborder">
								<thead>
								<tr>
									<th>Domain</th>
									<th>Addresses</th>
								</tr>
								</thead>

								<tbody>
								@foreach($o->addresses->sortBy('zone.domain.name')->groupBy('zone.domain.name') as $name => $children)
									<tr>
										<td><a href="{{ url('network',[$children->first()->zone->domain_id]) }}">{{ $name }}</a></td>
										<td>{{ $children->pluck('ftn3d')->join(', ') }}</td>
									</tr>
								@endforeach
								</tbody>
							</table>
						</td>
					</tr>

					<tr>
						<th>Echoarea Activity</th>
						<td>
							<table class="table noborder">
								<thead>
								<tr>
									<th>Domain</th>
									<th>Echoarea</th>
									<th class="text-end">#</th>
									<th class="text-end">Last</th>
								</tr>
								</thead>

								<tbody>
								@foreach($o->addresses->sortBy('zone.domain.name')->groupBy('zone.domain.name') as $net => $children)
									@if($children->pluck('echomails')->flatten()->count())
										@foreach($children->pluck('echomails')->flatten()->sortBy('echoarea.name')->groupBy('echoarea.name') as $name => $echomails)
											<tr>
												<td><a href="{{ url('network',[$children->first()->zone->domain_id]) }}">{{ $net }}</a></td>
												<td><a href="{{ url('echoarea/view',[$x=$echomails->first()->echoarea_id]) }}">{{ $name }}</a></td>
												<td class="text-end">{{ ($y=$children->pluck('echomail_from')->flatten()->where('echoarea_id',$x))->count() }}</td>
												<td class="text-end">{{ $y->max('datetime') }}</td>
											</tr>
										@endforeach
									@else
										<tr>
											<td><a href="{{ url('network',[$children->first()->zone->domain_id]) }}">{{ $net }}</a></td>
											<td colspan="3">No Echoarea Activity</td>
										</tr>
									@endif
								@endforeach
								</tbody>
							</table>
						</td>
					</tr>
				@endif
				</tbody>
			</table>
		</div>
	</div>
@endsection