<!DOCTYPE html>
<html>
    
     <head>
        <?php include("../includes/header-links.php");?>
       
        <!-- Bootstrap -->
        <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">
        <!--[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">System Users</div>
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                   
                                      <div class="btn-group">
                                         <a href="#myModal" data-toggle="modal" class="btn btn-primary"><i class="icon-user icon-white"></i>&nbsp;Add New User</a>
                                      </div>

                                      <!--Model begins here -->
                                      <div id="myModal" class="modal hide">
											<div class="modal-header" style="background-color: #08c; color: #ffff">
												<button data-dismiss="modal" class="close" type="button">&times;</button>
												<h3>Add new User</h3>
											</div>
											<div class="modal-body">
												<form action="" method="POST">
                                                <?php

                                                if($_SESSION['role']=="General Admin"){
                                                    
                                        
                                                ?>
                                            <label class="control-label" for="select01">Select Business</label>
                                          
                                            <select id="select01" class="chzn-select" name="business">
                                              <option disabled selected value="">Business</option>
<?php
$sql = "SELECT * FROM business where status=1";
$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['business_id']; ?>"><?php echo $row['business_name']; ?></option>
        <?php
    }
} else {
    echo "0 results";
}

?>
</select>
<?php 
}elseif($_SESSION['role']=='Super Admin'){
    ?>
    <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
}
?>
												<label>Full Name</label>
												<input type="text" name="fname" class="input-xlarge focused" placeholder="Full Name">
                                                <label>User Name</label>
                                                <input type="text" name="uname" class="input-xlarge focused" placeholder="User Name">
                                                <label>password</label>
                                                <input type="password" name="password" class="input-xlarge focused" placeholder="Password">
												<label>Contact</label>
												<input type="number" name="contact" class="input-xlarge focused" placeholder="Contact">
												<label>Role</label>
												<select name="role">
                                                <option disabled selected value="">Select</option>
                                                <?php
                                                if ($_SESSION['role']=='General Admin') {
                                                    ?>
                                                    <option value="Super Admin">Super Admin</option>  
                                                    <?php
                                                }else{
                                                ?>
                                                <option value="admin">admin</option>  
                                                <option value="attendant">attendant</option> 
                                                <?php
                                                }
                                                ?>

                                                                                     
                                                </select>
                                                
												<div class="form-actions">
                                          <button type="submit" name="add" class="btn btn-primary">Save User</button>
                                          <a href="users.php" class="btn btn-danger">Cancel</a>
                                        </div>	
												</form>
											</div>
										</div>
										<!--Model Ends here -->
                                     <div class="table-toolbar">
                                   </div>
                                   <?php
include '../includes/connection.php';
     if ($_SESSION['role']=='General Admin') {
        $sql = "SELECT *,(select business_name from business where business_id=users.business_id) as bsname FROM users where status=1 && Role='Super Admin'";
                       
        }elseif ($_SESSION['role']=='Super Admin') {
               $sql = "SELECT *,(select branch_name from branches where branch_id=users.branch_id) as bchname FROM users where status=1 && business_id='$business' && id!='$id_user' && branch_id!=0;"  ;
                        
         }else {
                 $sql = "SELECT * FROM users where status=1 && branch_id='$branch'";
                      
               }                              // Create 

$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    


                                   ?>
                                    
                                   <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>S/NO</th>
                                                <?php
                                                if ($_SESSION['role']=='General Admin') {
                                                   ?>
                                                   <th>Business Name</th>
                                                   <?php
                                                }
                                                ?>
                                                <?php
                                                if ($_SESSION['role']=='Super Admin') {
                                                   ?>
                                                   <th>Branch Name</th>
                                                   <?php
                                                }
                                                ?>
                                                
                                                <th>Full Name</th>
                                                <th>Contact</th>
                                                <th>Role</th>
                                                <th>Edit</th>
                                                <th>Suspend</th>
                                                <th>Delete</th>

                                            </tr>
                                        </thead>
                                        <tbody>
                                        
                                        <?php
                                        $no=0;
while($row = mysqli_fetch_assoc($result)) {
	$no++;
        ?>
<tr class="gradeU">
                                                <td><?php echo $no; ?></td>
                                                <?php
                                                if ($_SESSION['role']=='General Admin') {
                                                   ?>
                                                  <td><?php echo $row['bsname']; ?>
                                                   <?php
                                                }
                                                
                                                if ($_SESSION['role']=='Super Admin') {
                                                   ?>
                                                  <td><?php echo $row['bchname']; ?>
                                                   <?php
                                                }
                                                ?>
                                                <td><?php echo $row['name']; ?></td>
                                                <td><?php echo $row['contact']; ?></td>
                                                <td class="center"><?php echo $row['role']; ?></td>
                                                <td class="center">
                                                	<a href="edituser.php?id=<?php echo $row['id']; ?>"><button class="btn 
                                                    btn-primary"><i class="icon-edit icon-white"></i></button></a>

                                                </td>
                                                <?php 
    $sus=$row['suspend'];
    if ($sus==0) {
       ?>
       <td><a onclick = "confirmationReturn(this); return false;" href="return_user.php?id=<?php echo $row['id']; ?>&name=<?php echo $row['name']; ?>"><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_user.php?id=<?php echo $row['id']; ?>&name=<?php echo $row['name']; ?>"><button class="btn btn-info"><i class="icon-plane icon-white"></i>suspend</button></a>
    </td>
      <?php
    }
    ?> 
                                                <td>
                                                	<a onclick = "confirmationDelete(this); return false;" href="deleteuser.php?id=<?php echo $row['id']; ?>&name=<?php echo $row['name']; ?>"><button class="btn btn-danger"><i class="icon-trash icon-white"></i></button></a>
                                                	<?php $newid=$row['id'];?>
									

                                                </td>
                                            </tr>
                                            
                                            <?php


    }
} else {
    echo "0 results";
}

mysqli_close($con);


                                        ?>
                                        <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 User?");
        if(conf1){
            window.location = anchor.attr("href");
        }
    }    
    function confirmationReturn(anchor){
        var conf2 = confirm("Are you sure you want to Return this User?");
        if(conf2){
            window.location = anchor.attr("href");
        }
    }    
</script>
                                            
                                        </tbody>
                                    </table>
                                    
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </div>
        <!--/.fluid-container-->

        <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="../assets/DT_bootstrap.js"></script>
        <script>
        $(function() {
            
        });
        </script>
    </body>

<?php


if(isset($_POST['add'])){
	include '../includes/connection.php';
$fullname=$_POST['fname'];
$username2=$_POST['uname'];
$contact=$_POST['contact'];
if ($_SESSION[role]=='General Admin') {
    $bnss=$_POST['business'];
 $brr=0; 
}else{
    $bnss=$_SESSION['business_id'];
  $brr=$_POST['branch'];  
}
$role=$_POST['role'];
$password2=md5($_POST['password']);



    $code = mt_rand(100000, 999999);

    $sqlstfaff = mysqli_query($conn,"INSERT INTO staff (full_name, staff_code,branch_id)
VALUES ('$fullname', '$code','$branch')");

$sql = "INSERT INTO users (name, user_name, contact, password, role,user_code,branch_id,business_id)
VALUES ('$fullname', '$username2', '$contact','$password2', '$role','$code','$brr','$bnss')";

if (mysqli_query($conn, $sql)) {

        $id_user=$_SESSION['id'];
  $activity="Added a new User ".$fullname. " as an ".$role;
  include '../includes/store_logs.php';
	?>

    <meta http-equiv="refresh" content="0;URL='users.php'" />  
    <?php
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
}

?>
 
</html>
