<?php


require_once('config.php');





if(($response["error"] == true))
{
    echo(json_encode($response));
}

if(($response["error"] == false))
{
    if((isset($_POST["request"])) && ($_POST["request"] == "add_to_cart"))
    {
        $logs           =   array();
        $product_id     =   "";
        $quantity       =   "";

        if((!isset($_SESSION["GOLDFAYRE"])))
        {
            $response["error"]          =   true;
            $response["error_msg"]      =   "You are requested to login to your account first and try again.";
        }


        if((empty($_POST["quantity"])) || (!isset($_POST["quantity"])))
        {
            $response["error"]      =   true;
            $response["error_msg"]  =   "An Error Occurred. The quantity is required";
        }
        else
        {
            $quantity   =   intval($_POST["quantity"]);
        }


        if(($response["error"] == false))
        {
            if((!isset($_POST["pid"])) || (empty($_POST["pid"])))
            {
                $response["error"]      =   true;
                $response["error_msg"]  =   "An Error Occurred while adding the product to the cart. You are requested to try again later.";
            }
            else
            {
                $product_id     =   intval($_POST["pid"]);
            }

        }






        if(($response["error"] == false))
        {
            $sql    =   "   SELECT M.id ID, M.title TITLE, M.description DESCRIPTION, M.category_id CATEGORY_ID, M.price PRICE, 
                            M.discount_price DISCOUNT_PRICE, M.product_status PRODUCT_STATUS, M.register_on REGISTER_ON, M.quantity QUANTITY 
                            FROM product_mst M
                            WHERE M.id = ?";
            $stmt = $db->prepare($sql); 
            $stmt->bind_param("s", $product_id);
            $stmt->execute();
            $result     =   $stmt->get_result();
            $row        =   mysqli_fetch_assoc($result);
            if(($row))
            {
                $logs["PRODUCT_MST"]    =   $row;
            }
            else
            {
                $response["error"]      =   true;
                $response["error_msg"]  =   "An Error Occurred. No product found in the database with this title. You are requested to try again later.";
            }
        }
        if(($response["error"] == false))
        {
            if((count($_SESSION["GOLDFAYRE"]["CART"]) > 0))
            {
                foreach($_SESSION["GOLDFAYRE"]["CART"] as $k => $v)
                {
                    if(($v["PID"] == $product_id))
                    {
                        $v["QTY"]   =   (intval($v["QTY"]) + 1);
                    }
                    else
                    {
                        $item                               =   array();
                        $item["PID"]                        =   intval($product_id);
                        $item["QTY"]                        =   intval($quantity);
                        $item["PRICE"]                      =   floatval($logs["PRODUCT_MST"]["PRICE"]);
                        $item["PRODUCT"]                    =   $logs["PRODUCT_MST"];  
                        $_SESSION["GOLDFAYRE"]["CART"][]    =   $item;
                    }
                    $_SESSION["GOLDFAYRE"]["CART"][$k]  =   $v;
                }
            }
            else
            {
                $item                               =   array();
                $item["PID"]                        =   intval($product_id);
                $item["QTY"]                        =   intval($quantity);
                $item["PRICE"]                      =   floatval($logs["PRODUCT_MST"]["PRICE"]);
                $item["PRODUCT"]                    =   $logs["PRODUCT_MST"];  
                $_SESSION["GOLDFAYRE"]["CART"][]    =   $item;
            }
        }
        if(($response["error"] == false))
        {
            $response["message"]    =   "The product has been added to the cart successfully.";
        }
        echo(json_encode($response));
    }

}












































?>