<?php

function ext_exam_settings_field() {
  $external_examinations = ($option = get_option('external_examinations')) ? implode(',', $option) : 'Common Entrance Examination, Junior WAEC';
  textarea(['label' => 'External Examinations (seperate with commas)', 'id' => 'external_examinations', 'value' => $external_examinations, 'placeholder' => 'Common Entrance Examination, Junior WAEC']);
}

function ext_exam_save_settings_field($args) {
  update_option('external_examinations', explode(',', $args['external_examinations']));
}

function extexam_mock_exam_broadsheet_filter($args = array()) {
  $option = get_option('external_examinations');
  if(!$option || _count($option) < 1) {
    echo "No Exam to query";
    return;
  }
  $default = [
    "ele-class" => "col-md-3",
    "id" => "ext_exam",
    "name" => "ext_exam",
    "label" => "Examination",
    "cont-id" => "exam-".rand(111, 999)
  ];
  $args = array_merge($default, $args);
  $option = trim_array($option);
  $ext_exam = [];
  if(_count($option)) : foreach ($option as $key => $ext_exam_name) :
    $ext_exam[$ext_exam_name] = $ext_exam_name;
  endforeach; endif;
  ?>
  <div class="<?php echo $args['ele-class'] ?>" id="<?php echo $args['cont-id'] ?>">
      <?php select(['label' => $args['label'], 'id' => $args['id'], 'name' => $args['name']], $ext_exam); ?>
    </div>
  <?php
}

function display_ext_result_filter() {
	global $post_meta;
	$all_class = ['All' => 'all'];
	$all_class = array_merge($all_class, get_select_options_from_post(['type' => 'class'], 'class_name'));
	$all_category = get_select_options_from_post(['type' => 'category'], 'category_name');
	$option = get_option('external_examinations');
    $option = trim_array($option);
    $ext_exam = [];
    if(_count($option)) : foreach ($option as $key => $ext_exam_name) :
    	$ext_exam[$ext_exam_name] = $ext_exam_name;
    endforeach; endif;

  ?>
  <div class="panel panel-custom bg-white">
    <div class="panel-heading">
      <h1>Search</h1>
    </div>
    <div class="panel-body instruction">
      <form id="search-form" method="post">
      <div class="row">
      	<div class="col-md-4">
          <?php select(['label' => 'Select External Exam', 'id' => 'ext_exam', 'required' => 'required'], $ext_exam, $_POST['ext_exam']); ?>
        </div>
        <div class="col-md-4">
          <?php select(['label' => 'Class', 'id' => 'class', 'data-form' => 'search-form', 'required' => 'required'], $all_class, $_POST['class']); ?>
        </div>
        <div class="col-md-4">
          <?php select(['label' => 'Category', 'id' => 'category', 'data-form' => 'search-form', 'required' => 'required'], $all_category, $_POST['category']); ?>
        </div>
      </div> 
      </form> 
    </div>
  </div>
  <?php
  	$post_meta['plugin_name'] = external_examination::$plugin_name;
	add_page_javascript('contents/plugins/external_examination/js/q.js');
}

function display_mock_result_filter2() {
  global $post_meta;
  $all_class = get_select_options_from_post(['type' => 'class'], 'class_name');
  $all_category = ['All' => 'all'];
  $all_category = array_merge($all_category, get_select_options_from_post(['type' => 'category'], 'category_name'));
  $option = get_option('external_examinations');
  $option = trim_array($option);
  $ext_exam = [];
  if(_count($option)) : foreach ($option as $key => $ext_exam_name) :
    $ext_exam[$ext_exam_name] = $ext_exam_name;
  endforeach; endif;

  $term = (isset($_POST['term'])) ? $_POST['term'] : get_option('current_academic_term');
  $all_terms = get_select_options_from_post_q(['type' => 'academic_term'], 'term_name');


  ?>
  <div class="panel panel-custom bg-white">
    <div class="panel-heading">
      <h1>Search</h1>
    </div>
    <div class="panel-body instruction">
      <form id="search-form" method="post">
      <div class="row">
        <div class="col-md-3">
          <?php select(['label' => 'Select External Exam', 'id' => 'ext_exam', 'data-form' => 'search-form', 'required' => 'required'], $ext_exam, $_POST['ext_exam']); ?>
        </div>
        <div class="col-md-3">
          <?php select(['label' => 'Class', 'id' => 'class', 'data-form' => 'search-form', 'required' => 'required'], $all_class, $_POST['class']); ?>
        </div>
        <div class="col-md-3">
          <?php select(['label' => 'Category', 'id' => 'category', 'data-form' => 'search-form', 'required' => 'required'], $all_category, $_POST['category']); ?>
        </div>
        <div class="col-md-3">
          <?php select(['label' => 'Term/Session', 'id' => 'term', 'data-form' => 'search-form', 'required' => 'required'], $all_terms, $term); ?>
        </div>
      </div> 
      </form> 
    </div>
  </div>
  <?php
  $post_meta['plugin_name'] = external_examination::$plugin_name;
  add_page_javascript('contents/plugins/external_examination/js/mock-filter2.js');
}

