<?php
session_start();

if (@$_GET['check'] == "delete") {
    include('../../conf/conn.php');

    $product_name = mysqlSecurity($conn, $_GET["name"]);
    $username = $_SESSION["username"];
    $query = "DELETE FROM `cart` WHERE `cart`.`product_name` = '$product_name' and `cart`.`username` = '$username'";
    $result = mysqli_query($conn, $query);
    if ($_GET["from"] == "cart") {

        $_SESSION["page"] = 7;
    } else {

        $query = "select * from cart where username='$username'";
        $result = mysqli_query($conn, $query);
        if (!mysqli_num_rows($result) > 0) {
            $_SESSION["page"] = 7;
        } else {
            $_SESSION["page"] = 2;
        }
    }
    $url = "../e-shop.php";
    echo "<script language='javascript' 
    type='text/javascript'>";
    echo "window.location.href='$url'";
    echo "</script>";
} elseif (!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;
}

if (@$_GET['check'] == "infopage") {
    $product_name = $_POST["name"];
    $id = mysqlSecurity($conn, $_POST["id"]);
    $query = "select * from products where id='$id'";
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_assoc($result);

    $amount = mysqlSecurity($conn, $_POST["quantity"]);
    $username = $_SESSION["username"];
    $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");

    $query = "select * from cart where username='$username' and id=$id";
    $result = mysqli_query($conn, $query);
    if (!$row = mysqli_fetch_assoc($result)) {
        $_SESSION["page"] = 1;
        $query = "insert into cart(username,id,product_name,image,original_price,price,amount,time) values ('$username','$id','$product_name','$image','$original_price','$price','$amount','$time')";
        $result = mysqli_query($conn, $query);
        $url = "../e-shop.php";
        echo "<script language='javascript' 
		type='text/javascript'>";
        echo "window.location.href='$url'";
        echo "</script>";
    } else {
        $_SESSION["page"] = 1;
        $query = "select * from cart where username='$username' and id=$id";
        $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.id=$id";
        $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', '1251610718262776', {
                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=1251610718262776&ev=PageView&noscript=1"
                   /></noscript>
    <!-- DO NOT MODIFY -->
    <!-- End Facebook Pixel Code -->

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