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

<section class="content-header">
	
	<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';} ?> Driver Details</h4>
		</div>
		<div class="col-md-6" style="text-align: right;">
		    <a href="driver.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="driver-list.php" class="btn btn-info btn-sm"><i class="fa fa-list"></i> Back to Drivers List</a>
		</div>
	</div>
	
	
</section>
<?php
    if(isset($_GET['key']) && $_GET['key']!=''){
        $location_id = id_decrypt($_GET['key']);
        $statement = $pdo->prepare("SELECT * FROM tbl_drivers 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">

	<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="required" class="col-sm-3 control-label">Driver 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="" class="col-sm-3 control-label">Mobile Number <span class="required"></span></label>
							<div class="col-sm-8">
								<input type="text"  class="form-control" placeholder="Phone Number" name="mobile_no" 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">Alter Mobile Number <span class="optional"></span></label>
							<div class="col-sm-8">
								<input type="text"  class="form-control" placeholder="Alter Number" name="alter_mobile_no" value="<?php if($partner_count){ echo $partner_result[0]['alter_mobile_no'];} ?>">
								
							</div>
						</div>
						
						<div class="form-group">
							<label for="" class="col-sm-3 control-label">License No <span class="required"></span></label>
							<div class="col-sm-8">
								<input type="text"  class="form-control" placeholder="License No" name="license_no" value="<?php if($partner_count){ echo $partner_result[0]['license_no'];} ?>" required>
							</div>
						</div>
						
						
						<div class="form-group">
							<label for="" class="col-sm-3 control-label">Vehicle No <span class="required"></span></label>
							<div class="col-sm-8">
								<input type="text"  class="form-control" placeholder="Vehicle No" name="vehicle_no" value="<?php if($partner_count){ echo $partner_result[0]['vehicle_no'];} ?>" required>
							</div>
						</div>
						
						<div class="form-group">
							<label for="" class="col-sm-3 control-label">Vehicle Type <span class="required"></span></label>
							<div class="col-sm-8">
								<input type="text"  class="form-control" placeholder="Vehicle Type" name="vehicle_type" value="<?php if($partner_count){ echo $partner_result[0]['vehicle_type'];} ?>" >
							</div>
						</div>
						
						
						
						<div class="form-group">
							<label for="" class="col-sm-3 control-label">Id Proof <span class="required"></span></label>
							<div class="col-sm-8">
							    <?php if($partner_count && $partner_result[0]['id_proof']!=''){ ?>
							        <img src="img/driver-proof/<?=$partner_result[0]['id_proof']?>" style="width:30%;" >
							    <?php } ?>
								<input type="file"  class="form-control" name="id_proof" value="">
								<input type="hidden" name="old_id_proof" value="<?php if($partner_count){ echo $partner_result[0]['id_proof'];} ?>">
							</div>
						</div>
						
					
	
						
						<div class="form-group">
							<label for="" class="col-sm-3 control-label">Status <span class="required"></span></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>
					   <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 Driver 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;">Driver 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 onclick="location.reload();"><button class="btn btn-info btn-sm"><i class="fa fa-refresh"></i> Reset Form</button></a>
                            &nbsp;&nbsp;
                            <a href="driver-list.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_driver'); <?php } ?>
               <?php if(!isset($_GET['key'])){ ?>  formData.append('type', 'create_driver'); <?php } ?>
                    $.ajax({
                            url: 'apis/driver.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>