<!-- $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('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_recent->count() ? $o->logs_recent->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('domain/view',[$children->first()->zone->domain_id]) }}">{{ $name }}</a></td>
										<td>{{ $children->pluck('ftn3d')->join(', ') }}</td>
									</tr>
								@endforeach
								</tbody>
							</table>
						</td>
					</tr>

					<tr>
						<!-- @todo Need to included where this system authored a message, ie: fftn_id in echomail -->
						<th>Echoarea Activity</th>
						<td>
							<table class="table noborder">
								<thead>
								<tr>
									<th>Domain</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_id') as $did => $domain_addresses)
									@foreach(\App\Models\Echoarea::select([DB::raw('count(echomails.*) as count'),DB::raw('max(echomails.datetime) as most_recent')])
										->join('echomails',['echomails.echoarea_id'=>'echoareas.id'])
										->join('echomail_seenby',function($join) use ($domain_addresses) {
											return $join->on('echomail_seenby.echomail_id','echomails.id')
												->whereIn('echomail_seenby.address_id',$domain_addresses->pluck('id'));
												})
										->get() as $o)
										<tr>
											<td><a href="{{ url('domain/view',[$did]) }}">{{ $domain_addresses->first()->zone->domain->name }}</a></td>

											@if($o->count)
												<td class="text-end">{{ $o->count }}</td>
												<td class="text-end">{{ $o->most_recent }}</td>
											@else
												<td class="text-end" colspan="2">No Echoarea Activity</td>
											@endif
										</tr>
									@endforeach
								@endforeach
								</tbody>
							</table>
						</td>
					</tr>
				@endif
				</tbody>
			</table>
		</div>
	</div>
@endsection