<?PHP 
require_once('core.php');
if(!isset($_SESSION['id']))
{
header("Location: index.php");
exit();
}
$menu = "shop";
$page = "badge";
$erreur = false;
if (isset($_POST['add'])) {
    $id = intval($_POST['id']);
    if ($id != "") {
        $SelectBadges = $bdd->prepare('SELECT * FROM pluscms_boutique_badges WHERE id = ? LIMIT 1');
        $SelectBadges->execute(array($id));
        $f = $SelectBadges->fetch();
        $SelectBadges2 = $bdd->prepare('SELECT * FROM user_badges WHERE user_id = ? AND badge_id = ? LIMIT 1');
        $SelectBadges2->execute(array($U['id'],$f['code']));
        if ($SelectBadges->rowCount() == 1) {
            if ($U['vip_points'] < $f['prix']) {
                $message = "Tu n'a pas assez de Diamants !";
                $erreur = true;
            } elseif ($SelectBadges2->rowCount() == 1) {
                $message = "Tu possède déjà ce badge !";
                $erreur = true;
            }
            if ($erreur == false) {
            $AddBadge = $bdd->prepare('INSERT INTO user_badges (user_id,badge_id,badge_slot) VALUES (?,?,?)');
            $AddBadge->execute(array($U['id'],$f['code'],0));
            $UPPoints = $bdd->prepare('UPDATE users SET vip_points = vip_points - ? WHERE id = ? LIMIT 1');
            $UPPoints->execute(array($f['prix'],$U['id']));
            $message = "Félicitation le badge est désormais dans ton inventaire !";
        }
        } else {
        $erreur = true;
        $message = "Désolé le badge demander n'existe pas !";
        }
    } else {
        $erreur = true;
        $message = "Une erreur est survenue veuillez ré-essayer !";
    }
}
?>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title><?PHP echo htmlentities(trim($cms['nom_hotel'])); ?> - Badges </title>
        <link type="text/css" href="<?PHP echo htmlspecialchars(trim($cms['lien'])); ?>/web/css/material.css" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="<?PHP echo htmlspecialchars(trim($cms['lien'])); ?>/web/css/nav.css">
        <script src="<?PHP echo htmlspecialchars(trim($cms['lien'])); ?>/web/js/jquery.js"></script>
        <script type="text/javascript" src="<?PHP echo htmlspecialchars(trim($cms['lien'])); ?>/web/js/smoothscroll.js"></script>
        <script src="//api.dedipass.com/v1/pay.js"></script>
    </head> 
    <body>
    <?PHP require_once('template/header/template_header.php'); ?>
        <div class="container_24">
        <?PHP require_once('template/header/template_nav.php'); ?>
        <?PHP if (isset($message) && $erreur == true) { ?>
                <div class="grid_24">
            <div id="erreur">
                <div class="title red">
                    <?PHP echo htmlentities(trim($message)); ?>
                </div>
            </div>
        </div>
        <?PHP } elseif (isset($message) && $erreur == false) { ?>
        <div class="grid_24">
            <div id="good">
                <div class="title red">
                    <?PHP echo htmlentities(trim($message)); ?>
                </div>
            </div>
        </div>
        <?PHP } ?>
        <div class="grid_24" style="text-align: center;">
            <img src="web/img/boutique/header_shop.png">
        </div>
        <?PHP
        $SelectBadges = $bdd->prepare('SELECT * FROM pluscms_boutique_badges');
        $SelectBadges->execute();
        while ($ba = $SelectBadges->fetch()) { ?>
            <div class="grid_5">
                <div id="contentBox" class="activity" style="overflow: hidden;">
                    <div class="head">
                        <div class="badgeImage" style="background-image: url('https://images.habbo.com/c_images/album1584/<?PHP echo htmlspecialchars(trim($ba['code'])); ?>.gif');"></div>
                        <?PHP echo htmlspecialchars(trim($ba['type'])); ?>
                    </div>
                    <div class="text" style="text-align: center;">
                    <form method="post">
                    <input style="display: none;" name="id" value="<?PHP echo intval($ba['id']); ?>">
                    <button name="add" class="button raised green" style="padding: 10px 10px 30px 10px;line-height: 0px;border: none;">
                            <div class="center"><?PHP echo htmlspecialchars(intval($ba['prix'])); ?> <img style="position: relative;top: 3px;" src="web/img/me/diamonds.png"></div>
                    </button>
                    </form>
                    </div>
                </div>
            </div> 
            <?PHP } ?> 
            <div class="grid_24">
                <div id="contentBox">
                    <div class="title red">
                        <?PHP echo htmlentities(trim($cms['nom_hotel'])); ?> est propulsé par PlusCMS par Dodone.
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>