<?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';} ?> Generate Zone Details</h4>
		</div>
		<div class="col-md-6" style="text-align: right;">
		    <a href="pickup_points.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="pickup_points_list.php" class="btn btn-info btn-sm"><i class="fa fa-list"></i> Back to Generate Zone List</a>
		</div>
	</div>
	
	
</section>
<?php
    if(isset($_GET['key']) && $_GET['key']!=''){
        $location_id = id_decrypt($_GET['key']);
        $statement = $pdo->prepare("SELECT * FROM tbl_pickup_points 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-12 col-sm-12 col-md-6 col-lg-6">
    					 	<div class="box box-warning">
    					        <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 class="row">
            					          <div class="col-12 col-sm-12 col-md-12 col-lg-12"> 
            					              <div class="row">
            					                  <div class="col-sm-12 col-md-12 col-lg-12">
                					                  <label for="" class="control-label">Select Client <span style='color:red;'>*</span></label>
                                						<div class="">
                                							 <select name="client_id" id="client_id" class="form-control select2">
                        										<?php 
                            							        $statement = $pdo->prepare("SELECT * FROM `tbl_client_new` WHERE status=1");
                                    							$statement->execute();
                                    							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                                        						 foreach($result as $rows) { ?>
                        											<option value="<?=$rows['id'];?>" <?php if($partner_count && $partner_result[0]['client_id']==$rows['id']){ echo "selected";} ?>>
                        												<?=$rows['name']; ?>
                        											</option>
                        									    <?php } ?>
                            								</select>
                                						</div>
            					                   </div>
            					                   
            					                   <div class="col-sm-12 col-md-12 col-lg-12">
                					                  <label for="" class="control-label">From<span style='color:red;'>*</span></label>
                                						<div class="">
                                							 <input type="text" name="client_from" id="client_from" class="form-control" placeholder="Enter From">
                                						</div>
            					                   </div>
            					                   
            					                   <div class="col-sm-12 col-md-12 col-lg-12">
                					                  <label for="" class="control-label">Select Zone <span style='color:red;'>*</span></label>
                                						<div class="">
                                							 <select name="zone_id" id="zone_id" class="form-control select2">
                        										<?php 
                            							        $statement = $pdo->prepare("SELECT * FROM `tbl_zone` WHERE status=1");
                                    							$statement->execute();
                                    							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                                        						 foreach($result as $rows) { ?>
                        											<option value="<?=$rows['id'];?>" <?php if($partner_count && $partner_result[0]['client_id']==$rows['id']){ echo "selected";} ?>>
                        												<?=$rows['zone_name']; ?>
                        											</option>
                        									    <?php } ?>
                            								</select>
                                						</div>
            					                   </div>
            					                   
            					                  <div class="col-sm-12 col-md-12 col-lg-12" id="awb-scan">
                                        			    <div class="search-box">
                                        			        <label  class="control-label">Search City, Pincode, State</label>
                                        				<input placeholder="Search The City Here" type="text" class="form-control" name="awb_scan" id="awb_scan" style="border-radius: 20px;background: cornsilk;"> 
                                        				<div class="result" style="overflow-y: scroll;max-height: 267px;top: 56%;margin-left: 8%;width: 90%;margin-top:8%;"></div>
                                        				</div>
                                        			</div>
                                        			<div class="col-sm-12 col-md-12 col-lg-12" id="awb-scan">
                                        			    <br>
                                        			    <table id="scaned_table_data" class="table table-bordered table-striped" style="font-size: 12px;">
                                							<thead>
                                								<tr>
                                									<th style="text-align: center;">#</th>
                                									<th style="min-width:100px;">Pincode</th>
                                									<th  style="text-align: center;">City</th>
                                									<th>State</th>
                                									<th>Service Available</th>
                                								</tr>
                                							</thead>
                                							<tbody id="scaned_table_data_body">
                                							    
                                							</tbody>
                                						
                                							
                                						</table>
                                        			    
                                        			</div>
            					                   
            					              </div>
            					           </div>
            					     </div>
            				    </div>
            			    </div>
            			 </div>
            			 
            			 <div class="col-12 col-sm-12 col-md-6 col-lg-6">
    					 	<div class="box box-warning">
    					        <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 class="row">
            					          <div class="col-12 col-sm-12 col-md-12 col-lg-12"> 
            					          <center><h4> Copy client Details</h4></center>
            					              <div class="row">
            					                  <div class="col-sm-12 col-md-12 col-lg-12">
                					                  <label for="" class="control-label">Select Client <span style='color:red;'>*</span></label>
                                						<div class="">
                                							 <select name="copy_client_id" id="copy_client_id" class="form-control select2">
                        										<?php 
                            							        $statement = $pdo->prepare("SELECT * FROM `tbl_client_new` WHERE status=1");
                                    							$statement->execute();
                                    							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                                        						 foreach($result as $rows) { ?>
                        											<option value="<?=$rows['id'];?>" <?php if($partner_count && $partner_result[0]['client_id']==$rows['id']){ echo "selected";} ?>>
                        												<?=$rows['name']; ?>
                        											</option>
                        									    <?php } ?>
                            								</select>
                                						</div>
            					                   </div>
            					                   
            					                   <div class="col-sm-12 col-md-12 col-lg-12">
            					                       <br>
                					                  <label for="" class="control-label">Select States <span style='color:red;'>*</span></label>
                                						<div class="">
                                							 <select name="states_id" id="states_id" class="form-control select2">
                        										<?php 
                            							        $statement = $pdo->prepare("SELECT * FROM `tbl_state`");
                                    							$statement->execute();
                                    							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                                        						 foreach($result as $rows) { ?>
                        											<option value="<?=$rows['id'];?>" <?php if($partner_count && $partner_result[0]['client_id']==$rows['id']){ echo "selected";} ?>>
                        												<?=$rows['state']; ?>
                        											</option>
                        									    <?php } ?>
                            								</select>
                                						</div>
            					                   </div>
            					                   
            					                   <div class="col-sm-12 col-md-12 col-lg-12">
            					                       <br>
                					                  <label for="" class="control-label">Select City <span style='color:red;'>*</span></label>
                                						<div class="">
                                							 <select name="city_id" id="city_id" class="form-control select2" multiple>
                        										<?php 
                            							        $statement = $pdo->prepare("SELECT city FROM `pincode`  GROUP BY city");
                                    							$statement->execute();
                                    							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                                        						 foreach($result as $rows) { ?>
                        											<option value="<?=$rows['city'];?>" <?php if($partner_count && $partner_result[0]['client_id']==$rows['id']){ echo "selected";} ?>>
                        												<?=$rows['city']; ?>
                        											</option>
                        									    <?php } ?>
                            								</select>
                                						</div>
            					                   </div>
            					                 
            					                
            					                   
            					              </div>
            					           </div>
            					     </div>
            				    </div>
            			    </div>
            			 </div>
            			
					   </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>-->
								    <button id='btn_submit' class="btn btn-success btn-sm"><i id="update_fa" class="fa fa-paper-plane"></i>Save</span> </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="pickup_points.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="pickup_points_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'); ?>

<style>
    .search-box{
        font-size: 14px;
    }
    .search-box {
        padding: 0px 10px;
        font-size: 14px;
    }
    .result{
        position: absolute;        
        z-index: 999;
        top: 89%;
        left: 0;
    }
    .search-box, .result{
        width: 100%;
        box-sizing: border-box;
    }
    /* Formatting result items */
    .result div{
        margin: 0;
        background: #d4d4d4;
        padding: 1px 5px;
        border: 1px solid #CCCCCC;
        border-top: none;
        cursor: pointer;
        margin-top: 0px;
    }
    .result div:hover{
        background: #00a65a;
        color: white;
        padding:0px;
        font-weight:800;
    }
    .delete:hover{
        background: red;
        color: white;
        padding:5px;
        font-weight:800;
    }
</style>

<audio id="scanned_sound">
  <source src="sound/sound.mp3" type="audio/mpeg">
</audio>
<audio id="scanned_wrong_sound">
  <source src="sound/wrong-sound.mp3" type="audio/mpeg">
</audio>

<script>
const array = [];
const awb_array = [];
    $(document).ready(function(){
        $('#awb_scan').on("keyup", function(e){
       
        if(e.keyCode!=17 && e.keyCode==13){
                var search_text = $(this).val();
                var resultDropdown = $(this).siblings(".result");
               
                if(search_text.length>1){
                    
                    $.get("custom-api/city-search-api.php", {search_text: search_text}).done(function(data){
                        resultDropdown.html(data);
                        //console.log('ganesan:'+data);
                        if(data==''){
                            var ds='<div><center style="padding:10px;color:red;"> No Data Found...</center><div>';
                            resultDropdown.html(ds);
                            var sound = document.getElementById("scanned_wrong_sound"); 
                            $('.search-box input[type="text"]').select();
                            sound.play();
                        }else{
                            var numItems = $('.drop-down-awb').length;
                            if(numItems==1 && e.keyCode==13){
                                var ids=parseInt($('.drop-down-awb').attr('awb_id'));
                                if(!array.includes(ids)){
                                    product_selection(ids);
                                }else{
                                    $('.search-box input[type="text"]').focus();
                                    $('.search-box input[type="text"]').select();
                                    var ds='<div><center style="padding:10px;color:black;"> This AWB Number Already <b style="color:yellow"> Added </b></center><div>';
                                    $(".result").html(ds);
                                    var sound = document.getElementById("scanned_wrong_sound"); 
                                    sound.play();
                                }
                            } 
                        }
                    });
                } else{
                    resultDropdown.empty();
                }
    }
        });
        
        
        
        
        $('#btn_submit').on("click", function(){
        if(array.length>0){
                $("#form_submit").prop('disabled', true);
                
                var client_id = $('#client_id').val();
                var client_from = $('#client_from').val();
                var zone_id = $('#zone_id').val();
                var states_id = $('#states_id').val();/*
                var client_id = $('#client_id').val();*/
               
               $.ajax({
                          type:'POST',
                          url:'custom-api/generate-zone-crete-api.php',
                          data:{inserted_id:array,type:'create_zone',client_id:client_id,client_from:client_from,zone_id:zone_id,states_id:states_id},
                          dataType: 'json',
                          beforeSend: function() {
    						$('#update_fa').addClass('fa fa-spinner fa-spin');
    					  },
                          success:function(response){
                              $("#form_submit").prop('disabled', false);
                              if(response['error']==false){
                                      $("#update_fa").removeClass("fa fa-spinner fa-spin");
                                      $('#update_fa').addClass('fa fa-paper-plane');
                                      alert(response['meassge']);
                                      /* $('#text_update_status').text($("#update_status :selected").text());*/
                                        $('#scaned_table_data_body').empty();
                                        array.splice(0, array.length);
                                    
                                     
                                  }else{
                                       $("#update_fa").removeClass("fa fa-spinner fa-spin");
                                      $('#update_fa').addClass('fa fa-paper-plane');
                                      //alert(response['meassge']);
                                      
                                  }
                    
                          }
                        }); 
        }else{
            alert('scan min one pincode details entry to update status');;
        }
    });
        
        
        
     
    });
    
    
    
    
    function product_selection(id){
                if(array.includes(id)) {
                    var sound = document.getElementById("scanned_wrong_sound"); 
                    sound.play();
                    var ds='<div><center style="padding:10px;color:black;"> This AWB Number Already <b style="color:yellow"> Added </b></center><div>';
                    $(".result").html(ds);
                }else{
                   /* var update_status=$('#update_status').val();
                    var client_id = $('#client_id').val();
                    var branch_name = $('#branch_name').val();*/
                    $.ajax({
                      type:'POST',
                      url:'custom-api/get-city-information.php',
                      data:{pincode:id,type:'pincode_info'},
                      dataType: 'json',
                      success:function(response){
                          $('#scaned_table_data').prepend(response['data']);
                          array.push(id);
                          var sound = document.getElementById("scanned_sound"); 
                          sound.play(); 
                          $(".result").empty();
                      }
                    });
                }
                $('.search-box input[type="text"]').val('');
                $('.search-box input[type="text"]').focus();
            }
            
            
function removeA(arr) {
    var what, a = arguments, L = a.length, ax;
    while (L > 1 && arr.length) {
        what = a[--L];
        while ((ax= arr.indexOf(what)) !== -1) {
            arr.splice(ax, 1);
        }
    }
    return arr;
}          
function remove(id)
{
    if (confirm('Confirm if you want to remove this Pincode Details')){
        $('table#scaned_table_data tr#pic_'+id).remove();
        removeA(array,id);
    }
}
</script>
<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>
