@php
	use App\Models\Netmail;
	use App\Classes\FTN\Message;
@endphp

@extends('layouts.app')
@section('htmlheader_title')
	Verify Packet
@endsection

@section('content')
	<form class="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($results->count())
		<h2>Archive Results: <strong class="highlight">{{ $filename }}</strong></h2>

		<div class="accordion" id="accordion_packet">
			@foreach($results as $item)
				@foreach ($item as $file => $result)
					<div class="accordion-item">
						<h3 class="accordion-header">
							<span class="accordion-button" id="packet" data-bs-toggle="collapse" data-bs-target="#collapse_item_{{ $loop->parent->index }}_{{ $loop->index }}" aria-expanded="false">Packet Results&nbsp;<strong class="highlight">{{ $file }}</strong></span>
						</h3>

						<div id="collapse_item_{{ $loop->parent->index }}_{{ $loop->index }}" class="accordion-collapse collapse @if($loop->parent->first)show @endif" aria-labelledby="packet" data-bs-parent="#accordion_packet">
							<div class="accordion-body">
								<p>Packet <strong class="highlight">{{ $file }}</strong> (type <strong class="highlight">{{ $result->type }}</strong>) is from <strong class="highlight">{{ $result->fftn->ftn }}</strong> to <strong class="highlight">{{ $result->tftn->ftn }}</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" id="accordion_file_{{ $loop->parent->index }}">
									<!-- MSG DUMP -->
									@foreach ($result as $msg)
										<div class="row">
											<div class="col-12">
												<h4 class="accordion-header">
													<span class="accordion-button" id="pktmsg" data-bs-toggle="collapse" data-bs-target="#collapse_msg_{{ $loop->parent->parent->index }}_{{ $loop->index }}" aria-expanded="false">
														@if($msg instanceof Netmail) Netmail @else Echomail&nbsp;<strong>{{ $msg->echoarea->name }}</strong> @endif : {{ $msg->msgid }}
													</span>
												</h4>

												<div id="collapse_msg_{{ $loop->parent->parent->index }}_{{ $loop->index }}" class="accordion-collapse collapse @if($result->messages->count() === 1 && $loop->parent->first)show @endif" aria-labelledby="pktmsg" data-bs-parent="#accordion_file_{{ $loop->parent->parent->index }}">
													<div class="accordion-body">
														@if ($msg->errors)
															@foreach ($msg->errors->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->datetime }}</strong>
															</div>
															<div class="col-4">
																FLAGS: <strong class="highlight">{{ $msg->flags()->keys()->join(', ') }}</strong>
															</div>
														</div>

														<div class="row pb-2">
															<div class="col-4">
																FROM: <strong class="highlight">{!! Message::tr($msg->from) !!}</strong> (<strong class="highlight">{{ $msg->fftn->ftn }}</strong>)
															</div>
															<div class="col-4">
																TO: <strong class="highlight">{!! Message::tr($msg->to) !!}</strong>@if($msg instanceof Netmail) (<strong class="highlight">{{ $msg->tftn->ftn }}</strong>) @endif
															</div>
														</div>

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

														<div class="row pb-2">
															<div class="col-8">
																<div class="pad pb-0">
																	<pre class="highlight">{!! Message::tr($msg->msg_src) !!}</pre>
																</div>
															</div>
														</div>

														@if($msg instanceof Netmail)
															<div class="row pb-2">
																<div class="col-8">
																	VIA: <br><strong class="highlight">{!! $msg->path->join('</strong> -> <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->kludges->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

									<!-- PKT DUMP -->
									<div class="row">
										<div class="col-12">
											<div class="accordion-item">
												<h4 class="accordion-header">
													<span class="accordion-button collapsed" id="pktdump" data-bs-toggle="collapse" data-bs-target="#collapse_hex_{{ $loop->parent->index }}" aria-expanded="false">
														Packet Dump
													</span>
												</h4>

												<div id="collapse_hex_{{ $loop->parent->index }}" class="accordion-collapse collapse" aria-labelledby="pktdump" data-bs-parent="#accordion_file_{{ $loop->parent->index }}">
													<div class="accordion-body">
<pre>
{{ $hexdump }}
</pre>
													</div>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				@endforeach
			@endforeach
		</div>
	@endif
@endsection