<?php
session_start();
require("php_con.php");
//require("check_user.php");
require_once("function.php");
if(isset($_COOKIE['user_id'])){
}
else{
	echo "<script>window.location='logout';</script>";
}

$success_msg=0;

if(isset($_GET['product_id'])){
	$product_id=mysqli_real_escape_string($connation,$_GET['product_id']);
	$edit_qury=mysqli_query($connation,"SELECT * FROM product_details WHERE product_id='$product_id'");
	$edit_resalt=mysqli_fetch_array($edit_qury);
	
	$edit_price_qury=mysqli_query($connation,"SELECT * FROM price_details WHERE price_systemid='$edit_resalt[product_systemid]' ORDER BY price_id DESC");
	$edit_price_resalt=mysqli_fetch_array($edit_price_qury);	
	
	$a=$edit_price_resalt['price_sale']-$edit_price_resalt['price_buy'];
	$gp_rate=$a/$edit_price_resalt['price_sale']*100;
	
	$system_id=$edit_resalt['product_systemid'];
	$system_product_id=$edit_resalt['product_id'];
}
else{
	$system_id=time();
	$system_product_id=time();
}

if(isset($_POST['add_bt'])){
	
	$product_systemid=mysqli_real_escape_string($connation,$_POST['product_systemid']);
	$system_product_id=mysqli_real_escape_string($connation,$_POST['system_product_id']);
	$product_barcode=mysqli_real_escape_string($connation,$_POST['product_barcode']);
	$product_productid=mysqli_real_escape_string($connation,$_POST['product_productid']);
	$product_name=mysqli_real_escape_string($connation,$_POST['product_name']);
	$product_name_sinhala=mysqli_real_escape_string($connation,$_POST['product_name_sinhala']);
	$product_alertqty=mysqli_real_escape_string($connation,$_POST['product_alertqty']);
	$product_supplier=mysqli_real_escape_string($connation,$_POST['product_supplier']);
	$product_catagory=mysqli_real_escape_string($connation,$_POST['product_catagory']);
	$product_unit=mysqli_real_escape_string($connation,$_POST['product_unit']);
	
	$price_name=mysqli_real_escape_string($connation,$_POST['price_name']);
	$price_buy=mysqli_real_escape_string($connation,$_POST['price_buy']);
	$price_sale=mysqli_real_escape_string($connation,$_POST['price_sale']);
	$price_wholesale=mysqli_real_escape_string($connation,$_POST['price_wholesale']);
	$price_discount=mysqli_real_escape_string($connation,$_POST['price_discount']);
	$price_special=mysqli_real_escape_string($connation,$_POST['price_special']);
	
	$stock_qty=mysqli_real_escape_string($connation,$_POST['stock_qty']);
	
	if(empty($_POST['product_stock_maintain'])){
		$product_stock_maintain=0;
	}
	else{
		$product_stock_maintain=1;
	}
	
	if(empty($_POST['product_add_discount'])){
		$product_add_discount=0;
	}
	else{
		$product_add_discount=1;
	}
	
	if(empty($_POST['product_affect_stock'])){
		$product_affect_stock=0;
	}
	else{
		$product_affect_stock=1;
	}	
	
	
	if(mysqli_query($connation,"INSERT INTO product_details(product_systemid, product_barcode, product_productid, product_name, product_name_sinhala, product_add_discount, product_alertqty, product_supplier, product_catagory, product_unit, product_stock_maintain, product_affect_stock, product_addtime) VALUES ('$product_systemid','$product_barcode','$product_productid','$product_name','$product_name_sinhala','$product_add_discount','$product_alertqty','$product_supplier','$product_catagory','$product_unit','$product_stock_maintain','$product_affect_stock','$current_date $current_time')")){
		
		$pad_main_product=mysqli_insert_id($connation);
		
		mysqli_query($connation,"INSERT INTO price_details(price_systemid, price_name, price_buy, price_sale, price_wholesale, price_special, price_discount, price_addtime) VALUES ('$product_systemid','$price_name','$price_buy','$price_sale','$price_wholesale','$price_special','$price_discount','$current_date $current_time')");
		
		mysqli_query($connation,"INSERT INTO stock_details(stock_systemid, stock_qty, stock_type, stock_info, stock_addtime) VALUES ('$product_systemid','$stock_qty','1','Opening Stock','$current_date $current_time')");
		
		mysqli_query($connation,"UPDATE product_affect_details SET pad_main_product='$pad_main_product' WHERE pad_main_product='$system_product_id'");
		
		$success_msg=1;
	}
	else{
		$success_msg=2;
	}
	
	if(empty($_POST['check_barcode'])){}
	else{
	$a=setting_view("next_barcode")+1;
	setting_update("next_barcode",$a);
	}
	
}

