<?php 

function assign_form_teachers_cb() {
	global $query_link;
	global $post_meta;
	$post = new post;
	$user = new user;
	$classes = $post->get_post_by_meta_data(['type' => 'class']);
	$categories = $post->get_post_by_meta_data(['type' => 'category']);
	$category_mapping = get_option('category_mapping');
	$ft_mapping = get_option('form_teacher_mapping');

	$sorting = new sorting;
	$output = $sorting->sort_by_meta_value($classes, 'class_name', 'ASC');	
	$staff_opt = [];
	$categories = $post->get_post_by_meta_data(['type' => 'category']);
	$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 Form 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 id="bootstrap-data-table" class="table table-bordered">
            <thead>
              <tr>
                <th>SN</th>
				<th>Class Name</th>
				<th>Form Teacher</th>
              </tr>
            </thead>
            <tbody>
<?php
$i = 1;
if(_count($output) > 0) : 
	foreach ($output as $class_meta) {
		if (_count($categories)) :
			foreach ($categories as $category_meta) {
				if(!in_array($category_meta['id'], $category_mapping[$class_meta['id']]))
					continue;
				$name = "form_teacher[".$class_meta['id']."][".$category_meta['id']."]";
	 ?>
	 			<tr>
	 				<td><?php echo $i; ?></td>
	 				<td><?php echo get_class_name($class_meta, $category_meta); ?></td>
	 				<td><?php select(['label' => '', 'id' => 'form_teacher', 'name' => $name], $staff_opt, $ft_mapping[$class_meta['id']][$category_meta['id']]) ?></td>
	 			</tr>

	<?php $i++; 
			}
		endif;
	} 
endif;
			?>
            </tbody>
          </table>
                </div>
            </div>
        </div>
    </div>
    <input type="hidden" name="action" id="action" value="assign_form_teachers">
	
</form>
	<?php
	add_page_javascript('app/assets/js/page-scripts/submit.js');
}