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

<section class="content-header" style="background-color: white;">
	
	<div class="row">
		<div class="col-md-6" style="text-align: left;">
			<h4> <?php if(isset($_GET['key']) && $_GET['key']!=''){ echo 'Update';}else { echo 'Add';} ?> Delivery Boy Details</h4>
		</div>
		<div class="col-md-6" style="text-align: right;">
		    <a href="delivery_man..php<?php if(isset($_GET['key']) && $_GET['key']!=''){ echo '?key='.$_GET['key'];} ?>" class="btn btn-success btn-sm"><i class="fa fa-refresh"></i> Reset Form</a>&nbsp;
			<a href="delivery_man_details.php" class="btn btn-info btn-sm"><i class="fa fa-list"></i> Back to List</a>
		</div>
	</div>
	
	
</section>
<?php
    if(isset($_GET['key']) && $_GET['key']!=''){
        $location_id = id_decrypt($_GET['key']);
        $statement = $pdo->prepare("SELECT * FROM tbl_delivery_boy where id=?");
		$statement->execute(array($location_id));
		$partner_result = $statement->fetchAll(PDO::FETCH_ASSOC);
		$partner_count = $statement->rowCount();
    }else{
        $partner_count=0;
    }
    
?>

<section class="content" style="background-color: white;">

	<div class="row">
		<div class="col-md-12">
			
				<div class="box box-info">
					<div class="box-body" style="box-shadow: rgba(0, 0, 0, 0.16) 0px 3px 6px, rgba(0, 0, 0, 0.23) 0px 3px 6px;">
					    
					<div id="content-div">
					 <form id="form_details" class="form-horizontal" action="" method="post" enctype="multipart/form-data" autocomplete="new-password"> 
					 
					 
					    <div class="col-md-7">
					        
					        
					    <div class="form-group">
    						<label for="" class="col-sm-3 control-label">Select Branch <span style='color:red;'>*</span></label>
    						<div class="col-sm-8">
    							 <select name="branch_id" id="branch_id" class="form-control select2">
    							     <option value="">Select Branch</option>
															<?php 
															$condi='';
            	        
                                                	        if($session['id']!=1 && $session_branch_ids!='0'){
                                                                $condi.= " and id IN ($session_branch_ids)";
                                                            }
                        							       $statement = $pdo->prepare("SELECT * FROM `tbl_branch` WHERE status=1 $condi");
                                							$statement->execute();
                                							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                                    						 foreach($result as $rows) { ?>
																<option value="<?=$rows['id'];?>" <?php if($partner_count && $partner_result[0]['branch_id']==$rows['id']){ echo "selected";} ?>>
																	<?=$rows['name']; ?>
																</option>
																<?php } ?>
														</select>
    						</div>
    					</div>
					        <!--age , email, experience,salary, education, mother, father, mail, age  -->
					    <div class="form-group">
							<label for="required" class="col-sm-3 control-label"> Name <span class="required"></span></label>
							<div class="col-sm-8">
								<input type="text" autocomplete="off" placeholder="Name" class="form-control" name="name" value="<?php if($partner_count){ echo $partner_result[0]['name'];} ?>" required >
							</div>
						</div>
					    <div class="form-group">
							<label for="required" class="col-sm-3 control-label"> Age </label>
							<div class="col-sm-8">
								<input type="number" autocomplete="off" placeholder="  Age" class="form-control" name="age" value="<?php if($partner_count){ echo $partner_result[0]['age'];} ?>" required >
							</div>
						</div>
						
					    <div class="form-group">
							<label for="required" class="col-sm-3 control-label"> Email </label>
							<div class="col-sm-8">
								<input type="mail" autocomplete="off" placeholder="Mail" class="form-control" name="mail" value="<?php if($partner_count){ echo $partner_result[0]['email'];} ?>" required >
							</div>
						</div>
		
					    <div class="form-group">
							<label for="required" class="col-sm-3 control-label"> Father Name </label>
							<div class="col-sm-8">
								<input type="text" autocomplete="off" placeholder="father" class="form-control" name="father" value="<?php if($partner_count){ echo $partner_result[0]['father'];} ?>" required >
							</div>
						</div>
					    <div class="form-group">
							<label for="required" class="col-sm-3 control-label"> Mother Name </label>
							<div class="col-sm-8">
								<input type="text" autocomplete="off" placeholder="mother" class="form-control" name="mother" value="<?php if($partner_count){ echo $partner_result[0]['mother'];} ?>" required >
							</div>
						</div>
					    <div class="form-group">
							<label for="required" class="col-sm-3 control-label"> Education  </label>
							<div class="col-sm-8">
								<input type="text" autocomplete="off" placeholder="education" class="form-control" name="education" value="<?php if($partner_count){ echo $partner_result[0]['education'];} ?>" required >
							</div>
						</div>
					    <div class="form-group">
							<label for="required" class="col-sm-3 control-label"> Salary  </label>
							<div class="col-sm-8">
								<input type="text" autocomplete="off" placeholder="salary" class="form-control" name="salary" value="<?php if($partner_count){ echo $partner_result[0]['salary'];} ?>" required >
							</div>
						</div>
					    <div class="form-group">
							<label for="required" class="col-sm-3 control-label"> Experience  </label>
							<div class="col-sm-8">
								<input type="text" autocomplete="off" placeholder="experience" class="form-control" name="experience" value="<?php if($partner_count){ echo $partner_result[0]['experience'];} ?>" required >
							</div>
						</div>
					
						<div class="form-group">
							<label for="" class="col-sm-3 control-label">Phone Number </label>
							<div class="col-sm-8">
								<input type="text"  class="form-control" placeholder="Phone Number" name="mobile" value="<?php if($partner_count){ echo $partner_result[0]['mobile_no'];} ?>" required>
								<input type="hidden"  class="form-control" name="id" value="<?php if($partner_count){ echo $partner_result[0]['id'];} ?>" >
							</div>
						</div>
						
						<div class="form-group">
							<label for="" class="col-sm-3 control-label">Address </label>
							<div class="col-sm-8">
								<textarea class="form-control" name="address" placeholder="Address" id="address" style="height:100px;"><?php if($partner_count){ echo $partner_result[0]['address'];} ?></textarea>
							</div>
						</div>
						
					
						
						<div class="form-group" >
							<label for="" class="col-sm-3 control-label">City </label>
							<div class="col-sm-8">
								<input type="text"  class="form-control" name="city" placeholder="City" value="<?php if($partner_count){ echo $partner_result[0]['city'];} ?>">
							</div>
						</div>
						
						
						<div class="form-group" >
							<label for="" class="col-sm-3 control-label">Pincode </label>
							<div class="col-sm-8">
								<input type="text"  class="form-control" name="pincode" placeholder="Pincode" value="<?php if($partner_count){ echo $partner_result[0]['pincode'];} ?>">
							</div>
						</div>
						
						<div class="form-group" >
							<label for="" class="col-sm-3 control-label">State </label>
							<div class="col-sm-8">
								<input type="text"  class="form-control" name="state" placeholder="State" value="<?php if($partner_count){ echo $partner_result[0]['state'];} ?>">
							</div>
						</div>
						
						<div class="form-group" >
							<label for="" class="col-sm-3 control-label">Country </label>
							<div class="col-sm-8">
								<input type="text"  class="form-control" name="country" placeholder="Country" value="<?php if($partner_count){ echo $partner_result[0]['country'];}else{ echo "INDIA"; } ?>">
							</div>
						</div>
						
						
							
	
						
						<div class="form-group">
							<label for="" class="col-sm-3 control-label">Status </label>
							<div class="col-sm-8">
							    <select class="form-control select2" name="status" id="status">
							        <option value="1" <?php if($partner_count && $partner_result[0]['status']==1){ echo "selected";} ?>>Active</option>
							        <option value="0" <?php if($partner_count && $partner_result[0]['status']==0){ echo "selected";} ?>>In-Active</option>
							    </select>
							</div>
						</div>
						
						
						<div class="form-group" >
							<label for="" class="col-sm-3 control-label">Delivery Man Proof </label>
							<div class="col-sm-8">
								<input type="file"  class="form-control" name="delivery_man_proof" id="delivery_man_proof" placeholder="Delivery Man Proof" value="">
								<input type="hidden" name="old_delivery_man_proof" value="<?php if($partner_count){ echo $partner_result[0]['delivery_man_proof'];} ?>">
							</div>
						</div>
						
						
						 <?php if($partner_count && $partner_result[0]['delivery_man_proof']!=''){ ?>	   
						<div class="form-group" >
							<label for="" class="col-sm-3 control-label">Existing Proof </label>
							<div class="col-sm-8">
								<img src="img/delivery-man-proof/<?=$partner_result[0]['delivery_man_proof']?>"  class="form-control" style="width:200px;height:80px;object-fit:contain">
							</div>
						</div>
						
						 <?php } ?>
						
						
						
					   </div>
					   <div class="col-md-4">
					       
						
					   </div>
					
						
						
						<div class="form-group">
							<div class="col-sm-12">
								<center><button type="submit" id="submit" class="btn btn-success btn-sm" name="form1"><i class="fa fa-save"></i>  Save Pickup Points Details</button></center>
							</div>
						</div>
						
					</div>	
					</form>		
					
