@use(App\Models\SystemLog)
@use(Carbon\Carbon)

@extends('layouts.app')
@section('htmlheader_title')
	Stats
@endsection

@section('content')
	<div class="row">
		<div class="col-12">
			<h2>System Status</h2>

			<div class="accordion" id="accordion_status">
				<!-- Totals -->
				<div class="accordion-item">
					<h3 class="accordion-header">
						<span class="accordion-button" id="totals" data-bs-toggle="collapse" data-bs-target="#collapse_totals" aria-expanded="false" aria-controls="collapse_totals">Totals</span>
					</h3>

					<div id="collapse_totals" class="accordion-collapse collapse {{ (! $flash=session()->pull('accordion')) ? 'show' : '' }}" aria-labelledby="totals" data-bs-parent="#accordion_status">
						<div class="accordion-body">
							<p>This system has processed the following so far in the last 7 days:</p>

							<div class="row">
								<!-- Netmail -->
								<div class="col-3">
									<div class="card bg-success">
										<div class="card-header p-2">
											<span class="card-title w-100 text-dark" style="font-size: 125%;"><i class="bi bi-envelope-at"></i> Netmail
												<button type="button" class="btn float-end me-0" style="background-color: darkgreen; color: white;">{{ number_format(($x=\App\Models\Netmail::where('netmails.created_at','>=',\Carbon\Carbon::now()->subWeek()->startOfDay())
														->join('addresses',['addresses.id'=>'netmails.fftn_id'])
														->join('zones',['zones.id'=>'addresses.zone_id'])
														->join('domains',['domains.id'=>'zones.domain_id'])
														->orderBy('domains.name')
														->with('fftn.zone.domain')
														->get())
													->count()) }}
												</button>
											</span>
										</div>

										@if($x->count())
											<div class="card-body text-dark">
												<p class="card-text">Networks:</p>
												<ul class="p-0 m-0">
													@foreach($x->groupBy('fftn.zone_id') as $oo)
														<li class="m-0">{{ $oo->first()->fftn->zone->domain->name }}: {{ number_format($oo->count()) }}</li>
													@endforeach
												</ul>
											</div>
										@endif
									</div>
								</div>

								<!-- Echomail -->
								<div class="col-3">
									<div class="card bg-primary">
										<div class="card-header p-2">
											<span class="card-title w-100 text-dark" style="font-size: 125%;"><i class="bi bi-envelope"></i> Echomail
												<button type="button" class="btn float-end me-0" style="background-color: darkblue; color: white;">{{ number_format(($x=\App\Models\Echomail::where('echomails.created_at','>=',\Carbon\Carbon::now()->subWeek()->startOfDay())
														->join('addresses',['addresses.id'=>'echomails.fftn_id'])
														->join('zones',['zones.id'=>'addresses.zone_id'])
														->join('domains',['domains.id'=>'zones.domain_id'])
														->orderBy('domains.name')
														->orderBy('zones.zone_id')
														->with('fftn.zone.domain')
														->get())
													->count()) }}
												</button>
											</span>
										</div>

										@if($x->count())
											<div class="card-body text-dark">
												<p class="card-text">Networks:</p>
												<ul class="p-0 m-0">
													@foreach($x->groupBy('fftn.zone_id') as $oo)
														<li class="m-0">{{ sprintf('Z%d (%s)',$oo->first()->fftn->zone->zone_id,$oo->first()->fftn->zone->domain->name) }}: {{ number_format($oo->count()) }}</li>
													@endforeach
												</ul>
											</div>
										@endif
									</div>
								</div>

								<!-- File -->
								<div class="col-3">
									<div class="card bg-danger">
										<div class="card-header p-2">
											<span class="card-title w-100 text-dark" style="font-size: 125%;"><i class="bi bi-folder"></i> Files
												<button type="button" class="btn float-end me-0" style="background-color: darkred; color: white;">{{ number_format(($x=\App\Models\File::where('files.created_at','>=',\Carbon\Carbon::now()->subWeek()->startOfDay())
														->join('addresses',['addresses.id'=>'files.fftn_id'])
														->join('zones',['zones.id'=>'addresses.zone_id'])
														->join('domains',['domains.id'=>'zones.domain_id'])
														->orderBy('domains.name')
														->with('fftn.zone.domain')
														->get())
													->count()) }}
												</button>
											</span>
										</div>

										@if($x->count())
											<div class="card-body text-dark">
												<p class="card-text">Networks:</p>
												<ul class="p-0 m-0">
													@foreach($x->groupBy('fftn.zone_id') as $oo)
														<li class="m-0">{{ sprintf('Z%d (%s)',$oo->first()->fftn->zone->zone_id,$oo->first()->fftn->zone->domain->name) }}: {{ number_format($oo->count()) }}</li>
													@endforeach
												</ul>
											</div>
										@endif
									</div>
								</div>

								<!-- DNS -->
								<div class="col-3">
									<div class="row">
										<div class="col-12">
											<div class="card bg-light">
												<div class="card-header p-2">
													<span class="card-title w-100 text-dark" style="font-size: 125%;"><i class="bi bi-pc-display"></i> Systems
														<button type="button" class="btn float-end me-0" style="background-color: black; color: white;">
															{{ number_format(SystemLog::distinct('system_id')->where('created_at','>=',Carbon::now()->subWeek()->startOfDay())->count()) }}
														</button>
													</span>
												</div>
											</div>
										</div>
									</div>

									<div class="row pt-3">
										<div class="col-12">
											<div class="card bg-secondary">
												<div class="card-header p-2">
													<span class="card-title w-100 text-dark" style="font-size: 125%;"><i class="bi bi-globe"></i> DNS Queries
														<button type="button" class="btn float-end me-0" style="background-color: black; color: white;">N/A</button>
													</span>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>

				<!-- Uncollected Mail -->
				<div class="accordion-item">
					<h3 class="accordion-header">
						<span class="accordion-button collapsed" id="uncollected" data-bs-toggle="collapse" data-bs-target="#collapse_uncollected" aria-expanded="true" aria-controls="collapse_uncollected">Uncollected Mail</span>
					</h3>

					<div id="collapse_uncollected" class="accordion-collapse collapse {{ ($flash==='uncollected') ? 'show' : '' }}" aria-labelledby="uncollected" data-bs-parent="#accordion_status">
						<div class="accordion-body">
							<div class="row">
								<div class="col-12">
									<p>Nodes with uncollected mail as at <strong class="highlight">{{ $date }}</strong>:</p>

									<table class="table monotable w-100" id="mailfiles">
										<thead>
										<tr>
											<th>Network</th>
											<th>System</th>
											<th>Address</th>
											<th class="text-end">Echomails</th>
											<th class="text-end">Netmails</th>
											<th class="text-end">Files</th>
											<th>Last Session</th>
											<th>Poll Mode</th>
											<th>Auto Hold</th>
										</tr>
										</thead>

										<tbody>
										@foreach($uncollected as $o)
											<tr>
												<td>{{ $o->zone->domain->name }}</td>
												<td>
													<a href="{{ url('system/addedit',$o->system_id) }}">{{ $o->system->name }}</a>
													@if (($x=$o->uplink()) && ($x->id !== $o->id))
														<br><small>[via <a href="{{ url('system/addedit',$x->system_id) }}">{{ $x->ftn4d }}</a>]</small>
													@endif
													@if($o->is_down || $o->is_hold)
														<br><div class="btn btn-sm btn-secondary p-1 m-0"><small>@if($o->is_down)DOWN @elseif($o->is_hold)HOLD @else? @endif</small></div>
													@endif
												</td>
												<td>{{ $o->ftn4d }}</td>
												<td class="text-end">{{ number_format($o->uncollected_echomail ?? 0) }}</td>
												<td class="text-end">{{ number_format($o->uncollected_netmail ?? 0) }}</td>
												<td class="text-end">{{ number_format($o->uncollected_files ?? 0) }}</td>
												<td>{{ $o->system->last_seen?->format('Y-m-d H:i') }}</td>
												<td>{{ is_null($o->system->pollmode) ? 'HOLD' : ($o->system->pollmode ? 'CRASH' : 'DAILY') }}</td>
												<td>{{ $o->system->autohold ? 'YES' : 'NO' }}</td>
											</tr>
										@endforeach
										</tbody>
									</table>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
@endsection

@section('page-css')
	@css('datatables')
@append
@section('page-scripts')
	@js('datatables')

	<script type="text/javascript">
		$(document).ready(function() {
			$('#mailfiles').DataTable({
				paging: true,
				pageLength: 25,
				searching: true,
				orderFixed: [0,'asc'],
				order: [[1,'asc']],
				conditionalPaging: {
					style: 'fade',
					speed: 500
				},
				rowGroup: {
					dataSrc: [0],
				},
				columnDefs: [
					{
						targets: [0],
						visible: false,
					},
				],
				language: {
					paginate: {
						previous: '&lt;&lt;',
						next: '&gt;&gt;'
					}
				}
			});
		});
	</script>
@append