<?php

/**
 * Created by PhpStorm.
 * User: albertogiunta
 * Date: 25/01/16
 * Time: 10:47
 */
session_start();
require "../Controller/DatabaseConnection.php";
require "../Controller/CatalogRequestsController.php";
$conn = new DatabaseConnection();
$req = new CatalogRequestsController($conn->get_db_link());
if (isset($_GET["logout"])) {
    if ($_GET["logout"] == "ok") {
        $_SESSION["username"] = null;
        $_SESSION["password"] = null;
        session_destroy();
        header("Location: ../View/home.php");
    }
}
if (isset($_GET["detailsId"])) {
    $details = $req->getDetailedInformation($_GET["detailsId"]);
    echo json_encode($req->parseArray($details)[0]);
}
if (isset($_GET["prodName"])) {
    $searchResults = $req->getSearchResults($_GET["prodName"]);
    $parsedArray = $req->parseArray($searchResults);
    $encoded = json_encode($parsedArray);
    echo $encoded;
}
}
if (!isset($_COOKIE['orderCriteria'])) {
    setcookie('orderCriteria', 'ASC');
    $_COOKIE['orderCriteria'] = 'ASC';
}
//Se ho premuto "aggiungi al carrello", ho passato alla pagina un id
require "header.php";
require "../Controller/TileController.php";
require "../Controller/DatabaseConnection.php";
require "../Controller/CatalogRequestsController.php";
?>


<?php 
$conn = new DatabaseConnection();
$req = new CatalogRequestsController($conn->get_db_link());
$tileC = new TileController();
$catalogItems = '';
$elemInPage = 12;
// change number also in pagination.js
$visibility = '';
$products = $req->getCatalogArray();
for ($i = 0; $i < sizeof($products); $i++) {
    if ($i >= $elemInPage) {
        $visibility = 'hidden';
    }
    $catalogItems = $catalogItems . $tileC->getSmallSquare($visibility, $products[$i]->getId(), $products[$i]->getName(), $products[$i]->getPrice(), $products[$i]->getImage());
}
$turkey = '<div class="catalog-cont">';
$stuffing = $req->getTitleAndPaging(true) . $catalogItems . $req->getTitleAndPaging(false);
echo $tileC->getSearchSection() . $tileC->getDetailedProduct() . $tileC->insertIntoDiv($turkey, $stuffing);
    </div>



<?php 
ob_start();
echo '<div id="left-col">';
$totprice = 0;
if (isset($_COOKIE["user"])) {
    $cookie = $_COOKIE["user"];
    $cookie = stripcslashes($cookie);
    $basket_item = json_decode($cookie, true);
    $tileC = new TileController();
    $conn = new DatabaseConnection();
    $dbcontroller = new CatalogRequestsController($conn->get_db_link());
    $products = $dbcontroller->getBasketItem(array_keys($basket_item));
    echo '<h2 class="title">Articoli nel carrello</h2><div class="basket-item"> ';
    for ($i = 0; $i < sizeof($products); $i++) {
        $qnt = $basket_item[$products[$i]->getId()];
        $price = $products[$i]->getPrice();
        $totprice = $totprice + $qnt * $price;
        echo $tileC->getBasketTileItem($products[$i]->getId(), $products[$i]->getName(), $price, $products[$i]->getImage(), $qnt);
    }
    echo '</div>';
} else {
    echo "<h2 class=\"title\">Articoli nel carrello</h2><div class=\"basket-item\"> Non hai amigurumi nel carrello! 😢</div>";
}
echo '<div id="cont">
                <div id="summary">
                    <p class="title">Metodo di spedizione</p>