function display_mock_result_filter() {
  global $post_meta;
  $all_class = get_select_options_from_post(['type' => 'class'], 'class_name');
  $all_category = get_select_options_from_post(['type' => 'category'], 'category_name');
  $option = get_option('external_examinations');
  $option = trim_array($option);
  $term = (isset($_POST['term'])) ? $_POST['term'] : get_option('current_academic_term');
  $all_terms = get_select_options_from_post_q(['type' => 'academic_term'], 'term_name');
  $ext_exam = [];
  if(_count($option)) : foreach ($option as $key => $ext_exam_name) :
    $ext_exam[$ext_exam_name] = $ext_exam_name;
  endforeach; endif;

  $selOptions = array();

  if(isset($_POST['class']) && isset($_POST['category'])) {
    $options = get_class_courses($_POST['class'], $_POST['category']);
    if(_count($options)) {
      foreach ($options as $course_id => $course_name) {
        $selOptions[$course_name] = $course_id;
      }
    }
  }


  ?>
  <div class="panel panel-custom bg-white">
    <div class="panel-heading">
      <h1>Search</h1>
    </div>
    <div class="panel-body instruction">
      <form id="search-form" method="post">
      <div class="row">
        <div class="col-md-3">
          <?php select(['label' => 'Select External Exam', 'id' => 'ext_exam', 'data-form' => 'search-form', 'required' => 'required'], $ext_exam, $_POST['ext_exam']); ?>
        </div>
        <div class="col-md-3">
          <?php select(['label' => 'Class', 'id' => 'class', 'data-form' => 'search-form', 'required' => 'required'], $all_class, $_POST['class']); ?>
        </div>
        <div class="col-md-3">
          <?php select(['label' => 'Category', 'id' => 'category', 'data-form' => 'search-form', 'required' => 'required'], $all_category, $_POST['category']); ?>
        </div>
        <div class="col-md-3">
          <?php select(['label' => 'Subject/Course', 'id' => 'course', 'data-form' => 'search-form', 'required' => 'required'], $selOptions , $_POST['course']); ?>
        </div>
        <div class="col-md-3">
          <?php select(['label' => 'Term/Session', 'id' => 'term', 'data-form' => 'search-form', 'required' => 'required'], $all_terms, $term); ?>
        </div>
      </div> 
      </form> 
    </div>
  </div>
  <?php
    $post_meta['plugin_name'] = external_examination::$plugin_name;
  add_page_javascript('contents/plugins/external_examination/js/mock-filter.js');
}

function mock_result_card($args) {
  $post = new post;
  $grading = new grading;
  $courses = _unserialize($args['courses']);
  ?>
  <table class="table table-bordered text-center table-tiny-padding">
    <thead>
      <tr>
        <th colspan="2"></th>
        <th class="rotate90"><span>Examination</span></th>
        <th class="rotate90"><span>Grade</span></th>
        <th>Teacher's Remarks</th>
      </tr>
      <tr>
        <td colspan="2">Marks Obtainable</td>
        <td>100%</td>
        <td></td>
        <td></td>
      </tr>
    </thead>
    <tbody>
      <?php $i = 1; if(_count($courses)) : foreach ($courses as $key => $value): 
      ?>
      <tr>
        <td class="pt-0 pb-0"><?php echo $i; ?></td>
        <td class="text-left pt-0 pb-0"><?php echo $value['course_name'] ?></td>
        <td class="pt-0 pb-0"><?php echo ($value['score'] == 0) ? "-" : $value['score']; ?></td>
        <td class="pt-0 pb-0 <?php echo strtolower($grading->get_grade($value['score'])['grade']) == 'f' ? 'error' : 'green'; ?>"><?php echo $grading->get_grade($value['score'])['grade']; ?></td>
        <td class="pt-0 pb-0 text-left <?php echo strtolower($grading->get_grade($value['score'])['grade']) == 'f' ? 'error' : 'green'; ?>"><?php echo $grading->get_grade($value['score'])['description']; ?></td>
      </tr>       
      <?php $i++; endforeach; endif; ?>
    </tbody>
  </table>
  <?php
}

function ext_exam_more_teacher_set_button($args) {
  ?>
  <div class="<?php echo $args['container-class'] ?>">
    <div class="koweb-card card card-yellow text-center helper_step2">
      <div class="card-block">
        <h4 class="card-title">
          <i class="fa fa-certificate text-white" aria-hidden="true"></i>
        </h4>
        <p class="text-white">Mock Results</p>
      </div>
      <a class="card-footer text-white" href="compiled-mock-results">
        Open
      </a>
    </div>
  </div>
  <?php
}

// deprecating
function compile_mock_result_button($args) {
  $session = Session::getInstance();
  if (current_user_can('teacher_only') && get_form_teacher($args['class'], $args['category']) == $session->id || current_user_can('admin')):
    ?>
    <form method="post" action="<?php echo $args['url']; ?>" id="compile-result-form" data-submittingmsg="Preparing Results for Compilation. Please wait...">
      <?php 
      input(['label' => '', 'id' => 'session', 'type' => 'hidden', 'value' => $args['session']]);
      input(['label' => '', 'id' => 'term', 'type' => 'hidden', 'value' => $args['term']]);
      input(['label' => '', 'id' => 'class', 'type' => 'hidden', 'value' => $args['class']]);
      input(['label' => '', 'id' => 'category', 'type' => 'hidden', 'value' => $args['category']]);
      if(isset($args['ext_exam'])) {
        input(['label' => '', 'id' => 'ext_exam', 'type' => 'hidden', 'value' => $args['ext_exam']]);
      }
      alert(); submit_btn("compile_result", '<i class="fa fa-dot-circle-o"></i> Compile Result');
       ?>
    </form>
    <?php
  endif;
  global $post_meta;
  add_page_javascript('contents/plugins/external_examination/js/compile-result.js');
}