<script>
  function del(iden,action){
    var dataString = "id="+iden+"&action="+action;
    var status = $("#status").val();
    var identity = $('#loading')+iden;
    var confirms = confirm("Are you sure you want to continue?");
    if (confirms == true) {
    $.ajax({
        type: "POST",
        url: "control.php",
        cache : false,
        data : dataString,
        beforeSend : function(){
            $('#del'+iden).hide();
            $('#loading'+iden).show();
        },
        success : function(response){
            $('#loading'+iden).hide();
            document.getElementById('inputHere').innerHTML = response;
            $('#myModal').modal('show');
            window.location.reload(1);
        }
    });
}
}


 function refr(iden,id2,action){
    var dataString = "id="+iden+"&id2="+id2+"&action="+action;
    var status = $("#status").val();
    var identity = $('#loading')+iden;
    var confirms = confirm("Are you sure you want to continue?");
    if (confirms == true) {
    $.ajax({
        type: "POST",
        url: "control.php",
        cache : false,
        data : dataString,
        beforeSend : function(){
            $('#refr'+iden).hide();
            $('#refre'+iden).show();
        },
        success : function(response){
            $('#refre'+iden).hide();
            $('#abc'+iden).show();
            document.getElementById('inputHere').innerHTML = response;
            $('#myModal').modal('show');
        }
    });
}
}
</script>

                  <!-- page content -->
        <div class="right_col" role="main">
          <div class="">
            <div class="page-title">
              <div class="title_left">
                <h3><a href="main.php">Dashboard</a>/<a href=" ">Register Carry-Over Courses</a></h3>
              </div>
            </div>

            <div class="clearfix"></div>

            <div class="row">
              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>Register Carry-Over Courses</h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                      <table id="datatable-responsive" class="table table-striped table-bordered" cellspacing="0" width="100%">
                            <thead>
                             <tr>
                                <th>S/N</th>
                                <th>level</th>
                                <th>Semester</th>
                                <th>Course</th>
                                <th>Lecturer</th>
                                <th>Action</th>
                              </tr>
                            </thead>
                            <tbody>
                        <?php
                         $select = $conn->query("SELECT * FROM package WHERE department = '$dept' AND semester = '$activesemester' AND level > $level ORDER BY level");
                            $fetch = $select->fetchAll(PDO::FETCH_OBJ);
                            $count = $select->rowCount();
                            $serial = 1;
                            if ($count > 0) {
                              foreach ($fetch as $value) {
                                $iden = $value->id;
                                $level = $value->level;
                                $semester = $value->semester;
                                $course = $value->course;
                                $lecturer = $value->lecturer;
                            ?>
                              <tr>
                                <td><?php echo $serial; ?></td>
                                <td><?php echo $level; ?></td>
                                <td><?php echo $semester; ?></td>
                                <td>
                                  <?php
                                    $select2 = $conn->query("SELECT * FROM course WHERE id = '$course' LIMIT 1");
                                  $fetch2 = $select2->fetchAll(PDO::FETCH_OBJ);
                                  $count2 = $select2->rowCount();
                                  if ($count2 > 0) {
                                    foreach ($fetch2 as $value2) {
                                      $name2 = $value2->name;
                                      echo "$name2";
                                    }
                                  } 

                                    
                                  ?>                                    
                                  </td>
                                <td>
                                  <?php
                                  $select2 = $conn->query("SELECT * FROM lecturers WHERE id = '$lecturer' ");
                                  $fetch2 = $select2->fetchAll(PDO::FETCH_OBJ);
                                  $count2 = $select2->rowCount();
                                  if ($count2 > 0) {
                                    foreach ($fetch2 as $value2) {
                                      $sname = $value2->sname;
                                      $oname = $value2->oname;
                                      echo "$sname $oname";
                                    }
                                  } 

                                    
                                  ?>
                                  </td>
                                <td>
                                  <?php
                                  $select2 = $conn->query("SELECT * FROM stp WHERE studentid = '$id2' AND packageid = '$iden' ");
                                  $fetch2 = $select2->fetchAll(PDO::FETCH_OBJ);
                                  $count2 = $select2->rowCount();
                                  if ($count2 > 0) {
                                    foreach ($fetch2 as $key2 ) {
                                      $pid = $key2->packageid;
                                    }
                                  ?>
                                  <button class="btn btn-default btn-xs" onclick= "del('<?php echo $pid ?>', 'deletepackage')" id="del<?php echo $pid?>" title="Delete"><i class="fa fa-minus"></i></button>
                                  <span id="loading<?php echo $pid ?>" style="display:none"><img src="img/loading.gif"></span>
                                  <?php
                                  }else{
                                    ?>
                                  <button class="btn btn-default btn-xs" onclick= "refr('<?php echo $iden ?>','<?php echo $id2 ?>', 'addcourse')" id="refr<?php echo $iden?>" title="Add"><i class="fa fa-plus"></i></button>
                                  <span id="refre<?php echo $iden ?>" style="display:none"><img src="img/loading.gif"></span>
                                    <?php
                                  }
                                  ?>
                                  <div id="abc<?php echo $iden ?>" style="display: none;">
                                    <button class="btn btn-success btn-xs" title="Added">Added</button>
                                  </div>
                                </td>
                              </tr>
                              
                            <?php
                            $serial++;
                              }
                            }
                        ?>
                            </tbody>
                          </table>
                            </div>
                            </div>
              </div>
            </div>
          </div>
        </div>
        <!-- /page content -->


<div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-body text-center">
        <div id="inputHere">

        </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Ok</button>
        </div>
        </div>
        </div>
      </div>
    </div>


<div class="modal fade" id="myModal2" role="dialog">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-body text-center">
        <?php  
        if (isset($mess)) {
        echo "$mess";
        }
        ?>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Ok</button>
        </div>
      </div>
    </div>
  </div>

<?php
if(isset($mess)){
?>
<script type="text/javascript">
   $(window).load(function(){
   $('#myModal2').modal('show');
   }); 
</script>
<?php
  }
?>