<?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");

    $mocktest = "SELECT * FROM mocktest WHERE uploaded_by = '$_SESSION[uref]' AND status = 1";

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

    $admin_2_run = "SELECT admin_ref FROM users WHERE uref = '$_SESSION[uref]'";

    $admin_2_row2 = mysqli_query($con, $admin_2_run);

    $admin_2_row = mysqli_fetch_assoc($admin_2_row2);

    $admin_2 = $admin_2_row["admin_ref"];

?>

<!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 table-responsive">

          <h1 class="h3 mb-2 text-gray-800 text-center">Mock Test</h1>

          <hr>

          <div class="py-3">

            <a href="add-mocktest.php" class="btn btn-primary">Add Mock Test</a>

            <a href="#ViewResult" data-toggle="modal" class="btn btn-primary">View Result</a>

          </div>

          <!-- Modal -->

          <div class="modal fade" id="ViewResult" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">

            <div class="modal-dialog" role="document">

              <div class="modal-content">

                <div class="modal-header">

                  <h5 class="modal-title" id="exampleModalLabel">Mock test Details</h5>

                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">

                    <span aria-hidden="true">&times;</span>

                  </button>

                </div>

                <form method="GET" action="view-mocktest-result.php" id="ResultForm">

                <div class="modal-body">

                  <?php 

                  $cla = "SELECT * FROM class WHERE uploadedby = '$admin_2'";

                  $clresult = mysqli_query($con, $cla);

                  // $claRow = mysqli_fetch_array($clresult);

                  $sub = "SELECT * FROM subject WHERE uploadedby = '$admin_2'";

                  $subres = mysqli_query($con, $sub);

                  // $subRow = mysqli_fetch_array($subres);

                  ?>

                  <div class="form-group">

                    <label for="recipient-name" class="col-form-label">Class</label>

                    <select name="Class" id="Class" class="form-control" required>

                    <option value="" selected disabled >Select Class..</option>

                      <?php foreach($clresult as $classData){ ?>

                        <option value="<?php echo $classData['sid']; ?>"><?php echo $classData['sclass']; ?></option>

                        <?php } ?>

                    </select>

                  </div>

                  <div class="form-group">

                    <label for="message-text" class="col-form-label">Subject</label>

                    <select name="Subject" id="Subject" class="form-control" required>

                      <option value="" id="classMsg" selected disabled >Select Class First</option>

                    </select>

                  </div>

                </div>

                <div class="modal-footer"><span id="Error_message" class="text-danger font-weight-bold"></span>

                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>

                  <button type="submit" class="btn btn-primary">Save changes</button>

                </div>

                </form>

              </div>

            </div>

          </div>

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

            <thead>

              <tr>

                <th>Sl</th>

                <th>Name</th>

                <th>Class</th>

                <th>Subject</th>

                <th>Time(minutes)</th>

                <th>Questions</th>

                <th>Action</th>

              </tr>

            </thead>

            <tbody>

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

                $class = "SELECT sclass, sid FROM class WHERE sid = '$row[class_id]'";

                $classresult = mysqli_query($con, $class);

                $classRow = mysqli_fetch_array($classresult);

                $subject = "SELECT sc_name, sc_id FROM subject WHERE sc_id = '$row[subject_id]'";

                $subjectresult = mysqli_query($con, $subject);

                $subjectRow = mysqli_fetch_array($subjectresult);

                ?>

                <tr>

                <td><?php echo $key+1?></td>

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

                <td><?php echo $classRow['sclass'];?></td>

                <td><?php echo $subjectRow['sc_name'];?></td>

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

                <td>

                  <div class="py-3">

                    <a href="view-questions.php?mocktest=<?php echo $row['id'];?>" class="btn btn-primary">View Questions</a>

                  </div>

                </td>

                <td>

                      <a href="edit-mocktest.php?edit_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-mocktest.php?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>



$("#ResultForm").submit(function(e){

  e.preventDefault();

    var Class_data = $('#Class').val();

    var Subject_Data = $('#Subject').val();

    $.ajax({

        url: "add-question-ajax.php",

        type: "POST",

        data: {

          Subject_Data: Subject_Data,

        Class_data: Class_data

        },

        cache: false,

        success: function(result){

          if(result=="FALSE"){

            $("#Error_message").html("Data Not Found !");

            $('#Error_message').delay(3000).fadeOut('slow');

          }else{

            window.location='view-mocktest-result.php?Class='+Class_data+'&Subject='+Subject_Data+'';

          }

        

        }

    });

});

    $(document).ready(function () {

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

    });

$(document).ready(function() {

$('#Class').on('change', function() {

    var category_id = this.value;

    var admin_2 = "<?php echo $admin_2; ?>";

    $("#classMsg").hide();

    $.ajax({

        url: "notes-upload-CatSubcat-Ajax.php",

        type: "POST",

        data: {

        admin_2: admin_2,

        category_id: category_id

        },

        cache: false,

        success: function(result){

        $("#Subject").html(result);

        }

    });

});



});

</script>

  <?php

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

    $disable_sql = "UPDATE mocktest SET status='0' WHERE id = '$_GET[del_id]'";



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

            ?>

            <script>window.location = "view-mocktest.php";</script>

            <?php

        } else {

            ?>

            <script>

                alert('OOPS !!!!');

                setTimeout(() => {

                    window.location = 'view-mocktest.php';

                }, 1200);

            </script>

            <?php

        }



    }

  ?>

</body>



</html>