@extends('training.inc.master')

@section('title','Dashboard')

@section('content')

<main class="content">
				<div class="container-fluid p-0">

				<h1 class="h3 mb-3"><strong>Training</strong> Students Certified</h1>

				@include('training.alerts.alert')

					<div class="row">
						<div class="col-12">
							<div class="card">
								<div class="card-header">
									<h5 class="card-title mb-0"> Register Certified Student</h5>
								</div>
								<div class="card-body">
									<!-- Button trigger modal -->
												<button type="button" class="btn btn-success" data-toggle="modal" data-target="#RegisterStudent">
													Register Certified Student
												</button>
												
												<!-- Modal -->
												@include('training.modals.Certifiedstudentsregister')
								</div>
							</div>
						</div>
					</div>

					<div class="card">
								<div class="card-header">
									<h5 class="card-title">ST John Ambulance Kenya - Certified Trained Students</h5>
									<h6 class="card-subtitle text-muted">A table showing all the  Certified registered Students under the ST Johns Ambulance Training Program</h6>
								</div>
								<div class="card-body">
									<table id="datatables-buttons" class="table table-striped" style="width:100%">
										<thead>
											<tr>
												<th>ID</th>
												<th>Full Names</th>
												<th>National ID</th>
												<th>Phone</th>
												<th>Course</th>
												<th>Serial</th>
												<th>Status</th>
												<th>Approval Date</th>
												<th>Expery Date</th>
												<th>Training Station</th>
												<th>Action</th>
											</tr>
										</thead>
										<tbody>
											@foreach ($students as $student)
												<tr>
													<td>{{ $loop->iteration }}</td>
													<td>{{ $student->full_names }}</td>
													<td>{{ $student->national_id }}</td>
													<td>{{ $student->phone }}</td>
													<td>{{ $student->course_name }}</td>
													<td>{{ $student->cert_serial }}</td>
													<td>
														@if ($student->status == 'valid')
															<button type="button" class="btn btn-success">
																<i class="fas fa-check"></i> Valid
															</button>
														@else
															<button type="button" class="btn btn-danger">
																<i class="fas fa-times"></i> Expired
															</button>
														@endif
													</td>
													<td>{{ \Carbon\Carbon::parse($student->approvaldate)->format('d-m-Y') }}</td>
													<td>{{ \Carbon\Carbon::parse($student->experydate)->format('d-m-Y') }}</td>
													<td>{{ $student->training_location }}</td>
													<td>
														@if ($student->status == 'valid')
															<div class="dropdown">
																<button class="btn btn-secondary dropdown-toggle" type="button" 
																		id="dropdownMenuButton{{ $student->id }}" 
																		data-toggle="dropdown" 
																		aria-haspopup="true" 
																		aria-expanded="false">
																	Actions
																</button>
																<div class="dropdown-menu" aria-labelledby="dropdownMenuButton{{ $student->id }}">
																	
																	<form action="{{ route('generate-cert') }}" method="post" target="_blank">
																		@csrf
																		<input type="hidden" name="idno" value="{{ $student->cert_serial }}">
																		<button type="submit" class="dropdown-item">
																			<i class="fas fa-tasks"></i> Reprint Cert
																		</button>
																	</form>

																	<form action="{{ route('regenerateCert') }}" method="post" target="_blank">
																		@csrf
																		<input type="hidden" name="refno" value="{{ $student->cert_serial }}">
																		<button type="submit" class="dropdown-item">
																			<i class="fas fa-file-alt"></i> Reprint certificate
																		</button>
																	</form>

																	<a href="{{ url('/send-certification/' . $student->national_id) }}" 
																		class="dropdown-item" target="_blank">
																		<i class="fas fa-redo"></i> Resend certificate
																	</a>
																	<a href="javascript:void(0);" 
																		class="dropdown-item resend-cert" 
																		data-serial="{{ $student->cert_serial }}">
																		<i class="fas fa-redo"></i> E certify
																	</a>
																		<a href="{{ route('mini.certificate', ['serialNo' => $student->cert_serial]) }}"
																			class="dropdown-item" target="_blank">
																			<i class="fas fa-eye"></i> View Mini Cert
																		</a>
																</div>
															</div>
														@else
															<button type="button" class="btn btn-danger">
																<i class="fas fa-times"></i> Expired
															</button>
														@endif
													</td>


												</tr>
											@endforeach
										</tbody>
									</table>
									<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
									<script>
										$(document).on('click', '.resend-cert', function(e) {
											e.preventDefault();
											let certSerial = $(this).data('serial');

											$.ajax({
												url: "{{ route('resend-cert', ':serial') }}".replace(':serial', certSerial),
												type: "POST",
												data: {
													_token: "{{ csrf_token() }}",
												},
												success: function(response) {
													alert(response.message); // Or use a nicer toast
												},
												error: function(xhr) {
													alert(xhr.responseJSON.message || "Failed to resend certificate.");
												}
											});
										});
									</script>
								</div>
							</div>

				</div>
				
			</main>
@endsection