<?php 

function reassign_students_cb() {
	do_action('display_student_list_filter2');

	if(!isset($_POST['class'])) {
		return;
	}
	$user = new user;
	global $post_meta;
	if($_POST['class'] !== 'all') {
		$students = $user->get_user_by_meta_data(['role' => 'student', 'status' => 'active', 'class' => $_POST['class']]);
		$classname = get_class_name($_POST['class']);
	} else {
		$students = $user->get_user_by_meta_data(['role' => 'student', 'status' => 'active']);
		$classname = "All";
	}

	$category_mapping = get_option('category_mapping');
	$sorting = new sorting;
	$output = $sorting->sort_by_meta_value($students, 'surname', 'asc');
	$post = new post;
	$categories = $post->get_post_by_meta_data(['type' => 'category']);
	$categories = switch_result_keys($categories);
	?>
	<div class="row">
    <div class="col-md-12">
      <div class="panel panel-custom bg-white">
          <div class="panel-heading">
              <h1>Re-Assign Students</h1>
          </div>
            <div class="panel-body instruction">
              <form method="post" id="form" action="contents/plugins/eedu_portal/ajax.php">
              	<input type="hidden" name="class" id="class" value="<?php echo $_POST['class']; ?>">
              	<table class="table">
              		<thead>
              			<tr>
              				<th>SN</th>
              				<th>Name of Student</th>
              				<th>Class</th>
              				<th>Category</th>
              			</tr>
              		</thead>
              		<tbody>
              			<?php if (_count($output)): $i = 1; ?>
              				<?php foreach ($output as $key => $student_meta): ?>
              					<tr>
		              				<td><?= $i ?></td>
		              				<td><?= get_fullname($student_meta) ?></td>
		              				<td><?= ($classname == 'All') ? get_class_name($student_meta['class']) : $classname; ?></td>
		              				<?php foreach ($category_mapping[$student_meta['class']] as $category_id): ?>
		              				<td>
		              					<?php
		              					$state = ($category_id == $student_meta['category']) ? true : false;
		              					checkBox(['label' => $categories[$category_id]['category_name'], 'id' => $student_meta['id'].'-cat-'.$category_id, 'name' => 'category'.'['.$student_meta['id'].']', 'value' => $category_id, 'type' => 'radio'], $state);
		              					?>	              					
		              				</td>
		              				<?php endforeach; ?>
		              			</tr>
              				<?php $i++; endforeach ?>
              			<?php endif ?>
              		</tbody>
              	</table>
                <?php alert(); submit_btn('reassign_students', 'Submit', 'submit'); ?>
              </form>
        </div>
      </div>
    </div>
  </div>
  <?php
  add_page_javascript('app/assets/js/page-scripts/submit.js');
}