<?php  include "header.php";

$ids=$_GET['id'];

if(isset($_POST['update_user'])){
        try{
               
        $name = $_POST['name'];
        $email = $_POST['email'];
        $role = $_POST['role'];
        $password = $_POST['password'];
        $phone = $_POST['phone'];
        $address = $_POST['address'];
        $username = $_POST['username'];
        $ids = $_POST['ids'];
        $emp_code = $_POST['employee_code'];
            
        
            
$statement = $pdo->prepare("UPDATE tbl_users SET name=?, email=?, role=?, password=?, mobile=?, address=?, username=?, `emp_code`=? WHERE id=?");
$result = $statement->execute(array($name, $email, $role, $password, $phone, $address, $username, $emp_code, $ids));

                    
                    
                    if($result){
                        $_SESSION['message'] = "User is Updated Successfully";
                        
                        
                        /*echo "<script>alert('User is Updated Successfully!');</script>";*/
                        
                        header("location: users.php");
                        
                        //$message = 'User is updated successfully!';
                        //unset($_POST);    
                    }else{
                        $warning="Something went wrong try again...";
                    }  
                }catch(Exception $e){
                    $error=$e;
                }   
    }
    
    $statement = $pdo->prepare("SELECT * FROM tbl_users e where e.id=".$ids);
    $statement->execute();
    $emp_data = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();


if($totalData==1){
?>
<div class="content-wrapper">
        <section class="content-header">
            <h1>Edit User</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Edit User</li>            </ol>
        </section>


        <div class="col-lg-12 alerts">
            <div id="custom-alerts" style="display:none;">
                <div class="alert alert-dismissable">
                    <div class="custom-msg"></div>
                </div>
            </div>
            
            <?php if ($error)  { ?>
            <div class="alert alert-danger alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-ban"></i> Error</h4>
                <?= $error; ?>
            </div>
            <?php $error=0;} if ($warning) { ?>
            <div class="alert alert-warning alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-warning"></i> Warning</h4>
                <?= $warning; ?>
            </div>
            <?php  $warning=0;} if ($message) { ?>
            <div class="alert alert-success alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4>    <i class="icon fa fa-check"></i> Success</h4>
                <?= $message; ?>
            </div>
            <?php $message=0; } ?>
        </div>
        <div class="clearfix"></div>







<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary">
                
                
                <!--<div class="box-header">
                    <h3 class="box-title">Please fill in the information below</h3>
                </div>-->
                
                
                <div class="box-body">
                    
                    <div id="content-div">
                    
                    <div class="col-lg-12">

                        <form id="form_details">
                            
                            
                        <input type="hidden" name="ids" value="<?=$emp_data[0]['id']?>" class="form-control tip" id="id"  required="required" />
                                    
                        

                                                                 
                        <div class="row">
                            
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="name">Name</label>                                    
                                    <input type="text" name="name" value="<?=$emp_data[0]['name']?>" placeholder="Enter the Name" class="form-control tip" id="name"  required="required" />
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="email">Email</label>                                    
                                    <input type="email" name="email" value="<?=$emp_data[0]['email']?>" placeholder="Enter the Email" class="form-control tip" id="email"  required="required" />
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="email">Select Role</label>                                   
                                    <select name="role" class="form-control" style="width:100%" id="role" required="required">
                                    <option value="">Select Role Name</option>
                                    <?php $statement = $pdo->prepare("SELECT * FROM `tbl_role` where status=1 and id!=1");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>" <?php if($emp_data[0]['role']==$result['id']){echo "selected";} ?>><?=$result['role_name']?></option>
                                          <?php  }?>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="employee_code">Employee Code</label>                                    
                                    <input type="text" name="employee_code" value="<?=$emp_data[0]['emp_code']?>" placeholder="Enter the Employee Code" class="form-control tip" id="employee_code"  required="required" />
                                </div>
                            </div>
                            
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="username">Username</label>                                   
                                     <input type="text" name="username" placeholder="Enter the Username" value="<?=$emp_data[0]['username']?>"  class="form-control tip" id="username"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="password">Password</label>                                   
                                     <input type="password" name="password" placeholder="Enter the Password" value="<?=$emp_data[0]['password']?>"  class="form-control tip" id="password"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="phone">Mobile Number</label>                                   
                                     <input type="number" name="phone" placeholder="Enter the Mobile Number" value="<?=$emp_data[0]['mobile']?>"  class="form-control tip" id="phone"  required="required" />
                                </div>
                            </div>
                            
                            
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="optional" for="address">Address</label>                                   
                                     <textarea type="text" name="address" placeholder="Enter the Address" value=""  class="form-control tip" id="address" rows="5"><?=$emp_data[0]['address']?></textarea>
                                </div>
                            </div>
                            
                            
                        </div>
                        
                        
      
                        

                        <div class="form-group" style="text-align:center;">
                            <input type="submit" name="update_user" value="Update User"  class="btn btn-success" />
                        </div>
                        
                        
                        </form>

                    </div>
                        <div class="form-group" style="text-align:center;">
                            <?php echo "<a href=users-edit.php?id=".$ids.">
                            <button class='btn btn-primary' >
                            <i class='fa fa-refresh'></i> Reset Form</button></a>"; ?> 
                                            
                             <a href="users.php"><button class="btn btn-warning" ><i class="fa fa-chevron-left"></i> Back to List</button></a>
                         </div>
                         
                    </div>   
                    
                    
