<?php
session_start();
if(isset($_SESSION['teacher'])){
$username = $_SESSION['teacher'];
 ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?php
    include '../admin/includes/styles.php';
    ?>  <title> Create Exam |  <?php echo $school_name; ?> </title>
    <?php
    $sql = "SELECT * FROM teachers WHERE username =:username";
    $conn->query($sql);
    $conn->bind(":username", $username);

    $result = $conn->fetchSingle();
    $teacher_name = $result->name;
    ?>

</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">

    <!-- Navbar -->
    <?php include 'includes/notifications.php';
    ?>
    <!-- /.navbar -->

    <!-- Main Sidebar Container -->
    <?php include "includes/sidebar.php"; ?>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0">Create a new Exam (CBT)</h1>
                    </div><!-- /.col -->
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="<?php echo $conn->teacher_url(); ?>">Home</a></li>
                            <li class="breadcrumb-item active">Create Exam</li>
                        </ol>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content-header -->

        <!-- Main content -->
        <section class="content">
      <div class="container-fluid">
   
        <div class="row">
          <div class="col-md-8">
           <div class="card card-info">
              <div class="card-header">
                <h3 class="card-title">Enter exam details below</h3>
              </div>
              <!-- /.card-header -->
              <!-- form start -->
               <form  method="POST" id="create-exam">
                <div class="card-body">

                    <div class="form-group row">
                        <label for="title" class="col-sm-3 col-form-label">Exam Title <span style="color: #f00;">*</span></label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="title" required data-parsley-trigger="keyup" name="title">
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="db_group" class="col-sm-3 col-form-label">Class <span
                                    style="color: #f00;">*</span></label>
                        <div class="col-sm-9">
                            <?php
                            //fetch all classes
                            $sql = "SELECT class FROM sections WHERE teacher_name =:teacher_name";
                            $conn->query($sql);
                            $conn->bind(":teacher_name", $teacher_name);
                            $class = $conn->fetchColumn();

                            ?>
                            <select name="class_id" id="classID" class="form-control select2" required>
                                <option value="">Select class</option>
                                <?php
                                if ($conn->rowCount() > 0) {
                                    $conn->query("SELECT name FROM classes WHERE id = '$class'"); 
                                    $class_name = $conn->fetchColumn();
                                    echo "<option value='$class'>$class_name</option>"; 
                                   
                                } else {
                                    ?>
                                    <option value="">No classes found</option><?php
                                }
                                ?>
                            </select>
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="section_id" class="col-sm-3 col-form-label">Section <span
                                    style="color: #f00;">*</span></label>
                        <div class="col-sm-9">
                            <select name="section_id" id="section" class="form-control select2" required>
                                <option value="">Select section</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="subject_id" class="col-sm-3 col-form-label">Subject <span style="color: #f00;">*</span></label>
                        <div class="col-sm-9">
                            <select name="subject_id" id="subject" class="form-control select2" required>
                                <option value="">Select subject</option>

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

                    <div class="form-group row">
                        <label for="term_id" class="col-sm-3 col-form-label">Term <span style="color: #f00;">*</span></label>
                        <div class="col-sm-9">
                        <?php
                        //fetch all classes
                        $sql = "SELECT * FROM terms";
                        $conn->query($sql);
                        $terms = $conn->fetchMultiple();

                        ?>
                        <select name="term_id" id="term_id" class="form-control select2" required>
                            <option value="">Select term</option>
                            <?php
                            if ($conn->rowCount() > 0) {
                                foreach ($terms as $term) {
                                    ?>
                                <option value="<?php echo $term->id ?>">
                                    <?php echo $term->name ?> </option><?php
                                }
                            } else {
                                ?>
                                <option value="">No terms found</option><?php
                            }
                            ?>
                        </select>
                    </div>
                    </div>

                    <div class="form-group row">
                        <label for="academic_session" class="col-sm-3 col-form-label">Academic Session <span style="color: #f00;">*</span></label>
                        <div class="col-sm-9">
                            <?php
                              //fetch active session from general settings
                              $sql = "SELECT current_school_session FROM general_settings";
                              $conn->query($sql);
                              $active_session = $conn->fetchColumn();
                            ?>
                            <input type="text" class="form-control" id="academic_session" required data-parsley-trigger="keyup" name="academic_session" value="<?php echo $active_session;?>">
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="exam_date" class="col-sm-3 col-form-label">Exam Date <span style="color: #f00;">*</span></label>
                        <div class="col-sm-9">
                            <input type="date" class="form-control" id="exam_date" required data-parsley-trigger="keyup" name="exam_date">
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="time_start" class="col-sm-3 col-form-label">Start Time <span style="color: #f00;">*</span></label>
                        <div class="col-sm-9">
                            <input type="time" class="form-control" id="time_start" name="time_start" required>
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="time_end" class="col-sm-3 col-form-label">End Time <span style="color: #f00;">*</span></label>
                        <div class="col-sm-9">
                            <input type="time" class="form-control" id="time_end" name="time_end" required>
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="instruction" class="col-sm-3 col-form-label">Instruction</label>
                        <div class="col-sm-9">
                            <textarea class="form-control" id="instruction" name="instruction"></textarea>
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="minimum_percentage" class="col-sm-3 col-form-label">Minimum Percentage to Pass <span style="color: #f00;">*</span></label>
                        <div class="col-sm-9">
                            <input type="number" class="form-control" id="minimum_percentage" name="minimum_percentage" required>
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="status" class="col-sm-3 col-form-label">Status</label>
                        <div class="col-sm-9">
                            <select class="form-control" id="status" name="status">
                                <option value="draft">Draft</option>
                                <option value="published">Published</option>
                            </select>
                        </div>
                    </div>

                   <input type="submit" class="btn btn-primary" value="Create Exam" id="submit">
               </form>
           </div>

              <div id="success-msg"></div>

            </div>
    <!-- /.card -->
          </div>
        </div>
        <!-- /.row (main row) -->
      </div><!-- /.container-fluid -->
    </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <?php include "../admin/includes/footer.php"; ?>
       
    <script>

//process create exam
$('#create-exam').parsley();
$('#create-exam').on('submit', function (event) {
    event.preventDefault();
    if($('#create-exam').parsley().isValid()){
        $.ajax({
            url: 'exam_process.php',
            method: 'POST',
            data: new FormData(this),
            contentType: false,
            cache: false,
            processData: false,
            beforeSend: function () {
                $('#submit').attr('disabled', 'disabled');
                $('#submit').val('Creating Exam, pls wait.....');
            },
            success: function (data) {
                $('#create-exam').parsley().reset();
                $('#submit').attr('disabled', false);
                $('#submit').val('Submit');
                $('#success-msg').html(data);

                //redirect to exam lists page after 2 seconds
                setTimeout(function () {
                    window.location.href = '<?php echo $conn->teacher_url();?>exam_list';
                }, 2000);
            }
        });
    }

});


$(document).ready(function(){
$('.select2').select2();

})

$(document).ready(function () {
  $('#classID').on('change', function () {
      var classID = $(this).val();
      if (classID) {
          $.ajax({
              type: 'POST',
              url: 'fetch_section.php',
              data: 'classesID=' + classID,//append the classID
              success: function (html) {
                  $('#section').html(html);
              }

          });
      } else {
          $('#section').html('<option value="">select class  first</option>');
      }
  })

  //fetch subjects

  $('#classID').on('change', function () {
      var classID = $(this).val();
      if (classID) {
          $.ajax({
              type: 'POST',
              url: 'fetch_subject.php',
              data: 'classesID=' + classID,//append the classID
              success: function (html) {
                  $('#subject').html(html);
              }

          });
      } else {
          $('#subject').html('<option value="">select class  first</option>');
      }
  })
})

</script>

    <?php
    }else{
      ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no">

        <?php include '../admin/includes/styles.php'; ?>

        <title>Login to continue |  <?php echo $school_name; ?> </title>

        <!-- END PAGE LEVEL PLUGINS/CUSTOM STYLES -->

    </head>
    <body>
    <div class="container" style="margin-top: 20%;">
        <div class="row">
            <div class="col-lg-12 col-md-12">
                <div class="section-title text-center">
                    <h6> You need to login to continue </h6>
                    <div class="spinner-border text-info" role="status">
                        <span class="sr-only">Loading...</span>
                        <meta http-equiv='refresh' content='3; <?php echo $teacher_login_link; ?>'>
                    </div>
                    <p>Redirecting to the login  page...</p>
                </div>
            </div>
        </div>
    </div>

    <?php
    }

  ?>
