
<?php
session_start();
error_reporting(0);
@ini_set('display_errors', 0);
$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];
?>
<!DOCTYPE html>
<html>
<?php include '../includes/connection.php';
include("../includes/header-links.php");
 ?>
    
    <head>
      <?php
    include("../includes/header-links.php");
    
$permissions = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM  
  permissions,users WHERE  permissions.role_id = users.role 
  AND users.id='$id_user' AND users.business_id ='$business' AND users.branch_id ='$branch'"));

if($permissions['view_staff'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?php
}
?>
    ?>
        
        <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="../bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="../assets/styles.css" rel="stylesheet" media="screen">
        <link href="../assets/DT_bootstrap.css" rel="stylesheet" media="screen">
        <link rel="stylesheet" href="../data-table-css/bootstrap-table.css">
    <link rel="stylesheet" href="../data-table-css/bootstrap-editable.css">
     <link href="../vendors/chosen.min.css" rel="stylesheet" media="screen">
        <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="vendors/flot/excanvas.min.js"></script><![endif]-->
        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <script src="../vendors/modernizr-2.6.2-respond-1.1.0.min.js"></script>
    </head>
    
    <body>
        <?php include '../includes/header.php'; ?>
        <div class="container-fluid">
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
                <div class="span10" id="content">

                    
                    

                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Staff List</div>
                                      <div class="muted pull-right">
                                      <?php 
                                      //if ($_SESSION['role']=='Super Admin') {
                                      if( $permissions['set_staff_role'] == 1){
                                        
                                      
                                      ?>
                                   <a href="#setroles" data-toggle="modal" class="btn btn-warning"><i class="icon-pencil icon-white"></i>&nbsp;Set Roles</a>
                                   <?php 
                                      
                                      }
                                      ?>
                                   </div>
                                   <div id="setroles" class="modal hide">
                                            <div class="modal-header" style=" text-align: center;">
                                                <button data-dismiss="modal" class="close" type="button">&times;</button>
                                                <h3>Register Staff</h3>
                                            </div>
                                            <div class="modal-body">
                                                <div class="row-fluid">
                                                    <div class="span12">
                                                        <div class="block">
                            
                            
                                    <form  method="POST" action="">
                                    
                                           <div class="control-group" align="center">
                                          <label style="text-align: center; font-size: 25px;" >Role</label><br>
                                          
                                          
                                            <input type="text" class="form-control" id="inputt01" placeholder="Role" name="role" required="required" style="font-size: 25; height: 30px; width: 80%">
                                           
                                          
                                          </div>
                                          <div class="control-group" align="center">
                                          <label style="text-align: center; font-size: 25px;" >Description</label><br>
                                          <textarea name="description" class="form-control" style="font-size: 25; height: 50px; width: 80%">
                                              
                                          </textarea>
                                            
                                           
                                          
                                          </div>
                                   
                                        <div class="form-control" align="center">
                                          <button type="submit" name="addrole" class="btn btn-primary">Save </button>
                                          <button data-dismiss='modal' class="btn btn-warning">Cancel</button>
                                        </div>
                                      
                                    </form>
<?php
if(isset($_POST['addrole'])){
    $role=$_POST['role'];
    $description=$_POST['description'];

    $sql = "INSERT INTO staff_roles (role,description,branch_id,business_id)
VALUES ('$role','$description','$branch','$business')";

if (mysqli_query($conn, $sql)) {
    $activity=" Added a new role ".$role;

    $log_sql=mysqli_query($conn,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$activity','$branch','$business')");

if ($log_sql) {
    ?>
    <meta http-equiv="refresh" content="0;URL='staff.php'" /> 
    <?php
} else {
    echo "Error: Failed to add log <br>" . mysqli_error($conn);
}

    
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}



}
?>
</div>
</div>
</div>
                                   </div>
                                   </div>
                                   <?php 
                        if( $permissions['add_staff'] == 1){
                            ?>
                                <div class="muted pull-right">
                                <a href="#myModal" data-toggle="modal" class="btn btn-primary"><i class="icon-plus icon-white"></i>&nbsp;Add Staff</a>
                                </div>
                                <?php
                         }
                             ?>
                                  <div id="myModal" class="modal hide">
                                            <div class="modal-header" style=" text-align: center;">
                                                <button data-dismiss="modal" class="close" type="button">&times;</button>
                                                <h3>Register Staff</h3>
                                            </div>
                                            <div class="modal-body">
                                                <div class="row-fluid">
                                                    <div class="span12">
                                                        <div class="block">
                            
                            
                                    <form class="form-horizontal" method="POST" action="">
                                      <fieldset>
                                        
                                       <?php
                                       if ($_SESSION['branch_id']=='0') {
                                       // if( $permissions['staff_branch'] == 1){
                                         ?>
                                         <label class="control-label" for="select01">Select Branch</label>
                                         <select  class="input-xlarge focused" name="branch">
                                              <option disabled selected value="">Branch</option>
<?php
$sql = "SELECT * FROM branches where status=1 && business_id='$business'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        ?>
<option value="<?php echo $row['branch_id']; ?>"><?php echo $row['branch_name']; ?></option>
        <?php
    }
} else {
    echo "0 results";
}

?>
</select>
                                         <?php
                                        } 
                                        ?>
                                          <div class="control-group">
                                          <label class="control-label" for="inputt01">Full Names</label>
                                          <div class="controls">
                                          
                                            <input type="text" class="form-control" id="inputt01" placeholder="Full Name" name="name">
                                           
                                          </div>
                                          </div>
                                   
                                       
                                        <div class="control-group">
                                          <label class="control-label" for="select01">Select Role</label>
                                          <div class="controls">
                                            <select id="select01" class="chzn-select" name="role">
                                              <option disabled="">Role</option>
                                             <?php
