<?php  include "header.php";

if(isset($_GET['id'])){
    $ids = $_GET['id'];
    $title = "Update";
    $ajax = "update_users";
    $success = "Updated";
    
    $statement = $pdo->prepare("SELECT * FROM tbl_users e where e.id=?");
    $statement->execute(array($ids));
    $datas = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    
    $branch_ids = explode(",",$datas[0]['branch_ids']);

}else{
    $ids = 0;
    $title = "Create";
    $ajax = "create_users";
    $success = "Created";
    
    $branch_ids = "";
}




?>


<div class="content-wrapper">
        <section class="content-header">
            <h1><?=$title?> Users</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active"><?=$title?> Users</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" style="box-shadow: rgba(0, 0, 0, 0.16) 0px 3px 6px, rgba(0, 0, 0, 0.23) 0px 3px 6px;">

                <div class="box-body">
                    
                    <div id="content-div">
                    
                    <div class="col-lg-12">

                        <form id="form_details" autocomplete="off">
                            
                            <input type="hidden" name="ids" value="<?=$ids?>"  class="form-control" id="ids"  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="<?php if($datas){ echo $datas[0]['name']; }?>" placeholder="Enter the Name" class="form-control" id="name"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="mobile">Mobile Number</label>                                   
                                    <input type="number" name="mobile" placeholder="Enter the Mobile Number" value="<?php if($datas){ echo $datas[0]['mobile']; }?>"  class="form-control" id="mobile"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="optional" for="alter_mobile">Alter Mobile Number</label>                                   
                                     <input type="number" name="alter_mobile" placeholder="Enter the Alter Mobile Number" value="<?php if($datas){ echo $datas[0]['alter_mobile']; }?>"  class="form-control" id="alter_mobile"  />
                                </div>
                            </div>
                            
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="email">Email</label>                                    
                                    <input type="email" name="email" value="<?php if($datas){ echo $datas[0]['email']; }?>" placeholder="Enter the Email" class="form-control" id="email"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="role">Role</label>                                   
                                    <select name="role" class="form-control select2" style="width:100%" id="role" required="required">
                                        <option value="">Select Role</option>
                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_role` where status=1 and id!=0");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>"  <?php if(isset($datas) && $datas[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="<?php if($datas){ echo $datas[0]['emp_code']; }?>" placeholder="Enter the Employee Code" class="form-control" id="employee_code"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="username">User Name</label>                                   
                                     <input type="username" name="username" placeholder="Enter the User Name" value="<?php if($datas){ echo $datas[0]['username']; }?>" class="form-control" id="username"  autocomplete="false" required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="password">Password</label>                                   
                                     <input type="text" name="password" placeholder="Enter the Password" value="<?php if($datas){ echo $datas[0]['password']; }?>"  class="form-control" id="password" autocomplete="off"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="hrm_password">HRM Password</label>                                   
                                     <input type="text" name="hrm_password" placeholder="Enter the Password" value="<?php if($datas){ echo $datas[0]['hrm_password']; }?>" class="form-control" id="hrm_password" autocomplete="off"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="optional" for="address">Address</label>                                   
                                     <textarea name="address" placeholder="Enter the Address" class="form-control tip" id="address" ><?php if($datas){ echo $datas[0]['address']; }?></textarea>
                                </div>
                            </div>
                            
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="required" for="branch_ids">Branch</label>                                    
                                    <select name="branch_ids[]" id="branch_ids" class="form-control select2" multiple>
    							        <option value='0' <?php if(in_array(0, $branch_ids)){ echo "selected";} ?>>All Branch's</option> 
    							        <?php 
    							        	$statement = $pdo->prepare("SELECT * FROM tbl_business_branch where 1=1 and status=1");
                							$statement->execute();
                							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                						 foreach($result as $rows){ ?>
                						     <option value="<?=$rows['id']?>" <?php if(in_array($rows['id'], $branch_ids)){ echo "selected";} ?>><?=$rows['location_name']?></option>
                						<?php } ?>
    							    </select>
                                </div>
                            </div>
                            
                            
                            
                            
                            
                        </div>
                        
                        
                        

                        <div class="form-group" style="text-align:center;">
                            <input type="submit"  id="submit" value="Submit" class="btn btn-success" />
                        </div>
                        
                        
                        </form>

                    </div>
                        <div class="form-group" style="text-align:center;">
                            <span onclick="location.reload();"><button class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</button></span>
                            <a href="users-list.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 <?=$success?> Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <span onclick="location.reload();"><button class="btn btn-info"><i class="fa fa-refresh"></i> Reset Form</button></span>
                            &nbsp;&nbsp;
                            <a href="users-list.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"; ?>


<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', '<?=$ajax?>');
                    $.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']);
                                     $('#form_details').trigger("reset");
                                     $("#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>