<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 add this faculty to archive, 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);
        }
    });
}
}
</script>
<?php
if (isset($_POST['update'])) {
    $name = $_POST['name'];
    $iden = $_POST['iden'];
    if (empty($name)) {
       $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 faculty WHERE name = '$name'");
    $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('name' => $name, );
        update('faculty',$in,'id',$iden);
        $mess = "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>successfully updated to $name</b></p></div>";
    }
    }
}
?>
                  <div class="x_content">
                      <table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                            <thead>
                             <tr>
                                <th>S/N</th>
                                <th>Name</th>
                                <th>Action</th>
                              </tr>
                            </thead>
                            <tbody>
                        <?php
                         $select = $conn->query("SELECT * FROM faculty ORDER BY name ASC");
                            $fetch = $select->fetchAll(PDO::FETCH_OBJ);
                            $count = $select->rowCount();
                            $serial = 1;
                            if ($count > 0) {
                              foreach ($fetch as $value) {
                                $iden = $value->id;
                                $name = $value->name;
                            ?>
                              <tr>
                                <td><?php echo $serial; ?></td>
                                <td><?php echo $name; ?></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="Archive" id="del<?php echo $iden?>" onclick= "del('<?php echo $iden ?>', 'delfaculty')"><i class="fa fa-archive"></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="form-group">
                                                <label for="name">Name</label>
                                                <input type="text" name="name" required="" id="name"class="form-control" placeholder="<?php echo $name?>">
                                                <input type="hidden" name="iden" value="<?php echo $iden; ?>">
                                            </div>
                                            <button class="btn btn-success" name="update">Update</button>
                                        </form>
                                        </div>
                                        </div>
                                      </div>
                                    </div>
                              </tr>
                              
                            <?php
                            $serial++;
                              }
                            }
                        ?>
                            </tbody>
                          </table>
                            </div>


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