@extends('layouts.app')

@section('htmlheader_title')
	@if($o->exists) Update @else Add @endif User
@endsection

@section('content')
	<form class="row g-0 needs-validation" method="post" novalidate>
		@csrf

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

							<div class="row">
								<div class="col-4">
									<label for="name" class="form-label">Name</label>
									<div class="input-group has-validation">
										<span class="input-group-text"><i class="bi bi-tag-fill"></i></span>
										<input type="text" class="form-control @error('name') is-invalid @enderror" id="name" placeholder="Name" name="name" value="{{ old('name',$o->name) }}" required @cannot('admin',$o)disabled @endcannot autofocus>
										<span class="invalid-feedback" role="alert">
											@error('name')
												{{ $message }}
											@else
												A name is required.
											@enderror
										</span>
									</div>
								</div>

								<div class="col-3">
									<label for="alias" class="form-label">BBS Alias</label>
									<div class="input-group has-validation">
										<span class="input-group-text"><i class="bi bi-tag-fill"></i></span>
										<input type="text" class="form-control @error('alias') is-invalid @enderror" id="alias" placeholder="alias" name="alias" value="{{ old('alias',$o->alias) }}" @cannot('admin',$o)disabled @endcannot>
										<span class="invalid-feedback" role="alert">
											@error('alias')
												{{ $message }}
											@enderror
										</span>
									</div>
								</div>

								<!-- Forward Netmail -->
								<div class="col-4">
									<label for="system_id" class="form-label">Forward Netmails</label>
									<div class="input-group has-validation">
										<span class="input-group-text"><i class="bi bi-envelope-at-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 ($o->systems 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 }}
											@enderror
										</span>
									</div>
								</div>
							</div>

							<div class="row">
								<div class="col-4">
									<label for="email" class="form-label">Email</label>
									<div class="input-group has-validation">
										<span class="input-group-text"><i class="bi bi-person-badge"></i></span>
										<input type="text" class="form-control @error('email') is-invalid @enderror" id="email" placeholder="Email" name="email" value="{{ old('email',$o->email) }}" required @cannot('admin',$o)disabled @endcannot>
										<span class="invalid-feedback" role="alert">
											@error('email')
												{{ $message }}
											@else
												Email required for login.
											@enderror
										</span>
									</div>
								</div>

								<div class="col-1">
									<label for="active" class="form-label">Active</label>
									<div class="input-group">
										<div class="btn-group" role="group">
											<input type="radio" class="btn-check" name="active" id="active_yes" value="1" required @cannot('admin',$o)disabled @endcannot @if(old('active',$o->active))checked @endif>
											<label class="btn btn-outline-success" for="active_yes">Yes</label>

											<input type="radio" class="btn-check btn-danger" name="active" id="active_no" value="0" required @cannot('admin',$o)disabled @endcannot @if(! old('active',$o->active))checked @endif>
											<label class="btn btn-outline-danger" for="active_no">No</label>
										</div>
									</div>
								</div>

								<div class="offset-1 col-1">
									<label for="admin" class="form-label">Site Admin</label>
									<div class="input-group">
										<div class="btn-group" role="group" @if($user->id === $o->id)data-bs-toggle="tooltip" title="You cannot demote yourself" @endif>
											<input type="radio" class="btn-check" name="admin" id="admin_yes" value="1" required @cannot('admin',$o)disabled @endcannot @if(old('admin',$o->admin))checked @endif>
											<label class="btn btn-outline-success" for="admin_yes">Yes</label>

											<input type="radio" class="btn-check btn-danger" name="admin" id="admin_no" value="0" required @if(($user->id === $o->id) || $user->cannot('admin',$o)) disabled  @endif @if(! old('admin',$o->admin))checked @endif>
											<label class="btn btn-outline-danger" for="admin_no">No</label>
										</div>
									</div>
								</div>
							</div>

							<div class="row">
								<div class="col-12">
									<label for="pgp_pubkey" class="form-label">PGP Public Key</label>
									<textarea class="form-control @error('pgp_pubkey')is-invalid @enderror" rows=3 name="pgp_pubkey" placeholder="PGP Public Key..." @cannot('admin',$o)disabled @endcannot>{{ old('pgp_pubkey',$o->pgp_pubkey) }}</textarea>
									<span class="invalid-feedback" role="alert">
										@error('pgp_pubkey')
											{{ $message }}
										@enderror
									</span>
								</div>
							</div>

							<div class="row">
								<div class="col-12">
									<a href="{{ url('user') }}" class="btn btn-danger">Cancel</a>
									@can('admin',$o)
										<button type="submit" name="submit" class="btn btn-success float-end">@if ($o->exists)Save @else Add @endif</button>
									@endcan
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>

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

				<table class="table monotable">
					<thead>
					<tr>
						<th>System</th>
					</tr>
					</thead>

					<tbody>
					@foreach ($o->systems as $o)
						<tr>
							<th><a href="{{ url('system/addedit',[$o->id]) }}">{{ $o->name }}</a></th>
							<th class="text-end">{!! $o->akas->pluck('ftn')->join('<br>') !!}</th>
						</tr>
					@endforeach
					</tbody>
				</table>
			</div>
		</div>
	</form>
@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')

	@if($user->id === $o->id)
		<script>
			var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
			var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
				return new bootstrap.Tooltip(tooltipTriggerEl)
			})
		</script>
	@endif

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