<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 dea(iden,action){
    var dataString = "id="+iden+"&action="+action;
    var status = $("#status").val();
    var identity = $('#deloading')+iden;
    var confirms = confirm("Clicking this will deactivate this staff and will not be able to use the admin again unless activated, are you sure you want to continue?");
    if (confirms == true) {
    $.ajax({
        type: "POST",
        url: "control.php",
        cache : false,
        data : dataString,
        beforeSend : function(){
            $('#dea'+iden).hide();
            $('#deloading'+iden).show();
        },
        success : function(response){
            $('#deloading'+iden).hide();
            var active = "activate";
            document.getElementById('inputHere').innerHTML = response;
            document.getElementById('deactive'+iden).innerHTML = "<button class='btn btn-default btn-xs' title='Activate' id='act"+iden+"' onclick= \"act(\'"+iden+"\', \'"+active+"\')\"><i class='fa fa-plus'></i> activate</button><span id='actloading"+iden+"' style='display:none'><img src='../img/loading.gif'></span>";
            $('#myModal').modal('show');
            /*window.location.reload(1);*/
        }
    });
}
}

  function act(iden,action){
    var dataString = "id="+iden+"&action="+action;
    var status = $("#status").val();
    var identity = $('#actloading')+iden;
    var confirms = confirm("Clicking this will activate this staff, are you sure you want to continue?");
    if (confirms == true) {
    $.ajax({
        type: "POST",
        url: "control.php",
        cache : false,
        data : dataString,
        beforeSend : function(){
            $('#act'+iden).hide();
            $('#actloading'+iden).show();
        },
        success : function(response){
            $('#actloading'+iden).hide();
            var deactive = "deactivate";
            document.getElementById('inputHere').innerHTML = response;
            document.getElementById('deactive'+iden).innerHTML = "<button class='btn btn-default btn-xs' title='Deactivate' id='dea"+iden+"' onclick= \"dea(\'"+iden+"\', \'"+deactive+"\')\"><i class='fa fa-minus'></i> deactivate</button><span id='deloading"+iden+"' style='display:none'><img src='../img/loading.gif'></span>";
            $('#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'])) {
    $name = $_POST['name'];
    $department = $_POST['department'];
    $iden = $_POST['iden'];
    if (empty($name) || empty($department)) {
       $mess = "<div style='color:#e6102e'><i class='fa fa-warning fa-5x'></i><p><b>Please fill all empty fields</b></p></div>";
    }else{
    $fetch1 = $conn->query("SELECT * FROM department WHERE id = $department");
    $result1 = $fetch1->fetchAll(PDO::FETCH_OBJ);
    $count1 = $fetch1->rowCount();
    foreach ($result1 as $key) {
     $departmentname = $key->name;
    }

    $fetch = $conn->query("SELECT * FROM course WHERE name = '$name' AND under = $department");
    $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>Nothing Changed</b></p></div>";
    }else{
        $in = array('name' => $name,
                    'under' => $department, 
        );
        update('course',$in,'id',$iden);
        $mess = "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>successfully updated to $name under $departmentname</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 Staff</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">
                    <div class="row">
                        <h4>View Staff</h4>
                    </div>
                    
                    <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>Name</th>
                                <th>Action</th>
                              </tr>
                            </thead>
                            <tbody>
                        <?php
                         $select = $conn->query("SELECT * FROM users WHERE priviledge = 'staff' OR priviledge = 'deactivated'");
                            $fetch = $select->fetchAll(PDO::FETCH_OBJ);
                            $count = $select->rowCount();
                            $serial = 1;
                            if ($count > 0) {
                              foreach ($fetch as $value) {
                                $iden = $value->id;
                                $name = $value->username;
                                $priviledge = $value->priviledge;
                            ?>
                              <tr>
                                <td><?php echo $serial; ?></td>
                                <td><?php echo $name; ?></td>
                                <td>
                                <?php
                                if ($priviledge == "staff") {
                                ?>
                                <button class="btn btn-default btn-xs" onclick= "refr('<?php echo $iden ?>', 'refreshstaff')" id="refr<?php echo $iden?>" title="reset password"><i class="fa fa-refresh"></i></button>
                                  <span id="refre<?php echo $iden ?>" style="display:none"><img src="../img/loading.gif"></span>
                                <button class="btn btn-default btn-xs" title="Deactivate" id="dea<?php echo $iden?>" onclick= "dea('<?php echo $iden ?>', 'deactivate')"><i class="fa fa-minus"></i> deactivate
                                </button><span id="deloading<?php echo $iden ?>" style="display:none"><img src="../img/loading.gif"></span>
                                <?php
                                }elseif ($priviledge == "deactivated") {
                                ?>
                                <button class="btn btn-default btn-xs" title="Activate" id="act<?php echo $iden?>" onclick= "act('<?php echo $iden ?>', 'activate')"><i class="fa fa-plus"></i> activate
                                </button><span id="actloading<?php echo $iden ?>" style="display:none"><img src="../img/loading.gif"></span>
                                
                                <?php
                                }
                                ?>
                                <span id="deactive<?php echo $iden ?>">
                                  
                                </span>

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

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