$sql = "SELECT * FROM staff_roles where business_id='$business'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['role']; ?></option>
        <?php
    }
} else {
    echo "0 results";
}
                                             ?>
                                            </select>
                                          </div>
                                        </div>
                                        
                                        <div class="form-actions">
                                          <button type="submit" name="save_staff" class="btn btn-primary">Save Staff</button>
                                          <button data-dismiss='modal' class="btn btn-warning">Cancel</button>
                                        </div>
                                      </fieldset>
                                    </form>
<?php
if(isset($_POST['save_staff'])){
    $name=$_POST['name'];
    $role=$_POST['role'];
    $code = mt_rand(100000, 999999);

   if ($_SESSION['branch_id']=='0') {
    // if( $permissions['staff_branch'] == 1){
$branc=$_POST['branch'];
      }else{
$branc=$_SESSION['branch_id'];
  }

    $sql = "INSERT INTO staff (full_name, staff_code, role,branch_id,business_id)
VALUES ('$name', '$code', '$role','$branc','$business')";

if (mysqli_query($conn, $sql)) {
    $activity=" Added a new Staff<br>
    <b>Full Names: </b>".$name."<br>
    <b>Code: </b>".$code."<br>
    <b>Role: </b>".$role;

    $log_sql=mysqli_query($conn,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$activity','$branc','$business')");

if ($log_sql) {
    ?>
    <meta http-equiv="refresh" content="0;URL='staff.php'" /> 
    <?php
} else {
    echo "Error: Failed to add log <br>" . mysqli_error($conn);
}

    
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}



}
?>

                               
                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                </div>
                            </div>
                                     <div class="datatable-dashv1-list custom-datatable-overright" style="padding: 14px;">
                                    <div id="toolbar">
                                        <select class="form-control">
                                                <option value="">Export Basic</option>
                                                <option value="all">Export All</option>
                                                <option value="selected">Export Selected</option>
                                            </select>
                                    </div>
                                    <table id="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="true" data-key-events="true" data-show-toggle="true" data-resizable="true" data-cookie="true"
                                        data-cookie-id-table="saveId" data-show-export="true" data-click-to-select="true" data-toolbar="#toolbar" data-export-options='{"fileName": "Testing_filename"}'>
                                        <thead>
                                            <tr>
                                                <th data-field="id">S/NO</th>
                                                <?php
                                       if ($_SESSION['branch_id']=='0') {
                                        // if( $permissions['staff_branch'] == 1){

                                         ?>
                                         <th>Branch</th>
                                         <?php
                                       }
                                         ?>
                                                <th>Staff Name</th>
                                                <th>Number</th>
                                                <th>Role</th>
                                                <?php 
                        if( $permissions['edit_staff'] == 1){
                            ?>
                                                <th>Edit</th>
                                                <?php
                                       }
                                         ?>
                                                <?php 
                        if( $permissions['suspend_staff'] == 1){
                            ?>
                                                <th>Suspend</th>

                                                <?php
                                       }
                                         ?>

                                                <?php 
                        if( $permissions['delete_staff'] == 1){
                            ?>
                                                <th>Delete</th>
                                                    <?php
                                       }
                                         ?>
                                            </tr>
                                        </thead>
                                        <tbody>
<?php

  //if ($_SESSION['role']=='Super Admin') {
if( $_SESSION['branch_id'] == 0){
                                  
$sql = "SELECT id, full_name, staff_code,suspend, (select role from staff_roles where id=staff.role)as role,(select branch_name from branches where branch_id=staff.branch_id) as bchname FROM staff where status=1 && business_id='$business' && branch_id!=0 && role!='' order by id DESC";
}else{
$sql = "SELECT id, full_name, staff_code,suspend, (select role from staff_roles where id=staff.role)as role FROM staff where status=1 && branch_id='$branch' && role!='' order by id DESC";
}
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    $sn=0;
    while($row = mysqli_fetch_assoc($result)) {
        $sn++;
        ?>
<tr>
    <td></td>
    <td><?php echo $sn; ?></td>
            <?php
          if ($_SESSION['branch_id']=='0') {
            // if( $permissions['staff_branch'] == 1){
                                         ?>
  <td><?php echo $row['bchname']; ?></td>
                                         <?php
                                       }
                                         ?>
    <td><?php echo $row['full_name']; ?></td>
    <td><?php echo $row['staff_code']; ?></td>
    <td><?php echo $row['role']; ?></td>
<?php
          //if ($_SESSION['role']=='Super Admin') {
            if( $permissions['edit_staff'] == 1){
                                         ?>
    <td><a href="#edit<?php echo $row['id']; ?>" data-toggle="modal" ><button class="btn btn-primary"><i class="icon-edit icon-white"></i></button></a>
            <div id="edit<?php echo $row['id']; ?>" class="modal hide">
             <div class="modal-header" style="text-align: center; background-color: #08c; color: white;">
                 <button data-dismiss="modal" class="close" type="button">&times;</button>
                 <h3>Edit Staff</h3>
                 </div>
                <div class="modal-body">
                    <?php
                    $edid=$row['id'];
                    $editvalues=mysqli_fetch_array(mysqli_query($con,"SELECT id,full_name,role, (select role from staff_roles where id=staff.role)as role1 from staff where id='$edid'"));
                    $eeid=$editvalues['id'];
                    $efname=$editvalues['full_name'];
                    $erole=$editvalues['role'];
                    $erole1=$editvalues['role1'];
                    ?>
                          <form class="form-horizontal" method="POST" action="">
                                      <fieldset>
                                        
                                       
                                          <div class="control-group">
                                            <input type="hidden" name="edid" value="<?php echo $eeid;?>">
                                          <label class="control-label" for="inputt01">Full Names</label>
                                          <div class="controls">
                                          
                                            <input type="text" class="form-control" id="inputt01" value="<?php echo $efname; ?>" name="ename">
                                           
                                          </div>
                                          </div>
                                   
                                       
                                        <div class="control-group">
                                          <label class="control-label" for="select01">Select Role</label>
                                          <div class="controls">
                                            <select id="select01" class="chzn-select" name="erole">
                                              <option value="<?php echo $erole ?>"><?php echo $erole1 ?></option>
                                             <?php
                                             $branch_id=$_SESSION['branch_id'];
$sqle = "SELECT * FROM staff_roles where branch_id='$branch_id'";
$resulte = mysqli_query($conn, $sqle);

if (mysqli_num_rows($resulte) > 0) {
    // output data of each row
    while($rowe = mysqli_fetch_assoc($resulte)) {
        ?>
<option value="<?php echo $rowe['id']; ?>"><?php echo $rowe['role']; ?></option>
        <?php
    }
} else {
    echo "0 results";
}
                                             ?>
                                            </select>
                                          </div>
                                        </div>
                                        
                                        <div class="form-actions">
                                          <button type="submit" name="save" class="btn btn-primary">Save </button>
                                          <button data-dismiss='modal' class="btn btn-warning">Cancel</button>
                                        </div>
                                      </fieldset>
                                    </form>
                </div>
            </div>
    </td>
    <?php
    }
    ?>
     <?php 
                        if( $permissions['suspend_staff'] == 1){
                            ?>
    <?php 
    $sus=$row['suspend'];
    if ($sus==0) {
       ?>
       <td><a onclick = "confirmationReturn(this); return false;" href="return.php?staffid=<?php echo $row['id'];?>"><button class="btn btn-warning"><i class="icon-ok icon-white"></i>Return</button></a>
    </td>
       <?php
    }else{
      ?>
      <td><a onclick = "confirmationSuspend(this); return false;" href="suspend.php?staffid=<?php echo $row['id'];?>"><button class="btn btn-info"><i class="icon-plane icon-white"></i>suspend</button></a>
    </td>
      <?php
    }
    ?> 
    <?php
    }
    ?>



     <?php 
                        if( $permissions['delete_staff'] == 1){
                            ?>
   <td><a onclick = "confirmationDelete(this); return false;" href="delete_staff.php?staffid=<?php echo $row['id'];?>"><button class="btn btn-danger"><i class="icon-trash icon-white"></i></button></a>
   </td>
    <?php
                         }
                             ?>
    
  
</tr>
        <?php
    }
} else {
    echo "0 results".mysqli_error($conn);
}
?>
 <script type = "text/javascript">
    function confirmationDelete(anchor){
        var conf = confirm("Are you sure you want to delete this record?");
        if(conf){
            window.location = anchor.attr("href");
        }
    }    
    function confirmationSuspend(anchor){
        var conf1 = confirm("Are you sure you want to Suspend this staff?");
        if(conf1){
            window.location = anchor.attr("href");
        }
    }    
    function confirmationReturn(anchor){
        var conf2 = confirm("Are you sure you want to Return this staff?");
        if(conf2){
            window.location = anchor.attr("href");
        }
    }    
