<?php
if (!class_exists('Authenticate')) require_once("common_cadline_libraries/crm_test.class.php");

$param      = '';
$result     = '';
$authCode   = '';

if (isset($_POST["result_ok"]) || isset($_POST["result_cancel"])) {
    $param  = isset($_POST["result_ok"]) ? $_POST["result_ok"] : $_POST["result_cancel"];
    $result = isset($_POST["result_ok"]) ? "result_ok" : "result_cancel";
}
if (isset($_POST["authCode"]) || isset($_GET["authCode"])) $authCode = isset($_POST["authCode"]) ? $_POST["authCode"] : $_GET["authCode"];

$decSession = isset($_POST["session"]) ? $_POST['session'] : '';
$session    = base64_encode($decSession);
$lang       = isset($_POST['lang']) ? $_POST['lang'] : '';

$auth = new Authenticate();
$auth->setAuthCode($authCode);
$auth->setAuthSession($session);
$auth->auth();

$texts = "SELECT * FROM `" . CRMADATBAZIS . "`.`webform_results` WHERE session_id = ? AND result = ? ORDER BY insertDate DESC";
Database::getInstance()->query($texts, array('ss', $decSession, "{$result}={$param}"));
$res = Database::getInstance()->fetchNext();

$fileName = Webform::getLanguageFile($lang);

require_once($fileName);
?>

<html>

<head>
    <title><?= $regSuccess_msg['register'] ?></title>

    <link href="/public/img/icons/<?= $common_msg['favicon'] ?>" rel="shortcut icon" type="image/vnd.microsoft.icon" />
    <link rel="stylesheet" href="../css/bootstrap.min.css" />

    <script src="../js/clipboard.min.js"></script>

    <style>
        .container {
            text-align: center;
            margin-left: 0% !important;
        }

        .btn-success {
            position: absolute;
            top: 50%;
        }

        .alert-warning {
            position: absolute;
            top: 40%;
            left: 27%;
        }

        img {
            position: absolute;
            margin-top: 2%;
            margin-left: 1%;
        }

        .alert-info {
            position: absolute;
            bottom: 10%;
            left: 27%;
        }
    </style>
</head>

<body>
    <a href="<?= $common_msg['webpage'] ?>" target="_blank"><img src="images/<?= $common_msg['logo'] ?>" /></a>
    <?php if ($param != '') : ?>
        <div class="container">
            <div class="alert alert-warning" role="alert">
                <?= $res["message"] ?> <br>
            </div>
            <button class="btn btn-lg btn-success"><?= $res["button"] ?></button>
            <br />

            <?php if (isset($_POST['note']) && $_POST['note'] == 'true') : ?>
                <div class="alert alert-info" role="alert">
                    <?= $result_msg['help'] ?>
                </div>
            <?php endif; ?>
        </div>

        <script>
            var clipboard = new Clipboard('.btn-success', {
                text: function() {
                    return '<' + '?xml version="1.0"?' + '><WebFormResult>?session=<?= $session ?>&<?= $result ?>=<?= $param ?></WebFormResult>';
                }
            });
        </script>
    <?php endif; ?>
</body>

</html>