<?php

    session_start();

    include "../db.php";

    date_default_timezone_set('Asia/Kolkata');

    $currentTime = date('M j, Y g:i a');



    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) && !isset($_SESSION['uname']) )

        header("Location: logout.php");

    $question = "SELECT * FROM question WHERE mocktest_id = '$_GET[mocktest]'";

    $result = mysqli_query($con, $question);

    $mocktest = "SELECT * FROM mocktest WHERE id = '$_GET[mocktest]'";

    $mockresult = mysqli_query($con, $mocktest);

    $mockrow = mysqli_fetch_array($mockresult);

?>



<!DOCTYPE html>

<html lang="en">



<head>

  <?php include "includes/meta.php"; ?>

  <?php include "includes/css.php"; ?>





  <style>

    .modal.fade .modal-dialog {

      width: 100%;

      margin: 0 auto;

    }

    .dataTables_info{

      display:none;

    }

    .dataTables_length{

      display:none;

    }

    #MockTest thead tr{

      background: #00428a;

      color:#fff;

    }

    .btn-primary{

      background: #00428a;

      border-color: #00428a;

      color:#fff;

    }

  </style>

</head>



<body id="page-top" class="mt-55">

  <div style="margin-top: 70px;"></div>

  <?php include "includes/header2.php"; ?>

  <div class="container">

    <div class="row">

      <div class="col-12 col-sm-12 col-md-12">

        <div cla ss="card-body">

          <h1 class="h3 mb-2 text-gray-800 text-center"><?php echo $mockrow['mocktest_name'].'('.$mockrow['mock_time'].' Minutes)'?></h1>

          <hr>

          <div class="py-3">

          <!-- <a href="view-mocktest.php" class="btn btn-warning text-sm-center mt-4 mr-2"><i class="fa fa-arrow-left"></i> Back</a> -->

            <a href="view-mocktest.php" class="btn btn-warning text-sm-center mr-2"><i class="fa fa-arrow-left"> </i> Back</a>

            <a href="add-question.php?mocktest=<?php echo $_GET['mocktest']?>" class="btn btn-primary">Add New Question</a>

          </div>

          <table id="MockTest" class="table table-striped table-borderless" style="width:100%">

            <thead>

              <tr>

                <th>Sl</th>

                <th>Question</th>

                <th>Correct Option</th>

                <th>Marks</th>

                <th>Difficulty</th>

                <th>Action</th>

              </tr>

            </thead>

            <tbody>

              <?php foreach($result as $key => $row){

                ?>

                <tr>

                <td width="5%"><?php echo $key+1?></td>

                <td width="50%">

                  <?php 

                  if(!empty($row['question_name'])){

                    echo $row['question_name'];

                    

                  }else{

                    $path = "./upload/question/";

                    ?>

                    <img src="<?php echo $path.$row['question_image'];?>" width="100%"alt="">

                  <?php

                  }

                  ?>

                </td>

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

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

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

                <td>

                      <a href="edit-question.php?question_id=<?php echo $row['id'];?>" class="m-2 btn btn-success btn-sm" style="width:35px;" title="Click to Edit"><i class="fa fa-pencil-square-o" aria-hidden="true" style="font-size:17px;"></i> </a>

                      <a href="view-questions.php?mocktest=<?php echo $_GET['mocktest']?>&del_id=<?php echo $row['id'];?>" class="delete m-2 btn btn-danger btn-sm" style="width:35px;" title="Click to Delete"><i class="fa fa-trash-o" aria-hidden="true" style="font-size:19px;"></i> </a>

                </td>

              </tr>

              <?php }?>

            </tbody>

          </table>

          <hr>

        </div>

      </div>

    </div>

  </div>



  <?php include "includes/ft.php"; ?>

  <?php include "includes/js.php"; ?>

  <script src="https://cdn.datatables.net/1.13.2/js/jquery.dataTables.min.js"></script>

  <script src="https://cdn.datatables.net/1.13.2/js/dataTables.bootstrap4.min.js"></script>

  <script>

    $(document).ready(function () {

      $('#MockTest').DataTable();

    });

  </script>

  <?php

   if ( isset($_GET["del_id"]) ) {  

    $disable_sql = "DELETE FROM question WHERE id = '$_GET[del_id]'";



        if ( mysqli_query($con, $disable_sql) ) {

            ?>

            <script>window.location = "view-questions.php?mocktest=<?php echo $_GET['mocktest'];?>";</script>

            <?php

        } else {

            ?>

            <script>

                alert('OOPS !!!!');

                setTimeout(() => {

                    window.location = 'view-questions.php?mocktest=<?php echo $_GET['mocktest'];?>';

                }, 1200);

            </script>

            <?php

        }



    }

  ?>

</body>



</html>