<?php
 session_start();
require_once('operations.php');

$db = new Operations();
$db1 = new Operations();

if (!isset($_SESSION['staffno'])) {
   header( "Location: index.php" );
}

$statement = $db->getAllSchedule();

if(isset($_POST['submit1'])){
	$id = $_POST['id'];
	
	$db->deleteExamSchedule($id);
  $db->deleteQuestionsByExamcode($id);
  ?>
  <script>
alert('Exam Schedule Delete successfully');

window.location.href='allSchedule.php';

</script>
  <?php
}


if(isset($_POST['submit'])){
	$examcode = $_POST['examcode'];
  $status = "";
  
  $db1->findExamsById($examcode);

  $examstatus = $db1->status;

  if($examstatus == "closed") {

    $count = $db->getCountQuestionsByExamID($examcode);
    if($count > 1) {
      $status = "opened";
$db->updateExamStatus($examcode,$status);
  
?>
<script>
alert('exam opened successfully');
window.location.href='allSchedule.php';
</script>
    <?php


    }

    else {


      ?>
<script>
alert('add questions before opening exam');
</script>
      <?php
    }

  }
	else {
    $status = "closed";
    $db->updateExamStatus($examcode,$status);
    ?>
<script>
alert('exam closed successfully');
window.location.href='allSchedule.php';
</script>
    <?php

  }

}
?>
<!--
=========================================================
* Argon Dashboard - v1.2.0
=========================================================
* Product Page: https://www.creative-tim.com/product/argon-dashboard


