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

@php
use App\Models\System;
@endphp

@section('content')
	<div class="row">
		<div class="col-12">
			<h2>About BBS Systems</h2>
			<p>BBS Systems Send and Receive Echomail and Files.</p>
		</div>
	</div>

	<div class="row">
		<div class="col-12">
			<p>This system is aware of the following systems
				@can('create',(new System))(you can <a href="{{ url('user/system/register') }}">register</a> more):@endcan
				@can('admin',(new System))(you can <a href="{{ url('system/addedit') }}">add</a> more):@endcan
			</p>

			@if (System::active()->count() === 0)
				@can('create',(new System))
					<p>There are no systems setup, to <a href="{{ url('system/addedit') }}">set up your first</a>.</p>
				@else
					<p class="pad">There are no systems - you need to ask an admin to create one for you.</p>
				@endcan
			@else
				<table class="table monotable" id="system">
					<thead>
					<tr>
						<th>ID</th>
						<th>System</th>
						<th>Sysop</th>
						<th>Location</th>
						<th>Connect</th>
						<th>Address</th>
						<th>ZeroTier ID</th>
						<th>Addresses</th>
						<th>Active</th>
					</tr>
					</thead>

					<tbody>
					@foreach (System::with(['addresses.zone.domain'])->get() as $oo)
						<tr>
							<td><a href="{{ url('system/addedit',[$oo->id]) }}" @cannot('update_nn',$oo)class="disabled" @endcannot>{{ $oo->id }}</a></td>
							<td>{{ $oo->name }}</td>
							<td>{{ $oo->sysop }}</td>
							<td>{{ $oo->location }}</td>
							<td>
								@switch($oo->method)
									@case(23)<a href="telnet://{{ $oo->address }}:{{ $oo->port }}">Telnet</a>@break
									@case(22)<a href="ssh://{{ $oo->address }}:{{ $oo->port }}">SSH</a>@break
									@case(519)<a href="rlogin://{{ $oo->address }}:{{ $oo->port }}">rlogin</a>@break
									@default No details
								@endswitch
							</td>
							<td>{{ $oo->addresses->pluck('ftn')->join(', ') }}</td>
							<td>{{ $oo->zt_id }}</td>
							<td>{{ $oo->addresses->count() }}</td>
							<td>{{ $oo->active ? 'Active' : 'Not Active' }}</td>
						</tr>
					@endforeach
					</tbody>
				</table>
			@endif
		</div>
	</div>
@endsection

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

	<script type="text/javascript">
		$(document).ready(function() {
			$('table tr').click(function() {
				var href = $(this).find('a:not(.disabled)').attr('href');

				if (href)
					window.location = href;
			});

			$('#system').DataTable({
				paging: true,
				pageLength: 25,
				searching: true,
				autoWidth: false,
				conditionalPaging: {
					style: 'fade',
					speed: 500 // optional
				},
				order: [1,'asc'],
				orderFixed: [8,'asc'],
				language: {
					paginate: {
						previous: '&lt;&lt;',
						next: '&gt;&gt;'
					}
				},
				rowGroup: {
					dataSrc: [8],
				},
				columnDefs: [
					{
						targets: [5,6,8],
						visible: false,
					}
				],
			});
		});
	</script>
@append