<?php 

function academic_session_cb() {
	$session = Session::getInstance();
	global $post_meta;
	if(!isset($_GET['id']))
		return_method();
	$post = new post;
	$session_meta = $post->get_post_meta($_GET['id']);
	$token = new_token();
	$terms = $post->get_post_by_meta_data(['type' => 'academic_term', 'acsession' => $session_meta['id']]);
	$current_term = get_option('current_academic_term');
	?>
	<div class="row padding-top">
		<div class="col-md-4">
			<div class="card">
			    <div class="card-header">
			        <h4>Navigation</h4>
			    </div>
			    <div class="card-body">
			        <!-- <p class="text-muted m-b-15">Pills can also be displayed vertically. Just add the <code>.nav-stacked</code> class:</p> -->

			        <div class="nav flex-column nav-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
			          <a class="nav-link" id="v-pills-home-tab" data-toggle="tab" href="#calendar-tab" role="tab" aria-controls="v-pills-home" aria-selected="false">Calendar</a>
			          <?php foreach ($terms as $key => $term_meta): ?>
			          	<a class="nav-link<?php echo ($term_meta['id'] == $current_term) ? ' active show' : ''; ?>" id="v-pills-profile-tab" data-toggle="tab" href="#term<?php echo $term_meta['id'] ?>" role="tab" aria-controls="v-pills-profile" aria-selected="true"><?php echo $term_meta['term_name']; ?></a>
			          <?php endforeach ?>
			          <?php if (current_user_can("add_new_term")): ?>
			          	<a class="nav-link btn-primary text-center popmeup" data-width=500 data-height=700 href="add-new-term?session=<?php echo $_GET['id']; ?>"><i class="fas fa-plus"></i>&nbsp;Add New Term</a>
			          <?php endif ?>
			      </div>
			      <hr>
			    <?php 
				if(!promotion_done_in_session($_GET['id']) && get_option("current_academic_session") == $_GET['id']) {
					do_action('promote_students_term', ['acsession' => $_GET['id'], 'session_meta' => $session_meta, 'current_term' => $current_term]); 
				}
				?>

			    </div>
			</div>
		</div>
		<div class="col-md-8">
			<div class="tab-content pl-3 p-1" id="navContent">
                <div class="tab-pane fade" id="calendar-tab" role="tabpanel" aria-labelledby="home-tab">
                    <div class="card">
                    	<div class="card-body">
                    		<?php do_action('get_calendar'); ?>
                    	</div>
					</div>
                </div>
                <?php foreach ($terms as $key => $term_meta): 
                	$result = $post->get_post_by_meta_data(['type' => 'compiled_result', 'student_id' => $session->id, 'term' => $term_meta['id'], 'session' => $term_meta['acsession']]);
                	$has_result = false;
                	if(_count($result) > 0) {
                		$has_result = true;
                		$result = array_shift($result);
                	}
                	?>
                <div class="tab-pane fade<?php echo ($term_meta['id'] == $current_term) ? ' active show' : ''; ?>" id="term<?php echo $term_meta['id'] ?>" role="tabpanel" aria-labelledby="home-tab">
                    <div class="card">
                    	<div class="card-header">
                    		<h4><?php echo $term_meta['term_name']; ?></h4>
                    		<?php if ($term_meta['id'] == $current_term && current_user_can("maintain_class_list")): ?>
                    			<button id="maintainclasslist" data-session="<?php echo $term_meta['acsession'] ?>" data-term="<?php echo $term_meta['id'] ?>" class="btn btn-primary pull-right">Auto Maintain Class List</button>
                    		<?php endif ?>
                    	</div>
						<div class="card-body">
							<table class="table table-bordered">
								<tr>
									<td class="pt-1 pb-0">Term Name</td>
									<td class="pt-1 pb-0"><?php echo $term_meta['term_name'] ?></td>
								</tr>
								<tr>
									<td class="pt-1 pb-0">Status</td>
									<td class="pt-1 pb-0"><?php echo $current_term == $term_meta['id'] ? "<strong class='green'>In Session</span>" : "<strong class='red'>Not In Session</span>" ?></td>
								</tr>
								<tr>
									<td class="pt-1 pb-0">Academic Session</td>
									<td class="pt-1 pb-0"><?php echo get_session_name($term_meta['acsession']) ?></td>
								</tr>
								<tr>
									<td class="pt-1 pb-0">Starting Date</td>
									<td class="pt-1 pb-0"><?php echo ($term_meta['term_begins'] != 0) ? date(_DATE_FORMAT_, $term_meta['term_begins']) : date(_DATE_FORMAT_, $term_meta['posted_on']); ?></td>
								</tr>
								<tr>
									<td class="pt-1 pb-0">Closing Date</td>
									<td class="pt-1 pb-0"><?php echo ($term_meta['term_ends'] != 0) ? date(_DATE_FORMAT_, $term_meta['term_ends']) : ''; ?></td>
								</tr>
								<tr>
									<td class="pt-1 pb-0">Start Up Test</td>
									<td class="pt-1 pb-0"><span class="badge badge-primary"><?php echo ($term_meta['startup_test_starts'] != 0) ? date(_DATE_FORMAT_, $term_meta['startup_test_starts']) : ''; ?></span> to <span class="badge badge-primary"><?php echo ($term_meta['startup_test_ends'] != 0) ? date(_DATE_FORMAT_, $term_meta['startup_test_ends']) : ''; ?></span></td>
								</tr>
								<tr>
									<td class="pt-1 pb-0">Midterm Test</td>
									<td class="pt-1 pb-0"><span class="badge badge-primary"><?php echo ($term_meta['midterm_test_starts'] != 0) ? date(_DATE_FORMAT_, $term_meta['midterm_test_starts']) : ''; ?></span> to <span class="badge badge-primary"><?php echo ($term_meta['midterm_test_ends'] != 0) ? date(_DATE_FORMAT_, $term_meta['midterm_test_ends']) : ''; ?></span></td>
								</tr>
								<tr>
									<td class="pt-1 pb-0">Midterm Break</td>
									<td class="pt-1 pb-0"><span class="badge badge-primary"><?php echo ($term_meta['midterm_break_starts'] != 0) ? date(_DATE_FORMAT_, $term_meta['midterm_break_starts']) : ''; ?></span> to <span class="badge badge-primary"><?php echo ($term_meta['midterm_break_ends'] != 0) ? date(_DATE_FORMAT_, $term_meta['midterm_break_ends']) : ''; ?></span></td>
								</tr>
								<tr>
									<td class="pt-1 pb-0">Next Term Begins</td>
									<td class="pt-1 pb-0"><?php echo ($term_meta['next_term_begins'] != 0) ? date(_DATE_FORMAT_, $term_meta['next_term_begins']) : ''; ?></td>
								</tr>
								<tr>
									<td class="pt-2 pb-0" colspan="2">
										<?php if (current_user_can('student') && $has_result): ?>
											<a href="result-sheet?id=<?=$result['id'] ?>&token=<?php echo $token; ?>" class="btn-info btn btn-block">View My Result</a>
										<?php endif ?>
										<?php if (current_user_can('admin')): ?>
											<a href="academic-term?id=<?php echo base64_encode($term_meta['id']) ?>&token=<?php echo $token; ?>" class="btn btn-sm btn-primary pull-right popmeup" data-width=500 data-height=700>Edit Term Calendar</a>
										<?php endif ?>
										<?php if (current_user_can('super_admin') && $term_meta['id'] != $current_term): ?>
											<a data-title="Are you sure?" data-content="You are about to make <?php echo $term_meta['term_name'].' '.$session_meta['session_name']; ?> the current academic term. Kindly confirm that this is the actual term in session." href="?action=<?php echo base64_encode('make_term_current'); ?>&id=<?php echo $_GET['id'] ?>&term=<?php echo $term_meta['id']; ?>" class="btn btn-sm btn-success pull-left confirmThis">Activate Term As Current</a>
										<?php endif ?>
									</td>
								</tr>
								
							</table>
							
							<?php do_action('get_results_button_set', ['term_meta' => $term_meta, 'container-class' => 'col-lg-4', 'token' => $token]); ?>
							
						</div>
					</div>
                </div>
                <?php endforeach ?>
            </div>
			
		</div>
		<div class="col-md-12">
			<?php do_action('students_not_promoted', ['session' => $_GET['id']]); ?>
		</div>
	</div>
	<?php
	add_page_javascript('app/assets/js/page-scripts/js-functions.js');
	add_page_javascript('app/assets/js/page-scripts/confirm.js');
	add_page_javascript('contents/plugins/eedu_portal/js/update-students-in-class.js');
	add_page_javascript('contents/plugins/eedu_portal/js/maintainclasslist.js');
}