<!-- $o = Setup::class -->
@php
use App\Models\Setup;
use App\Classes\Protocol\{Binkp,EMSI,DNS};
@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">
						<!-- System -->
						<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()->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">
									@if(! $o->system_id)
										Add a <a href="{{ url('system/addedit') }}">NEW System</a>
									@else
										<a href="{{ url('system/addedit',[$o->system_id]) }}">Edit</a> System
									@endif
								</span>
							</div>
						</div>

						<!-- System Addresses -->
						<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->akas->sortBy('zone.domain.name')->groupBy('zone_id') as $addresses)
									<tr>
										<th>{{ $addresses->first()->zone->domain->name }}</th>
										<th class="text-end">{!! $addresses->pluck('ftn')->join('<br>') !!}</th>
									</tr>
									@endforeach
									</tbody>
								</table>
							@endif
						</div>
					</div>

					<div class="row">
						<!-- Site Permissions -->
						<div class="col-6">
							<h3>Site Permissions</h3>

							<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->optionGet(Setup::O_HIDEAKA,'options_options'))) checked @endif>
								<label class="form-check-label" for="hideaka">Hide AKA to different Domains</label>
							</div>
						</div>

						<!-- ZeroTier -->
						<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">
							<!-- Binkp Settings -->
							<div class="col-6">
								<div class="row">
									<div class="col-12">
										<h3>BINKP Settings</h3>

										<div class="form-group row pt-0">
											<div class="col-9">
												<label class="col-form-label pt-2" for="binkp_bind">Listing Interface</label>
											</div>
											<div class="col-3">
												<input class="form-control text-end @error('binkp_bind')is-invalid @enderror" type="text" id="binkp_bind" name="binkp_bind" value="{{ old('binkp_bind',$o->binkp_bind) }}">
											</div>
										</div>

										<div class="form-group row pt-0">
											<div class="col-9">
												<label class="col-form-label pt-2" for="binkp_port">Listing Port</label>
											</div>
											<div class="col-3">
												<input class="form-control text-end @error('binkp_port')is-invalid @enderror" type="text" id="binkp_port" name="binkp_port" value="{{ old('binkp_port',$o->binkp_port) }}">
											</div>
										</div>

										<div class="form-check form-switch">
											<input class="form-check-input" type="checkbox" id="binkp_active" name="binkp_active" value="1" @if(old('binkp_active',$o->binkp_active)) checked @endif>
											<label class="form-check-label" for="binkp_active">Listen for BINKP connections</label>
										</div>

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

										<div class="mt-1 form-check form-switch">
											<input class="form-check-input" type="checkbox" id="binkp_comp" name="binkp[comp]" value="{{ Binkp::F_COMP }}" @if(old('binkp.comp',$o->optionGet(Binkp::F_COMP,'binkp_options'))) checked @endif>
											<label class="form-check-label" for="binkp_comp">Compression Enabled</label>
										</div>

										<div class="mt-1 form-check form-switch">
											<input class="form-check-input" type="checkbox" id="binkp_md" name="binkp[md]" value="{{ Binkp::F_MD }}" @if(old('binkp.md',$o->optionGet(Binkp::F_MD,'binkp_options'))) checked @endif>
											<label class="form-check-label" for="binkp_md">CRAM-MD5 Mode <sup>*</sup></label>
										</div>

										<!-- @todo Force turning off this toggle, if md5 is not selected -->
										<div class="mt-1 form-check form-switch">
											<input class="form-check-input" type="checkbox" id="binkp_mdforce" name="binkp[mdforce]" value="{{ Binkp::F_MDFORCE }}" @if(old('binkp.mdforce',$o->optionGet(Binkp::F_MDFORCE,'binkp_options'))) checked @endif>
											<label class="form-check-label" for="binkp_mdforce">No Plaintext Passwords</label>
										</div>

										<div class="mt-1 form-check form-switch">
											<input class="form-check-input" type="checkbox" id="binkp_crypt" name="binkp[cr]" value="{{ Binkp::F_CRYPT }}" @if(old('binkp.cr',$o->optionGet(Binkp::F_CRYPT,'binkp_options'))) checked @endif>
											<label class="form-check-label" for="binkp_crypt">Crypt mode <sup>*</sup></label>
										</div>

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

										<div class="mt-1 form-check form-switch">
											<input class="form-check-input" type="checkbox" id="binkp_mpwd" name="binkp[mpwd]" value="{{ Binkp::F_MULTIPASS }}" @if(old('binkp.mpwd',$o->optionGet(Binkp::F_MULTIPASS,'binkp_options'))) checked @endif disabled>
											<label class="form-check-label" for="binkp_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="binkp_nd" name="binkp[nd]" value="{{ Binkp::F_NODUPE }}" @if(old('binkp.nd',$o->optionGet(Binkp::F_NODUPE,'binkp_options'))) checked @endif>
											<label class="form-check-label" for="binkp_nd">No Dupes Mode</label>
										</div>

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

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

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

							<!-- EMSI Settings -->
							<div class="col-6">
								<div class="row">
									<div class="col-12">
										<h3>EMSI Settings</h3>

										<div class="form-group row pt-0">
											<div class="col-9">
												<label class="col-form-label pt-2" for="emsi_bind">Listing Interface</label>
											</div>
											<div class="col-3">
												<input class="form-control text-end @error('emsi_bind')is-invalid @enderror" type="text" id="emsi_bind" name="emsi_bind" value="{{ old('emsi_bind',$o->emsi_bind) }}">
											</div>
										</div>

										<div class="form-group row pt-0">
											<div class="col-9">
												<label class="col-form-label pt-2" for="emsi_port">Listing Port</label>
											</div>
											<div class="col-3">
												<input class="form-control text-end @error('emsi_port')is-invalid @enderror" type="text" id="emsi_port" name="emsi_port" value="{{ old('emsi_port',$o->emsi_port) }}">
											</div>
										</div>

										<div class="form-check form-switch">
											<input class="form-check-input" type="checkbox" id="startemsi" name="emsi_active" value="1" @if(old('emsi_active',$o->emsi_active)) checked @endif>
											<label class="form-check-label" for="startemsi">Listen for EMSI connections</label>
										</div>

										<div class="mt-1 form-check form-switch">
											<input class="form-check-input" type="checkbox" id="emsi_nrq" name="emsi[nrq]" value="{{ EMSI::F_IGNORE_NRQ }}" @if(old('emsi.nrq',$o->optionGet(EMSI::F_IGNORE_NRQ,'emsi_options'))) checked @endif>
											<label class="form-check-label" for="emsi_nrq">Ignore NRQ</label>
										</div>

										<div class="mt-1 form-check form-switch">
											<input class="form-check-input" type="checkbox" id="emsi_doprevent" name="emsi[doprevent]" value="{{ EMSI::F_DO_PREVENT }}" @if(old('emsi.doprevent',$o->optionGet(EMSI::F_DO_PREVENT,'emsi_options'))) checked @endif>
											<label class="form-check-label" for="emsi_doprevent">Send an EMSI_INQ on connect</label>
										</div>

									</div>
								</div>

								<div class="row pt-5">
									<div class="col-12">
										<h3>DNS Settings</h3>

										<div class="form-group row pt-0">
											<div class="col-9">
												<label class="col-form-label pt-2" for="dns_bind">Listing Interface</label>
											</div>
											<div class="col-3">
												<input class="form-control text-end @error('dns_bind')is-invalid @enderror" type="text" id="dns_bind" name="dns_bind" value="{{ old('dns_bind',$o->dns_bind) }}">
											</div>
										</div>

										<div class="form-group row pt-0">
											<div class="col-9">
												<label class="col-form-label pt-2" for="dns_port">Listing Port</label>
											</div>
											<div class="col-3">
												<input class="form-control text-end @error('dns_port')is-invalid @enderror" type="text" id="dns_port" name="dns_port" value="{{ old('dns_port',$o->dns_port) }}">
											</div>
										</div>

										<div class="form-check form-switch">
											<input class="form-check-input" type="checkbox" id="startdns" name="dns_active" value="1" @if(old('dns_active',$o->dns_active)) checked @endif>
											<label class="form-check-label" for="startdns">Listen for DNS connections</label>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>

					<div class="row">
						<div class="col-6">
							<h3>Echomail Settings</h3>

							<div class="form-group row pt-0">
								<div class="col-2">
									<input class="form-control text-end" type="text" id="msgs_pkt" name="msgs_pkt" value="{{ old('msgs_pkt',$o->msgs_pkt ?: Setup::MAX_MSGS_PKT) }}">
								</div>
								<div class="col-10">
									<label class="col-form-label pt-2" for="msgs_pkt">Max Messages per Packet</label>
								</div>
							</div>
						</div>
					</div>

					<div class="row pt-5">
						<div class="col-2">
							<a href="{{ url('domain') }}" class="btn btn-danger">Cancel</a>
						</div>

						<span class="col-6 mt-auto mx-auto text-center align-bottom">
						@if($errors->count())
							<span class="btn btn-sm btn-danger" role="alert">
								<h3 class="text-white">There were errors with the submission.</h3>

								<ul>
								@foreach ($errors->all() as $message)
									<li>{{ $message }}</li>
								@endforeach
								</ul>
							</span>
						@endif
						</span>

						@can('admin',$o)
							<div class="col-2">
								<button type="submit" name="submit" class="btn btn-success float-end">@if ($o->exists)Save @else Add @endif</button>
							</div>
						@endcan
					</div>
				</form>
			</div>
		</div>
	</div>
@endsection

@section('page-css')
	@css('select2')

	<style>
		#content h3 {
			margin-bottom: 5px;
		}
		#content ul li:last-child {
			margin-bottom: inherit;
		}
	</style>
@append
@section('page-scripts')
	@js('select2')

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