<?php 

function mark_attendance_cb() {
	global $post_meta;
	do_action("performance_filter");
	if(!isset($_POST['term']))
		return;
	$class = $_POST['class'];
	$category = $_POST['category'];
	$students = get_class_students(['class_id' => $class, 'category_id' => $category]);
	$term = $_POST['term'];
	$past_dates = get_past_dates($term);
	$option = "attendance_".$term."_".$class;
	$attendance = get_option($option);
	?>
	
	<div class="panel panel-custom bg-white">
	    <div class="panel-heading">
	        <h1>Attendance register for <?php echo get_class_name($class, $category); ?>&nbsp;<?php echo get_term_name($term); ?></h1>
	    </div>
	    <form method="post" action="contents/plugins/eedu_portal/ajax.php" id="form">
	    	<button class="btn btn-primary" id="submit"><i class="fas fa-hdd-o"></i>&nbsp;Save</button>
	        <div class="panel-body instruction">
		        
		        	<?php alert(); ?>
		        	<input type="hidden" name="action" id="action" value="mark_attendance">
					
					<input type="hidden" name="class" value="<?php echo $class; ?>">
					<input type="hidden" name="term" value="<?php echo $term; ?>">
					<div class="table-responsive">
						<table class="table table-bordered">
							<thead>
								<tr>
									<?php foreach ($past_dates as $week => $days_arr): ?>
										<th rowspan="2" colspan="2">Student Name</th>
										<th class="text-center" colspan="<?php echo _count($days_arr) ?>">Week <?php echo $week; ?></th>	
									<?php endforeach ?>
								</tr>
								<tr>
									<?php foreach ($past_dates as $week => $days_arr): ?>
										<?php foreach ($days_arr as $key => $day): ?>
											<th class="text-center"><?php echo date('D', $day); ?></th>
										<?php endforeach ?>
									<?php endforeach ?>
								</tr>
							</thead>
							<tbody>
								<?php if (_count($students) > 0): $i = 1; ?>
									<?php foreach ($students as $student_id => $student_meta): ?>
										<tr>
											<?php foreach ($past_dates as $week => $days_arr): ?>
												<td><?php echo $i; ?></td>
												<td><?php echo $student_meta['surname'].' '.$student_meta['other_names']; ?></td>
												<?php foreach ($days_arr as $key => $day): ?>
													<td>
														<?php 
														if (isset($attendance[$day][$student_meta['id']])) {
															input(['label' => '', 'id' => 'attendance', 'name' => 'att['.$day.']['.$student_meta['id'].']', 'type' => 'checkbox', 'value' => $student_meta['id'], 'checked' => 'checked']);
														} else {
															input(['label' => '', 'id' => 'attendance', 'name' => 'att['.$day.']['.$student_meta['id'].']', 'type' => 'checkbox', 'value' => $student_meta['id']]);
														}
														?>
													</td>
												<?php endforeach ?>
											<?php endforeach ?>
										</tr>
									<?php $i++; endforeach; ?>
								<?php endif ?>
							</tbody>	
							
						</table>
					</div>
			</div>
		</form>
	</div>
	<?php
	add_page_javascript('app/assets/js/page-scripts/submit.js');
}