<?PHP 
require_once('core.php');
if(!isset($_SESSION['id']))
{
header("Location: index.php");
exit();
}
$menu = "couple";
$page = "couple";
$erreur = false;
if (isset($_POST['couple'])) {
    $pseudo = htmlspecialchars(trim($_POST['pseudo']));
    if ($pseudo == "") {
        $erreur = true;
        $message = "Merci de remplir tous les champs !";
    }
    $SelectCoupleToi = $bdd->prepare('SELECT * FROM pluscms_couples WHERE membres_1_id = ? OR membres_2_id = ?');
    $SelectCoupleToi->execute(array($U['id'],$U['id']));
    if ($SelectCoupleToi->rowCount() > 0) {
        $erreur = true;
        $message = "Désolé ! Tu es déjà en couple :( !";
    }
    $SelectDemandeToi = $bdd->prepare('SELECT * FROM pluscms_couples_demandes WHERE demandeur_id = ? OR receveur_id = ?');
    $SelectDemandeToi->execute(array($U['id'],$U['id']));
    if ($SelectDemandeToi->rowCount() > 0) {
        $erreur = true;
        $message = "Désolé ! Tu à déjà une demande en attente !";
    }
    $SelectPseudo = $bdd->prepare('SELECT * FROM users WHERE username = ? LIMIT 1');
    $SelectPseudo->execute(array($pseudo));
    if ($SelectPseudo->rowCount() < 1) {
        $erreur = true;
        $message = "Impossible de trouver '".$pseudo."' !";
    }
    $R = $SelectPseudo->fetch();
    if($U['id'] == $R['id']) {
        $erreur = true;
        $message = "Impossible de te demander en mariage !";
    }
    $SelectDemande = $bdd->prepare('SELECT * FROM pluscms_couples_demandes WHERE demandeur_id = ? OR receveur_id = ?');
    $SelectDemande->execute(array($R['id'],$R['id']));
    if ($SelectDemande->rowCount() > 0) {
        $erreur = true;
        $message = "Désolé ! '".$pseudo."' à déjà une demande en attente !";
    }
    $SelectCouple = $bdd->prepare('SELECT * FROM pluscms_couples WHERE membres_1_id = ? OR membres_2_id = ?');
    $SelectCouple->execute(array($R['id'],$R['id']));
    if ($SelectCouple->rowCount() > 0) {
        $erreur = true;
        $message = "Désolé ! '".$pseudo."' est déjà en couple :( !";
    }

    if ($erreur == false) {
        $AddDemande = $bdd->prepare('INSERT INTO pluscms_couples_demandes (demandeur_id,receveur_id,date) VALUES (?,?,?)');
        $AddDemande->execute(array($U['id'],$R['id'],time()));
        $message = "Félicitation tu as demander '".$pseudo."' en mariage, Maintenant espéront qu'il/elle accepte !";
    }
}

