<?php 
session_start();
require("php_con.php");
require("function.php");

$bill_id=mysqli_real_escape_string($connation,$_GET['bill_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);

if(isset($_POST['up_pro_btn'])){
	$product_name=mysqli_real_escape_string($connation,$_POST['product_name']);
	$product_name_sinhala=mysqli_real_escape_string($connation,$_POST['product_name_sinhala']);
	$product_productid=mysqli_real_escape_string($connation,$_POST['product_productid']);
	if(mysqli_query($connation,"UPDATE product_details SET product_productid='$product_productid',product_name='$product_name',product_name_sinhala='$product_name_sinhala' WHERE product_id='$product_id'")){
	echo "<script>window.location='edit_pos_item.php?bill_id=$bill_id&product_id=$product_id';</script>";
	}
}

if(isset($_POST['price_update_btn'])){
	$price_id=mysqli_real_escape_string($connation,$_POST['price_id']);
	$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_special=mysqli_real_escape_string($connation,$_POST['price_special']);
	$price_discount=mysqli_real_escape_string($connation,$_POST['price_discount']);
	mysqli_query($connation,"UPDATE price_details SET price_name='$price_name',price_buy='$price_buy',price_sale='$price_sale',price_wholesale='$price_wholesale',price_special='$price_special',price_discount='$price_discount' WHERE price_id='$price_id'");
	mysqli_query($connation,"UPDATE sale_list SET  sale_buy_price='$price_buy',sale_real_sale_price='$price_sale',sale_saleprice='$price_sale' WHERE sale_billid='$bill_id' AND sale_barcode='$edit_resalt[product_barcode]'");
	echo "<script>window.location='edit_pos_item.php?bill_id=$bill_id&product_id=$product_id';</script>";
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
	
<link rel="stylesheet" href="main_css.css">
	
<style>
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Firefox */
input[type=number] {
  -moz-appearance: textfield;
}	
</style>
	
</head>
<body style="padding: 10px; margin: 0px;">
	
<p><a href="sale_list.php?bill_id=<?php echo $bill_id; ?>" class="btn btn-sm btn-dark">Back to list</a></p>
	
<?php if(!setting_view("pos_product_edit")==1){ ?>
<p class="mt-4 text-center text-danger">Access fail!<br>Please contact admin user.</p>
<?php exit(); } ?>
	
<form method="post" autocomplete="off">
	
<table class="table-sm">
  <tbody>
    <tr>
      <td>Barcode</td>
      <td><b><?php if(isset($_GET['product_id'])){echo $edit_resalt['product_barcode'];} ?></b></td>
    </tr>
    <tr>
      <td>Product Name</td>
      <td><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>Product Name (සිංහල)</td>
      <td><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>Product ID</td>
      <td><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>
  </tbody>
</table>
	
<p align="right"><button name="up_pro_btn" type="submit" class="btn btn-sm btn-success">Update Product Details</button></p>
	
</form>
	
<hr>
	
<b>Price Details</b>

<table class="table table-sm" style="font-size: 14px; text-transform: capitalize;">
        <thead>
          <tr class="table-active">
				<td>Badge Name</td>
				<td>Cost Price</td>
                <td>MRP Price</td>
                <td>Wholesale Price</td>
				<td>Special Price</td>
                <td>Discount Price</td>
				<td>&nbsp;</td>
            </tr>
        </thead>
        <tbody>
			<?php
			$list_qury=mysqli_query($connation,"SELECT * FROM price_details WHERE price_systemid='$edit_resalt[product_systemid]' ORDER BY price_id DESC");
			while($list_resalt=mysqli_fetch_array($list_qury)){
			?>
  <form method="post" autocomplete="off">
	<input name="price_id" type="hidden" id="price_id" value="<?php echo $list_resalt['price_id']; ?>">
    <tr title="Update Time: <?php echo date("Y-m-d h:i:s A",strtotime($list_resalt['price_addtime'])); ?>">
				<td><input name="price_name" type="text" required="required" class="form-control form-control-sm" id="price_name" value="<?php echo $list_resalt['price_name']; ?>"></td>
				<td align="right"><input name="price_buy" type="number" required="required" class="form-control form-control-sm" id="price_buy" step="0.01" value="<?php echo number_format((float)$list_resalt['price_buy'],2,".",""); ?>"></td>
                <td align="right"><input name="price_sale" type="number" required="required" class="form-control form-control-sm" id="price_sale" step="0.01" value="<?php echo number_format((float)$list_resalt['price_sale'],2,".",""); ?>"></td>
                <td align="right"><input name="price_wholesale" type="number" required="required" class="form-control form-control-sm" id="price_wholesale" step="0.01" value="<?php echo number_format((float)$list_resalt['price_wholesale'],2,".",""); ?>"></td>
				<td align="right"><input name="price_special" type="number" required="required" class="form-control form-control-sm" id="price_special" step="0.01" value="<?php echo number_format((float)$list_resalt['price_special'],2,".",""); ?>"></td>
                <td align="right"><input name="price_discount" type="number" required="required" class="form-control form-control-sm" id="price_discount" step="0.01" value="<?php echo number_format((float)$list_resalt['price_discount'],2,".",""); ?>"></td>
				<td><button name="price_update_btn" type="submit" class="btn btn-sm btn-dark"><i class="fa fa-check"></i></button></td>
    </tr>
  </form>
            <?php } ?>
        </tbody>
</table>

</body>
</html>
<?php @mysqli_close($connation); ?>