<?php
	session_start();
	require_once 'db_connection.php'; 
	$tempid = $_POST['tempid'];
	$discount = $_POST['discount'];
	$distype1 = $_POST['distype1'];
    $cartitems = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobiletemporarycart WHERE tempid ='$tempid'"));
    	

	$pid = $cartitems['proid'];

    $products = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileproducts WHERE prid ='$pid'"));

	$price = $products['sellprice'];
	$qty = $cartitems['pro_qty'];
	$pro_price = $cartitems['pro_price'];

	
	$tempdetails = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileproducts WHERE prid='$pid'"));


	//$tempdetails = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobiletemporarycart WHERE prid='$tempid'"));

	$lastchar = substr($discount, -1); 

	/*if($lastchar=="%"){
		
		$disrate = intval(substr($discount, 0, -1));
		$dis =  ($tempdetails['sellprice']/100)*$disrate;
		$pprice = $tempdetails['sellprice']-$dis;
		$distype = 1;
	}else{
		$pprice = $tempdetails['sellprice']-$discount;
		$distype = 0;
	}
*/

if($lastchar=="%"){
		
	$disrate = intval(substr($discount, 0, -1));
	$dis =  ($price/100)*$disrate;
	$pprice = $price-$dis;
	$distype = 1;

		$dis2 =  ($pro_price/100)*$disrate;

		$pprice2 = $pro_price-$dis2;
	
}else{
	$pprice = $price-$discount;
	

	$pprice2 = $pro_price-$discount;


	$distype = 0;
}











	$sql3 = "UPDATE mobiletemporarycart SET pro_discount='$discount',pro_dis_price='$pprice2',discount_type='$distype1' WHERE tempid='$tempid'";
	if ($conn->query($sql3) === TRUE) {
		echo "1";
	}else{
		echo 0;
	}

	?>
