<?php require_once 'header.php'; ?>
<?php if($session_id==1 || $allPermissions['rate_entry']['read']=="1"){ ?>
<section class="content-header" style="background-color: white;">
	<div class="row">
		<div class="col-md-6" style="text-align: left;">
			<h4> <i class="fa fa-rupee"></i> <?php if(isset($_GET['key']) && $_GET['key']!=''){ echo 'Update';}else { echo '';} ?> Client Rate Master Details</h4>
		</div>
		<div class="col-md-6" style="text-align: right;">
		    
		    <a href="client-rate-entry.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="index.php" class="btn btn-info btn-sm"><i class="fa fa-list"></i> Back to Client Rate Master List</a>
		</div>
	</div>
</section>
<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">
        							      <option value=''>Select Client</option>
										<?php 
										
										$conditions_rate='';
										if($session_client_ids==0){
                                            $conditions_rate.="";
                                        }else{
                                            $conditions_rate.=" and id IN ($session_client_ids)";
                                        }
    							        $statement = $pdo->prepare("SELECT * FROM `tbl_client_new` WHERE status=1 $conditions_rate");
            							$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-2 col-md-2 col-lg-2" style="margin-top: 5px;">
                				<label for="" class="control-label">Select Rate Type <span style='color:red;'>*</span></label>
        						<div class="" style="margin-top: 5px;">
        							 <select name="rate_type" id="rate_type" class="form-control select2">
										<option value="1">Pcs Rate</option>
										<option value="2">Slab Rate</option>
										<option value="3">Per Kg Rate</option>
    								</select>
        						</div>
		                   </div>
		                   <div class="col-sm-2 col-md-2 col-lg-2" style="margin-top: 5px;">
                				<label for="" class="control-label">Select Mode <span style='color:red;'>*</span></label>
        						<div class="" style="margin-top: 5px;">
        							 <select name="mode" id="mode" class="form-control select2">
										<option value="SURFACE">SURFACE</option>
										<option value="EXPRESS">EXPRESS</option>
    								</select>
        						</div>
		                   </div>
		                    <div class="col-sm-2 col-md-2 col-lg-2" style="margin-top: 5px;">
                				<label for="" class="control-label">Select POD Type <span style='color:red;'>*</span></label>
        						<div class="" style="margin-top: 5px;">
        							 <select name="pod_type" id="pod_type" class="form-control select2">
										<option value="Pickup POD">Pickup POD</option>
										<option value="Delivery POD">Delivery POD</option>
    								</select>
        						</div>
		                   </div>
		                   <div class="clearfix"></div>
		                   
		                   <div class="col-6 col-sm-6 col-md-6 col-lg-6" style="margin-top: 10px;">
    					 	    <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-sm-4 col-md-4 col-lg-4" style="margin-top: 5px;">
                                				<label for="" class="control-label">From Rate Type <span style='color:red;'>*</span></label>
                        						<div class="" style="margin-top: 5px;">
                        							 <select name="from_rate_type" id="from_rate_type" class="form-control select2">
                										<option value="1">City Wise</option>
                										<option value="2">Zone Wise</option>
                    								</select>
                        						</div>
                		                   </div>
                		                   
                		                   <div class="col-sm-8 col-md-8 col-lg-8" style="margin-top: 5px;" id="div_from_city">
                                				<label for="" class="control-label">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" style="width:100%;">
                    								</select>
                        						</div>
                		                   </div>
                		                   <div class="col-sm-8 col-md-8 col-lg-8" style="margin-top: 5px;" id="div_from_zone">
                                				<label for="" class="control-label">From Zone <span style='color:red;'>*</span></label>
                        						<div class="" style="margin-top: 5px;">
                        							 <select name="from_zone" id="from_zone" class="form-control select2" style="width:100%;">
                    								</select>
                        						</div>
                		                   </div>
                		                   
                		                   
                		                   
                		                   
            					        </div>
            					   </div>
            					 </div>
            				</div>
            			   <div class="col-6 col-sm-6 col-md-6 col-lg-6" style="margin-top: 10px;">
    					 	<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-sm-4 col-md-4 col-lg-4" style="margin-top: 5px;">
                                				<label for="" class="control-label">To Rate Type <span style='color:red;'>*</span></label>
                        						<div class="" style="margin-top: 5px;">
                        							 <select name="to_rate_type" id="to_rate_type" class="form-control select2">
                										<option value="1">City Wise</option>
                										<option value="2">Zone Wise</option>
                    								</select>
                        						</div>
                		                   </div>
                		                   
                		                   <div class="col-sm-8 col-md-8 col-lg-8" style="margin-top: 5px;" id="div_to_city">
                                				<label for="" class="control-label">To City <span style='color:red;'>*</span></label>
                        						<div class="" style="margin-top: 5px;">
                        							 <select name="to_city" id="to_city" class="form-control select2" style="width:100%;">
                    								</select>
                        						</div>
                		                   </div>
                		                   <div class="col-sm-8 col-md-8 col-lg-8" style="margin-top: 5px;" id="div_to_zone">
                                				<label for="" class="control-label">To Zone <span style='color:red;'>*</span></label>
                        						<div class="" style="margin-top: 5px;">
                        							 <select name="to_zone" id="to_zone" class="form-control select2" style="width:100%;">
                    								</select>
                        						</div>
                		                   </div>
                		                   
                		                   
                		                   
                		                   
            					        </div>
            					   </div>
            					 </div>
            				</div>
		                   <div class="clearfix"></div>
		                    <!--<div class="col-sm-12" style="margin-top: 5px;">
								<center>
								    <span class="btn btn-warning btn-sm"><i class="fa fa-eye"></i> GO & SHOW RATE UPDATE DETAILS</span> 
								</center>
							</div>-->
		                   
		                   <div class="col-sm-3 col-md-3 col-lg-3 normal_rate" style="margin-top: 1px;" >
                				<label for="" class="control-label">Per Kg Min Rate <span style='color:red;'>*</span></label>
        						<div class="" style="margin-top: 5px;">
        							<input type="text" name="min_rate" class="form-control normal_rate_txt" onkeypress="return isNumberKey(this, event);"/>
        						</div>
		                   </div>
		                  
		                   
		                   
		                   <div class="clearfix"></div>
		                   <div class="col-sm-12 col-md-12 col-lg-12" style="margin-top: 10px;">
		                       <!--<table style="width:100%;" id="table_pcs_rate">
		                           <tr>
		                               <th>Action</th>
		                               <th style="width:10%">ID</th>
		                               <th style="width:30%">From</th>
		                               <th style="width:30%">To</th>
		                               <th>Rate (Per Kg)</th>
		                           </tr>
		                           <tr>
		                               <td>
		                                   <button class="btn btn-danger btn-xs mr-2" contenteditable="false" style="cursor: pointer;"><i class="fa fa-trash"></i> Delete</button>
		                               </td>
		                               <td></td>
		                               <td style="width:30%"> 
		                                   <select name="" id="" class="form-control select2">
        										<?php 
            							        $statement = $pdo->prepare("SELECT * FROM `pincode` where 1=1 GROUP by city ORDER by city ASC");
                    							$statement->execute();
                    							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                        						 foreach($result as $rows) { ?>
        											<option value="<?=$rows['id'];?>"> <?=$rows['city']; ?></option>
        									    <?php } ?>
            								</select>
		                               
		                               </td>
		                               <td style="width:30%">
		                                    <select name="" id="" class="form-control select2">
        										<?php 
            							        $statement = $pdo->prepare("SELECT * FROM `pincode` where 1=1 GROUP by city ORDER by city ASC");
                    							$statement->execute();
                    							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                        						 foreach($result as $rows) { ?>
        											<option value="<?=$rows['id'];?>"> <?=$rows['city']; ?></option>
        									    <?php } ?>
            								</select>
		                               
		                               </td>
		                               <td><input type="text" class="form-control input-sm" onkeypress="return isNumberKey(this, event);"/></td>
		                           </tr>
		                           <tfoot>
		                               <td></td>
		                               <td></td>
		                               <td></td>
		                               <td></td>
		                               <td>
		                                   <button class="btn btn-info btn-xs mr-2" contenteditable="false" style="cursor: pointer;"><i class="fa fa-plus"></i> Add New City Row</button>
		                               </td>
		                           </tfoot>
		                       </table>-->
		                       
		                       
		                       <table style="width:100%;" id="table_slab_rate">
		                           <tr>
		                               <th>Action</th>
		                               <th style="width:5%">ID</th>
		                               <th style="width:20%">From City</th>
		                               <th style="width:20%">Destination City</th>
		                               <th class="slab_rate">Min Weight</th>
		                               <th class="slab_rate">Max Weight</th>
		                               <th>Rate (Per Kg)</th>
		                           </tr>
		                           <tbody id="table_slab_rate_body">
		                               
		                           </tbody>
		                           
		                           <tfoot>
		                               <td></td>
		                               <td></td>
		                               <td></td>
		                               <td></td>
		                               <td class="slab_rate"></td>
		                               <td class="slab_rate"></td>
		                               <td>
		                                   
		                                   <span onclick="table_row_append()" class="btn btn-info btn-xs mr-2" contenteditable="false" style="cursor: pointer;"><i class="fa fa-plus"></i> Add New City Row</span>
		                               </td>
		                           </tfoot>
		                       </table>
		                   </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 }?>
