<?php

/*
 * Copyright (C) 2021 Echedey López Romero <elr@disroot.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

function FilterValue($Data) { // Filtrado basico
    $Data = trim($Data);
    $Data = stripslashes($Data);
    $Data = htmlspecialchars($Data);

    return $Data;
}

function ValidateRegex($Regex, $Data) {
    if (preg_match($Regex, $Data)) {
        return true;
    } else {
        return false;
    }
}

function GeneratePossibleValues($Data, $Content, $FieldName) {
    $PossibleValues = [];

    foreach ($Data as $Key => $Value) {
        if ($Value[$FieldName] === $Content) {
            array_push($PossibleValues, $Value);
        }
    }

    return $PossibleValues;
}

function CheckIdListsMatching($List1, $List2) {
    $FoundMatching = false;

    $Position1 = 0;
    while ($Position1 < count($List1) && !$FoundMatching) {
        $Position2 = 0;

        while ($Position2 < count($List2) && !$FoundMatching) {
            if (isset($List1[$Position1]['id'])) {
                if (isset($List2[$Position2]['id'])) {
                    if ($List1[$Position1]['id'] === $List2[$Position2]['id']) {
                        $FoundMatching = true;
                    }
                } else {
                    if ($List1[$Position1]['id'] === $List2[$Position2]) {
                        $FoundMatching = true;
                    }
                }
            } else {
                if (isset($List2[$Position2]['id'])) {
                    if ($List1[$Position1] === $List2[$Position2]['id']) {
                        $FoundMatching = true;
                    }
                } else {
                    if ($List1[$Position1] === $List2[$Position2]) {
                        $FoundMatching = true;
                    }
                }
            }

            $Position2++;
        }

        $Position1++;
    }

    return $FoundMatching;
}

function CheckListValue($Data, $Content, $FieldName) {
    $Exists = false;
    
    $Position = 0;
    while ($Position < count($Data) && !$Exists) {
        if ($Data[$Position][$FieldName] === $Content) {
            $Exists = true;
        }
        
        $Position++;
    }

    return $Exists;
}

function GenerateFieldValues($Data, $FieldName) {
    $FieldValues = [];

    foreach ($Data as $Key => $Value) {
        array_push($FieldValues, $Value[$FieldName]);
    }

    return $FieldValues;
}

function DumpCanaryData($File, $DependentValues, $FieldName, $NoId = false, $NewId = '') {
    $Data = json_decode(file_get_contents($File), true);
    $CanaryData = [];

    $FieldValues = GenerateFieldValues($DependentValues, $FieldName);

    foreach ($Data as $Key => $Value) {
        if (in_array($Value[$FieldName], $FieldValues, true)) {
            if ($NoId === true && $NewId !== '') {
                $Value[$NewId] = "$Key";
                array_push($CanaryData, $Value);
            } else {
                array_push($CanaryData, $Value);
            }
        }
    }

    return $CanaryData;
}

function GetProduct($Products, $Id) {
    $Product = [];

    $Position = 0;
    while ($Position < count($Products) && empty($Product)) {
        if ($Products[$Position]['id'] === $Id) {
            $Product = $Products[$Position];
        }

        $Position++;
    }

    return $Product;
}

function DefineCookie($Name, $Value) {
    setcookie($Name, $Value, time() + 365 * 24 * 60 * 60, '.');
}

function AddProductCookie($Products, $Id, &$List, $CookieName, $Delete = false) {
    if (CheckListValue($Products, $Id, 'id')) {
        if (!in_array($Id, $List)) {
            array_push($List, $Id);
        } else if (in_array($Id, $List) && $Delete) {
            $Key = array_search($Id, $List);
            unset($List[$Key]);
        }

        sort($List); 
        DefineCookie($CookieName, serialize($List));
    }
}

function AddProductCart($Products, $Id, &$Cart, $Delete = false) {
    if (CheckListValue($Products, $Id, 'id')) {
        if (in_array($Id, array_keys($Cart))) {
            if ($Delete) {
                unset($Cart[$Id]);
            } else {
                $Cart[$Id] += 1;
            }
        } else {
            $Cart[$Id] = 1;
        }
    }

    ksort($Cart);
    $_SESSION['cart'] = serialize($Cart);
}

function CountCart($Cart) {
    $QuantityCart = 0;

    foreach ($Cart as $Id => $QuantityPerId) {
        $QuantityCart += $QuantityPerId;
    }

    return $QuantityCart;
}

function CalculateCosts($Products, $Cart) {
    $Costs = [0, 0, 0];

    foreach ($Cart as $ProductId => $ProductQuantity) {
        if (CheckListValue($Products, $ProductId, 'id')) {
            $Costs[0] += round(floatval(GetProduct($Products, $ProductId)['price']) * $ProductQuantity, 2);
        }
    }

    if ($Costs[0] >= 500) {
        $Costs[1] = 0;
    } else {
        $Costs[1] = round($Costs[0] * (10 / 100), 2);
    }

    $Costs[2] = round($Costs[0] + $Costs[1], 2);

    return $Costs;
}

function CheckRepliesEmpty($Replies) {
    $IsSomethingEmpty = false;
    
    foreach ($Replies as $Key => $Value) {
        if (!in_array($Key, ['information', 'complement'])) {
            if ($Value === '') {
                $IsSomethingEmpty = true;
            }
        }
    }
    
    return $IsSomethingEmpty;
}

function ShowTopButtons($Show, $UserButton, $Cart, $CartShowed) {
    $HTML = '';

    if ($Show) {
        $HTML .= '<div class="col-lg-3 col-md-8 col-sm-10 col-12 mx-auto p-4">' . PHP_EOL;
        $HTML .= '<div class="container">' . PHP_EOL;
        $HTML .= '<div class="row mb-2">' . PHP_EOL;
        $HTML .= '<div class="col-12 px-0 text-center">' . PHP_EOL;
        $HTML .= '<a class="h-100 btn btn-block btn-' . $UserButton[0] . ' t-large" href="' . $UserButton[1] . '"><i class="fas ' . $UserButton[2] . '"></i> ' . $UserButton[3] . '</a>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '<div class="row">' . PHP_EOL;
        $HTML .= '<div class="col-12 px-0 text-center">' . PHP_EOL;

        if ($CartShowed) {
            $HTML .= '<a class="h-100 btn btn-block btn-secondary t-large" href=".">Return</a>' . PHP_EOL;
        } else {
            $HTML .= '<a class="h-100 btn btn-block btn-info t-large" href=".?cart=all"><i class="fas fa-shopping-cart"></i> Cart (<span id="quantitycart">' . CountCart($Cart) . '</span>)</a>' . PHP_EOL;
        }

        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
    } else {
        $HTML .= '<div class="col"></div>' . PHP_EOL;
    }

    return $HTML;
}

function ShowPolicy() {
    $HTML = '';

    $HTML .= '<form method="POST" action="." class="col-lg-4 col-md-6 col-sm-10 col-12 mx-auto">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-primary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<div class="col-12 px-0 text-center">' . PHP_EOL;
    $HTML .= '<span class="t-large">Cookies agreement</span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-12">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-secondary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<p class="col-12 mb-0">' . PHP_EOL;
    $HTML .= 'This website uses cookies to guarantee you basic functionality '
            . 'such as favourites, etc.' . PHP_EOL;
    $HTML .= '</p>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<p class="col-12 mb-0">' . PHP_EOL;
    $HTML .= 'You must accept them in order to continue browsing this website.'
            . PHP_EOL;
    $HTML .= '</p>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-md-6 col-12">' . PHP_EOL;
    $HTML .= '<button type="submit" class="btn btn-success btn-lg btn-block" name="operation" value="accept">Accept</button>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="col-md-6 col-12">' . PHP_EOL;
    $HTML .= '<button type="submit" class="btn btn-danger btn-lg btn-block" name="operation" value="cancel">Cancel</button>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</form>' . PHP_EOL;

    return $HTML;
}

function ShowWarning() {
    $HTML = '';

    $HTML .= '<div class="col-lg-4 col-md-6 col-sm-10 col-12 mx-auto">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-primary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<div class="col-12 text-center">' . PHP_EOL;
    $HTML .= '<span class="t-large">Cookies warning</span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-12">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-secondary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<p class="col-12 mb-0">' . PHP_EOL;
    $HTML .= 'You cannot continue to browse in the website without accepting '
            . 'the "Cookies agreement".' . PHP_EOL;
    $HTML .= '</p>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-12">' . PHP_EOL;
    $HTML .= '<a class="btn btn-secondary btn-lg btn-block" href=".">Return</a>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;

    return $HTML;
}

function ShowLogin($Location, $LoginErrors) {
    $HTML = '';

    $HTML .= '<form method="POST" action="' . $Location . '" class="col-lg-4 col-md-6 col-sm-10 col-12 mx-auto">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-primary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<div class="col-12 text-center">' . PHP_EOL;
    $HTML .= '<span class="t-large">Login</span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-12">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-secondary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<div class="form-group col-12 mb-0">' . PHP_EOL;
    $HTML .= '<label for="username">Username</label>' . PHP_EOL;
    $HTML .= '<input type="text" class="form-control" name="username" id="username" required="" />' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="form-group col-12 mb-0">' . PHP_EOL;
    $HTML .= '<label for="password">Password</label>' . PHP_EOL;
    $HTML .= '<input type="password" class="form-control" name="password" id="password" required="" />' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;

    if (count($LoginErrors) > 0) {
        $HTML .= '<div class="row mb-3">' . PHP_EOL;
        $HTML .= '<div class="col-12">' . PHP_EOL;
        $HTML .= '<ul class="mb-0 p-3 pl-4 rounded bg-danger text-white">' . PHP_EOL;

        foreach ($LoginErrors as $Value) {
            $HTML .= '<li>' . $Value . '</li>' . PHP_EOL;
        }

        $HTML .= '</ul>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
    }

    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-12">' . PHP_EOL;
    $HTML .= '<button type="submit" class="btn btn-success btn-lg btn-block" name="validatelogin">Continue</button>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</form>' . PHP_EOL;

    return $HTML;
}

function PrintProductCard($Product, $Favourite, $InView, $InCart = false, $Quantity = -1) {
    $HTML = '';

    $HTML .= '<div class="col mb-3 mx-auto">' . PHP_EOL;
    $HTML .= '<div class="card">' . PHP_EOL;
    $HTML .= '<img src="' . $Product['image'] . '" class="card-img-top" alt="' . $Product['name'] . '"/>' . PHP_EOL;
    $HTML .= '<div class="card-body">' . PHP_EOL;
    $HTML .= '<div class="card-title">' . PHP_EOL;
    $HTML .= '<div class="container-fluid">' . PHP_EOL;

    if (!$InCart) {
        $HTML .= '<div class="row mb-2">' . PHP_EOL;

        if ($Favourite) {
            $HTML .= '<div class="col-12 px-0 text-center">' . PHP_EOL;
            $HTML .= '<i class="fas fa-heart favourite" data-id="' . $Product['id'] . '"></i>' . PHP_EOL;
            $HTML .= '</div>' . PHP_EOL;
        } else {
            $HTML .= '<div class="col-12 px-0 text-center">' . PHP_EOL;
            $HTML .= '<i class="far fa-heart favourite" data-id="' . $Product['id'] . '"></i>' . PHP_EOL;
            $HTML .= '</div>' . PHP_EOL;
        }

        $HTML .= '</div>' . PHP_EOL;
    }

    $HTML .= '<div class="row">' . PHP_EOL;
    $HTML .= '<span class="col-7 d-block pl-0 font-weight-bold text-center">'
            . $Product['name'] . '</span>' . PHP_EOL;
    $HTML .= '<span class="col-5 d-block pr-0 font-weight-bold text-center">US $'
            . $Product['price'] . '</span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;

    $HTML .= '<div class="container-fluid card-text">' . PHP_EOL;

    if ($InCart) {
        $HTML .= '<div class="row mb-2">' . PHP_EOL;
        $HTML .= '<div class="form-group col-12 px-0 text-center">' . PHP_EOL;
        $HTML .= '<label for="quantityproduct-' . $Product['id'] . '">Quantity</label>' . PHP_EOL;
        $HTML .= '<input type="number" class="form-control quantityproduct" id="quantityproduct-'
                . $Product['id'] . '" min="1" value="' . $Quantity . '" '
                . 'data-id="' . $Product['id'] . '" data-price="' . $Product['price'] . '" />' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;

        $HTML .= '<div class="row mb-2">' . PHP_EOL;
        $HTML .= '<div class="col-12 px-0">' . PHP_EOL;
        $HTML .= '<a class="btn btn-block btn-lg btn-danger removecart" href=".?nocart=' . $Product['id'] . '" >'
                . '<i class="fas fa-cart-plus"></i> Remove of cart</a>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
    } else {
        $HTML .= '<div class="row mb-2">' . PHP_EOL;
        $HTML .= '<p class="col-12 px-0">' . $Product['description'] . '</p>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
    }


    $HTML .= '</div>' . PHP_EOL;

    if ($InView) {
        $HTML .= '<button type="button" class="btn btn-block btn-lg btn-info addcart" '
                . 'data-id="' . $Product['id'] . '"><i class="fas fa-cart-plus"></i> Add to cart</button>' . PHP_EOL;
    } else {
        $HTML .= '<a class="btn btn-block btn-lg btn-primary" href=".?view='
                . $Product['id'] . '">Show details</a>' . PHP_EOL;
    }

    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;

    return $HTML;
}

function ShowProduct($Products, $Id, $Favourites) {
    $HTML = '';

    if (CheckListValue($Products, $Id, 'id')) {
        $HTML .= '<div class="col-xl-5 col-lg-7 col-md-10 col-12 mx-auto">' . PHP_EOL;
        $HTML .= '<div class="container-fluid border border-primary rounded">' . PHP_EOL;
        $HTML .= '<div class="row my-3">' . PHP_EOL;
        $HTML .= '<div class="col"></div>' . PHP_EOL;
        $HTML .= '<h2 class="col-xl-6 col-lg-4 col-12 mb-lg-0 mb-2 text-center">Product</h2>' . PHP_EOL;
        $HTML .= '<div class="col text-center">' . PHP_EOL;
        $HTML .= '<a class="h-100 btn btn-block btn-secondary t-large" href=".">Return</a>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '<div class="row">' . PHP_EOL;

        if (in_array($Id, $Favourites)) {
            $HTML .= PrintProductCard(GetProduct($Products, $Id), true, true);
        } else {
            $HTML .= PrintProductCard(GetProduct($Products, $Id), false, true);
        }

        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
    } else {
        $HTML .= ShowProductWarning();
    }

    return $HTML;
}

function ShowProductWarning() {
    $HTML = '';

    $HTML .= '<div class="col-lg-4 col-md-6 col-sm-10 col-12 mx-auto">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-primary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<div class="col-12 text-center">' . PHP_EOL;
    $HTML .= '<span class="t-large">Inexistent product</span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-12">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-secondary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<p class="col-12 mb-0">' . PHP_EOL;
    $HTML .= 'The selected product doesn\'t exist. Please, return back and choose a '
            . 'correct product.' . PHP_EOL;
    $HTML .= '</p>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-12">' . PHP_EOL;
    $HTML .= '<a class="btn btn-secondary btn-lg btn-block" href=".">Return</a>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;

    return $HTML;
}

function FilterFavourites($Products, $Favourites) {
    $FavouriteProducts = [];

    foreach ($Products as $Position => $Product) {
        if (in_array($Product['id'], $Favourites)) {
            array_push($FavouriteProducts, $Product);
        }
    }

    return $FavouriteProducts;
}

function ShowCart($Products, $Cart, $Costs) {
    $HTML = '';

    $HTML .= '<div class="col-lg-10 col-12 mx-auto">' . PHP_EOL;

    $HTML .= '<div class="container-fluid border border-primary rounded">' . PHP_EOL;

    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<h2 class="col-12 mb-0 text-center">Cart</h2>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;

    if (count($Cart) !== 0) {
        $HTML .= '<div class="row row-cols-lg-2 row-cols-1">' . PHP_EOL;

        foreach ($Cart as $ProductCartId => $ProductCartQuantity) {
            if (CheckListValue($Products, $ProductCartId, 'id')) {
                $HTML .= PrintProductCard(GetProduct($Products, $ProductCartId), false, false, true, $ProductCartQuantity);
            }
        }

        $HTML .= '</div>' . PHP_EOL;
    } else {
        $HTML .= '<div class="row mb-3">' . PHP_EOL;
        $HTML .= '<span class="col-12 d-block my-auto text-center t-large">No products in the cart</span>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
    }

    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-xl-4 col-md-6 col-sm-10 col-12 mx-auto">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-primary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<span class="col-md-6 col-12 mb-lg-0 mb-2 text-center font-weight-bold">Subtotal</span>' . PHP_EOL;
    $HTML .= '<span class="col-md-6 col-12 text-center">US $<span id="subtotal">'
            . $Costs[0] . '</span></span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<span class="col-md-6 col-12 mb-lg-0 mb-2 text-center font-weight-bold">Shipping</span>' . PHP_EOL;
    $HTML .= '<span class="col-md-6 col-12 text-center">US $<span id="shipping">'
            . $Costs[1] . '</span></span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<span class="col-md-6 col-12 mb-lg-0 mb-2 text-center font-weight-bold">Total</span>' . PHP_EOL;
    $HTML .= '<span class="col-md-6 col-12 text-center">US $<span id="total">'
            . $Costs[2] . '</span></span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;

    if ($Costs[2] > 0) {
        $HTML .= '<div class="row mb-3">' . PHP_EOL;
        $HTML .= '<div class="col-md-6 col-12 text-center mx-auto">' . PHP_EOL;
        $HTML .= '<a class="btn btn-lg btn-block btn-primary" href=".?checkout">Checkout</a>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
    }

    $HTML .= '</div>' . PHP_EOL;

    $HTML .= '</div>' . PHP_EOL;

    $HTML .= '</div>' . PHP_EOL;

    return $HTML;
}

function CreateSelect($Data, $Name, $FieldValue, $FieldName, $Selected, $Disabled = false) {
    $HTML = '';

    if (!$Disabled) {
        $HTML .= '<select name="' . $Name . '" class="form-control"
          id="' . $Name . '" required="">' . PHP_EOL;
    } else {
        $HTML .= '<select name="' . $Name . '" class="form-control"
          id="' . $Name . '" disabled="" required="">' . PHP_EOL;
    }
    $HTML .= '<option value="">-- Select --</option>' . PHP_EOL;

    foreach ($Data as $Key => $Value) {
        if ($Value[$FieldValue] === $Selected) {
            $HTML .= '<option value="' . $Value[$FieldValue] . '" selected="">'
                    . $Value[$FieldName] . '</option>' . PHP_EOL;
        } else {
            $HTML .= '<option value="' . $Value[$FieldValue] . '">'
                    . $Value[$FieldName] . '</option>' . PHP_EOL;
        }
    }

    $HTML .= '</select>' . PHP_EOL;

    return $HTML;
}

function ShowCheckout($Replies, $Selects, $CheckoutErrors, $Costs) {
    $HTML = '';

    $HTML .= '<form method="POST" action=".?checkout" class="col-lg-8 col-12 mb-lg-0 mb-2 mx-auto">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-primary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<div class="col"></div>' . PHP_EOL;
    $HTML .= '<h2 class="col-xl-6 col-md-4 col-12 mb-md-0 mb-2 text-center">Checkout</h2>' . PHP_EOL;
    $HTML .= '<div class="col text-center">' . PHP_EOL;
    $HTML .= '<a class="h-100 btn btn-block btn-secondary t-large" href=".?cart=all">Return</a>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-12">' . PHP_EOL;
    $HTML .= '<div class="container-fluid">' . PHP_EOL;
    $HTML .= '<div class="row mb-2">' . PHP_EOL;
    $HTML .= '<span class="col-12 bg-info rounded p-1 text-white text-center font-weight-bold t-large">Personal Information</span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-2">' . PHP_EOL;
    $HTML .= '<div class="form-group col-lg-6 col-12 mb-lg-0 mb-2">' . PHP_EOL;
    $HTML .= '<label class="font-weight-bold" for="firstname">First name (*)</label>' . PHP_EOL;
    $HTML .= '<input type="text" class="form-control" id="firstname" name="firstname" pattern="[a-zA-ZñÑáéíóúÁÉÍÓÚü\s]{2,50}" value="' . $Replies['firstname'] . '" required="" title="Between 2 and 50 word characters"/>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="form-group col-lg-6 col-12 mb-lg-0 mb-2">' . PHP_EOL;
    $HTML .= '<label class="font-weight-bold" for="lastname">Last name (*)</label>' . PHP_EOL;
    $HTML .= '<input type="text" class="form-control" id="lastname" name="lastname" pattern="[a-zA-ZñÑáéíóúÁÉÍÓÚü\s]{2,60}" value="' . $Replies['lastname'] . '" required="" title="Between 2 and 60 word characters"/>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-2">' . PHP_EOL;
    $HTML .= '<div class="form-group col-lg-6 col-12 mx-auto">' . PHP_EOL;
    $HTML .= '<label class="font-weight-bold" for="telephone">Telephone (*)</label>' . PHP_EOL;
    $HTML .= '<input type="tel" class="form-control" id="telephone" name="telephone" value="' . $Replies['telephone'] . '" pattern="[0-9]{9}" required="" title="Number of 9 digits"/>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row">' . PHP_EOL;
    $HTML .= '<div class="form-group col-12">' . PHP_EOL;
    $HTML .= '<label class="font-weight-bold" for="information">Additional information</label>' . PHP_EOL;
    $HTML .= '<textarea class="form-control" name="information" id="information" rows="3" maxlength="2000">' . $Replies['information'] . '</textarea>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-12">' . PHP_EOL;
    $HTML .= '<div class="container-fluid">' . PHP_EOL;
    $HTML .= '<div class="row mb-2">' . PHP_EOL;
    $HTML .= '<span class="col-12 bg-info rounded p-1 text-white text-center font-weight-bold t-large">Shipment</span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-2">' . PHP_EOL;
    $HTML .= '<div class="form-group col-lg-6 col-12">' . PHP_EOL;
    $HTML .= '<label class="font-weight-bold" for="country">Country (*)</label>' . PHP_EOL;
    $HTML .= $Selects[0];
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="form-group col-lg-6 col-12">' . PHP_EOL;
    $HTML .= '<label class="font-weight-bold" for="province">Province (*)</label>' . PHP_EOL;
    $HTML .= $Selects[1];
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-2">' . PHP_EOL;
    $HTML .= '<div class="form-group col-lg-6 col-12">' . PHP_EOL;
    $HTML .= '<label class="font-weight-bold" for="municipality">Municipality (*)</label>' . PHP_EOL;
    $HTML .= $Selects[2];
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="form-group col-lg-6 col-12">' . PHP_EOL;
    $HTML .= '<label class="font-weight-bold" for="zipcode">ZIP code (*)</label>' . PHP_EOL;
    $HTML .= $Selects[3];
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-2">' . PHP_EOL;
    $HTML .= '<div class="form-group col-lg-6 col-12">' . PHP_EOL;
    $HTML .= '<label class="font-weight-bold" for="roadtype">Road type (*)</label>' . PHP_EOL;
    $HTML .= $Selects[4];
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="form-group col-lg-6 col-12">' . PHP_EOL;
    $HTML .= '<label class="font-weight-bold" for="roadname">Road name (*)</label>' . PHP_EOL;
    $HTML .= '<input type="text" class="form-control" id="roadname" name="roadname" pattern="[a-zA-ZñÑáéíóúÁÉÍÓÚü\s]{2,80}" value="' . $Replies['roadname'] . '" required="" title="Between 2 and 80 word characters"/>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row">' . PHP_EOL;
    $HTML .= '<div class="form-group col-md-6 col-12">' . PHP_EOL;
    $HTML .= '<label class="font-weight-bold" for="number">Number (*)</label>' . PHP_EOL;
    $HTML .= '<input type="number" name="number" class="form-control" id="numero" min="0" max="999" value="' . $Replies['number'] . '" required="" />' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="form-group col-md-6 col-12">' . PHP_EOL;
    $HTML .= '<label class="font-weight-bold" for="complement">Complement</label>' . PHP_EOL;
    $HTML .= '<input type="text" name="complement" class="form-control" id="complement"  pattern="[a-zA-ZñÑáéíóúÁÉÍÓÚü\d\s]{2,50}" value="' . $Replies['complement'] . '" title="Between 2 and 50 alphanumeric characters"/>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;

    if (count($CheckoutErrors) > 0) {
        $HTML .= '<div class="row mb-3">' . PHP_EOL;
        $HTML .= '<div class="col-12">' . PHP_EOL;
        $HTML .= '<ul class="mb-0 p-3 pl-4 rounded bg-danger text-white">' . PHP_EOL;

        foreach ($CheckoutErrors as $Value) {
            $HTML .= '<li>' . $Value . '</li>' . PHP_EOL;
        }

        $HTML .= '</ul>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
    }

    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-xl-4 col-md-6 col-sm-10 col-12 mx-auto">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-primary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<span class="col-md-6 col-12 mb-lg-0 mb-2 text-center font-weight-bold">Subtotal</span>' . PHP_EOL;
    $HTML .= '<span class="col-md-6 col-12 text-center">US $<span id="subtotal">'
            . $Costs[0] . '</span></span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<span class="col-md-6 col-12 mb-lg-0 mb-2 text-center font-weight-bold">Shipping</span>' . PHP_EOL;
    $HTML .= '<span class="col-md-6 col-12 text-center">US $<span id="shipping">'
            . $Costs[1] . '</span></span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<span class="col-md-6 col-12 mb-lg-0 mb-2 text-center font-weight-bold">Total</span>' . PHP_EOL;
    $HTML .= '<span class="col-md-6 col-12 text-center">US $<span id="total">'
            . $Costs[2] . '</span></span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-md-6 col-12 text-center mx-auto">' . PHP_EOL;
    $HTML .= '<button type="submit" class="btn btn-lg btn-block btn-primary" name="validatecheckout">Process shipment</button>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</form>' . PHP_EOL;

    return $HTML;
}

function ShowEnd() {
    $HTML = '';

    $HTML .= '<div class="col-lg-4 col-md-6 col-sm-10 col-12 mx-auto">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-primary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<div class="col-12 text-center">' . PHP_EOL;
    $HTML .= '<span class="t-large">Checkout</span>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-12">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-secondary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<p class="col-12 mb-0">' . PHP_EOL;
    $HTML .= 'The order has been processed correctly.' . PHP_EOL;
    $HTML .= '</p>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<p class="col-12 mb-0">' . PHP_EOL;
    $HTML .= 'You can return to the main webpage.' . PHP_EOL;
    $HTML .= '</p>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-3">' . PHP_EOL;
    $HTML .= '<div class="col-12">' . PHP_EOL;
    $HTML .= '<a class="btn btn-secondary btn-lg btn-block" href=".">Return</a>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;

    return $HTML;
}

function ShowCatalogue($Products, $Viewed, $Favourites, $FilterFavourites) {
    $HTML = '';

    $HTML .= '<div class="col-lg-8 col-12 mb-lg-0 mb-2">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-primary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<div class="col text-center"></div>' . PHP_EOL;
    if ($FilterFavourites) {
        $HTML .= '<h2 class="col-xl-6 col-md-4 col-12 mb-md-0 mb-2 text-center">Favourites</h2>' . PHP_EOL;
        $HTML .= '<div class="col text-center">' . PHP_EOL;
        $HTML .= '<a class="h-100 btn btn-block btn-secondary t-large" href=".">Return</a>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
    } else {
        $HTML .= '<h2 class="col-xl-6 col-md-4 col-12 mb-md-0 mb-2 text-center">Products</h2>' . PHP_EOL;
        $HTML .= '<div class="col text-center">' . PHP_EOL;
        $HTML .= '<a class="h-100 btn btn-block btn-danger t-large" href=".?favourite=all"><i class="far fa-heart"></i> Favourites</a>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
    }
    $HTML .= '</div>' . PHP_EOL;

    if (count($Products) !== 0) {
        $HTML .= '<div class="row row-cols-lg-2 row-cols-1">' . PHP_EOL;

        foreach ($Products as $Index => $Product) {
            if (in_array($Product['id'], $Favourites)) {
                $HTML .= PrintProductCard($Product, true, false);
            } else {
                $HTML .= PrintProductCard($Product, false, false);
            }
        }

        $HTML .= '</div>' . PHP_EOL;
    } else {
        $HTML .= '<div class="row mb-3">' . PHP_EOL;
        $HTML .= '<span class="col-12 d-block my-auto text-center t-large">No products in the catalogue</span>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
    }

    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;

    $HTML .= '<div class="col-lg-4 col-12">' . PHP_EOL;
    $HTML .= '<div class="container-fluid border border-primary rounded">' . PHP_EOL;
    $HTML .= '<div class="row my-3">' . PHP_EOL;
    $HTML .= '<div class="col-12 text-center">' . PHP_EOL;
    $HTML .= '<h2 class="mb-0">Viewed</h2>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;

    if (CheckIdListsMatching($Products, $Viewed) !== false) {
        $HTML .= '<div class="row row-cols-1">' . PHP_EOL;

        foreach ($Products as $Index => $Product) {
            if (in_array($Product['id'], $Viewed)) {
                if (in_array($Product['id'], $Favourites)) {
                    $HTML .= PrintProductCard($Product, true, false);
                } else {
                    $HTML .= PrintProductCard($Product, false, false);
                }
            }
        }

        $HTML .= '</div>' . PHP_EOL;
    } else {
        $HTML .= '<div class="row mb-3">' . PHP_EOL;
        $HTML .= '<span class="col-12 d-block my-auto text-center t-large">No products viewed</span>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
    }

    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;

    return $HTML;
}
