<?php


require_once('config.php');





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


if((!isset($_SESSION["GOLDFAYRE"])))
{
    $response["error"]          =   true;
    $response["error_msg"]      =   "An Error Occurred. You are requested to login first ";
}


if(($response["error"] == false))
{
    if((isset($_POST["request"])) && ($_POST["request"] == "get_cart"))
    {

        $cart               =   array();
        $payable            =   0;
        $shipping_charges   =   0;


        if((!isset($_SESSION["GOLDFAYRE"])))
        {
            $response["error"]      =   true;
            $response["error_msg"]  =   "An Error Occurred. You are requested to login first.";
        }

        if(($response["error"] == false))
        {
            if((!isset($_SESSION["GOLDFAYRE"]["CART"])))
            {
                $response["error"]          =   true;
                $response["error_msg"]      =   "An Error Occurred. The Cart is empty.";
            }

        }


        if(($response["error"] == false))
        {
            if((isset($_SESSION["GOLDFAYRE"]["CART"])))
            {
                $cart   =   $_SESSION["GOLDFAYRE"]["CART"];
                

                foreach($cart as $k => $v)
                {
                    $item_price         =   0;
                    $quantity           =   0;
                    $item_total         =   0;
                    $item_price         =   $v["PRICE"];
                    $quantity           =   $v["QTY"];
                    $item_total         =   ($quantity * $item_price);
                    $payable            =   $payable + $item_total;
                    $v["SUB_TOTAL"]     =   $item_total;
                    $cart[$k]           =   $v;
                }

            }

        }


        if(($response["error"] == false))
        {
            $html   =   '<h4 class="d-flex justify-content-between align-items-center mb-3">';
            $html  .=   '<span class="text-primary">Your cart</span>';
            $html  .=   '<span class="badge bg-primary rounded-pill">'.count($cart).'</span>';
            $html  .=   '</h4>';
            $html  .=   '<ul class="list-group mb-3">';
            foreach($cart as $k => $v)
            {
                $html  .=   '<li class="list-group-item d-flex justify-content-between lh-sm">';
                $html  .=   '<div>';
                $html  .=   '<h6 class="my-0" style="padding-bottom: 5px; color: black">'.$v["PRODUCT"]["TITLE"].'</h6>';
                $html  .=   '<small class="text-muted">Quantity : '.$v["QTY"].'</small>';
                $html  .=   '</div>';
                $html  .=   '<span class="text-muted">'.$v["SUB_TOTAL"].' £</span>';
                $html  .=   '</li>';
            }
            $html  .=   '<li class="list-group-item d-flex justify-content-between">';
            $html  .=   '<span>Total Payable</span>';
            $html  .=   '<strong>'.$payable.' £</strong>';
            $html  .=   '</li>';
            $html  .=   '</ul>';
            $response["html"]   =   $html;
        }












        echo(json_encode($response));
    }


}












































?>