@php
use App\Models\Setup;
@endphp

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

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

	<div class="row pt-0">
		<div class="col-12">
			<div class="greyframe titledbox shadow0xb0">
				<h2 class="cap">@if($o->exists) Update @else Initial @endif Setup</h2>

				<form class="row g-0 needs-validation" method="post" novalidate>
					@csrf

					<div class="row">
						<div class="col-4">
							<label for="system_id" class="form-label">System</label>
							<div class="input-group has-validation">
								<span class="input-group-text"><i class="bi bi-tag-fill"></i></span>
								<select style="width: 80%;" class="form-select @error('system_id') is-invalid @enderror" id="system_id" name="system_id" required @cannot('admin',$o)disabled @endcannot>
									<option value="">&nbsp;</option>
									@foreach (\App\Models\System::active()->orderBy('name')->cursor() as $oo)
										<option value="{{ $oo->id }}" @if(old('system_id',$o->system_id)==$oo->id)selected @endif>{{ $oo->name }}</option>
									@endforeach
								</select>
								<span class="invalid-feedback" role="alert">
									@error('system_id')
										{{ $message }}
									@else
										A system is required.
									@enderror
							</span>
								<span class="input-helper">Add a <a href="{{ url('ftn/system/addedit') }}">NEW System</a></span>
							</div>
						</div>

						<div class="col-4 ms-auto">
							@if ($o->exists)
								<table class="table monotable">
									<thead>
									<tr><th colspan="2">System Addresses</th></tr>
									</thead>
									<tbody>
									@foreach ($o->system->addresses->groupBy('zone_id') as $zones)
									<tr>
										<th>{{ $zones->first()->zone->domain->name }}</th>
										<th class="text-end">{!! join('<br>',$zones->pluck('ftn')->toArray()) !!}</th>
									</tr>
									@endforeach
									</tbody>
								</table>
							@endif
						</div>
					</div>

					<div class="row">
						<div class="col-6">
							<h3>Site Permissions</h3>
							<!-- @todo
							* Inbound Working Dir
							-->
							<div class="form-check form-switch">
								<input class="form-check-input" type="checkbox" id="hideaka" name="options[hideaka]" value="{{ Setup::O_HIDEAKA }}" @if(old('options.hideaka',$o->binkpOptionGet(Setup::O_HIDEAKA))) checked @endif disabled>
								<label class="form-check-label" for="hideaka">Hide AKA to different Domains <sup>not implemented</sup></label>
							</div>
						</div>

						<div class="col-6">
							<h3>ZeroTier API</h3>
							<!-- @todo
							* Host/Port/Key
							-->
						</div>
					</div>

					<div class="row">
						<div class="col-12">
							<h4>Protocol Configuration</h4>
						</div>

						<div class="row">
							<div class="col-6">
								<h3>BINKP Settings</h3>
								<p>Bink has been configured to listen on <strong>{{ Setup::BINKP_BIND }}</strong>:<strong>{{ Setup::BINKP_PORT }}</strong></p>

								<div class="form-check form-switch">
									<input class="form-check-input" type="checkbox" id="startbinkd" name="options[binkd]" value="{{ Setup::O_BINKP }}" @if(old('options.binkd',$o->optionGet(Setup::O_BINKP))) checked @endif>
									<label class="form-check-label" for="startbinkd">Listen for BINKP connections</label>
								</div>

								<div class="mt-1 form-check form-switch">
									<input class="form-check-input" type="checkbox" id="opt_cht" name="binkp[cht]" value="{{ Setup::BINKP_OPT_CHT }}" @if(old('binkp.cht',$o->binkpOptionGet(Setup::BINKP_OPT_CHT))) checked @endif disabled>
									<label class="form-check-label" for="opt_cht">Chat Mode <sup>not implemented</sup></label>
								</div>

								<div class="mt-1 form-check form-switch">
									<input class="form-check-input" type="checkbox" id="opt_md" name="binkp[md]" value="{{ Setup::BINKP_OPT_MD }}" @if(old('binkp.md',$o->binkpOptionGet(Setup::BINKP_OPT_MD))) checked @endif>
									<label class="form-check-label" for="opt_md">CRAM-MD5 Mode</label>
								</div>

								<div class="mt-1 form-check form-switch">
									<input class="form-check-input" type="checkbox" id="opt_cr" name="binkp[cr]" value="{{ Setup::BINKP_OPT_CR }}" @if(old('binkp.cr',$o->binkpOptionGet(Setup::BINKP_OPT_CR))) checked @endif disabled>
									<label class="form-check-label" for="opt_cr">Crypt mode <sup>not implemented</sup></label>
								</div>

								<div class="mt-1 form-check form-switch">
									<input class="form-check-input" type="checkbox" id="opt_mb" name="binkp[mb]" value="{{ Setup::BINKP_OPT_MB }}" @if(old('binkp.mb',$o->binkpOptionGet(Setup::BINKP_OPT_MB))) checked @endif>
									<label class="form-check-label" for="opt_mb">Multi-Batch mode<sup>*</sup></label>
								</div>

								<div class="mt-1 form-check form-switch">
									<input class="form-check-input" type="checkbox" id="opt_mpwd" name="binkp[mpwd]" value="{{ Setup::BINKP_OPT_MPWD }}" @if(old('binkp.mpwd',$o->binkpOptionGet(Setup::BINKP_OPT_MPWD))) checked @endif disabled>
									<label class="form-check-label" for="opt_mpwd">Multi-Password Mode <sup>not implemented</sup></label>
								</div>

								<div class="mt-1 form-check form-switch">
									<input class="form-check-input" type="checkbox" id="opt_nd" name="binkp[nd]" value="{{ Setup::BINKP_OPT_ND }}" @if(old('binkp.nd',$o->binkpOptionGet(Setup::BINKP_OPT_ND))) checked @endif>
									<label class="form-check-label" for="opt_nd">No Dupes Mode</label>
								</div>

								<div class="mt-1 form-check form-switch">
									<input class="form-check-input" type="checkbox" id="opt_nda" name="binkp[nda]" value="{{ Setup::BINKP_OPT_NDA }}" @if(old('binkp.nda',$o->binkpOptionGet(Setup::BINKP_OPT_NDA))) checked @endif>
									<label class="form-check-label" for="opt_nda">No Dupes Mode - Asymmetric<sup>*</sup></label>
								</div>

								<div class="mt-1 form-check form-switch">
									<input class="form-check-input" type="checkbox" id="opt_nr" name="binkp[nr]" value="{{ Setup::BINKP_OPT_NR }}" @if(old('binkp.nr',$o->binkpOptionGet(Setup::BINKP_OPT_NR))) checked @endif>
									<label class="form-check-label" for="opt_nr">Non-Reliable Mode<sup>*</sup></label>
								</div>

								<p class="pt-3"><sup>*</sup> Recommended Defaults</p>

								<table class="table monotable">
									{{--
									$this->binkp_options = ['m','d','r','b'];
									--}}
								</table>
							</div>

							<div class="col-6">
								<h3>EMSI Settings</h3>
								<p>Bink has been configured to listen on <strong>{{ Setup::EMSI_BIND }}</strong>:<strong>{{ Setup::EMSI_PORT }}</strong></p>

								<div class="form-check form-switch">
									<input class="form-check-input" type="checkbox" id="startemsi" name="options[emsi]" value="{{ Setup::O_EMSI }}" @if(old('options.emsi',$o->optionGet(Setup::O_EMSI))) checked @endif>
									<label class="form-check-label" for="startemsi">Listen for EMSI connections</label>
								</div>
							</div>
						</div>
					</div>

					<div class="row">
						<div class="col-12">
							<a href="{{ url('ftn/domain') }}" class="btn btn-danger">Cancel</a>
							@if($errors->count())
								<span class="pl-5 btn btn-sm btn-danger" role="alert">
									There were errors with the submission.
									@dump($errors)
								</span>
							@endif
							@can('admin',$o)
								<button type="submit" name="submit" class="btn btn-success mr-0 float-end">@if ($o->exists)Save @else Add @endif</button>
							@endcan
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
@endsection

@section('page-css')
	<link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css">
@append
@section('page-scripts')
	<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

	<script type="text/javascript">
		$(document).ready(function() {
			$('#system_id').select2();
		});
	</script>
@append