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

if(!isset($_GET['price_systemid'])){
	exit("System Error");
}

$view_qury=mysqli_query($connation,"SELECT * FROM product_details WHERE product_systemid='$_GET[price_systemid]'");
$view_resalt=mysqli_fetch_array($view_qury);

if(isset($_GET['page'])){
$page=$_GET['page'];
}
else{
$page=1;
}
$per_page=50;
$start_page=($page-1)*$per_page;

if(isset($_POST['add_btn'])){
	$price_systemid=mysqli_real_escape_string($connation,$_GET['price_systemid']);
	$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']);	
	
	if(mysqli_query($connation,"INSERT INTO
	price_details (price_id, price_systemid, price_name, price_buy, price_sale, price_wholesale, price_special, price_discount, price_addtime)
	VALUES (NULL, '$price_systemid', '$price_name', '$price_buy', '$price_sale', '$price_wholesale', '$price_special', '$price_discount', '$current_date $current_time')")){
	echo "<script>window.location='price-manage?price_systemid=$price_systemid';</script>";
	}
}

if(isset($_GET['price_systemid'])&&isset($_GET['remove'])){
	$price_systemid=mysqli_real_escape_string($connation,$_GET['price_systemid']);
	$price_id=mysqli_real_escape_string($connation,$_GET['remove']);
	
	$check_price=mysqli_query($connation,"SELECT * FROM price_details WHERE price_systemid='$price_systemid'");
	if(mysqli_num_rows($check_price)>1){	
	if(mysqli_query($connation,"DELETE FROM price_details WHERE price_id='$price_id'")){
	echo "<script>window.location='price-manage?price_systemid=$price_systemid&removed';</script>";
	}
	}
	else{
	echo "<script>window.location='price-manage?price_systemid=$price_systemid&error';</script>";
	}
}

if(isset($_POST['price_update_btn'])){
	$price_systemid=mysqli_real_escape_string($connation,$_GET['price_systemid']);
	$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'");
	echo "<script>window.location='price-manage?price_systemid=$price_systemid';</script>";
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="favicon.ico">
<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="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
	
<link rel="stylesheet" href="alert/sweetalert2.min.css">
<script src="alert/sweetalert2.min.js"></script>
</head>
<style>
	.table tr td{
		font-size: 14px;
		padding: 5px;
	}
	.table tr th{
		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">

	
<strong>Price Manage</strong>
	
<div style="text-align: right; margin-bottom: 10px;"><a href="product" class="btn btn-dark btn-sm"><i class="fa fa-backward"></i> Back to Product</a></div>
	
<div>
<b><?php echo $view_resalt['product_name']." - ".$view_resalt['product_name_sinhala']; ?></b><br>
<b>Barcode: <?php echo $view_resalt['product_barcode']; ?></b>
</div><br>
	
<div class="row">
	
<form method="post" class="input-form col-lg-6 col-sm-12 m-auto" autocomplete="off">

	<table style="width: 100%;" class="table-sm">
		<tbody>

			<tr>
			  <td class="w-50"> Badge Name<br>
			    <input name="price_name" type="text" required class="form-control form-control-sm" id="price_name" value="<?php echo date("Y-m-d"); ?> create badge" onFocus="this.select();"></td>
			  <td>&nbsp;</td>
		  </tr>
			<tr>
			  <td colspan="2">
<table style="width: 100%;">
  <tbody>
    <tr>
      <td>
    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();">
		</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();">
		</td>
      <td>
    GP Rate<br>
  <input name="gp_rate" type="text" class="form-control form-control-sm" id="gp_rate" readonly="readonly">
		</td>
    </tr>
  </tbody>
</table>
				</td>
		  </tr>
			<tr>
			  <td> 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();"></td>
			          <td><input name="price_discount" type="text" required 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;"></td>
		            </tr>
		          </tbody>
		        </table></td>
			  <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();"></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;"></td>
        </tr>
      </tbody>
    </table>
				
				</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();"></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;"></td>
		            </tr>
		          </tbody>
		        </table></td>
			  <td>&nbsp;</td>
		  </tr>
			<tr>
				<td colspan="2" align="right">
					<button type="submit" name="add_btn" class="btn btn-success btn-sm mt-2">
						Add Price
					</button>
				</td>
			</tr>

		</tbody>
	</table>

</form>
	
<div class="table-responsive mt-4">
	
<table class="table table-sm" style="font-size: 14px; text-transform: capitalize;">
        <thead>
          <tr class="table-active">
			  <td></td>
                <td>Update Time</td>
				<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
			$qury_str="SELECT * FROM price_details WHERE price_systemid='$_GET[price_systemid]' ORDER BY price_id DESC";
			
			$list_qury=mysqli_query($connation,"$qury_str LIMIT $start_page,$per_page");
			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>
		<td align="center"><a href="price-manage?price_systemid=<?php echo $_GET['price_systemid']; ?>&remove=<?php echo $list_resalt['price_id']; ?>" class="badge badge-danger" onClick="return confirm('Are you sure remove price?');"><i class="fa fa-trash"></i></a></td>
      <td><?php echo date("Y-m-d h:i:s A",strtotime($list_resalt['price_addtime'])); ?></td>
				<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>
	
<?php 
$total_record_qury=mysqli_query($connation,"$qury_str");
$total_record=mysqli_num_rows($total_record_qury);
$page_count=ceil($total_record/$per_page);
?>
<div class="text-right">
<span class="mr-2"><?php echo "Showing ".number_format((float)($start_page+1))."/".number_format((float)($start_page+$per_page))." page of ".number_format((float)$total_record)." entries"; ?></span>
<a href="price-manage?page=1" class="btn btn-sm btn-dark" <?php if($page<=1){echo "hidden='yes'";} ?>>First</a>
<a href="price-manage?page=<?php echo $page-1; ?>" class="btn btn-sm btn-dark" <?php if($page<=1){echo "hidden='yes'";} ?>>Previous</a>
<a href="price-manage?page=<?php echo $page+1; ?>" class="btn btn-sm btn-dark" <?php if($page>=$page_count){echo "hidden='yes'";} ?>>Next</a>
<a href="price-manage?page=<?php echo $page_count; ?>" class="btn btn-sm btn-dark" <?php if($page>=$page_count){echo "hidden='yes'";} ?>>Last</a>	
</div>

	
</div>		

		
	
</div>
</div>	
	
<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 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+"%)";	
		}

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