<?
session_start();
$result = 0;
$query_string = "";
$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
if(isset($_REQUEST['action']) && $_REQUEST['action']=='update'){
	///////////////////////////// Edit and Update coding /////////////////////////////////////////////////////////////////
	if(isset($_REQUEST['shipping_option_text'])){
		$site_shipping_id = $_REQUEST['site_shipping_policy_id'];
		$admin_user_id = $_SESSION['admin_user_id'];
		foreach($_REQUEST['shipping_option_text'] as $key=>$val){  
			if(is_array($_REQUEST['shipping_option_text'][$key])){
				foreach($_REQUEST['shipping_option_text'][$key] as $k=>$v){
					$current_date = date("Y-m-d");
					$site_shipping_policy_id = $k;
					$shipping_option_text = $_REQUEST['shipping_option_text'][$key][$k];
					$shipping_option_amount = $_REQUEST['shipping_option_amount'][$key][$k];
					$query = "UPDATE site_shipping_policy_delivery_options SET 
							shipping_option_text = '".$shipping_option_text."',
							shipping_option_amount = '".$shipping_option_amount."',
							updated_on = '".$current_date."'
							WHERE id = '".$site_shipping_policy_id."'";
					mysqli_query($conn,$query);
				}
			}else{
				$shipping_option_text = $_REQUEST['shipping_option_text'][$key];
				if($shipping_option_text == ""){
					continue;
				}
				$shipping_option_amount = $_REQUEST['shipping_option_amount'][$key];
				$query = "INSERT INTO site_shipping_policy_delivery_options(site_shipping_policy_id ,shipping_option_text,shipping_option_amount,updated_by) VALUES('".$site_shipping_id."','".$shipping_option_text."','".$shipping_option_amount."','".$admin_user_id."')";
				mysqli_query($conn,$query);
			}
		   
		}
		$result = 1;
	}
	//////////////////////////////////////// End edit and update coding /////////////////////////////////////////////
	if($result>0){
		$returnArray['status'] = "success";
		$returnArray['message'] = "Delivery Options updated sucessfully.";
	}else{
		$returnArray['message'] = "Error : Delivery Options not updated sucessfully.";
	}
	echo json_encode($returnArray);	
	die;
	 
	 
}

$id=$_REQUEST['id'];
$sql= "SELECT * FROM  `site_shipping_policy_delivery_options` WHERE site_shipping_policy_id = $id ORDER BY shipping_option_amount ";
$result_edit = mysqli_query($conn,$sql);
?>
<td width="100%" align="center" valign="top" bgcolor="" class="red">
<form method="post" id="booking_form" name="booking_form" onsubmit="return" action="" >
    <table width="800"  cellspacing="10" cellpadding="5" style="text-align:left;border:0px solid #dadada;margin:4px;margin-top:25px">
        <tbody>
			<tr>
                <td colspan="3" class="label" align="center"><h1>Add/Update delivery options.</h1></td>                
            </tr>
			
			<? if($result_edit){
			?>			
			<tr>
				<td>
				
					<table width="100%">
						<tr>
							<td width="80%" class="label" align="left"><h1>Shipping Text.</h1></td>
							<td width="10%" class="label" align="left"><h1>Amout</h1></td>
							<td width="10%" class="label" align="right"><h1>Delete</h1></td>
						</tr>
					</table>
				
				</td>
            </tr>			
			<? while($row_edit = mysqli_fetch_assoc($result_edit)){?>
				<tr>
					<td>					
						<table>
							<tr class="div_to_clone_class">
							<td class="label" align="left"><input size="100" type="text" name="shipping_option_text[][<? echo $row_edit['id'] ?>]" value="<? echo $row_edit['shipping_option_text'] ?>"></td>
							<td class="label" align="left"><input type="text" name="shipping_option_amount[][<? echo $row_edit['id'] ?>]" value="<? echo $row_edit['shipping_option_amount'] ?>"></td>
							<td align="right" style="float:right;width:63px;cursor:pointer;">
								  <a class="multiform_row_remover" title="<?php echo $row_edit['id'];?>"><img src="../images/del.gif" border="0" /></a>
							</td>
							</tr>
						</table>					
					</td>
				</tr>
			<? } ?>	
			<?}?>
			<tr id="div_to_clone" style="display:none" class="div_to_clone_class">		  
			<td style="border:0x solid red">
				<div>
					<table>
						<tr>
							<td class="label" align="left"><input size="100" type="text" name="shipping_option_text[]" value=""></td>
							<td class="label" align="left"><input type="text" name="shipping_option_amount[]" value=""></td>
							<td style="float:right;width:63px;cursor:pointer;">
								  <a class="multiform_row_remover" title=""><img src="../images/del.gif" border="0" style="padding-top:8px;cursor:pointer;float:right;margin-top:-6px"/></a>
							</td>
						</tr>
					</table>
				</div>
			</td>    		
		</tr>
			<tr>
				<td colspan="10" align="left"><span id="add_new_row" style="cursor:pointer"><b style="font-size:15px;">Add new (+)</b></span>	</td>
			</tr>
			<tr>
                
				<td colspan="2" align="left"><input class="reg_sbmt_btn" id="submit_form" type="submit" value="Submit" name="submit" /></td>
				<td>
				&nbsp;<input type="hidden" id="site_shipping_policy_id" name="site_shipping_policy_id" value="<? echo $id ?>" >
				<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
				
				</td>
                
            </tr>
        </tbody>
    </table>
	
</form>
</td>
<style>
#ui-datepicker-div{
	z-index:10000;
}
</style>
<script language="JavaScript">
$(function(){	
	////////////////////////////// For deleting multiple row //////////////////////////////////////////////////
	$('.multiform_row_remover').live("click",function(){
		if(confirm('Are you sure! You want to delete')){
			var site_shipping_policy_id = $(this).attr("title");
			if(site_shipping_policy_id){
			var form_data 	= {'site_shipping_policy_id' : site_shipping_policy_id };				
				$.ajax({
					async: false,
					type: "POST",
					url: site_url+"/sites/index?page=ajax_action&action=delete_shipping_policy",
					data: form_data,
					success: function(result){
						var resultObj = jQuery.parseJSON(result);
						if(resultObj.status == "success"){					
							alert(resultObj.message);
							//window.location = document.location.href;
						}else{
							alert(resultObj.message);
						}
					}
				});
			}			
			//$(this).parent().parent().remove();
			$(this).parents(".div_to_clone_class").remove();
		}					
		return false;
	});
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	////////////////////////// Cloning /////////////////////////////////
	$('#add_new_row').click(function() {
		$('#div_to_clone').show();
		var cloned_div = $('#div_to_clone').clone();
		$('#div_to_clone').live().after(cloned_div);
		$('#div_to_clone').hide();			
	});
	////////////////////////////////////////////////////////////////////
});
</script>