@extends('layouts.app')

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

		<div class="row">
			<div class="col-8">
				<div class="greyframe titledbox shadow0xb0">
					<h2 class="cap">Upload Packet for Analysis</h2>
					<p class="small">This packet will NOT be processed.</p>

					<div class="row">
						<div class="col-12">
							<label for="file" class="form-label">Packet</label>
							<div class="input-group has-validation">
								<input class="form-control @error('file') is-invalid @enderror" type="file" id="file" name="file[]" required>
								<span class="invalid-feedback" role="alert">
									@error('file')
										{{ $message }}
									@else
										A file is required.
									@enderror
								</span>
							</div>
						</div>
					</div>

					@if($errors->count())
						<div class="row">
							<span class="btn btn-sm btn-danger" role="alert" style="text-align: left;">
								There were errors with the submission.
								<ul>
									@foreach ($errors->all() as $error)
										<li>{{ $error }}</li>
									@endforeach
								</ul>
							</span>
						</div>
					@endif

					<div class="row">
						<div class="col-12">
							<a href="{{ url('/') }}" class="btn btn-danger">Cancel</a>
							<button type="submit" name="submit" class="btn btn-success float-end">Process</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</form>

	@if($result)
		<h3>Packet Results</h3>
		<p>Packet <strong class="highlight">{{ $file->getClientOriginalName() }}</strong> (type <strong class="highlight">{{ $result->type }}</strong>) is from <strong class="highlight">{{ $result->fftn }}</strong> to <strong class="highlight">{{ $result->tftn }}</strong>, dated <strong class="highlight">{{ $result->date }}</strong>.</p>
		<p>This packet has <strong class="highlight">{{ $result->messages->count() }}</strong> messages and <strong class="highlight">{{ $result->password ? 'DOES' : 'does NOT' }}</strong> have a password.</p>
		<p>Tosser: <strong class="highlight">{{ $result->software->code }}</strong> (<strong class="highlight">{{ $result->software->name }}</strong>), version <strong class="highlight">{{ $result->software_ver }}</strong>. Capabilities: <strong class="highlight">{{ $result->capability }}</strong>.</p>
		@if ($result->messages->count() > 1)
			<p><small>You can expand each one</small></p>
		@endif
		<hr>
		<div class="accordion accordion-flush" id="accordion_packetdebug">
		@foreach ($result as $msg)
			<div class="row">
				<div class="col-12">
					<h4 class="accordion-header" id="packetdebug" data-bs-toggle="collapse" data-bs-target="#collapse_msg_{{ $loop->index }}" aria-expanded="false" aria-controls="collapse_addresses">
						@if($msg->isNetmail()) Netmail @else Echomail <strong>{{ $msg->echoarea }}</strong> @endif : {{ $msg->msgid }}
					</h4>

					<div id="collapse_msg_{{ $loop->index }}" class="accordion-collapse collapse @if($result->messages->count() == 1 && $loop->first)show @endif" aria-labelledby="packetdebug" data-bs-parent="#accordion_packetdebug">
						<div class="accordion-body">
							@if ($msg->errors)
								@foreach ($msg->errors->messages()->all() as $error)
									<div class="alert alert-danger">
										{{ $error }}
									</div>
								@endforeach
							@endif

							<div class="row pb-2">
								<div class="col-4">
									DATE: <strong class="highlight">{{ $msg->date }}</strong>
								</div>
								<div class="col-4">
									FLAGS: <strong class="highlight">{{ $msg->flags()->filter()->keys()->join(', ') }}</strong>
								</div>
							</div>

							<div class="row pb-2">
								<div class="col-4">
									FROM: <strong class="highlight">{{ $msg->user_from }}</strong> (<strong class="highlight">{{ $msg->fftn }}</strong>)
								</div>
								<div class="col-4">
									TO: <strong class="highlight">{{ $msg->user_to }}</strong> (<strong class="highlight">{{ $msg->tftn }}</strong>)
								</div>
							</div>

							<div class="row pb-2">
								<div class="col-8">
									SUBJECT: <strong class="highlight">{{ $msg->subject }}</strong>
								</div>
							</div>

							<div class="row pb-2">
								<div class="col-8">
									<div class="pad pb-0">
										<pre class="highlight">{!! \App\Classes\FTN\Message::tr($msg->message).sprintf("\r * Origin: %s",$msg->origin) !!}</pre>
									</div>
								</div>
							</div>

							@if($msg->isNetmail())
								<div class="row pb-2">
									<div class="col-8">
										VIA: <br><strong class="highlight">{!! $msg->via->join('</strong><br><strong class="highlight">') !!}</strong>
									</div>
								</div>
							@else
								<div class="row pb-2">
									<div class="col-8">
										SEENBY: <br><strong class="highlight">{!! $msg->seenby->join('</strong><br><strong class="highlight">') !!}</strong>
									</div>
								</div>

								<div class="row pb-2">
									<div class="col-8">
										PATH: <br><strong class="highlight">{!! $msg->path->join('</strong><br><strong class="highlight">') !!}</strong>
									</div>
								</div>
							@endif

							<div class="row pb-2">
								<div class="col-8">
									<strong>KLUDGES:</strong> <br>
									@foreach ($msg->kludge->sort(function($v,$k) { return $k; })->reverse() as $k => $v)
										<strong class="highlight">{{ $k }}</strong> {{ $v }}<br>
									@endforeach
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		@endforeach

			<h4 class="accordion-header" id="packetdebug" data-bs-toggle="collapse" data-bs-target="#collapse_hex" aria-expanded="false" aria-controls="collapse_hex">
				Packet Dump
			</h4>
			<div id="collapse_hex" class="accordion-collapse collapse" aria-labelledby="packetdebug" data-bs-parent="#accordion_packetdebug">
				<div class="accordion-body">
<pre>
{{ $hexdump }}
</pre>
				</div>
			</div>
		</div>
	@endif
@endsection