<?php 

function result_sheet_cb() {
      add_page_stylesheet("contents/plugins/eedu_portal/css/broadsheet.css", "eedu_broadsheetCss");
      add_page_stylesheet("contents/plugins/eedu_portal/css/result.css", "eedu_resultCss");
      $user = new user;
      $post = new post;

      if(!isset($_GET['id']))
            return_method();
      $result_meta = $post->get_post_meta($_GET['id']);

      if(!current_user_can("access_others_result_sheet")) {
        $session = Session::getInstance();
        if($result_meta['student_id'] !== $session->id) {
          return_method();
        }
      }
      $student_meta = $user->get_user_meta($result_meta['student_id']);
      $term_meta = $post->get_post_meta($result_meta['term']);
      // try {
      //   $locale = 'en_US';
      //   $nf = new NumberFormatter($locale, NumberFormatter::ORDINAL);
      //   $position = $nf->format($result_meta['position']);
      // } catch(Exception $e) {

      // } finally {
        $position = ordinal_suffix_of($result_meta['position']);
      // }
      if(isset($result_meta['attendance'])) {
            $attendance = _unserialize($result_meta['attendance']);
      }
      if ($_GET['media'] ?? '' !== "print"): ?>
        <div class="btn-group">
          <a href="result-sheet?id=<?php echo $result_meta['id'] ?>&media=print" class="btn btn-warning popmeup" data-width=1270 data-height=800>Print</a>
          <?php if ($term_meta['order'] == 3): ?>
            <a href="master-result?class=<?php echo $result_meta['class'] ?>&id=<?php echo $result_meta['student_id'] ?>&session=<?php echo $result_meta['session'] ?>" class="btn btn-primary">View Annual Result</a>
          <?php endif ?>
        </div>
      <?php endif ?>
      <div id="print" class="col-md-12">
            <div class="result">
            <div class="result-body pr-4 pl-4">
               <div class="table-responsive">
                  <table class="table page-break-avoid">
                        <tr>
                              <td style="border-top: none;">
                  <!-- <div class="row"> -->
                        <div class="col-md-12">
                              <?php echo eedu_portal_default_paperhead(['title-h' => 'h2']); ?>
                        </div>
                  <!-- </div> -->
                  <!-- <div class="row"> -->
                        <div class="col-md-12">
                          <table class="table no-border">
                            <tr>
                              <td colspan="2" class="pb-0"><div class="label mt-0 pt-0">Particulars</div></td> 
                              </tr>
                            <tr>
                              <td class="pt-0 pl-0 pr-0"><table class="table">
                                <tr>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Name</span><span><?php echo get_fullname($student_meta, true); ?></span></div></td>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Dob</span><span><?php echo $student_meta['dob']; ?></span></div></td>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Class</span><span><?php echo get_class_name($result_meta['class'], $result_meta['category']); ?></span></div></td>
                                </tr>
                                <tr>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Student ID</span><span><?php echo $student_meta['username']; ?></span></div></td>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Academic Session</span><span><?php echo get_session_name($result_meta['session']); ?></span></div></td>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Term</span><span><?php echo get_term_name($result_meta['term']); ?></span></div></td>
                                </tr>
                                <tr>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Number of students in class</span><span><?php echo $result_meta['std_count']; ?></span></div></td>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Position in class</span><span><?php echo $position." out of ".$result_meta['std_count']; ?></span></div></td>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Courses offered</span><span><?php echo $result_meta['course_count']; ?></span></div></td>
                                </tr>
                                <tr>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Total Marks Obtainable</span><span><?php echo $result_meta['tm_obtainable']; ?></span></div></td>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Total Marks Obtained</span><span><?php echo $result_meta['marks_obtained']; ?></span></div></td>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Term Started</span><span><?php echo isset($term_meta['term_begins']) ? date(_DATE_FORMAT_, $term_meta['term_begins']) : date(_DATE_FORMAT_, $term_meta['posted_on']); ?></span></div></td>
                                </tr>
                                <tr>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Term Ending</span><span><?php echo isset($term_meta['term_ends']) ? date(_DATE_FORMAT_, $term_meta['term_ends']) : ''; ?></span>
                                    </div></td>
                                  <td class="pr-0 pl-0 pt-0 pb-0"><div class="particular"><span>Next Term Begins</span><span><?php echo isset($term_meta['next_term_begins']) ? date(_DATE_FORMAT_, $term_meta['next_term_begins']) : ''; ?></span>
                                    </div>
                                    </td>
                                </tr>
                                <?php do_action('result-sheet-particulars-extra', $result_meta); ?>
                                </table>
                              </td>
                              <td class="pt-0 pl-0 pr-0"><div>
                                <div class="img-container"> <img src="<?php echo $result_meta['picture']; ?>" class="img img-responsive"> </div>
                                </div></td>
                            </tr>
                          </table>
                        </div>
                  <!-- </div> -->


                  <!-- <div class="row"> -->
                        <table class="table no-border">
                          <tr>
                            <td>
                              <div class="row">
                                    <div class="col-md-12">
                                          <div class="label mt-0 pt-0">Cognitive Record</div>
                                          <?php do_action('get_result_card', $result_meta); ?> 
                                          
                                          <?php if (get_option('include_attendance_report_in_result')): ?>

                                                <div class="label mt-0 pt-0">Attendance Report</div> <br>
                                                <div class="col-md-6 clear-padding-left clear-padding-right"><div class="particular"><span>Average Attendance</span><span><?php printf("%1\$.0f%%", $attendance['average']) ?></span></div></div>
                                                <div class="col-md-6 clear-padding-left clear-padding-right"><div class="particular"><span>Days Spent</span><span><?php echo $attendance['_past_dates'] ?></span></div></div>
                                                <div class="col-md-4 clear-padding-left clear-padding-right"><div class="particular"><span>Days Present</span><span><?php echo $attendance['_days_present'] ?></span></div></div>
                                                <div class="col-md-4 clear-padding-left clear-padding-right"><div class="particular"><span>Days Absent</span><span><?php echo $attendance['_days_absent'] ?></span></div></div>
                                                <div class="col-md-4 clear-padding-left clear-padding-right"><div class="particular"><span>Days Not Marked</span><span><?php echo $attendance['_dates_not_reported'] ?></span></div></div> 
                                          <?php endif ?>
                                          
                                          <br><div class="label mt-0 pt-0">Comments</div> <br>
                                          <div class="col-md-12 clear-padding-left clear-padding-right"><div class="particular">
                                            <span><?php echo get_option('pref_name_for_form_teacher'); ?>'s Comment:</span>
                                            <span><?php echo $result_meta['ft_comment']; ?></span></div></div>
                                          <?php do_action('result-sheet-comments-extra', $result_meta); ?>
                                    </div> 
                              </div>
<?php 
$principal_meta = $user->get_user_meta($result_meta['principal']);
$form_teacher_meta = $user->get_user_meta($result_meta['form_teacher']);
 ?>
                              <div class="row">
                                    <div class="col-md-12">
                                          <div class="label mt-0 pt-0">Endorsments</div> <br>
                                          <table class="table text-center">
                                                <tr>
                                                  <td><div class="result-signature signature"><img src="<?php echo $form_teacher_meta['signature']; ?>"></div>
                                                  <div class="result-signature name text-center"><?php echo get_fullname($form_teacher_meta); ?></div> <div class="result-signature designation text-center"><small><?php echo get_option('pref_name_for_form_teacher') ?></small></div> </td>
                                                  <td><div class="result-signature signature"><img src="<?php echo $principal_meta['signature']; ?>"></div>
                                                  <div class="result-signature name text-center"><?php echo get_fullname($principal_meta); ?></div> <div class="result-signature designation text-center"><small><?php echo get_option('pref_name_for_principal') ?></small></div> </td>
                                                </tr>
                                          </table>
                                    </div>
                              </div>
                              </td>
                              <td>
                              <div class="label mt-0 pt-0">Psychometrics Record</div>
                              <?php do_action('get_psychometrics', ['student_id' => $result_meta['student_id'], 'result_meta' => $result_meta]); ?>
                              </td>
                           </tr>
                        </table>
                  <!-- </div> -->
                  <!-- <div class="row"> -->
                        <div class="col-md-12">
                              <?php do_action('result-sheet-footer-extra', $result_meta); ?>
                        </div>
                  <!-- </div> -->
                        
                              </td>
                        </tr>
                  </table>
               </div>
            </div>
        </div>
      </div>
      <div class="page-break-before"></div>
      <?php
      global $post_meta;
      add_page_javascript('app/assets/js/page-scripts/js-functions.js');
}