<?php

if (session::isConnect("salarie")) {
    $return = array();
    if (event::checkUrl(core::getPost("qRcode")) != DOMAIN_EVENTS and event::checkUrl(core::getPost("qRcode")) != DOMAIN_CONTROL and PWA == 0) {
        $return["result"] = false;
        $return["description"] = "Le QR-Code n'est pas valide";
        $return["salarie"] = null;
        $return["evenement"] = null;
    } elseif (event::checkUrl(core::getPost("qRcode")) == DOMAIN_CONTROL) {
        $return["result"] = false;
        $return["description"] = "Ce QR-Code doit être validé par un contrôleur";
        $return["salarie"] = null;
        $return["evenement"] = null;
    } else {
        $md5Evenement = event::getEvenementbyQRCode(core::getPost("qRcode"))["e"];
        $checkEmarge = event::checkEvenementBySalarie($md5Evenement);
        if ($checkEmarge["result"] == TRUE) {
            $return = event::emargementEvenement(event::getIdEvenementByMd5($md5Evenement), session::getId("salarie"));
        } else {
            $return = $checkEmarge;
        }
        $return["salarie"] = session::getName("salarie", session::getId("salarie"));
    }
    echo json_encode($return);
    exit();
} elseif (session::isConnect()) {
    if (core::ifGet("s") and core::ifGet("e") and core::ifGet("k")) {
        $salarie = salaries::getSalarieById(core::getGet("s"));
        $evenement = event::getFicheByMd5(core::getGet("e"));
        $return = event::emargementEvenementByQRCode();
        $return["salarie"] = $salarie["prenom"] . " " . $salarie["nom"];
        $return["evenement"] = $evenement["titre"];
    } else {
        $return["result"] = false;
        $return["description"] = "Le QR-Code n'est pas valide";
        $return["salarie"] = null;
        $return["evenement"] = null;
    }
    echo json_encode($return);
    exit();
} else {
    header('HTTP/1.0 401 Unauthorized');
    exit();
}
