<?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 '';} ?> Generate Zone Details</h4>
		</div>
		<div class="col-md-6" style="text-align: right;">
		    <a href="generate-zones.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="generate-zone-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_generate_zone 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-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-sm-6 col-md-6 col-lg-6" style="margin-top: 5px;">
                				<label for="" class="control-label">Select Client <span style='color:red;'>*</span></label>
        						<div class="" style="margin-top: 5px;">
        							 <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-6 col-md-6 col-lg-6" style="margin-top: 5px;" hidden>
			                  <label for="" class="control-label">Copy Zone Details from Existing Client <span style='color:green;'>(Optional)</span></label>
        						<div class="" style="margin-top: 5px;">
        							 <select name="copy_client_id" id="copy_client_id" class="form-control select2">
        							     <option>-----Select Copy Client Name-----</option>
										<?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="clearfix"></div>
		                   <div class="col-sm-4 col-md-4 col-lg-4" style="margin-top: 5px;">
			                  <label for="" class="control-label">Select Zone <span style='color:red;'>*</span></label>
        						<div class="" style="margin-top: 5px;">
        							 <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]['zone_id']==$rows['id']){ echo "selected";} ?>>
												<?=$rows['zone_name']; ?>
											</option>
									    <?php } ?>
    								</select>
        						</div>
		                   </div>
		                   <div class="col-sm-4 col-md-4 col-lg-4" style="margin-top: 5px;">
			                  <label for="" class="control-label">Select From City <span style='color:red;'>*</span></label>
        						<div class="" style="margin-top: 5px;">
        							 <select name="from_city" id="from_city" class="form-control select2">
										<?php 
    							        $statement = $pdo->prepare("SELECT city FROM `pincode` GROUP by city ORDER by city ASC");
            							$statement->execute();
            							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                						 foreach($result as $rows) { ?>
											<option value="<?=$rows['city'];?>" <?php if($partner_count && $partner_result[0]['from_city']==$rows['city']){ echo "selected";} ?>>
												<?=$rows['city']; ?>
											</option>
									    <?php } ?>
    								</select>
        						</div>
		                   </div>
		                   <div class="col-sm-4 col-md-4 col-lg-4" style="margin-top: 5px;">
			                  <label for="" class="control-label">Select Destination State <span style='color:red;'>*</span></label>
        						<div class="" style="margin-top: 5px;">
        							 <select name="destination_state" id="destination_state" class="form-control select2">
										<?php 
    							        $statement = $pdo->prepare("SELECT state FROM `pincode` GROUP by state ORDER by state ASC");
            							$statement->execute();
            							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                						 foreach($result as $rows) { ?>
											<option value="<?=$rows['state'];?>" <?php if($partner_count && $partner_result[0]['destination_state']==$rows['state']){ echo "selected";} ?>>
												<?=$rows['state']; ?>
											</option>
									    <?php } ?>
    								</select>
        						</div>
		                   </div>
		                   
		                   
		                   
		                   <div class="col-sm-12 col-md-12 col-lg-12" style="margin-top: 5px;">
			                  <label for="" class="control-label">Select Destination Cities <span style='color:red;'>*</span></label>
        						<div class="" style="margin-top: 5px;">
        							<select name="destination_city[]" id="destination_city" class="form-control select2" multiple>
    								</select>
        						</div>
		                   </div>

							<div class="col-sm-12" style="margin-top: 5px;">
								<center>
								    <button id='btn_submit' class="btn btn-success btn-sm"><i id="update_fa" class="fa fa-paper-plane"></i> Save Zone Details</span> </button>
								</center>
							</div>
		          	</form>	         
			    </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:16px;">Zone 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="client-rate-entry.php"><button class="btn btn-info btn-sm"><i class="fa fa-refresh"></i> View Details</button></a>
                            
                        </center>
                    </div>
				
				</div>
	
		</div>
		
		
	</div>

</section>

<?php require_once('footer.php'); ?>

<script>
$(document).ready(function() {
    get_destination_city();
    $('#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', 'zone_generation'); <?php } ?>
               <?php if(!isset($_GET['key'])){ ?>  formData.append('type', 'zone_generation'); <?php } ?>
                    $.ajax({
                            url: 'apis/zone_generation.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();
                                    alert(response['message']);
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
    $("#destination_state,#client_id,#zone_id,#from_city").change(function() {
	    get_destination_city();
    });


    function get_destination_city(){
        $('#destination_city').empty();
        var destination_state =$('#destination_state').val();
        var client_id =$('#client_id').val();
        var zone_id =$('#zone_id').val();
        var from_city =$('#from_city').val();
        $.ajax({
            url: 'apis/get-drop-down-list.php',
            type: 'post',
            data: {type:'get_city_dropdown', destination_state:destination_state,client_id:client_id,zone_id:zone_id,from_city:from_city},
             success:function(response){
                 console.log(response);
                $('#destination_city').empty();
                $('#destination_city').append(response);
            }
        });
    }

</script>