if(isset($_POST['update_bt'])){
	
	$product_id=mysqli_real_escape_string($connation,$_GET['product_id']);
	
	$product_systemid=mysqli_real_escape_string($connation,$_POST['product_systemid']);
	$product_productid=mysqli_real_escape_string($connation,$_POST['product_productid']);
	$product_name=mysqli_real_escape_string($connation,$_POST['product_name']);
	$product_name_sinhala=mysqli_real_escape_string($connation,$_POST['product_name_sinhala']);
	$product_alertqty=mysqli_real_escape_string($connation,$_POST['product_alertqty']);
	$product_supplier=mysqli_real_escape_string($connation,$_POST['product_supplier']);
	$product_catagory=mysqli_real_escape_string($connation,$_POST['product_catagory']);
	$product_unit=mysqli_real_escape_string($connation,$_POST['product_unit']);
	$product_barcode=mysqli_real_escape_string($connation,$_POST['product_barcode']);
	
	$price_buy=mysqli_real_escape_string($connation,$_POST['price_buy']);
	$price_sale=mysqli_real_escape_string($connation,$_POST['price_sale']);
	$price_wholesale=mysqli_real_escape_string($connation,$_POST['price_wholesale']);
	$price_discount=mysqli_real_escape_string($connation,$_POST['price_discount']);
	$price_special=mysqli_real_escape_string($connation,$_POST['price_special']);
	
	if(empty($_POST['product_stock_maintain'])){
		$product_stock_maintain=0;
	}
	else{
		$product_stock_maintain=1;
	}
		
	if(empty($_POST['product_add_discount'])){
		$product_add_discount=0;
	}
	else{
		$product_add_discount=1;
	}
	
	if(empty($_POST['product_affect_stock'])){
		$product_affect_stock=0;
	}
	else{
		$product_affect_stock=1;
	}	
	
	if(mysqli_query($connation,"UPDATE product_details SET product_barcode='$product_barcode',product_productid='$product_productid',product_name='$product_name',product_name_sinhala='$product_name_sinhala',product_add_discount='$product_add_discount',product_alertqty='$product_alertqty',product_stock_maintain='$product_stock_maintain',product_affect_stock='$product_affect_stock',product_supplier='$product_supplier',product_catagory='$product_catagory',product_unit='$product_unit',product_addtime='$current_date $current_time' WHERE product_id='$product_id'")){
		
		mysqli_query($connation,"UPDATE price_details SET price_buy='$price_buy',price_sale='$price_sale',price_wholesale='$price_wholesale',price_special='$price_special',price_discount='$price_discount',price_addtime='$current_date $current_time' WHERE price_id='$edit_price_resalt[price_id]'");
		
		mysqli_query($connation,"UPDATE sale_list SET sale_barcode='$product_barcode' WHERE sale_barcode='$edit_resalt[product_barcode]'");
		
		mysqli_query($connation,"UPDATE return_details SET rd_barcode='$product_barcode' WHERE rd_barcode='$edit_resalt[product_barcode]'");
		
		echo "<script>window.location='edit-product?product_id=$product_id&updated';</script>";
	}
	else{
		$success_msg=4;
	}
	
	if(empty($_POST['check_barcode'])){}
	else{
	$a=setting_view("next_barcode")+1;
	setting_update("next_barcode",$a);
	}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title><?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
	
<script src="data_table/js/jquery-3.5.1.js"></script>
<script src="data_table/js/jquery.dataTables.min.js"></script>
<script src="data_table/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">
	
<link rel="stylesheet" href="alert/sweetalert2.min.css">
<script src="alert/sweetalert2.min.js"></script>
	
<link rel="stylesheet" href="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
</head>
<style>
	table tr td{
		font-size: 14px;
		padding: 5px;
	}
</style>
<body>
<script>
window.addEventListener("load",function(){
	var load_screen=document.getElementById('load_screen');
	document.body.removeChild(load_screen);
});
</script>

<div id="load_screen"><div id="loading"><img src="image/loading.gif" width="80px;"></div></div>
	
<?php require("header.php"); ?>
	
<div class="contaner">
<div class="row">
<form method="post" class="input-form col-lg-6 col-sm-12 m-auto" autocomplete="off">
	
<input name="product_systemid" type="hidden" class="form-control form-control-sm" readonly value="<?php echo $system_id ?>">
<input name="system_product_id" type="hidden" class="form-control form-control-sm" id="system_product_id" value="<?php echo $system_product_id ?>" readonly>
	
<div style="margin-bottom: 10px;"><a href="product" class="btn btn-dark btn-sm"><i class="fa fa-backward"></i> Back to Product</a></div>
	
<input name="next_barcode" type="hidden" id="next_barcode" value="<?php echo setting_view("next_barcode"); ?>">
	
<table style="width: 100%;">
<tbody>
	
<tr><td colspan="2"><b>Product Details</b></td></tr>
	
<tr>
  <td> Main Supplier<br>
    <select name="product_supplier" required="required" class="form-control form-control-sm" id="product_supplier">
      <option value="" hidden="yes">Select</option>
      <?php 
$supplier_qury=mysqli_query($connation,"SELECT * FROM supplier_details ORDER BY supplier_name");
while($supplier_resalt=mysqli_fetch_array($supplier_qury)){
?>
      <option <?php if(isset($_GET['product_id'])){if($edit_resalt['product_supplier']==$supplier_resalt['supplier_id']){echo "selected";}} ?> value="<?php echo $supplier_resalt['supplier_id']; ?>"><?php echo $supplier_resalt['supplier_name']; ?> - <?php echo $supplier_resalt['supplier_company']; ?> <?php echo "0".(int)$supplier_resalt['supplier_mobile']; ?></option>
      <?php } ?>
    </select></td>
  <td>Category<br>
    <select name="product_catagory" required="required" class="form-control form-control-sm" id="product_catagory">
      <option value="" hidden="yes">Select</option>
      <?php 
$catogary_qury=mysqli_query($connation,"SELECT * FROM catogary_list ORDER BY catogary_text");
while($catogary_resalt=mysqli_fetch_array($catogary_qury)){
?>
      <option <?php if(isset($_GET['product_id'])){if($edit_resalt['product_catagory']==$catogary_resalt['catogary_id']){echo "selected";}} ?> value="<?php echo $catogary_resalt['catogary_id']; ?>"><?php echo $catogary_resalt['catogary_text']; ?></option>
      <?php } ?>
    </select></td>
</tr>
	
<tr>
  <td> Unit<br>
    <select name="product_unit" required="required" class="form-control form-control-sm" id="product_unit">
      <option value="" hidden="yes">Select</option>
      <?php 
$unit_qury=mysqli_query($connation,"SELECT * FROM unit_list ORDER BY unit_text");
while($unit_resalt=mysqli_fetch_array($unit_qury)){
?>
      <option <?php if(isset($_GET['product_id'])){if($edit_resalt['product_unit']==$unit_resalt['unit_id']){echo "selected";}} ?> value="<?php echo $unit_resalt['unit_id']; ?>"><?php echo $unit_resalt['unit_text']; ?></option>
      <?php } ?>
    </select></td>
  <td>&nbsp;</td>
  </tr>
	
<tr>
<td colspan="2">
<hr>
</td>
</tr>
	
<tr>
<td colspan="2">
Product Name <em class="text-secondary">Please don't use <b class="text-danger">'</b> or <b class="text-danger">"</b></em><br>
<input name="product_name" type="text" required class="form-control form-control-sm" list="product_list" value="<?php if(isset($_GET['product_id'])){echo trim($edit_resalt['product_name']);} ?>">
</td>
</tr>
<tr>
  <td colspan="2">Product Name (සිංහල) <em class="text-secondary">Please don't use <b class="text-danger">'</b> or <b class="text-danger">"</b></em><br>
    <input name="product_name_sinhala" type="text" class="form-control form-control-sm" id="product_name_sinhala" value="<?php if(isset($_GET['product_id'])){echo $edit_resalt['product_name_sinhala'];} ?>"></td>
</tr>
	
<tr>
  <td class="w-50">
    Barcode <label class="badge badge-secondary"><input name="check_barcode" type="checkbox" id="check_barcode" onChange="display_barcode();"> Auto Generate Barcode</label><br>
  <input name="product_barcode" type="text" required class="form-control form-control-sm" id="product_barcode" value="<?php if(isset($_GET['product_id'])){echo $edit_resalt['product_barcode'];} ?>">
  </td>
  <td>
    Product ID<br>
  <input name="product_productid" type="text" class="form-control form-control-sm" value="<?php if(isset($_GET['product_id'])){echo $edit_resalt['product_productid'];} ?>">
  </td>
</tr>
	

<tr><td colspan="2"><hr><b>Price Details</b></td></tr>
	
<?php if(!isset($_GET['product_id'])){ ?>
<tr>
  <td>Price Badge Name<br>
    <input name="price_name" type="text" class="form-control form-control-sm" id="price_name" value="Product Create Badge"></td>
  <td>&nbsp;</td>
</tr>
<?php } ?>
<tr>
  <td colspan="2">
	  
<?php if(isset($_GET['product_id'])){ ?>
<p class="text-danger">Update <b><?php echo $edit_price_resalt['price_name']; ?></b> details</p>
<?php } ?>
	
<table style="width: 100%;">
  <tbody>
    <tr>
      <td class="pl-0">
    Cost Price<br>
  <input name="price_buy" type="text" required class="form-control form-control-sm" id="price_buy" pattern="[0-9]+([\.][0-9]{0,2})?" onKeyUp="cal_discount(); gp_cal(); cal_wholesale(); cal_special();" value="<?php if(isset($_GET['product_id'])){echo $edit_price_resalt['price_buy'];} ?>">
		</td>
      <td>
    MRP Price<br>
  <input name="price_sale" type="text" required class="form-control form-control-sm" id="price_sale" pattern="[0-9]+([\.][0-9]{0,2})?" onKeyUp="cal_discount(); gp_cal(); cal_wholesale(); cal_special();" value="<?php if(isset($_GET['product_id'])){echo $edit_price_resalt['price_sale'];} ?>">
		</td>
      <td class="pr-0">
    GP Rate<br>
  <input name="gp_rate" type="text" class="form-control form-control-sm" id="gp_rate" value="<?php if(isset($_GET['product_id'])){echo number_format((float)$a,2,".","")." (".number_format((float)$gp_rate,2)."%)"; } ?>" readonly="readonly">
		</td>
    </tr>
  </tbody>
</table>

	
	</td>
  </tr>
	
<tr>
  <td style="white-space: nowrap;"> Discount Price<br>
    <table style="width: 100%;">
      <tbody>
        <tr>
          <td class="pl-0"><input name="dis_cal_value" type="text" class="form-control form-control-sm" id="dis_cal_value" placeholder="%" onKeyUp="cal_discount();" value="<?php if(isset($_GET['product_id'])){ echo discount_cal($edit_price_resalt['price_sale'],$edit_price_resalt['price_discount']); } ?>"></td>
          <td><input name="price_discount" type="text" class="form-control form-control-sm" id="price_discount" placeholder="Amount" pattern="[0-9]+([\.][0-9]{0,2})?" onKeyUp="document.getElementById('dis_cal_value').value=0;" value="<?php if(isset($_GET['product_id'])){echo $edit_price_resalt['price_discount'];} ?>"></td>
        </tr>
      </tbody>
    </table></td>
  <td valign="middle"><label style="margin-left: 10px; margin-top: 25px; margin-bottom: 0px;">
    <input name="product_add_discount" type="checkbox" <?php if(isset($_GET['product_id'])){ if($edit_resalt['product_add_discount']==1){echo "checked";} } ?>>
    Add to bill</label></td>
</tr>
<tr>
  <td>Wholesale Price<br>
	  
    <table style="width: 100%;">
      <tbody>
        <tr>
          <td class="pl-0"><input name="dis_who_value" type="text" class="form-control form-control-sm" id="dis_who_value" placeholder="%" onKeyUp="cal_wholesale();" value="<?php if(isset($_GET['product_id'])){ echo discount_cal($edit_price_resalt['price_sale'],$edit_price_resalt['price_wholesale']); } ?>"></td>
          <td><input name="price_wholesale" type="text" class="form-control form-control-sm" id="price_wholesale" placeholder="Amount" pattern="[0-9]+([\.][0-9]{0,2})?" onKeyUp="document.getElementById('dis_who_value').value=0;" value="<?php if(isset($_GET['product_id'])){echo $edit_price_resalt['price_wholesale'];} ?>"></td>
        </tr>
      </tbody>
    </table>
	  
   </td>
  <td valign="middle">&nbsp;</td>
</tr>
<tr>
  <td>Special Price
    <table style="width: 100%;">
      <tbody>
        <tr>
          <td class="pl-0"><input name="dis_special_value" type="text" class="form-control form-control-sm" id="dis_special_value" placeholder="%" onKeyUp="cal_special();" value="<?php if(isset($_GET['product_id'])){ echo discount_cal($edit_price_resalt['price_sale'],$edit_price_resalt['price_special']); } ?>"></td>
          <td><input name="price_special" type="text" class="form-control form-control-sm" id="price_special" placeholder="Amount" pattern="[0-9]+([\.][0-9]{0,2})?" onKeyUp="document.getElementById('dis_special_value').value=0;" value="<?php if(isset($_GET['product_id'])){echo $edit_price_resalt['price_special'];} ?>"></td>
        </tr>
      </tbody>
</table></td>
  <td valign="middle">&nbsp;</td>
</tr>
	
<tr><td colspan="2"><hr><b>Stock Details</b></td></tr>
	
<tr>
<td class="pl-0">
Opening Balance<br>
<input name="stock_qty" type="text" required class="form-control form-control-sm" pattern="*d\" <?php if(isset($_GET['product_id'])){echo "disabled";} ?>>
</td>
<td class="pr-0">
Minimum level (Alert Level)<br>
<input name="product_alertqty" type="text" class="form-control form-control-sm" required pattern="[0-9]+([\.][0-9]{0,2})?" value="<?php if(isset($_GET['product_id'])){echo $edit_resalt['product_alertqty'];} ?>">
</td>
</tr>
	
<tr style="border: 1px dashed #CCCCCC; background-color: white;">
  <td colspan="2">
	  
<details>
  <summary>Will the stock of another product be affected?</summary>
	  
<table>
	<tbody>
		<tr>
			<td class="pl-0">
				<input name="pad_product_id" type="text" class="form-control form-control-sm" id="pad_product_id" placeholder="Product Code" list="product_list_code">
			</td>
			<td><input name="pad_qty" type="number" class="form-control form-control-sm" id="pad_qty" placeholder="Qty" step="0.001"></td>
			<td><button onClick="add_affect_list();" type="button" class="btn btn-sm btn-dark">Add</button></td>
		</tr>
	</tbody>
</table>
	  
	<div id="affect_load"></div>
</details>
	</td>
  </tr>
<tr>
  <td><label><input name="product_stock_maintain" type="checkbox" id="product_stock_maintain" <?php if(isset($_GET['product_id'])){ if($edit_resalt['product_stock_maintain']==1){echo "checked";} }else{echo "checked";} ?> > Stock Maintain</label></td>
  <td>&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="right">
<button name="<?php if(isset($_GET['product_id'])){echo "update_bt";}else{echo "add_bt";} ?>" type="submit" class="btn btn-success btn-sm mt-2"><?php if(isset($_GET['product_id'])){echo "Update Product";}else{echo "Add Product";} ?></button>
</td>
</tr>
	
</tbody>
</table>
	
</form>
		
	
</div>	
</div>	
	
<datalist id="product_list">
	<?php 
	$sug_list_qury=mysqli_query($connation,"SELECT * FROM product_details ORDER BY product_name");
	while($sug_list_resalt=mysqli_fetch_array($sug_list_qury)){
	?>
	<option><?php echo $sug_list_resalt['product_name']; ?></option>
	<?php } ?>
</datalist>
	
<datalist id="product_list_code">
	<?php 
	$sug_list_qury=mysqli_query($connation,"SELECT * FROM product_details ORDER BY product_name");
	while($sug_list_resalt=mysqli_fetch_array($sug_list_qury)){
	?>
	<option value="<?php echo $sug_list_resalt['product_barcode']; ?>"><?php echo $sug_list_resalt['product_name']; ?></option>
	<?php } ?>
</datalist>	


<?php if($success_msg==2){ ?>
<script>
    swal({
	title: "Error",
       html: "<br> Your entered details something is wrong, Please try again.",
       type: "error",
	   titleColor: "text-primary",
       showCancelButton: false,
       confirmButtonText: 'OK',
       showLoaderOnConfirm:false,
       buttonsStyling: false,
       confirmButtonClass: 'btn btn-success',
       cancelButtonClass: '',    
     
    });
</script>
<?php } ?>

<?php if($success_msg==1){ ?>
<script>
    swal({
	title: "Congratulation",
       html: "<br> Your entered details has been saved successfully.",
       type: "success",
	   titleColor: "text-primary",
       showCancelButton: false,
       confirmButtonText: 'OK',
       showLoaderOnConfirm:false,
       buttonsStyling: false,
       confirmButtonClass: 'btn btn-success',
       cancelButtonClass: '',     
    });
</script>
<?php } ?>
	
<?php if($success_msg==4){ ?>
<script>
    swal({
	title: "Error",
       html: "<br> Your entered details something is wrong, Please try again.",
       type: "error",
	   titleColor: "text-primary",
       showCancelButton: false,
       confirmButtonText: 'OK',
       showLoaderOnConfirm:false,
       buttonsStyling: false,
       confirmButtonClass: 'btn btn-success',
       cancelButtonClass: '',    
     
    });
</script>
<?php } ?>

<?php if(isset($_GET['updated'])){ ?>
<script>
    swal({
	title: "Congratulation",
       html: "<br> Your product details has been update successfully.",
       type: "success",
	   titleColor: "text-primary",
       showCancelButton: false,
       confirmButtonText: 'OK',
       showLoaderOnConfirm:false,
       buttonsStyling: false,
       confirmButtonClass: 'btn btn-success',
       cancelButtonClass: '',     
    });
</script>
<?php } ?>
	
<script>
	function cal_discount(){
		let price_sale=document.getElementById('price_sale').value;
		let dis_cal_value=document.getElementById('dis_cal_value').value;
		
		let dis_discount=price_sale-(price_sale/100*dis_cal_value);
		
		document.getElementById('price_discount').value=dis_discount.toFixed(2);
	}
	function cal_wholesale(){
		let price_sale=document.getElementById('price_sale').value;
		let dis_who_value=document.getElementById('dis_who_value').value;
		
		let dis_discount=price_sale-(price_sale/100*dis_who_value);
		
		document.getElementById('price_wholesale').value=dis_discount.toFixed(2);
	}
	function cal_special(){
		let price_sale=document.getElementById('price_sale').value;
		let dis_special_value=document.getElementById('dis_special_value').value;
		
		let dis_discount=price_sale-(price_sale/100*dis_special_value);
		
		document.getElementById('price_special').value=dis_discount.toFixed(2);
	}
	
	function display_barcode(){
		let next_barcode=document.getElementById('next_barcode').value;
		if(document.getElementById('check_barcode').checked==true){
			document.getElementById('product_barcode').value=next_barcode;
		}
		else{
			document.getElementById('product_barcode').value="";
		}
	}
	
	function gp_cal(){
		let price_buy=document.getElementById('price_buy').value;
		let price_sale=document.getElementById('price_sale').value;
		
		if(price_buy==""||price_sale==""){}
		else{		
		let a=price_sale-price_buy;
		let b=a/price_sale*100;
		
		let price=a.toFixed(2);
		let prasantage=b.toFixed(2);
		
		document.getElementById('gp_rate').value=price+" ("+prasantage+"%)";	
		}

	}
	
	function affect_load_function(system_product_id){
		const xhttp = new XMLHttpRequest();
		xhttp.onload = function() {
		document.getElementById("affect_load").innerHTML = this.responseText;
		}
		xhttp.open("GET", "ajax_affect_list.php?system_product_id="+system_product_id, true);
		xhttp.send();
	}
	
	function add_affect_list(){
		let pad_product_id=document.getElementById('pad_product_id').value;
		let pad_qty=document.getElementById('pad_qty').value;
		
		if(pad_product_id==""||pad_qty==""){
			alert("Please enter request value.");
		}
		else{
			const xhttp = new XMLHttpRequest();
			xhttp.onload = function() {
			document.getElementById("affect_load").innerHTML = this.responseText;
			affect_load_function(<?php echo $system_product_id; ?>);
			document.getElementById('pad_product_id').value="";
			document.getElementById('pad_qty').value="";
			}
			xhttp.open("GET", "ajax_affect_list.php?system_product_id=<?php echo $system_product_id; ?>&pad_product_id="+pad_product_id+"&pad_qty="+pad_qty, true);
			xhttp.send();
		}
	}
	
	function remove_afect_item(pad_id){
		if(confirm('Are your sure?')){
		const xhttp = new XMLHttpRequest();
		xhttp.onload = function() {
		document.getElementById("affect_load").innerHTML = this.responseText;
		affect_load_function(<?php echo $system_product_id; ?>);
		}
		xhttp.open("GET", "ajax_affect_list.php?pad_id="+pad_id+"&system_product_id=<?php echo $system_product_id; ?>", true);
		xhttp.send();
		}
	}
	
	affect_load_function(<?php echo $system_product_id; ?>);
</script>
	
</body>
</html>
<?php @mysqli_close($connation); ?>