<?php 

function assign_teachers_cb() {
	global $query_link;

	global $post_meta;
	$post = new post;
	$user = new user;

	do_action('display_student_list_filter');

	if(!isset($_POST['class'])) {
		return;
	}	
	$course_mapping = get_option('course_mapping');
	$class_course = $course_mapping[$_POST['class']][$_POST['category']];
	$teacher_mapping = get_option('teacher_mapping');
	$staff_opt = [];
	$staff = $user->get_user_by_meta_data(['role' => 'super_admin', 'status' => 'active']);
	$staff = array_merge($staff, $user->get_user_by_meta_data(['role' => 'admin', 'status' => 'active']));
	$staff = array_merge($staff, $user->get_user_by_meta_data(['role' => 'teacher', 'status' => 'active']));
	if(_count($staff) > 0){
        foreach ($staff as $value) {
            $staff_opt[$value['surname'].' '.$value['other_names']] = $value['id'];
        }
    }
	?>
<form method="post" action="contents/plugins/eedu_portal/ajax.php" id="form">
	<div class="row">
		<div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <strong class="card-title">Assign Teachers</strong>
                    <button class="btn btn-primary pull-right" id="submit" style="top: 4px;position: absolute;right: 4px;">Save Options</button>
                </div>
                <div class="card-body">
          <?php alert(); ?>    	
				<table class="table table-hover">
					<?php 
					input(['label' => '', 'id' => 'class', 'type' => 'hidden', 'value' => $_POST['class']]);
					input(['label' => '', 'id' => 'category', 'type' => 'hidden', 'value' => $_POST['category']]);
					if(_count($class_course)): foreach ($class_course as $course) {
						$course_meta = $post->get_post_meta($course);
						$name = "teacher[".$course."]";
					 ?>
					 <tr>
						<td><?php echo $course_meta['course_name']; ?></td>
						<td><?php 
							select(['label' => '', 'id' => 'teacher', 'name' => $name], $staff_opt, $teacher_mapping[$_POST['class']][$_POST['category']][$course]);
							$branches = $post->get_post_by_meta_data(['type' => 'subject_branch', 'course_id' => $course_meta['id']]);
							if(is_array($branches)) {
								if(_count($branches)) {
									?>
									<table class="table">
									<?php
									foreach ($branches as $branch) {
										?>
										<tr>
											<td><?php echo $branch['course_name']; ?></td>
											<td><?php select(['label' => '', 'id' => 'teacher', 'name' => "teacher[".$branch['id']."]"], $staff_opt, $teacher_mapping[$_POST['class']][$_POST['category']][$branch['id']]); ?></td>
										</tr>
										<?php
									}
									?>
									</table>
									<?php
								}
							}
						?></td>
					</tr>
					<?php } endif; ?>
				</table>
                </div>
            </div>
        </div>
    </div>
    <input type="hidden" name="action" id="action" value="assign_teachers">
	
</form>
	<?php
	add_page_javascript('app/assets/js/page-scripts/submit.js');
}