<?php 
ob_start();
session_start();
include "../inc/config.php";
$client_id=$_GET['client_id'] ?? 0;
?>

<div class="nav-tabs-custom">
					<ul class="nav nav-tabs">
						<li class="active"><a href="#tab_1" data-toggle="tab">Surface - Rate Card</a></li>
						<li><a href="#tab_2" data-toggle="tab">Express - Rate Card</a></li>
						<!--<li><a href="#tab_3" data-toggle="tab">5 kg Rate Card </a></li>-->
					</ul>
					<div class="tab-content">
					<!--******************************************************-->
          				<div class="tab-pane active" id="tab_1">
                            <div class="row">
                                <form class="form-horizontal" action="" method="post" id="surface_rate_card" enctype="multipart/form-data">
                                    <input type="hidden" class="client_ids" value="<?=$client_id?>" name="client_ids">
                                <?php 
                                    $statement = $pdo->prepare("SELECT * FROM `tbl_rate_type` where rate_type_mode='Surface'");
                    				$statement->execute();
                    				$rate_type_result = $statement->fetchAll(PDO::FETCH_ASSOC);
                    				foreach($rate_type_result as $res){
                                ?>
                                <div class='col-md-4 col-sm-6' style="padding: 0px 8px;">
                                    <div style="margin: 5px 1px;box-shadow: rgba(0, 0, 0, 0.19) 0px 10px 20px, rgba(0, 0, 0, 0.23) 0px 6px 6px;background-color: white;">    
                                        <table style="width:100%;">
                                            <tr>
                                                <th colspan="6" style="padding:5px 5px;background: #7489b7;color: pink;font-weight: bold;">
                                                    <center><b><?=$res['rate_type_name']?></b></center>
                                                    <input type="hidden" value="<?=$res['id']?>" name="rate_type_id[]">
                                                    
                                                </th>
                                            </tr>
                                            <tr>
                                                <th>Min Wt</th><th>Max Wt</th><th>Rate</th><th>Above Wt</th><th>Above Wt Rate</th><th>Type</th>
                                            </tr>
                                             <?php 
                                                $statement = $pdo->prepare("SELECT * FROM `tbl_rate_card` where client_id='".$client_id."' and rate_type_id=".$res['id']);
                                				$statement->execute();
                                				$rate_chart_result = $statement->fetchAll(PDO::FETCH_ASSOC);
                                			if($rate_chart_result){	
                                				foreach($rate_chart_result as $res_c){
                                            ?>
                                            <tr>
                                                <td style="width:15%;"><input name="rate_card_id<?=$res['id']?>[]" type="hidden" value="<?=$res_c['id']?>" class="form-control" readonly/>
                                                                        <input name="min_wt<?=$res['id']?>[]" type="text" value="<?=$res_c['min_wt']?>" class="form-control" readonly/></td>
                                                <td style="width:15%;"><input name="max_wt<?=$res['id']?>[]" type="text" value="<?=$res_c['max_wt']?>" class="form-control" readonly/></td>
                                                <td style="width:15%;"><input name="rate<?=$res['id']?>[]" type="text" value="<?=$res_c['rate']?>" class="form-control"/></td>
                                                <td style="width:15%;"><input name="above_wt<?=$res['id']?>[]" type="text" value="<?=$res_c['above_wt']?>" class="form-control"/></td>
                                                <td style="width:15%;"><input name="above_wt_rate<?=$res['id']?>[]" type="text" value="<?=$res_c['above_wt_rate']?>" class="form-control"/></td>
                                                <td><select name="ship_type<?=$res['id']?>[]" class="form-control"><option value="Forward">Forward</option>
                                                <option value="RTO">RTO</option>
                                                <option value="DTO">DTO</option>
                                                </select></td>
                                            </tr>
                                            <?php } 
                                            }else{
                                                $statement = $pdo->prepare("SELECT * FROM `tbl_rate_card` where client_id=0 and rate_type_id=1");
                                				$statement->execute();
                                				$rate_chart_result = $statement->fetchAll(PDO::FETCH_ASSOC);
                                			if($rate_chart_result){	
                                				foreach($rate_chart_result as $res_c){
                                            ?>
                                            <tr>
                                                <td style="width:15%;"><input name="rate_card_id<?=$res['id']?>[]" type="hidden" value="0" class="form-control" readonly/>
                                                                        <input style="background: aqua;" name="min_wt<?=$res['id']?>[]" type="text" value="<?=$res_c['min_wt']?>" class="form-control" readonly/></td>
                                                <td style="width:15%;"><input name="max_wt<?=$res['id']?>[]" type="text" value="<?=$res_c['max_wt']?>" class="form-control" readonly/></td>
                                                <td style="width:15%;"><input name="rate<?=$res['id']?>[]" type="text" value="<?=$res_c['rate']?>" class="form-control"/></td>
                                                <td style="width:15%;"><input name="above_wt<?=$res['id']?>[]" type="text" value="<?=$res_c['above_wt']?>" class="form-control"/></td>
                                                <td style="width:15%;"><input name="above_wt_rate<?=$res['id']?>[]" type="text" value="<?=$res_c['above_wt_rate']?>" class="form-control"/></td>
                                                <td><select name="ship_type<?=$res['id']?>[]" class="form-control"><option value="Forward">Forward</option>
                                                <option value="RTO">RTO</option>
                                                <option value="DTO">DTO</option>
                                                </select></td>
                                            </tr>
                                            <?php } 
                                            }
                                            
                                             }?>
                                        </table>
                                    </div>
                                </div>
                                <?php } ?>
                                <button type="submit" id='surface_rate_card_form_submit' class="btn btn-success pull-right" name="form1"><span id='submit_txt'><i id='submit_spin'></i> Update Rate Chart Details</span></button>
                            
                                </form>
                            </div>
          				</div>
          			<!--******************************************************-->
          				<!--******************************************************-->
          				<div class="tab-pane" id="tab_2">
                            <div class="row">
                                <form class="form-horizontal" action="" method="post" id="express_rate_card" enctype="multipart/form-data">
                                    <input type="hidden" class="client_ids" value="<?=$client_id?>" name="client_ids">
                                <?php 
                                    $statement = $pdo->prepare("SELECT * FROM `tbl_rate_type` where rate_type_mode='Express'");
                    				$statement->execute();
                    				$rate_type_result = $statement->fetchAll(PDO::FETCH_ASSOC);
                    				foreach($rate_type_result as $res){
                                ?>
                                <div class='col-md-4 col-sm-6' style="padding: 0px 8px;">
                                    <div style="margin: 5px 1px;box-shadow: rgba(0, 0, 0, 0.19) 0px 10px 20px, rgba(0, 0, 0, 0.23) 0px 6px 6px;background-color: white;">    
                                        <table style="width:100%;">
                                            <tr>
                                                <th colspan="6" style="padding:5px 5px;background: #7489b7;color: pink;font-weight: bold;">
                                                    <center><b><?=$res['rate_type_name']?></b></center>
                                                    <input type="hidden" value="<?=$res['id']?>" name="rate_type_id[]">
                                                </th>
                                            </tr>
                                            <tr>
                                                <th>Min Wt</th><th>Max Wt</th><th>Rate</th><th>Above Wt</th><th>Above Wt Rate</th><th>Type</th>
                                            </tr>
                                             <?php 
                                                $statement = $pdo->prepare("SELECT * FROM `tbl_rate_card` where client_id='".$client_id."' and rate_type_id=".$res['id']);
                                				$statement->execute();
                                				$rate_chart_result = $statement->fetchAll(PDO::FETCH_ASSOC);
                                			if($rate_chart_result){	
                                				foreach($rate_chart_result as $res_c){
                                            ?>
                                            <tr>
                                                <td style="width:15%;"><input name="rate_card_id<?=$res['id']?>[]" type="hidden" value="<?=$res_c['id']?>" class="form-control" readonly/>
                                                                        <input name="min_wt<?=$res['id']?>[]" type="text" value="<?=$res_c['min_wt']?>" class="form-control" readonly/></td>
                                                <td style="width:15%;"><input name="max_wt<?=$res['id']?>[]" type="text" value="<?=$res_c['max_wt']?>" class="form-control" readonly/></td>
                                                <td style="width:15%;"><input name="rate<?=$res['id']?>[]" type="text" value="<?=$res_c['rate']?>" class="form-control"/></td>
                                                <td style="width:15%;"><input name="above_wt<?=$res['id']?>[]" type="text" value="<?=$res_c['above_wt']?>" class="form-control"/></td>
                                                <td style="width:15%;"><input name="above_wt_rate<?=$res['id']?>[]" type="text" value="<?=$res_c['above_wt_rate']?>" class="form-control"/></td>
                                                <td><select name="ship_type<?=$res['id']?>[]" class="form-control"><option value="Forward">Forward</option>
                                                <option value="RTO">RTO</option>
                                                <option value="DTO">DTO</option>
                                                </select></td>
                                            </tr>
                                            <?php } 
                                            }else{
                                                $statement = $pdo->prepare("SELECT * FROM `tbl_rate_card` where client_id=0 and rate_type_id=9");
                                				$statement->execute();
                                				$rate_chart_result = $statement->fetchAll(PDO::FETCH_ASSOC);
                                			if($rate_chart_result){	
                                				foreach($rate_chart_result as $res_c){
                                            ?>
                                            <tr>
                                                <td style="width:15%;"><input name="rate_card_id<?=$res['id']?>[]" type="hidden" value="0" class="form-control" readonly/>
                                                                        <input style="background: aqua;" name="min_wt<?=$res['id']?>[]" type="text" value="<?=$res_c['min_wt']?>" class="form-control" readonly/></td>
                                                <td style="width:15%;"><input name="max_wt<?=$res['id']?>[]" type="text" value="<?=$res_c['max_wt']?>" class="form-control" readonly/></td>
                                                <td style="width:15%;"><input name="rate<?=$res['id']?>[]" type="text" value="<?=$res_c['rate']?>" class="form-control"/></td>
                                                <td style="width:15%;"><input name="above_wt<?=$res['id']?>[]" type="text" value="<?=$res_c['above_wt']?>" class="form-control"/></td>
                                                <td style="width:15%;"><input name="above_wt_rate<?=$res['id']?>[]" type="text" value="<?=$res_c['above_wt_rate']?>" class="form-control"/></td>
                                                <td><select name="ship_type<?=$res['id']?>[]" class="form-control"><option value="Forward">Forward</option>
                                                <option value="RTO">RTO</option>
                                                <option value="DTO">DTO</option>
                                                </select></td>
                                            </tr>
                                            <?php } 
                                            }
                                            
                                             }?>
                                        </table>
                                    </div>
                                </div>
                                <?php } ?>
                                <button type="submit" id='express_rate_card_form_submit' class="btn btn-success pull-right" name="form1"><span id='submit_txt'><i id='submit_spin'></i> Update Rate Chart Details</span></button>
                            
                                </form>
                            </div>
          				</div>
          			<!--******************************************************-->
          			
          			</div>
				</div>
				
<script>
$(document).ready(function() {
        $('#surface_rate_card_form_submit').on('click', function(e) {
			e.preventDefault();
			$.ajax({
					type: 'POST',
					url: 'apis/rate-chart-modify.php',
					data: $("#surface_rate_card").serialize(),
					beforeSend: function() {
						$('#submit_spin').addClass('fa fa-spinner fa-spin');
					},
					dataType: 'json',
					success: function(response) {
					    $("#form_submit").prop('disabled', true);
			            $('#submit_spin').removeClass('fa fa-spinner fa-spin');
			            console.log(response);
			            client_changed();
					}
				});
    });
    
    $('#express_rate_card_form_submit').on('click', function(e) {
			e.preventDefault();
			$.ajax({
					type: 'POST',
					url: 'apis/rate-chart-modify.php',
					data: $("#express_rate_card").serialize(),
					beforeSend: function() {
						$('#submit_spin').addClass('fa fa-spinner fa-spin');
					},
					dataType: 'json',
					success: function(response) {
					    $("#form_submit").prop('disabled', true);
			            $('#submit_spin').removeClass('fa fa-spinner fa-spin');
			            console.log(response);
			            client_changed();
					}
				});
    });
});
				
				