<?php require_once('footer.php'); ?>
<script>
$(document).ready(function() {
    from_rate_type();
    to_rate_type();
    rate_type();
    //get_rows()
    $('#form_details').on('submit', function(e){
                e.preventDefault();
                $('#loading-div').show();
                $('#content-div').hide();
                $("#submit").prop('disabled', true);
                var formData = new FormData(this);
                    $.ajax({
                            url: 'apis/client-rate-entry.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();
                                }
                            }
                        });
   });
});

    $("#rate_type").change(function() {
	    rate_type();
    });
    $("#from_rate_type").change(function() {
	    from_rate_type();
    });
    $("#to_rate_type").change(function() {
	    to_rate_type();
    });
    
    $("#client_id,#rate_type,#mode,#pod_type,#from_rate_type,#from_city,#from_zone,#to_rate_type,#to_city,#to_zone").change(function() {
	    get_rows();
    });
    
    
    
    function rate_type(){
        var rate_type=$('#rate_type').val();
        if(rate_type==2){ //slab
            $('.slab_rate').show();
            $('.normal_rate').hide();
            $('.normal_rate_txt').val(0);
            
        }else{
            $('.normal_rate').show();
            $('.slab_rate').hide();
            $('.slab_rate_txt').val('0');
            
        }
    }
    function from_rate_type(){
        var from_rate_type=$('#from_rate_type').val();
        if(from_rate_type==1){
            $('#from_zone').empty();
            $('#div_from_city').show();
            $('#div_from_zone').hide();
            get_from_city();
        }else{
            $('#from_city').empty();
            $('#div_from_city').hide();
            $('#div_from_zone').show();
            get_from_zone();
        }
}
    function to_rate_type(){
        var to_rate_type=$('#to_rate_type').val();
        if(to_rate_type==1){
            $('#to_zone').empty();
            $('#div_to_city').show();
            $('#div_to_zone').hide();
            get_to_city();
        }else{
            $('#to_city').empty();
            $('#div_to_city').hide();
            $('#div_to_zone').show();
            get_to_zone();
        }
}
    function get_from_city(){
        $('#from_city').empty();
        $.ajax({
            url: 'apis/get-drop-down-list.php',
            type: 'post',
            data: {type:'get_city_dropdown'},
             success:function(response){
                 //console.log(response);
                $('#from_city').empty();
                $('#from_city').append(response);
            }
        });
    }
    function get_from_zone(){
        $('#from_zone').empty();
        $.ajax({
            url: 'apis/get-drop-down-list.php',
            type: 'post',
            data: {type:'get_zone_dropdown'},
             success:function(response){
                 //console.log(response);
                $('#from_zone').empty();
                $('#from_zone').append(response);
            }
        });
    }
    function get_to_city(){
        $('#to_city').empty();
        $.ajax({
            url: 'apis/get-drop-down-list.php',
            type: 'post',
            data: {type:'get_city_dropdown'},
             success:function(response){
                // console.log(response);
                $('#to_city').empty();
                $('#to_city').append(response);
            }
        });
    }
    function get_to_zone(){
        $('#to_zone').empty();
        $.ajax({
            url: 'apis/get-drop-down-list.php',
            type: 'post',
            data: {type:'get_zone_dropdown'},
             success:function(response){
                // console.log(response);
                $('#to_zone').empty();
                $('#to_zone').append(response);
            }
        });
    }
    function table_row_append(){
        $.ajax({
            url: 'apis/get-table-row.php',
            type: 'post',
            data: {type:'get_table'},
             success:function(response){
                $('#table_slab_rate_body').append(response);
                rate_type();
            }
        });
    }
    
    function get_rows(){
        $('#table_slab_rate_body').empty();
        var client_id=$('#client_id').val();
        var rate_type=$('#rate_type').val();
        var mode=$('#mode').val();
        var pod_type=$('#pod_type').val();
        var from_rate_type=$('#from_rate_type').val();
        var from_city=$('#from_city').val();
        var from_zone=$('#from_zone').val();
        var to_rate_type=$('#to_rate_type').val();
        var to_city=$('#to_city').val();
        var to_zone=$('#to_zone').val();
        $.ajax({
            url: 'apis/get-existing-table-row.php',
            type: 'post',
            data: {type:'get_exisitng_table',client_id:client_id,rate_type:rate_type,mode:mode,pod_type:pod_type,from_rate_type:from_rate_type,from_city:from_city,from_zone:from_zone,to_rate_type:to_rate_type,to_city:to_city,to_zone:to_zone},
             success:function(response){
                 //console.log(response);
                $('#table_slab_rate_body').append(response);
                rate_types();
            }
        });
    }
   
    $(document).on('click', '.remove_man_delete', function() {
        if (confirm('Are You Sure Want to Delete This Row Details?')) {
            $(this).closest('.man_del').remove();
        }
        
    });
    $(document).on('click', '.remove_db_delete', function() {
        if (confirm('Are You Sure Want to Delete This Row Details?')) {
            
            var row = $(this).closest('.db_delete');
            var delete_id = row.find('input[name="ids[]"]').val();
            $.ajax({
                url: 'apis/delete.php',
                type: 'post',
                data: {type:'tbl_client_rate_master_delete',delete_id:delete_id},
                 success:function(response){
                     console.log(response);
                }
            });
            $(this).closest('.db_delete').remove();
        }
    });
    
    function rate_types(){
        var rate_type=$('#rate_type').val();
        if(rate_type==2){ //slab
            $('.slab_rate').show();
            $('.normal_rate').hide();
            $('.normal_rate_txt').val(0);
            
        }else{
            $('.normal_rate').show();
            $('.slab_rate').hide();
            $('.slab_rate_txt').val('0');
            
        }
    }




</script>


<style>
    th{
        padding:5px 5px;
        border:1px solid blueviolet;
        text-align:center;
    }td{
        padding:5px 5px;
        text-align:center;
    }
</style>