<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:16px;">Pickup Points Details <?php if(isset($_GET['key']) && $_GET['key']!=''){ echo 'Updated';}else { echo 'Added';} ?> Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <a href="delivery_man.php<?php if(isset($_GET['key']) && $_GET['key']!=''){ echo '?key='.$_GET['key'];}?>"><button class="btn btn-info btn-sm"><i class="fa fa-refresh"></i> View Updated Details</button></a>
                            &nbsp;&nbsp;
                            <a href="delivery_man_details.php"><button class="btn btn-warning btn-sm"><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </center>
                    </div>
					
					</div>
				</div>
	
		</div>
		
		
	</div>

</section>

<?php require_once('footer.php'); ?>
<script>
  var loadFile = function(event) {
    var output = document.getElementById('logo_output');
    output.src = URL.createObjectURL(event.target.files[0]);
    output.onload = function() {
      URL.revokeObjectURL(output.src) // free memory
    }
  };
</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);
               <?php if(isset($_GET['key']) && $_GET['key']!=''){ ?>  formData.append('type', 'update_location'); <?php } ?>
               <?php if(!isset($_GET['key'])){ ?>  formData.append('type', 'create_location'); <?php } ?>
                    $.ajax({
                            url: 'apis/delivery_man_details.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){
                                     $('#form_details').trigger("reset");
                                     $("#submit").prop('disabled', false);
                                     $('#success-div').show();
                                     $('#loading-div').hide();
                                }else{
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
</script>