<?php 

function edit_timetable_cb() {
	global $post_meta;
	global $app;
	$user_meta = $app->user_meta;
	$post = new post;
	$user = new user;
	do_action("class_category_filter");
	if(!isset($_POST['class'])) return;
	$class = $_POST['class'];
	$category = $_POST['category'];

	if(!current_user_can('admin')) {
		$teacher_id = $user_meta['id'];
		if(!is_form_teacher($teacher_id, $class, $category)) {
			echo '<p class="error">You cannot edit this class timetable</p>';
			return;
		}
	}

	$days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
	$courses = $post->get_post_by_meta_data(['type' => 'course']);
	if(_count($courses) < 1)
		return_method('add-courses');
	$teacher_mapping = get_option('teacher_mapping');
	$course_mapping = get_option('course_mapping');
	$class_course = isset($course_mapping[$class][$category]) ? $course_mapping[$class][$category] : [];
	$timetable = get_option('timetable')[$class][$category];
	if(_count($class_course) > 0) : foreach ($class_course as $value) {
		$course_meta = $post->get_post_meta($value);
		$activities[$course_meta['course_name']] = $course_meta['course_name'];
	} endif;
	$extra_activities = get_option('extra_activities');
	if(_count($extra_activities) > 0) : 
		foreach ($extra_activities as $key => $value) {
			$activities[trim($value)] = trim($value);
		} 
	endif;
	?>
	<div class="">
        <div class="card">
            <div class="card-body">
                <p class="card-text">
                	<div class="alert alert-warning" role="alert">
                        You are editing&nbsp;<strong><?=get_class_name($class, $category); ?></strong>&nbsp;Class Time-table
                    </div>
                	<div class="available-teachers-div">
                		<div class="available-teachers"><span>Teachers Reference</span></div>
    <?php 
if(_count($class_course) > 0) {
	foreach ($class_course as $value) {
		$course_meta_dis = $post->get_post_meta($value);
		$teacher_meta = $user->get_user_meta($teacher_mapping[$class][$category][$value]);
		?>
<div class="col-md-4">
    <div class="card bg-primary">
        <div class="card-body">
            <blockquote class="blockquote mb-0 text-light">
                <p class="text-light"><?php echo $course_meta_dis['course_name']; ?></p>
                <footer class="blockquote-footer text-light"><?=$teacher_meta['surname'].' '.$teacher_meta['other_names']?></footer>
            </blockquote>
        </div>
    </div>
</div>
		<?php
	}
}

     ?>
                	</div>
                </p>
            
		    <div class="col-md-12">
			<form id="form" method="post" action="contents/plugins/eedu_portal/ajax.php">
				<?php input(['label' => '', 'id' => 'class', 'type' => 'hidden', 'value' => $class]); 
					input(['label' => '', 'id' => 'category', 'type' => 'hidden', 'value' => $category]); 
				?>
				<table class="table table-bordered table-striped table-hover">
					<tr>
						<th>Start</th>
						<th>Stop</th>
						<?php 
						foreach ($days as $day) {
							echo '<th>'.$day.'</th>';
						}
						 ?>
					</tr>
		<?php 
		$i = 1;
		do {
			?>
					<tr>
						<td><?php input(['label' => '', 'id' => '', 'name' => "period[".$i."][start]", 'type' => 'time', 'value' => $timetable[$i]['start']]); ?></td>
						<td><?php input(['label' => '', 'id' => '', 'name' => "period[".$i."][stop]", 'type' => 'time', 'value' => $timetable[$i]['stop']]); ?></td>
						<?php 
						foreach ($days as $day) {
							?>
							<td><?php select(['label' => '', 'id' => '', 'name' => "period[".$i."][".$day."]"], $activities, $timetable[$i][$day]); ?></td>
							<?php
						}
						 ?>
					</tr>
			<?php
			$i++;
		} while ($i <= get_option('mtp'));

		 ?>

				</table>
				<!-- <a href="#" class="btn btn-danger" id="addMore" data-activate=false data-url="?action=<?php echo base64_encode('add_more_columns') ?>&table=timetable&class=<?php echo base64_encode($class) ?>"><i class="fas fa-plus"></i>&nbsp;Add More</a> -->
				<?php alert(); submit_btn('edit_timetable', 'Save Timetable'); ?>
			</form>
			</div>
			</div>
        </div>
    </div>
	<?php
	add_page_javascript('app/assets/js/page-scripts/submit.js');
}