</script>
                          <?php
if(isset($_POST['save'])){
    $eid=$_POST['edid'];
    $ename=$_POST['ename'];
    $erole=$_POST['erole'];
    $ecode = mt_rand(100000, 999999);

    $sql = "UPDATE staff SET full_name='$ename', role='$erole' where id='$eid'";

if (mysqli_query($conn, $sql)) {
    $activity=" Edited a  Staff<br>
    <b>Full Names: </b>".$ename."<br>
   
    <b>Role: </b>".$erole;

    $log_sql=mysqli_query($conn,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$activity','$branch','$business')");

if ($log_sql) {
    ?>
    <meta http-equiv="refresh" content="0;URL='staff.php'" /> 
    <?php
} else {
    echo "Error: Failed to add log <br>" . mysqli_error($conn);
}

    
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}



}
?>                 
                                        </tbody>
                                    </table>
                                </div> 
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </div>

        <!--/.fluid-container-->
        
        <link href="../vendors/datepicker.css" rel="stylesheet" media="screen">
        <link href="../vendors/uniform.default.css" rel="stylesheet" media="screen">
        <link href="../vendors/chosen.min.css" rel="stylesheet" media="screen">

        <link href="../vendors/wysiwyg/bootstrap-wysihtml5.css" rel="stylesheet" media="screen">

        <script src="../vendors/jquery-1.9.1.js"></script>
        <script src="../vendors/jquery.uniform.min.js"></script>
        <script src="../vendors/chosen.jquery.min.js"></script>
        <script src="../vendors/bootstrap-datepicker.js"></script>

        <script src="../vendors/wysiwyg/wysihtml5-0.3.0.js"></script>
        <script src="../vendors/wysiwyg/bootstrap-wysihtml5.js"></script>

        <script src="../vendors/wizard/jquery.bootstrap.wizard.min.js"></script>
        <script type="text/javascript" src="../vendors/jquery-validation/dist/jquery.validate.min.js"></script>
    <script src="../assets/form-validation.js"></script>


        <script src="../vendors/jquery-1.9.1.js"></script>
        <script src="../bootstrap/js/bootstrap.min.js"></script>
        <script src="../vendors/datatables/js/jquery.dataTables.min.js"></script>
         <script src="../data-table-js/bootstrap-table.js"></script>
    <script src="../data-table-js/tableExport.js"></script>
    <script src="../data-table-js/data-table-active.js"></script>
    <script src="../data-table-js/bootstrap-table-editable.js"></script>
    <script src="../data-table-js/bootstrap-editable.js"></script>
    <script src="../data-table-js/bootstrap-table-resizable.js"></script>
    <script src="../data-table-js/colResizable-1.5.source.js"></script>
    <script src="../data-table-js/bootstrap-table-export.js"></script>

        <script src="../assets/scripts.js"></script>
        <script src="../DT_bootstrap.js"></script>
        <script>
        $(function() {
            
        });
        </script>

    </body>

</html>