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

        // Teacher

        $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"];

        // Mocktest

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

        $ResultMock = mysqli_query($con, $FetchMock);

        $MockRow = mysqli_fetch_assoc($ResultMock);

        // Class

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

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

        $classRow = mysqli_fetch_array($classresult);

        // Subject

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

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

        $subjectRow = mysqli_fetch_array($subjectresult);

        

?>



<!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 justify-content-center mt-5">

      <div class="col-md-8">

        <div class="card shadow mt-3">

          <h5 class="card-header text-dark  text-center font-weight-bold"> Edit MockTest</h5>

          <div class="card-body">

            <form method="POST">

              <div class="row">

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

                  <label class="badge badge-dark">Name</label>

                  <input type="text" class="form-control" name="mname" placeholder="Enter Mocktest Name" required="" value="<?php echo $MockRow['mocktest_name'];?>">

                </div>

              </div>

              <div class="row">

                <div class="col-md-4 mt-2">

                <label class="badge badge-dark">Class</label>

                  <select class="form-control" name="mclass" id="category-dropdown" required>

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

                        <?php

                        $result = mysqli_query($con,"SELECT * FROM class WHERE uploadedby = '$admin_2'");

                        while($row = mysqli_fetch_array($result)) {

                        ?>

                        <option value="<?php echo $row['sid'];?>" <?php echo ($row['sid']== $MockRow['class_id']) ? 'selected' : '';?>><?php echo $row["sclass"];?></option>

                        <?php

                        }

                        ?>

                  </select>

                </div>

                <div class="col-md-4 mt-2">

                  <label class="badge badge-dark">Subject</label>

                    <select class="form-control" name="msubject" id="sub-category-dropdown">

                      <option id="classMsg" value="<?php echo $MockRow['subject_id'];?>" selected><?php echo $subjectRow['sc_name'];?></option>

                    </select>

                </div>

                <div class="col-md-4 mt-2">

                  <label class="badge badge-dark">Total Time</label>

                    <input type="text" name="mock_time" class="form-control" placeholder="Mocktest time in minutes" value="<?php echo $MockRow['mock_time'];?>">

                </div>

              </div>

              <div class="row justify-content-center">

                <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>

                <button type="submit" class="btn btn-success text-sm-center mt-4" name="update_mocktest">

                  <i class="fa fa-upload"></i> Upload

                </button>

              </div>

            </form>

          </div>

        </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>

  <script>

$(document).ready(function() {



    $('#category-dropdown').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){

            $("#sub-category-dropdown").html(result);

            }

        });

    });



});

</script>

<?php 

 if(isset($_POST['update_mocktest'])){

$mname = mysqli_real_escape_string($con, strip_tags($_POST['mname']));

$mclass = mysqli_real_escape_string($con, strip_tags($_POST['mclass']));

$msubject = mysqli_real_escape_string($con, strip_tags($_POST['msubject']));

$mock_time = mysqli_real_escape_string($con, strip_tags($_POST['mock_time']));



$teacher_ref = $_SESSION['uref'];

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

$ref = mt_rand().'_'.time();



$up_sql = "UPDATE mocktest SET mocktest_name='$mname', class_id ='$mclass', subject_id='$msubject', created_at='$currentTime', mock_time='$mock_time' WHERE id=$_GET[edit_id]";

// print_r($ins_sql);

// die();

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

    ?>

    <script>

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

    </script>

    <?php

  } else {

    ?>

    <script>

        window.location = "edir-mocktest.php?edit_id=<?php echo $_GET['edit_id']?>";

    </script>

    <?php

  }

 }

?>

</body>



</html>