<?php
error_reporting(0);
include '../includes/connection.php';
$id=$_GET['id'];
// Create connection
// Create connection

$sql = "SELECT * FROM users where id='$id'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $fullname=$row['name'];
        $user_name=$row['user_name'];
        $rol=$row['role'];
        $contact=$row['contact'];
        $pword=$row['password'];
        $staffcode=$row['user_code'];
    }
} else {
    echo "0 results";
}

mysqli_close($conn)
?>

<!DOCTYPE html>
<html>
    
    <head>
        <title>Forms</title>
        <!-- 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">
        <!--[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="span9" id="content">
                      <!-- morris stacked chart -->
                    <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Edit User</div>
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                     <form class="form-horizontal" action="" method="POST">
                                      <fieldset>
                                       

                                        <div class="control-group">
                                          <label class="control-label" for="focusedInput">Full Name</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" id="focusedInput" type="text" name="fname" value="<?php echo $fullname; ?>">
                                          </div>
                                        </div>
                                        <div class="control-group">
                                          <label class="control-label" for="focusedInput">User Name</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" id="focusedInput" type="text" name="uname" value="<?php echo $user_name; ?>">
                                          </div>
                                        </div>
                                            <div class="control-group">
                                          <label class="control-label" for="focusedInput">Password</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" id="focusedInput" type="password" name="password" >
                                          </div>
                                        </div>
                                        <div class="control-group">
                                          <label class="control-label" for="focusedInput">Contact</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" id="focusedInput" type="number" name="contact" value="<?php echo $contact; ?>">
                                          </div>
                                        </div>
                                        <div class="control-group">
                                          <label class="control-label" for="focusedInput">Role</label>
                                          <div class="controls">
                                            <select name="roles" required> 
                                                <option disabled value="" selected="">Select Role</option>
                                                <?php 
                                                   //if ($_SESSION['role']=='General Admin') {
                                                if( $permissions['create_super_user'] == 1){
                                                    ?>
                                                    <option Selected value="Super Admin">Super Admin</option>  
                                                    <?php
                                                }else{
                                                ?>
                                                <!-- <option value="admin">admin</option>  
                                                <option value="attendant">attendant</option>  -->
                                                 <?php
                                                include '../includes/connection.php';
                                                $id_user=$_SESSION['id'];
                                                $branch=$_SESSION['branch_id'];
                                                $business=$_SESSION['business_id'];

                                    $sql = "SELECT * from permissions where branch_id=$branch AND 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)) {
                                                
                                                $role_id =$row['role_id']; 
                                                $role=$row['role']; 
                                                ?>
                                             
                                                <option value="<?php echo $role_id?>"><?php echo $role?></option>
        
                                                              <?php   }
                                                } else {
    echo "0 results";
}
                                               
                                                ?>
                                                <?php
                                                }
                                                ?>
                                                
                                                

                                            </select>
                                          </div>
                                        </div>
                        
                                       
                                        <div class="form-actions">
                                          <button type="submit" name="edit" class="btn btn-primary">Save changes</button>
                                          <a href="users.php"><button type="submit" class="btn">Cancel</button></a>
                                        </div>
                                      </fieldset>
                                    </form>
                                    <?php
                                    if(isset($_POST['edit'])){
                                        $name=$_POST['fname'];
                                        $uname=$_POST['uname'];
                                        $contactt=$_POST['contact'];
                                        $pwd=md5($_POST['password']);
                                        $rolee=$_POST['roles'];
                                   
                                if(!empty($_POST['password'])){
                                     $sql = "UPDATE users set name='$name', user_name='$uname', contact='$contactt', role='$rol', password='$pwd' WHERE id='$id'";
                                }
                                else{

                                $sql = "UPDATE users set name='$name', user_name='$uname', contact='$contactt', role='$rolee' WHERE id='$id'";
                                 }
                                 $sql2=mysqli_query($conn,"UPDATE staff set full_name='$name' WHERE staff_code='$staffcode'");
                                if (mysqli_query($conn, $sql)) {
                                        $id_user=$_SESSION['id'];
                                          $activity="Editted User ".$fullname. " from (".$fullname.", ".$user_name.", ".$role.", ".$contact.") to (".$name.", ".$uname.", ".$rolee.", ".$contactt.")";
                                          
                                          $log_sql=mysqli_query($conn,"INSERT INTO logs(user_id, activity) VALUES 
('$id_user','$activity')");

if ($log_sql) {
    ?>

                                    <div class="alert alert-success">
                                        <button class="close" data-dismiss="alert">&times;</button>
                                        <strong>Success!</strong> User Details Updated Successfully.
                                        <meta http-equiv="refresh" content="3;URL='users.php'"/>  
                                    </div>
                                    <?php
} else {
    echo "Error: Failed to add log <br>" . mysqli_error($conn);
}

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

                                mysqli_close($conn);

                                    }
                                    // Create connection
                                

                                    ?>

                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>

              

                     <!-- wizard -->
    
                     <!-- validation -->
       


                </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="../bootstrap/js/bootstrap.min.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="../assets/scripts.js"></script>
        <script>

	jQuery(document).ready(function() {   
	   FormValidation.init();
	});
	

        $(function() {
            $(".datepicker").datepicker();
            $(".uniform_on").uniform();
            $(".chzn-select").chosen();
            $('.textarea').wysihtml5();

            $('#rootwizard').bootstrapWizard({onTabShow: function(tab, navigation, index) {
                var $total = navigation.find('li').length;
                var $current = index+1;
                var $percent = ($current/$total) * 100;
                $('#rootwizard').find('.bar').css({width:$percent+'%'});
                // If it's the last tab then hide the last button and show the finish instead
                if($current >= $total) {
                    $('#rootwizard').find('.pager .next').hide();
                    $('#rootwizard').find('.pager .finish').show();
                    $('#rootwizard').find('.pager .finish').removeClass('disabled');
                } else {
                    $('#rootwizard').find('.pager .next').show();
                    $('#rootwizard').find('.pager .finish').hide();
                }
            }});
            $('#rootwizard .finish').click(function() {
                alert('Finished!, Starting over!');
                $('#rootwizard').find("a[href*='tab1']").trigger('click');
            });
        });
        </script>
    </body>

</html>