<div class="container" id="loading-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
        <center> <img src="img/loading.gif" style="width: 250px;"><br><b class="blinking">Processing Please Wait...</b></center>
</div>
                    
                    
                    
                    
                    <div class="container" id="success-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
                        <center><b class="blinking" style="font-size:22px;">User Updated Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <a href="users-edit.php?id=<?=$ids?>"><button class="btn btn-info"><i class="fa fa-refresh"></i> Reset Form</button></a>
                            &nbsp;&nbsp;
                            <a href="users.php"><button class="btn btn-warning"><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </center>
                    </div>
                    
                    
                    
                         
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
</section>


<?php } include "footer.php";?>

<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<style>
.select2-container--default .select2-selection--multiple .select2-selection__choice {
    background-color: rgb(228, 228, 228);
    box-sizing: border-box;
    display: inline-block;
    margin-left: 5px;
    margin-top: 5px;
    position: relative;
    max-width: 100%;
    text-overflow: ellipsis;
    vertical-align: bottom;
    white-space-collapse: collapse;
    text-wrap: nowrap;
    border-width: 1px;
    border-style: solid;
    border-color: rgb(170, 170, 170);
    border-image: initial;
    border-radius: 4px;
    padding: 0px 10px 0px 20px;
    overflow: hidden;
    color: black;
    font-weight: 800;
}
.select2-container--default .select2-selection--multiple {
    background-color: white;
    border: 1px solid #aaa;
    border-radius: 4px;
    cursor: text;
    height: 100px;
    padding-bottom: 5px;
    padding-right: 5px;
    position: relative;
}
</style>
<script>
$(document).ready(function(){
    bus_code();
});


$("#role").change(function () {
    bus_code();
});


function bus_code(){
    var role = $("#role").val();
       // Role (2=Agent, 3=Driver)
       if(role==2){
            $("#bus_code_section").hide();
        }else if(role==3){
            $("#bus_code_section").show();
        }else{
            $("#bus_code_section").hide(); 
        }
}
</script>
<script>
$(document).ready(function() {
   $('#form_details').on('submit', function(e){
                e.preventDefault();
                $('#loading-div').show();
                $('#content-div').hide();
                $("#submit").prop('disabled', true);
                var formData = new FormData(this);
                formData.append('type', 'update_users');
                    $.ajax({
                            url: 'apis/users.php',
                            type: 'post',
                            dataType: 'json',
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            enctype: 'multipart/form-data',
                            success: function (response) {
                                console.log(response);
                                if(response['status']==200){
                                     alert(response['message']);
                                     $("#submit").prop('disabled', false);
                                     $('#success-div').show();
                                     $('#loading-div').hide();
                                }else{
                                    alert(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
</script>
<?php  ?>