* Copyright  Creative Tim (http://www.creative-tim.com)
* Coded by www.creative-tim.com



=========================================================
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-->
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="Start your development with a Dashboard for Bootstrap 4.">
  <meta name="author" content="Creative Tim">
  <title>Exam App</title>
  <!-- Favicon 
  <link rel="icon" href="assets/img/brand/favicon.png" type="image/png">
  -->
  <!-- Fonts -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700">
  <!-- Icons -->
  <link rel="stylesheet" href="assets/vendor/nucleo/css/nucleo.css" type="text/css">
  <link rel="stylesheet" href="assets/vendor/@fortawesome/fontawesome-free/css/all.min.css" type="text/css">
  <!-- Page plugins -->
  <!-- Argon CSS -->
  <link rel="stylesheet" href="assets/css/argon.css?v=1.2.0" type="text/css">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css" rel="stylesheet" >
  <link rel="stylesheet" href="https://cdn.datatables.net/1.10.24/css/dataTables.bootstrap4.min.css" type="text/css">
	
	
	

  <style>
  .back-to-top {
    cursor: pointer;
    position: fixed;
    bottom: 20px;
    right: 20px;
    display:none;
}

  </style>
</head>

<body>
  <!-- Sidenav -->
  <nav class="sidenav navbar navbar-vertical  fixed-left  navbar-expand-xs navbar-light bg-white" id="sidenav-main">
    <div class="scrollbar-inner">
      <!-- Brand -->
      <div class="sidenav-header  align-items-center">
        <a class="navbar-brand" href="javascript:void(0)">
          <h4>Exam App</h4>
        </a>
      </div>
      <div class="navbar-inner">
        <!-- Collapse -->
        <div class="collapse navbar-collapse" id="sidenav-collapse-main">
          <!-- Nav items -->
          <?php include('menu.html')  ?>
          <!-- Divider -->
          <hr class="my-3">
          <!-- Heading -->
          
        </div>
      </div>
    </div>
  </nav>
  <!-- Main content -->
  <div class="main-content" id="panel" >
    <!-- Topnav -->
    <nav class="navbar navbar-top navbar-expand navbar-dark bg-primary border-bottom">
        <ul class="navbar-nav align-items-center  ml-md-auto ">
            <li class="nav-item d-xl-none">
              <!-- Sidenav toggler -->
              <div class="pr-3 sidenav-toggler sidenav-toggler-dark" data-action="sidenav-pin" data-target="#sidenav-main">
                <div class="sidenav-toggler-inner">
                  <i class="sidenav-toggler-line"></i>
                  <i class="sidenav-toggler-line"></i>
                  <i class="sidenav-toggler-line"></i>
                </div>
              </div>
</li>
</ul>

        
      <div class="container-fluid" style = "background-color:white">
        
      </div>
    </nav>
    <!-- Header -->
    <!-- Header -->
    <div class="header bg-primary pb-6" 
      <div class="container-fluid" >
        <div class="header-body">
          <div class="row align-items-center py-4" >
            
          
          </div>
          <!-- Card stats -->
         
          <!-- put content  -->




          <div class="table-responsive">
    















<div class="container">    
    <div class="row">
    <table class="table align-items-center table-dark" id = "example" style="width:100%">
        <thead class="thead-dark">
            <tr>
                <th  >S/N</th>
                <th  >Subject</th>
                <th  >Exam code</th>
                <th   >Class</th>
             <!--    <th  >Duration</th> -->
                <th  >Date</th>
                <th>Term</th>
                <th>Session</th>
                <th  >Time</th>
                <th  >Edit</th>
                
               
               <th scope="col">Delete</th> 
                <th scope="col" >Exam status</th>
                
            </tr>
        </thead>
        <tbody class="list">
<?php
$rowCount1 = 0;
        while ($row = $statement->fetch(PDO::FETCH_ASSOC)){
            extract($row);
        $rowCount1++;

        ?>
            
            <tr>
                <td>
                   <?php  echo  $rowCount1;  ?> 
                </td>
                <td>
                <?php  echo $row['title'] ?>  
                </td>

                <td>
                <?php  echo $row['examcode'] ?>  
                </td>
                <td>
                <?php  echo $row['class'] ?>  
                </td>
<!--  
                <td>
               <?php  echo $row['duration'] ?>  

                </td>
-->
                <td>
                <?php  echo $row['date1'] ?>  

                </td>
                
                
                  <td>
                <?php  echo $row['term'] ?>  

                </td>
                
                  <td>
                <?php  echo $row['year'] ?>  

                </td>

                <td>
                <?php  echo $row['time'] ?>  
                </td>

                <td><a href="editSchedule.php?examcode=<?php echo $row['examcode'] ;?>&title=<?php echo urlencode($row['title']) ;?>&subject_id=<?php echo urlencode($row['subject_id']) ;?>" class="btn btn-primary ">Edit</a></td>

                    <td>
                    <form action="" method="post" name="form3">
            <input name="id" type="hidden" value="<?php  echo $row['examcode']?>">
            <input type="submit" value="DELETE" name="submit1" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this record?')"/>
            </form>
                    </td>
        

                    <td>
                    <form action="" method="post" name="form3">
            <input name="examcode" type="hidden" value="<?php echo $row['examcode']?>">
            <input type="submit" value="<?php if($row['status'] == "closed") {
              echo " open exam";
            } 
            else {
              echo "close exam";
            } ?>" name="submit" class="btn btn-danger" onclick="return confirm('Are you sure you want to perform this action?')"/>
            </form>
                    </td>

</tr>
<?php } ?>
</tbody>
</table>

</div>
</div>

</div>


          <!-- put content --->



          <!--- other content -->
        </div>
      </div>
    </div>
    <!-- Page content -->
    
      </div>
      <!-- Footer -->
      
      <footer class="footer pt-0">
        <div class="row align-items-center justify-content-lg-between">
          <div class="col-lg-6">
            
          </div>
          <div class="col-lg-6">
          
          </div>
        </div>
      </footer>
    </div>
  </div>
  <!-- Argon Scripts -->
  
  <script src="assets/vendor/jquery/dist/jquery.min.js"></script>
  
  <script src="assets/vendor/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
  <script src="assets/vendor/js-cookie/js.cookie.js"></script>
  <script src="assets/vendor/jquery.scrollbar/jquery.scrollbar.min.js"></script>
  <script src="assets/vendor/jquery-scroll-lock/dist/jquery-scrollLock.min.js"></script>
  <!-- Optional JS -->
  <script src="assets/vendor/chart.js/dist/Chart.min.js"></script>
  <script src="assets/vendor/chart.js/dist/Chart.extension.js"></script>
  <!-- Argon JS -->
 <script src="assets/js/argon.js?v=1.2.0"></script>

  <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
  <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
  <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.24/js/dataTables.bootstrap4.min.js"></script>
<script>
  $(document).ready(function() {
	$('#example').DataTable();
} );

	</script>
</body>

</html>