@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 Enrolment</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">Student Enrolment</h5>
								</div>
								<div class="card-body">
									<!-- Button trigger modal -->
												<button type="button" class="btn btn-success" data-toggle="modal" data-target="#RegisterStudent">
													Enroll a Student
												</button>
												
												<!-- Modal -->
												@include('training.modals.studentsenrolment')
								</div>
							</div>
						</div>
					</div>

					<div class="card">
								<div class="card-header">
									<h5 class="card-title">ST John Ambulance Kenya - Enrolled Students</h5>
									<h6 class="card-subtitle text-muted">A table showing all the registered Students under the ST Johns Ambulance Training Program</h6>
								</div>
								<div class="card-body">
									<table id="datatables-reponsive" class="table table-striped" style="width:100%">
										<thead>
											<tr>
												<th>ID</th>
												<th>Full Names</th>
												<th>National ID</th>
												<th>Station</th>
												<th>Course</th>
												<th>Phone</th>
												<th>Email</th>
												<th>Payment Mode</th>
												<th>Acc No</th>
												<th>Status</th>
												<th>Registration Date</th>
												
												<th>Action</th>
											</tr>
										</thead>
										<tbody>
											@foreach ($students as $student)
												<tr>
													<td>{{ $student->id }}</td>
													<td>{{ $student->name }}</td>
													<td>{{ $student->idno }}</td>
													<td>{{ $student->station }}</td>
													<td>{{ $student->course_name }}</td>
													<td>{{ $student->phone }}</td>
													<td>{{ $student->email }}</td>
													<td>{{ $student->paymentmode }}</td>
													<td>{{ $student->account }}</td>
													<td>
														
														@if($student->paystatus === 'paid')
															<span class="badge bg-success">Cleared</span>
														@else
															<span class="badge bg-danger">Unsettled</span>
																<!-- Button to trigger modal -->
																<button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#viewAccountModal{{ $student->id }}" title="Update Account">
																	<i class="fa fa-tasks"></i>
																</button>

																<!-- Include the modal -->
																@include('training.modals.updateaccountmodal', ['student' => $student])

																<!-- View Details Button -->
																<button type="button" class="btn btn-outline-info btn-sm" data-toggle="modal" data-target="#viewDetailsModal{{ $student->id }}" title="View Details">
																	<i class="fa fa-eye"></i>
																</button>

																@include('training.modals.viewStudentDetailsModal', ['student' => $student])
														@endif
													</td>
													<td>{{ \Carbon\Carbon::parse($student->created_at)->format('d-m-Y') }}</td>
													
													<td>
														<a href="{{ route('students.edit', $student->id) }}" class="btn btn-outline-primary" title="Edit Enrolment">
															<i class="fa fa-edit"></i>
														</a>
													@if($student->paystatus === 'paid')
														
													
													@if(!in_array($student->id, $admittedStudentIds))
														<a href="#admitStudentModal{{$student->id}}" title="Admit Student" data-toggle="modal" class="btn btn-success">
															<i class="fa fa-check"></i>
														</a>

														@include('training.modals.admitStudentModal', ['student' => $student])
													@endif

														<a href="{{ route('reprintReceipt', $student->id) }}" class="btn btn-primary" target="_blank" title="Reprint Receipt">
															<i class="fa fa-print"></i>
														</a>

														<button type="button" class="btn btn-secondary" onclick="sendReceipt({{ $student->id }})" title="Send Receipt">
															<i class="fa fa-envelope"></i>
														</button>
														<div id="status-{{ $student->id }}" class="status-message"></div>											

													@else
													

													@if($student->paystatus !== 'paid' && $student->paymentmode !== 'mpesa')
														<form action="{{ route('confirmManualPayment', $student->id) }}" method="POST" style="display:inline;">
															@csrf
															<button type="submit" class="btn btn-warning" title="Confirm Manual Payment">
																<i class="fa fa-check-circle"></i>
															</button>
														</form>
													@endif
															<form action="{{ route('settleBill', $student->id) }}" method="POST">
																@csrf
																<button type="submit" class="btn btn-success" title="Settle Bill"> 
																	<i class="fa fa-tasks"></i>
																</button>
															</form>														
													@endif

													</td>
												</tr>
											@endforeach
										</tbody>
									</table>
									<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
									<script>
											function sendReceipt(studentId) {
												$.ajax({
													url: '/send-receipt/' + studentId,
													type: 'POST',
													data: {
														_token: '{{ csrf_token() }}'
													},
													success: function(response) {
														$('#status-' + studentId).html('<span class="text-success">Receipt sent successfully.</span>');
													},
													error: function(xhr) {
														$('#status-' + studentId).html('<span class="text-danger">An error occurred. Please try again.</span>');
													}
												});
											}
									</script>

								</div>
							</div>

				</div>
				
			</main>
@endsection