<script>
  function del(iden,action){
    var dataString = "id="+iden+"&action="+action;
    var status = $("#status").val();
    var identity = $('#loading')+iden;
    var confirms = confirm("Clicking this will delete this person, 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,action){
    var dataString = "id="+iden+"&action="+action;
    var status = $("#status").val();
    var identity = $('#loading')+iden;
    var confirms = confirm("Clicking this will refresh password, 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();
            document.getElementById('inputHere').innerHTML = response;
            $('#myModal').modal('show');
        }
    });
}
}
</script>
<?php
if (isset($_POST['update'])) {
    $cor = $_POST['cor'];
    $iden = $_POST['iden'];
    if (empty($cor)) {
       $mess = "<div style='color:#e6102e'><i class='fa fa-warning fa-5x'></i><p><b>Please input a name</b></p></div>";
    }else{
    $fetch = $conn->query("SELECT * FROM classes WHERE id = '$iden' AND coordinator = '$cor' ");
    $result = $fetch->fetchAll(PDO::FETCH_OBJ);
    $count = $fetch->rowCount();
    if ($count > 0) {
        $mess = "<div style='color:#e6102e'><i class='fa fa-ban fa-5x'></i><p><b>Already Available</b></p></div>";
    }else{
        $in = array('coordinator' => $cor, );
        update('classes',$in,'id',$iden);
        $mess = "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>successfully updated</b></p></div>";
    }
    }
}
?>

                  <!-- 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=" ">View Classes</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>View Classes</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>Department</th>
                                <th>Class</th>
                                <th>Coordinator</th>
                                <th>Action</th>
                              </tr>
                            </thead>
                            <tbody>
                        <?php
                         $select = $conn->query("SELECT * FROM classes");
                            $fetch = $select->fetchAll(PDO::FETCH_OBJ);
                            $count = $select->rowCount();
                            $serial = 1;
                            if ($count > 0) {
                              foreach ($fetch as $value) {
                                $iden = $value->id;
                                $name = $value->name;
                                $department = $value->department;
                                $coordinator = $value->coordinator;
                            ?>
                              <tr>
                                <td><?php echo $serial; ?></td>
                                <td>
                                  <?php
                                    $select2 = $conn->query("SELECT * FROM department WHERE id = '$department' 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 echo "$name"; ?></td>
                                <td>
                                  <?php
                                    $select2 = $conn->query("SELECT * FROM lecturers WHERE id = '$coordinator' ");
                                  $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><button class="btn btn-default btn-xs" data-toggle="modal" data-target="#edit<?php echo $iden ?>" title="edit"><i class="fa fa-edit"></i></button>

                                <button class="btn btn-default btn-xs" title="delete" id="del<?php echo $iden?>" onclick= "del('<?php echo $iden ?>', 'delclass')"><i class="fa fa-trash"></i></button><span id="loading<?php echo $iden ?>" style="display:none"><img src="../img/loading.gif"></span>
                                </td>

                                <div class="modal fade" id="edit<?php echo $iden ?>" role="dialog">
                                    <div class="modal-dialog modal-sm">
                                      <div class="modal-content">
                                        <div class="modal-header">
                                          <button type="button" class="close" data-dismiss="modal">&times;</button>
                                          <h4 class="modal-title">Editing <?php echo "$name"; ?></h4>
                                        </div>
                                        <div class="modal-body">
                                        <form method="post">
                                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                      <input type="hidden" name="iden" value="<?php echo $iden ?>">
                                        <label for="cor" >Coordinator</label>
                                        <select class="form-control" name="cor" id="cor" required="">
                                            <option selected>.. Select a Coordinator</option>
                                            <?php
                                            $fetch = $conn->query("SELECT * FROM lecturers WHERE dept = '$department'");
                                              $result = $fetch->fetchAll(PDO::FETCH_OBJ);
                                              $count = $fetch->rowCount();
                                              if ($count > 0) {
                                                foreach ($result as $key) {
                                                  $did = $key->id;
                                                  $sname = $key->sname;
                                                  $oname = $key->oname;
                                            ?>
                                            <option value="<?php echo "$did"; ?>"><?php echo "$sname $oname"; ?></option>
                                            <?php
                                            }
                                            }
                                            ?>

                                        </select>
                                    </div>
                                </div>
                            </div>

                                            <button class="btn btn-success" name="update">Update</button>
                                        </form>
                                        </div>
                                        </div>
                                      </div>
                                    </div>
                              </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
  }
?>