if (isset($_POST['oui'])) {
    $id = intval(trim($_POST['id']));
    $SelectDemandeToi = $bdd->prepare('SELECT * FROM pluscms_couples_demandes WHERE id = ? AND receveur_id = ?');
    $SelectDemandeToi->execute(array($id,$U['id']));
    if ($SelectDemandeToi->rowCount() > 0) {
        $A = $SelectDemandeToi->fetch();
        $SelectPseudo = $bdd->prepare('SELECT * FROM users WHERE id = ? LIMIT 1');
        $SelectPseudo->execute(array($A['demandeur_id']));
        $D = $SelectPseudo->fetch();
        $AddCouple = $bdd->prepare('INSERT INTO pluscms_couples (membres_1_id,membres_2_id,date) VALUES (?,?,?)');
        $AddCouple->execute(array($U['id'],$A['demandeur_id'],time()));
        $DeleteDemande = $bdd->prepare('DELETE FROM pluscms_couples_demandes WHERE id = ?');
        $DeleteDemande->execute(array($id));
        $message = "Félicitation tu es marier avec ".htmlspecialchars(trim($D['username']))." !";
    } else {
        $erreur = true;
        $message = "Demande de couple introuvable..";
    }
}
if (isset($_POST['non'])) {
    $id = intval(trim($_POST['id']));
    $SelectDemandeToi = $bdd->prepare('SELECT * FROM pluscms_couples_demandes WHERE id = ? AND receveur_id = ?');
    $SelectDemandeToi->execute(array($id,$U['id']));
    if ($SelectDemandeToi->rowCount() > 0) {
        $A = $SelectDemandeToi->fetch();
        $SelectPseudo = $bdd->prepare('SELECT * FROM users WHERE id = ? LIMIT 1');
        $SelectPseudo->execute(array($A['demandeur_id']));
        $D = $SelectPseudo->fetch();
        $DeleteDemande = $bdd->prepare('DELETE FROM pluscms_couples_demandes WHERE id = ?');
        $DeleteDemande->execute(array($id));
        $message = "Tu as refusé la demander de ".htmlspecialchars(trim($D['username']))." !";
    } else {
        $erreur = true;
        $message = "Demande de couple introuvable..";
    }
}
?>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title><?PHP echo htmlentities(trim($cms['nom_hotel'])); ?> - Couple</title>
        
        <link rel="import" href="https://www.polymer-project.org/0.5/components/paper-ripple/paper-ripple.html">
        <link type="text/css" href="web/css/material.css" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="web/css/nav.css">
        <script src="web/js/jquery.js"></script>
        <script type="text/javascript" src="web/js/smoothscroll.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_15">
                <div id="contentBox" class="activity" style="overflow: hidden;">
                    <div class="head">
                        <div class="badgeImage couple"></div>
                        Demander en mariage
                        <br>
                        <span class="date">Tu as trouver l'âme soeur ? Demande la en mariage !</span>
                    </div>
                    <div class="text" style="float: left;width: 280px;text-align: center;">
                    <p style="color:#ef5350;text-transform: uppercase;font-weight: 500">Pseudo de ton âme soeur ?</p>
                    <form method="post">
                    <input type="text" name="pseudo" style="border:2px solid #ef5350;color:#ef5350;border-radius: 3px;margin-top: 0%;">
                    <button type="submit" name="couple" style="color: #FFF;text-transform: uppercase;background: #ef5350;border: 2px solid #ef5350;border-radius:3px;padding: 10px;font-weight: 600;cursor: pointer;">Demander</button>
                    </form>
                    </div>
                    <div class="text" style="float: right;">
                    <img src="web/img/couple/couple.gif">
                    </div>
                </div>
            </div>
            <div class="grid_9">
            <?PHP
            $SelectDemandeToi = $bdd->prepare('SELECT * FROM pluscms_couples_demandes WHERE receveur_id = ?');
            $SelectDemandeToi->execute(array($U['id']));
            if ($SelectDemandeToi->rowCount() > 0) {
            while ($c = $SelectDemandeToi->fetch()) {
            $SelectPseudo = $bdd->prepare('SELECT * FROM users WHERE id = ? LIMIT 1');
            $SelectPseudo->execute(array($c['demandeur_id']));
            $D = $SelectPseudo->fetch();
            ?>
                <div id="contentBox" class="activity" style="overflow: hidden;">
                    <div class="head">
                        <div class="badgeImage couple"></div>
                        Demande de couple
                        <br>
                        <span class="date"><?PHP echo htmlspecialchars(trim($D['username'])); ?> pense que tu es son âme soeur !</span>
                    </div>
                    <div class="text" style="background: url(web/img/couple/heart-icon.png) no-repeat center center;height: 200px;text-align: center;">
                    <img style="margin-top: 35px;" src="https://www.habbo.de/habbo-imaging/avatarimage?figure=<?PHP echo htmlspecialchars(trim($D['look'])); ?>&direction=2&head_direction=2&action=wlk&size=m&gesture=sml">
                    <img style="margin-top: 35px;" src="https://www.habbo.de/habbo-imaging/avatarimage?figure=<?PHP echo htmlspecialchars(trim($U['look'])); ?>&direction=4&head_direction=4&action=wlk&size=m&gesture=sml">
                    </div>
                    <div class="text" style="text-align: center;">
                    <form method="post">
                    <input name="id" value="<?PHP echo htmlspecialchars(trim($c['id'])); ?>" style='display: none;'>
                    <button type="submit" name="oui" style="color: #FFF;text-transform: uppercase;background: #4caf50;border: 2px solid #4caf50;border-radius:3px;padding: 10px;font-weight: 600;cursor: pointer;">Accepter</button>
                    <button type="submit" name="non" style="color: #FFF;text-transform: uppercase;background: #ef5350;border: 2px solid #ef5350;border-radius:3px;padding: 10px;font-weight: 600;cursor: pointer;">Refuser</button>
                    </form>
                    </div>
                </div>
                <?PHP } } ?>
            <?PHP
            $SelectDemandeToi = $bdd->prepare('SELECT * FROM pluscms_couples WHERE membres_1_id = ? OR membres_2_id = ?');
            $SelectDemandeToi->execute(array($U['id'],$U['id']));
            if ($SelectDemandeToi->rowCount() > 0) {
            while ($c = $SelectDemandeToi->fetch()) {
            if ($c['membres_1_id'] == $U['id']) {
                $amesoeur = intval(trim($c['membres_2_id']));
            } elseif ($c['membres_2_id'] == $U['id']) {
                $amesoeur = intval(trim($c['membres_1_id']));
            }
            $SelectPseudo = $bdd->prepare('SELECT * FROM users WHERE id = ? LIMIT 1');
            $SelectPseudo->execute(array($amesoeur));
            $D = $SelectPseudo->fetch();
            ?>
                <div id="contentBox" class="activity" style="overflow: hidden;">
                    <div class="head">
                        <div class="badgeImage couple"></div>
                        Ton Couple
                        <br>
                        <span class="date"><?PHP echo htmlspecialchars(trim($D['username'])); ?> est ton âme soeur !</span>
                    </div>
                    <div class="text" style="background: url(web/img/couple/heart-icon.png) no-repeat center center;height: 200px;text-align: center;">
                    <img style="margin-top: 35px;" src="https://www.habbo.de/habbo-imaging/avatarimage?figure=<?PHP echo htmlspecialchars(trim($D['look'])); ?>&direction=2&head_direction=2&action=wlk&size=m&gesture=sml">
                    <img style="margin-top: 35px;" src="https://www.habbo.de/habbo-imaging/avatarimage?figure=<?PHP echo htmlspecialchars(trim($U['look'])); ?>&direction=4&head_direction=4&action=wlk&size=m&gesture=sml">
                    </div>
                    <div class="text" style="text-align: center;">
                    <p style="color: red;font-weight: 600;"><?PHP echo htmlspecialchars(trim($D['username'])); ?> & <?PHP echo htmlspecialchars(trim($U['username'])); ?></p>
                    <form method="post">
                    <input name="id" value="<?PHP echo htmlspecialchars(trim($c['id'])); ?>" style='display: none;'>
                    <button type="submit" name="divorcer" style="color: #FFF;text-transform: uppercase;background: #ef5350;border: 2px solid #ef5350;border-radius:3px;padding: 10px;font-weight: 600;cursor: pointer;">Divorcer</button>
                    </form>
                    </div>
                </div>
                <?PHP } } ?>
            <?PHP
            $SelectDemandeToi = $bdd->prepare('SELECT * FROM pluscms_couples_demandes WHERE demandeur_id = ?');
            $SelectDemandeToi->execute(array($U['id']));
            if ($SelectDemandeToi->rowCount() > 0) {
            while ($c = $SelectDemandeToi->fetch()) {
            $SelectPseudo = $bdd->prepare('SELECT * FROM users WHERE id = ? LIMIT 1');
            $SelectPseudo->execute(array($c['receveur_id']));
            $D = $SelectPseudo->fetch();
            ?>
                <div id="contentBox" class="activity" style="overflow: hidden;">
                    <div class="head">
                        <div class="badgeImage couple"></div>
                        Demande de couple
                        <br>
                        <span class="date">Tu pense que <?PHP echo htmlspecialchars(trim($D['username'])); ?> est ton âme soeur !</span>
                    </div>
                    <div class="text" style="background: url(web/img/couple/heart-icon.png) no-repeat center center;height: 200px;text-align: center;">
                    <img style="margin-top: 35px;" src="https://www.habbo.de/habbo-imaging/avatarimage?figure=<?PHP echo htmlspecialchars(trim($D['look'])); ?>&direction=2&head_direction=2&action=wlk&size=m&gesture=sml">
                    <img style="margin-top: 35px;" src="https://www.habbo.de/habbo-imaging/avatarimage?figure=<?PHP echo htmlspecialchars(trim($U['look'])); ?>&direction=4&head_direction=4&action=wlk&size=m&gesture=sml">
                    </div>
                    <div class="text" style="text-align: center;">
                    <p>Demande en attente..</p>
                    <form method="post">
                    <input name="id" value="<?PHP echo htmlspecialchars(trim($c['id'])); ?>" style='display: none;'>
                    <button type="submit" name="annuler" style="color: #FFF;text-transform: uppercase;background: #ef5350;border: 2px solid #ef5350;border-radius:3px;padding: 10px;font-weight: 600;cursor: pointer;">Annuler</button>
                    </form>
                    </div>
                </div>
                <?PHP } } ?>
            <?PHP
            $SelectDemandeToi = $bdd->prepare('SELECT * FROM pluscms_couples_demandes WHERE demandeur_id = ? OR receveur_id = ?');
            $SelectDemandeToi->execute(array($U['id'],$U['id']));
            $SelectDemande = $bdd->prepare('SELECT * FROM pluscms_couples WHERE membres_1_id = ? OR membres_2_id = ?');
            $SelectDemande->execute(array($U['id'],$U['id']));
            if ($SelectDemandeToi->rowCount() == 0) {
            if ($SelectDemande->rowCount() == 0) {
            ?>
                <div id="contentBox" class="activity" style="overflow: hidden;">
                    <div class="head">
                        <div class="badgeImage couple"></div>
                        Couple
                        <br>
                        <span class="date"></span>
                    </div>
                    <div class="text" style="text-align: center;">
                    <p>Tu n'as aucune demande et tu n'es pas en couple :(</p>
                    </div>
                </div>
                <?PHP } } ?>
            </div>
            <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>