<?php 

function cp_manage_quiz_cb() {
	if(!isset($_GET['id'])) return_method();
	$post = new post;
	$quiz_meta = $post->get_post_meta($_GET['id']);
	$course_name = get_course_name($quiz_meta['course']);
	$class_name = get_class_name($quiz_meta['class']);
	$term_name = get_term_name($quiz_meta['term']);
	// var_dump($quiz_meta);
	?>
	<div class="nav-wrapper">
	    <ul class="nav nav-pills nav-fill flex-column flex-md-row" id="tabs-icons-text" role="tablist">
	    	<li class="nav-item">
	            <a class="nav-link mb-sm-2 mb-md-0" id="tabs-icons-text-0-tab" data-toggle="tab" href="#stream-tab" role="tab" aria-controls="tabs-icons-text-2" aria-selected="false"><i class="fas fa-rss mr-2"></i>Stream</a>
	        </li>
	        <li class="nav-item">
	            <a class="nav-link mb-sm-3 mb-md-0 active" id="tabs-icons-text-1-tab" data-toggle="tab" href="#tabs-icons-text-1" role="tab" aria-controls="tabs-icons-text-1" aria-selected="true"><i class="ni ni-folder-17 mr-2"></i>Quiz Contents</a>
	        </li>
	        <li class="nav-item">
	            <a class="nav-link mb-sm-2 mb-md-0" id="tabs-icons-text-2-tab" data-toggle="tab" href="#comments-tab" role="tab" aria-controls="tabs-icons-text-2" aria-selected="false"><i class="ni ni-chat-round mr-2"></i>Question Bank</a>
	        </li>
	        <li class="nav-item">
	            <a class="nav-link mb-sm-3 mb-md-0" id="tabs-icons-text-3-tab" data-toggle="tab" href="#students-tab" role="tab" aria-controls="tabs-icons-text-3" aria-selected="false"><i class="ni ni-single-02 mr-2"></i>Grading</a>
	        </li>
	    </ul>
	</div>
	<div class="card shadow">
		<div class="card-img-top-container">
			<img class="card-img-top" src="<?php echo cp_get_random_banner(); ?>" alt="Card image cap">
			<div class="card-labels">
				<h3><?php echo $class_name." ".$course_name; ?></h3>
				<h2><?php echo $quiz_meta['test'] ?></h2>
				<p><?php echo $term_name; ?></p>
			</div>
		</div>
		
	    <div class="card-body">
	        <div class="tab-content" id="myTabContent">
	        	<div class="tab-pane fade" id="stream-tab" role="tabpanel" aria-labelledby="tabs-icons-text-2-tab">
	                <?php 
						render_page_content("cp-classwork-stream");
					 ?>
	            </div>
	            <div class="tab-pane fade show active" id="tabs-icons-text-1" role="tabpanel" aria-labelledby="tabs-icons-text-1-tab">
	            	<div class="row">
	            		<div class="col-12">
	            			<div class="cp-combo-box">
	            				<div class="cp-combo-box-header">
	            					
	            				</div>
	            				<div class="cp-combo-box-body">
	            					<div class="row">
	            						<div class="col-md-4">
	            							<span class="stand-out">
	            								<h2 id="mt-4 mb-2 text-center">DETAILS</h2>
	            								<table class="table">
	            									<tr>
	            										<td class="pt-0 pb-0">Date</td>
	            										<td class="pt-0 pb-0"><?php echo date(_DATE_FORMAT_, $quiz_meta['posted_on']) ?></td>
	            									</tr>
	            									<tr>
	            										<td class="pt-0 pb-0">Class</td>
	            										<td class="pt-0 pb-0"><?php echo $class_name ?></td>
	            									</tr>
	            									<tr>
	            										<td class="pt-0 pb-0">Subject</td>
	            										<td class="pt-0 pb-0"><?php echo $course_name ?></td>
	            									</tr>
	            									<tr>
	            										<td class="pt-0 pb-0">Starts on</td>
	            										<td class="pt-0 pb-0"><?php echo date(_DATE_FORMAT_, $quiz_meta['from']) ?></td>
	            									</tr>
	            									<tr>
	            										<td class="pt-0 pb-0">Ends on</td>
	            										<td class="pt-0 pb-0"><?php echo date(_DATE_FORMAT_, $quiz_meta['to']) ?></td>
	            									</tr>
	            									<tr>
	            										<td class="pt-0 pb-0">Time Allowed</td>
	            										<td class="pt-0 pb-0"><?php echo $quiz_meta['time_allowed'] ?> Minutes</td>
	            									</tr>
	            									<tr>
	            										<td class="pt-0 pb-0">Max. Questions</td>
	            										<td class="pt-0 pb-0"><?php echo $quiz_meta['max_questions'] ?></td>
	            									</tr>
	            									<tr>
	            										<td class="pt-0 pb-0">Total Marks</td>
	            										<td class="pt-0 pb-0"><?php echo $quiz_meta['total_marks'] ?>%</td>
	            									</tr>
	            									<tr>
	            										<td class="">Quiz Status</td>
	            										<td class=""><?php echo $quiz_meta['status']; 
	            											if($quiz_meta['status'] == "published") {
	            												?>
	            												&nbsp;<a href="javaScript:void(0);" class="btn btn-sm btn-primary ajax-action" data-action-callback="cp_unpublish_quiz" data-id="<?php echo $quiz_meta['id'] ?>" data-callback="refreshPage">Unpublish</a>
	            												<?php
	            											} else {
	            												?>
	            												&nbsp;<a href="javaScript:void(0);" class="btn btn-sm btn-primary ajax-action" data-action-callback="cp_publish_quiz" data-id="<?php echo $quiz_meta['id'] ?>" data-callback="refreshPage">Publish</a>
	            												<?php
	            											}
	            										?>
	            										</td>
	            									</tr>
	            								</table>
	            								<hr>
	            								<ul class="nav nav-pills justify-content-end">
												    <li class="nav-item dropdown mr-2 mr-md-0">
												      	<a href="javaScript:void(0);" class="nav-link py-2 px-3 active" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
													        <span class="d-none d-md-block"><i class="ni ni-fat-add mr-2"></i>ADD QUESTION</span>
													        <span class="d-md-none"><i class="ni ni-fat-add"></i></span>
												      	</a>
												      	<div class="dropdown-menu  dropdown-menu-right">
													        <a href="cp-add-objective-question?id=<?php echo $quiz_meta['id'] ?>" class="dropdown-item pt-1 pb-1 popmeup" data-width=850 data-height=500>
													          <i class="fas fa-dice"></i>
													          <span>Objective</span>
													        </a>
													        <a href="cp-add-theory-question?id=<?php echo $quiz_meta['id'] ?>" class="dropdown-item pt-1 pb-1 popmeup" data-width=850 data-height=500>
													          <i class="fas fa-feather-alt"></i>
													          <span>Theory</span>
													        </a>
													        <!-- <a href="#comments-tab" data-toggle="tab" class="dropdown-item pt-1 pb-1" data-width=700 data-height=500>
													          <i class="fas fa-link"></i>
													          <span>Question Bank</span>
													        </a> -->
												      	</div>
												    </li>
												</ul>
	            							</span>
	            						</div>
	            						<div class="col-md-8">
	            							<?php 
	            								$post = new post;
												$questions = $post->get_post_by_meta_data(['type' => 'cp_quiz_question', 'quiz' => $quiz_meta['id']]);
												$written = $post->get_post_by_meta_data(["type" => "cp_write_quiz", "quiz" => $quiz_meta['id']]);
	            							 ?>
	            							<div class="row">
									            <div class="col-lg-6">
									              <div class="card card-stats">
									                <!-- Card body -->
									                <div class="card-body">
									                  <div class="row">
									                    <div class="col">
									                      <h5 class="card-title text-uppercase text-muted mb-0">Questions</h5>
									                      <span class="h2 font-weight-bold mb-0"><?php echo ($questions) ? _count($questions) : 0; ?></span>
									                    </div>
									                    <div class="col-auto">
									                      <div class="icon icon-shape bg-gradient-red text-white rounded-circle shadow">
									                        <i class="ni ni-money-coins"></i>
									                      </div>
									                    </div>
									                  </div>
									                  <p class="mt-3 mb-0 text-sm">
									                    <span class="text-success mr-2">Click Here </span>
									                    <span class="text-nowrap">to manage</span>
									                  </p>
									                </div>
									              </div>
									            </div>
									            <div class="col-lg-6">
									              <div class="card card-stats">
									                <!-- Card body -->
									                <div class="card-body">
									                  <div class="row">
									                    <div class="col">
									                      <h5 class="card-title text-uppercase text-muted mb-0">Students Written</h5>
									                      <span class="h2 font-weight-bold mb-0"><?php echo ($written) ? _count($written) : 0; ?></span>
									                    </div>
									                    <div class="col-auto">
									                      <div class="icon icon-shape bg-gradient-orange text-white rounded-circle shadow">
									                        <i class="fa fa-users"></i>
									                      </div>
									                    </div>
									                  </div>
									                  <p class="mt-3 mb-0 text-sm">
									                    <span class="text-success mr-2">Click Here </span>
									                    <span class="text-nowrap">to manage</span>
									                  </p>
									                </div>
									              </div>
									            </div>
									          </div>
									          <?php 
									          	$questions = cp_parse_questions($questions); 
									          	$count = _count($questions);
									          ?>
									          <link rel="stylesheet" type="text/css" href="contents/plugins/classroom_plus/template/assets/css/quiz.css">
									          <div class="row">
									          	<div class="col-md-12">
									          		<div class="privew">
												      <div class="questionsBox">
												        <div id="slider">
												            <div class="slides-container">
												              <?php if (_count($questions)): ?>
												                <?php foreach ($questions as $question): ?>
												                  <?php if ($question['question_meta']['question_type'] == 'objective'): ?>
												                    <div class="slide">
												                      <div class="questions"><?php echo $question['question_meta']['question'] ?></div>
												                        <ul class="answerList">
												                          <?php foreach ($question['question_meta']['option']['optionvalue'] as $key => $value): ?>
												                            <li>
												                                <label>
												                                    <input type="radio" name="objective[<?php echo $question['question_meta']['correct_option'] ?>]" value="<?php echo $key ?>" <?php echo ($question['question_meta']['correct_option'] === $key) ? "checked" : ""; ?>>&nbsp;<?php echo $value ?></label>
												                            </li>
												                          <?php endforeach ?>
												                        </ul>
												                    </div>
												                  <?php endif ?>
												                <?php endforeach ?>
												              <?php endif ?>
												                
												            </div>
												            <div class="questionsRow">
												              <a href="#" class="button prevquestions" id="prevquestions">Back</a> 
												              <a href="#" class="button nextquestions" id="nextquestions">Next</a>

												              <span class="pager">1 of <?php echo $count ?></span>
												            </div>
												            

												        </div>
												          
												      </div>
												    </div>
									          	</div>
									          </div>

	            						</div>
	            					</div>
	            				</div>
	            			</div>
	            			<!-- <div class="cp-combo-box">
	            				<div class="cp-combo-box-header">
	            					<h2>Materials</h2>
	            				</div>
	            				<div class="cp-combo-box-body">
	            					<div id="materials" data-lesson_id="<?php echo $lesson_meta['id'] ?>"></div>
	            				</div>
	            			</div>
	            			<div class="cp-combo-box">
	            				<div class="cp-combo-box-header">
	            					<h2>Assignment</h2>
	            				</div>
	            				<div class="cp-combo-box-body">
	            					<div></div>
	            				</div>
	            			</div> -->
	            		</div>
	            	</div>
	            </div>
	            <div class="tab-pane fade" id="comments-tab" role="tabpanel" aria-labelledby="tabs-icons-text-2-tab">
	                <div id="question-bank" data-class="<?php echo $quiz_meta['class'] ?>" data-course="<?php echo $quiz_meta['course'] ?>" data-quiz="<?php echo $quiz_meta['id'] ?>"></div>
	            </div>
	            <div class="tab-pane fade" id="students-tab" role="tabpanel" aria-labelledby="tabs-icons-text-3-tab">
	            	<div id="grading" data-class="<?php echo $quiz_meta['class'] ?>" data-quiz="<?php echo $quiz_meta['id'] ?>"></div>
	            </div>
	        </div>
	    </div>
	</div>
	<?php
	global $post_meta;
  	add_page_javascript('app/assets/js/page-scripts/js-functions.js');
  	add_page_javascript('app/assets/js/page-scripts/init-datatable.js');
  	add_page_javascript('contents/plugins/classroom_plus/js/manage-quiz.js');
	add_page_javascript('contents/plugins/classroom_plus/js/write-quiz.js');
}