<?php session_start();

if(!isset($_POST['submit'])){
	exit('Fail!');
}else{
include('../../conf/conn.php');
}


if(!@$_SESSION["username"]){
	$Tusername= date("Y-m-d H:i:s").rand();
	$_SESSION["username"]=$Tusername;
	$_SESSION["Tusername"]=$Tusername;
	$_SESSION["id"]=rand();
	}

$product_name=$_POST["name"];
$query = "select * from products where name='$product_name'";
$result=mysqli_query($conn,$query);
$row = mysqli_fetch_assoc($result);

$amount=htmlspecialchars($_POST["quantity"]);
$username=$_SESSION["username"];	
$id=$row["id"];
$image=$row["image"];
$price=$row["price"];
if(isset($row["original price"])){
	$original_price=$row["original price"];	
}else{
	$original_price=$row["price"];
}
$time=date("Y-m-d H:i:s");

if($_GET['letter']==8){
	
	$product_name=$_POST["name"].'<br>'
	.'<div class="'.$_POST["1color"].'">'.$_POST["1tel"].'</div>'
	.'<div class="'.$_POST["2color"].'">'.$_POST["2tel"].'</div>'
	.'<div class="'.$_POST["3color"].'">'.$_POST["3tel"].'</div>'
	.'<div class="'.$_POST["4color"].'">'.$_POST["4tel"].'</div>'
	.'<div class="'.$_POST["5color"].'">'.$_POST["5tel"].'</div>'
	.'<div class="'.$_POST["6color"].'">'.$_POST["6tel"].'</div>'
	.'<div class="'.$_POST["7color"].'">'.$_POST["7tel"].'</div>'
	.'<div class="'.$_POST["8color"].'">'.$_POST["8tel"].'</div>';
	
	$query = "select * from cart where username='$username' and product_name='$product_name'";
	$result=mysqli_query($conn,$query);
	if(!$row = mysqli_fetch_assoc($result)){
		$_SESSION["page"]=1;
		$query = "insert into cart(id,username,product_name,image,price,amount,time) values ('$id','$username','$product_name','$image','$price','$amount','$time')";
		$result=mysqli_query($conn,$query);
	}else{
		$_SESSION["page"]=1;
		$query = "select * from cart where username='$username' and product_name='$product_name'";
		$result=mysqli_query($conn,$query);
		$row = mysqli_fetch_assoc($result);
		$amount=$row["amount"]+$amount;
		$query = "UPDATE cart SET amount = '$amount' WHERE cart.username = '$username' and cart.product_name = '$product_name'";
		$result=mysqli_query($conn,$query);
		$addcartinfo="1";
		$_SESSION["addcartinfo"]=$addcartinfo;
	}
		
}elseif($_GET['letter']==15){
    
	$product_name=$_POST["name"].'<br>'
	.'<div class="'.$_POST["1color"].'">'.$_POST["1tel"].'</div>'
	.'<div class="'.$_POST["2color"].'">'.$_POST["2tel"].'</div>'
	.'<div class="'.$_POST["3color"].'">'.$_POST["3tel"].'</div>'
	.'<div class="'.$_POST["4color"].'">'.$_POST["4tel"].'</div>'
	.'<div class="'.$_POST["5color"].'">'.$_POST["5tel"].'</div><br>'
	.'<div class="'.$_POST["6color"].'">'.$_POST["6tel"].'</div>'
	.'<div class="'.$_POST["7color"].'">'.$_POST["7tel"].'</div>'
	.'<div class="'.$_POST["8color"].'">'.$_POST["8tel"].'</div>'
	.'<div class="'.$_POST["9color"].'">'.$_POST["9tel"].'</div>'
	.'<div class="'.$_POST["10color"].'">'.$_POST["10tel"].'</div><br>'
	.'<div class="'.$_POST["11color"].'">'.$_POST["11tel"].'</div>'
	.'<div class="'.$_POST["12color"].'">'.$_POST["12tel"].'</div>'
	.'<div class="'.$_POST["13color"].'">'.$_POST["13tel"].'</div>'
	.'<div class="'.$_POST["14color"].'">'.$_POST["14tel"].'</div>'
	.'<div class="'.$_POST["15color"].'">'.$_POST["15tel"].'</div>';

	$query = "select * from cart where username='$username' and product_name='$product_name'";
	$result=mysqli_query($conn,$query);
	if(!$row = mysqli_fetch_assoc($result)){
		$_SESSION["page"]=1;
		$query = "insert into cart(id,username,product_name,image,price,amount,time) values ('$id','$username','$product_name','$image','$price','$amount','$time')";
		$result=mysqli_query($conn,$query);
	}else{
		$_SESSION["page"]=1;
		$query = "select * from cart where username='$username' and product_name='$product_name'";
		$result=mysqli_query($conn,$query);
		$row = mysqli_fetch_assoc($result);
		$amount=$row["amount"]+$amount;
		$query = "UPDATE cart SET amount = '$amount' WHERE cart.username = '$username' and cart.product_name = '$product_name'";
		$result=mysqli_query($conn,$query);
		$addcartinfo="1";
		$_SESSION["addcartinfo"]=$addcartinfo;
	}
}else{

	$product_name=$_POST["name"].'<br>'
	.'<div class="'.$_POST["1color"].'">'.$_POST["1tel"].'</div>'
	.'<div class="'.$_POST["2color"].'">'.$_POST["2tel"].'</div>'
	.'<div class="'.$_POST["3color"].'">'.$_POST["3tel"].'</div>'
	.'<div class="'.$_POST["4color"].'">'.$_POST["4tel"].'</div>'
	.'<div class="'.$_POST["5color"].'">'.$_POST["5tel"].'</div><br>'
	.'<div class="'.$_POST["6color"].'">'.$_POST["6tel"].'</div>'
	.'<div class="'.$_POST["7color"].'">'.$_POST["7tel"].'</div>'
	.'<div class="'.$_POST["8color"].'">'.$_POST["8tel"].'</div>'
	.'<div class="'.$_POST["9color"].'">'.$_POST["9tel"].'</div>'
	.'<div class="'.$_POST["10color"].'">'.$_POST["10tel"].'</div><br>'
	.'<div class="'.$_POST["11color"].'">'.$_POST["11tel"].'</div>'
	.'<div class="'.$_POST["12color"].'">'.$_POST["12tel"].'</div>'
	.'<div class="'.$_POST["13color"].'">'.$_POST["13tel"].'</div>'
	.'<div class="'.$_POST["14color"].'">'.$_POST["14tel"].'</div>'
	.'<div class="'.$_POST["15color"].'">'.$_POST["15tel"].'</div><br>'
	.'<div class="'.$_POST["16color"].'">'.$_POST["16tel"].'</div>'
	.'<div class="'.$_POST["17color"].'">'.$_POST["17tel"].'</div>'
	.'<div class="'.$_POST["18color"].'">'.$_POST["18tel"].'</div>'
	.'<div class="'.$_POST["19color"].'">'.$_POST["19tel"].'</div>'
	.'<div class="'.$_POST["20color"].'">'.$_POST["20tel"].'</div>';

	$query = "select * from cart where username='$username' and product_name='$product_name'";
	$result=mysqli_query($conn,$query);
	if(!$row = mysqli_fetch_assoc($result)){
		$_SESSION["page"]=1;
		$query = "insert into cart(id,username,product_name,image,price,amount,time) values ('$id','$username','$product_name','$image','$price','$amount','$time')";
		$result=mysqli_query($conn,$query);
	}else{
		$_SESSION["page"]=1;
		$query = "select * from cart where username='$username' and product_name='$product_name'";
		$result=mysqli_query($conn,$query);
		$row = mysqli_fetch_assoc($result);
		$amount=$row["amount"]+$amount;
		$query = "UPDATE cart SET amount = '$amount' WHERE cart.username = '$username' and cart.product_name = '$product_name'";
		$result=mysqli_query($conn,$query);
		$addcartinfo="1";
		$_SESSION["addcartinfo"]=$addcartinfo;
	}
}
$url = "../e-shop.php";  
echo "<script language='javascript' 
type='text/javascript'>";  
echo "window.location.href='$url'";  
echo "</script>";
?>
<html>
<head>
<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '1705821553071403', {
em: 'insert_email_variable,'
});
fbq('track', 'PageView');
 
fbq('track', 'AddToCart');
 
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=1705821553071403&ev=PageView&noscript=1"
/></noscript>
<!-- DO NOT MODIFY -->
<!-- End Facebook Pixel Code -->

</head>
<body>
</body>
</html>