<?php require_once "header.php";  ?>

<?php 
if(isset($_GET['id'])){
    $ids = $_GET['id'];
    
$statement = $pdo->prepare("SELECT * FROM tbl_users e where e.id=?");
$statement->execute(array($ids));
$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();

  $title = "Update";
  $success = "Updated";
  $ajax = "update_users";  
  $client_ids = explode(",",$datas[0]['client_ids']);
  $branch_ids = explode(",",$datas[0]['branch_ids']);
  
  
}else{
    
  $ids = 0;
  $title = "Add";
  $success = "Added";
  $ajax = "create_users";
  $client_ids = "";
  $branch_ids = "";
  
}
?>





<section class="content">

        <div class="row">
    		<div class="col-md-4" style="text-align: left;">
    			<h4><i class="fa fa-users"></i> <?=$title?> Users Details</h4>
    		</div>
    		<div class="col-md-8" style="text-align: right;"> 
    		    <a href="auth-users-list.php" class="btn btn-warning btn-sm"><i class="fa fa-arrow-left"></i> Back to Users List</a>
    		</div>
    	</div> 
    	
    	
    	
    	
	<div class="row">
	    <div class="col-md-12">

			
			
				<div class="box box-info" style="box-shadow: rgba(0, 0, 0, 0.02) 0px 1px 3px 0px, rgba(27, 31, 35, 0.15) 0px 0px 0px 1px;">
				
				
				<form class="form-horizontal" id="form_details">
					<div class="box-body" id="content-div">
					    
					    <input type="hidden" name="ids" value="<?=$ids?>" />
					    
						
						
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Name <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="name" value="<?php if($datas){ echo $datas[0]['name']; }?>" />
							</div>
						</div>
						
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Email <span>*</span></label>
							<div class="col-sm-6">
								<input type="email" autocomplete="off" class="form-control" name="email" id="email" value="<?php if($datas){ echo $datas[0]['email']; }?>" />
							</div>
						</div>
						
						<div class="form-group" hidden>
							<label for="" class="col-sm-2 control-label">Username <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="username">
							</div>
						</div>
						
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Password <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="password" id="password" value="<?php if($datas){ echo $datas[0]['password']; }?>" />
							</div>
						</div>
						
						<?php if($ids!=1){?>
						<div class="form-group" hidden>
							<label for="" class="col-sm-2 control-label">Address <span>*</span></label>
							<div class="col-sm-6">
								<textarea class="form-control" name="address" id="address" style="height:100px;"><?php if($datas){ echo $datas[0]['address']; }?></textarea>
							</div>
						</div>
						
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Select User Type <span>*</span></label>
							<div class="col-sm-6">
							    <select name="user_type" id="user_type" class="form-control select2">
							       <option value=''>Select User Type</option> 
							       <?php 
							        	$statement = $pdo->prepare("SELECT * FROM tbl_user_type WHERE 1=1 and id!=1 and status=1");
            							$statement->execute();
            							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
            						 foreach($result as $rows){?>
            						     <option value="<?=$rows['id']?>" <?php if($datas && $rows['id']==$datas[0]['user_type_id']){ echo "selected"; }?>><?=$rows['name']?></option>
            						<?php } ?>
							    </select>
							</div>
						</div>
						
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Select Role <span>*</span></label>
							<div class="col-sm-6">
							    <select name="role" id="role" class="form-control select2">
							       <option value=''>Select Role</option> 
							       <?php 
							        	$statement = $pdo->prepare("SELECT * FROM tbl_role WHERE 1=1 and id!=1 and status=1");
            							$statement->execute();
            							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
            						 foreach($result as $rows){?>
            						     <option value="<?=$rows['id']?>" <?php if($datas && $rows['id']==$datas[0]['role_id']){ echo "selected"; }?>><?=$rows['role_name']?></option>
            						<?php } ?>
							    </select>
							</div>
						</div>
					
						
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Select Branch <span>*</span></label>
							<div class="col-sm-6">
							    <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_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['name']?></option>
            						<?php } ?>
							    </select>
							</div>
						</div>
						
						
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Select Client <span>*</span></label>
							<div class="col-sm-6">
							    <select name="client_ids[]" id="client_ids" class="form-control select2" multiple>
							       <!--<option value='0' <?php if(in_array(0, $client_ids)){ echo "selected";} ?>>All Client's</option> -->
							       <?php 
							        	$statement = $pdo->prepare("SELECT * FROM tbl_user where 1=1 and id!=0");
            							$statement->execute();
            							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
            						 foreach($result as $rows){  
            						 ?>
            						     <option value="<?=$rows['id']?>" <?php if(in_array($rows['id'], $client_ids)){ echo "selected";} ?>><?=$rows['full_name']?></option>
            						<?php } ?>
							    </select>
							</div>
						</div>
						
						
						
						
						
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Status <span>*</span></label>
							<div class="col-sm-6">
								<select name="status" class="form-control" id="status">
								    <option value="1" <?php if($datas && 1==$datas[0]['status']){ echo "selected"; }?>>Active</option>
								    <option value="0" <?php if($datas && 0==$datas[0]['status']){ echo "selected"; }?>>In-Active</option>
								</select>
							</div>
						</div>
						
						<?php }?>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label"></label>
							<div class="col-sm-6">
								<button type="submit" id="submit" class="btn btn-success pull-left" name="submit">Submit</button>
							</div>
						</div>
						
						
						
						
						
					</div>
				</form>
					
					
<div class="container" id="loading-div" style="display: none; margin-top: 40px; margin-bottom: 40px; padding-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; padding-bottom: 40px;">
                        <center><b class="blinking" style="font-size:22px;">Users <?=$success?> Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <a onclick="location.reload();"><button class="btn btn-info"><i class="fa fa-refresh"></i> Reset Form</button></a>
                            &nbsp;&nbsp;
                            <a href="auth-users-list.php"><button class="btn btn-warning"><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </center>
                    </div>
				</div>
			
			
			
		</div>
	</div>
	

</section>



<?php require_once "footer.php"; ?>

<script>
$(document).ready(function() {
    $('#branch_ids').on('change', function(){
             get_new_client();
    });
   $('#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']);
                                     
                                      $('#success-div').show();
                                      $("#submit").prop('disabled', false);
                                      $('#loading-div').hide();
                                }else{
                                    alert(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
    });
});

function get_new_client(){
    $('#client_ids').empty();
     
    var branch_id=$('#branch_ids').val();
    console.log('New Client Id Change');
    $.ajax({
        url: 'apis/get-drop-down-list.php',
        type: 'post',
        data: {type:'get_new_client_dropdown_bulk',branch_id:branch_id},
         success:function(response){
            $('#client_ids').empty();
            $('#client_ids').append(response);
           
        }
    });
}
</script>