<?php 

function admission_settings() {
	$classes = get_select_options_from_post(['type' => 'class'], 'class_name');
  $cut_off = ($option = get_option('ent_exam_cut_off')) ? $option : 150;
	label("Admission Module Settings");
	select(['label' => 'Classes of Admission', 'id' => 'classes_of_admission', 'name' => 'classes_of_admission[]', 'multiple' => 'multiple'], $classes, get_option('classes_of_admission'));
  input(['label' => 'Cut Off Mark', 'id' => 'ent_exam_cut_off', 'class' => 'form-control numbers-only', 'value' => $cut_off]);
}

function save_admission_settings($args) {
	update_option('classes_of_admission', $args['classes_of_admission']);
  update_option('ent_exam_cut_off', $args['ent_exam_cut_off']);
}

function ent_exam_filter() {
	global $post_meta;
  $all_class = select_options_from_classes_of_admission();


  $selOptions = array();

  if(isset($_POST['adm_class'])) {
    $selOptions = get_entrance_exam_subjects($_POST['adm_class']);
  }

  $adm_class = isset($_POST['adm_class']) ? $_POST['adm_class'] : $_GET['adm_class'];
  $adm_term = isset($_POST['adm_term']) ? $_POST['adm_term'] : $_GET['adm_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-6">
          <?php select(['label' => 'Class of Admission', 'id' => 'adm_class', 'data-form' => 'search-form', 'required' => 'required'], $all_class, $adm_class); ?>
        </div>
		    <div class="col-md-6">
          <?php select(['label' => 'Term/Session of Admission', 'id' => 'adm_term', 'data-form' => 'search-form', 'required' => 'required'], $all_terms, $adm_term); ?>
        </div>
      </div> 
      </form> 
    </div>
  </div>
  <?php
  add_page_javascript('contents/plugins/admission/js/ent-exam-filter.js');
}

function candidate_filter() {
  global $post_meta;
  $all_class = select_options_from_classes_of_admission();


  $selOptions = array();

  if(isset($_POST['adm_class'])) {
    $selOptions = get_entrance_exam_subjects($_POST['adm_class']);
  }
  $all_grp = ['All Candidates' => 'all_candidates', 'Waiting List' => 'waiting_list', 'Passed Candidates' => 'passed_candidates', 'Failed Candidates' => 'failed_candidates', 'No Record Candidates' => 'no_record_candidates'];
  $_GET['grp'] = !isset($_GET['grp']) ? 'all_candidates' : $_GET['grp'];
  $grp = isset($_POST['grp']) ? $_POST['grp'] : $_GET['grp'];
  $adm_class = isset($_POST['adm_class']) ? $_POST['adm_class'] : $_GET['adm_class'];
  $adm_term = isset($_POST['adm_term']) ? $_POST['adm_term'] : $_GET['adm_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-4">
          <?php select(['label' => 'Group', 'id' => 'grp', 'data-form' => 'search-form', 'required' => 'required'], $all_grp, $grp); ?>
        </div>
        <div class="col-md-4">
          <?php select(['label' => 'Class of Admission', 'id' => 'adm_class', 'data-form' => 'search-form', 'required' => 'required'], $all_class, $adm_class); ?>
        </div>
        <div class="col-md-4">
          <?php select(['label' => 'Term/Session of Admission', 'id' => 'adm_term', 'data-form' => 'search-form', 'required' => 'required'], $all_terms, $adm_term); ?>
        </div>
      </div> 
      </form> 
    </div>
  </div>
  <?php
  add_page_javascript('contents/plugins/admission/js/candidate-filter.js');
}

function set_registration_fee() {
  $all_class = select_options_from_classes_of_admission();
  $all_terms = get_select_options_from_post_q(['type' => 'academic_term'], 'term_name');
  $fee_categories = get_select_options_from_post(['type' => 'sfm_description', 'status' => 'active'], 'description');
  $gender = ['Both' => 'Both', 'Male' => display_gender('Male'), 'Female' => display_gender('Female')]
  ?>
  <div class="col-12">
    <div class="card card-primary">
      <div class="card-header">
        <strong class="card-title">Set Registration Fee Amount</strong>
      </div>
      <div class="card-body">
        <!-- <div class="col-md-12">
          <p class="alert-info pl-3 pr-3 mb-4 pt-3 pb-3 text-justified">
            Note that this fee is only applied to students registered within the specified term. If all the students in the class are new, you can include the whole registration fee and the school fees, but if some students in the class are not new, you should only apply the difference which is the registration fee for freshers.
          </p>
        </div> -->
        <form method="post" action="contents/plugins/admission/ajax.php" id="set_registration_fee_amount_form">
          <div class="col-md-4">
            <?php select(['label' => 'Class of Admission', 'id' => 'adm_class', 'data-form' => 'search-form', 'required' => 'required'], $all_class);
                  select(['label' => 'Term/Session of Admission', 'id' => 'adm_term', 'data-form' => 'search-form', 'required' => 'required'], $all_terms);
             ?>
          </div>
          <div class="col-md-4">
            <?php select(['label' => 'Gender', 'id' => 'gender', 'data-form' => 'search-form', 'required' => 'required'], $gender, 'Both'); ?>
            <div class="form-group">
              <label>Amount</label>
              <div class="input-group">
                <input type="hidden" name="action" value="set_registration_fee">
                <input type="text" id="amount" name="amount" class="form-control numbers-only" required>
                <button id="submit-acc-fee" class="submit btn btn-primary">Save</button>
              </div>
            </div>
          </div>
          <div class="col-md-4">
            <?php select(['label' => 'Select Category', 'id' => "cat", 'data-form' => 'search-form', 'required' => 'required'], $fee_categories); ?>
          </div>
          <div class="col-md-12"><?php alert(); ?></div>
        </form>
      </div>
    </div>
  </div>
  <?php
  global $post_meta;
  add_page_javascript('app/assets/js/page-scripts/koweb.js');
  add_page_javascript('app/assets/js/page-scripts/submit.js');
  add_page_javascript('contents/plugins/admission/js/registration-fee.js');
}

function display_offer_admission_form($args) {
  if(!current_user_can("offer_admission")) return;
  $all_terms = analyze_terms();
  $all_terms = array_merge($all_terms['upcoming'], $all_terms['current']);
  if(!isset($args['all_class'])) {
    $all_class = select_options_from_classes_of_admission();
  } else $all_class = $args['all_class'];
  ?>
  <div id="dialog-form" title="Offer Admission Slip" style="display: none;">
    <div>
      Name: <span id="studentName"></span></br>
    </div>
   
    <form id="offer_admission_form" method="post">
      <fieldset>
        <?php select(['label' => 'Resumption Term', 'id' => 'term_of_admission', 'required' => 'required'], $all_terms, $args['adm_term']);
        select(['label' => 'Class of Admission', 'id' => 'class_of_admission', 'required' => 'required'], $all_class, $args['adm_class']); ?>
      <input type="hidden" name="student_id" id="student_id" value="" class="">
      <input type="hidden" name="gender" id="gender" value="" class="">
      <input type="hidden" name="action" id="action" value="offer_admission">
        <!-- Allow form submission with keyboard without duplicating the dialog button -->
        <div id="alert" class="alert alert-success" role="alert"><span></span></div>
        <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
      </fieldset>
    </form>
  </div>
  <?php
  global $post_meta;
  add_page_javascript('app/assets/js/page-scripts/koweb.js');
  add_page_javascript('contents/plugins/admission/js/offer-admission.js');
}