<?php
header("Access-Control-Allow-Origin: *");
include_once "includes/general_required.php";
// redirect to app url

if (Session::has("product")) {
    $product = Session::get("product");
    $p_name = $product[ew("name_key")];

    $msisdn = Session::flash("msisdn");
    if(!$msisdn){
        General::redirect(url());
    }
     $redirect_url = "" ;
    switch ($product["service_type"]) {
        case "ivr":
            $shortcode = find($product, "shortcode_ivr");
            $redirect_url = "tel:$shortcode";
            $open_app_lable = ew("call now!");
            $app_will_open_label = ew("The dialer will be opened in");
            break;
        case "digital":
            $redirect_url = find($product, "redirect_url_digital");
            
            $redirect_url = str_replace("[MSISDN]", "249".General::tokenizerPhone($msisdn), $redirect_url);
            $app_will_open_label = ew("The app will be opened in");
            $open_app_lable = ew("click to open the app");
            if ($product["subscription_mode"] != "sub") {
                $app_will_open_label = ew("Will redirect automatically in");
                $open_app_lable = ew("get the product!");
            }
            break;
        case "sms":
            $shortcode = find($product, "shortcode_ivr");
            $redirect_url = "sms:$shortcode";
            $open_app_lable = ew("read now!");
            $app_will_open_label = ew("The messenger will be opened in");
            break;
    }

    $page_title = $p_name . " | " . ew("Thanks page");
    
 
 $has_callback = false;
 $callback= "";
    if(Session::has("click_id")){
        $has_callback = true;         
        $click_id = Session::get("click_id");
        $callback = find($product, "callback");
        if($click_id && $callback){
            if(strpos($callback, "?") !== false){
                  $callback .= "&click_id=$click_id";
                  //$callback .= "&$GET";
            } else {
                   $callback .= "?click_id=$click_id";
                   //$callback .= "?$GET";
            }
            $response = ""; // General::callURL($callback, null, false, 10);

            General::writeEvent(json_encode([
                "URL" => $callback,
                "ProductCode" => $product['code'],
                "MSISDN" => $msisdn,
                "Response" => $response,
            ]),"he-tracking");
        }
    }
    
} else {
    General::redirect(url());
}

include_once file_path("includes/header.php");
?>

<?php include_once file_path("includes/body_header.php"); ?>

<?php include_once file_path("includes/nav.php") ?>
<!-- TEXT -->
<div class="warp uk-container  uk-flex-center uk-text-center" data-uk-scrollspy="target: > .animate; cls: uk-animation-slide-bottom-small ; delay: 300">
    <div class="uk-card uk-card-default uk-card-body  uk-flex-center uk-margin-auto animate">
        <div class="uk-animation-toggle" tabindex="0">
            <img class="uk-animation-stroke" width="300" height="300" src="<?= url("assets/images/undraw_order_confirmed_aaw7.svg") ?>" alt="thanks">
        </div>
        <div class="uk-alert-success" uk-alert>
            <p> <?= $product["sub_message"] . "," ?>
                <?php if ($redirect_url) { ?>
                    <?php if ($product["subscription_mode"] != "sub") { ?>
                        <?= $app_will_open_label ?> <span id="secs"></span> <?= ew("seconds...") ?>
                    <?php } ?>
                    <a href="<?= $redirect_url ?>" id="redirect_url"><?= $open_app_lable ?></a>
                <?php } ?>
            </p>
        </div>
    </div>

</div>

<!-- /TEXT -->
<?php include_once file_path("includes/footer-html.php") ?>

<?php include_once file_path("includes/body_footer.php"); ?>

<script>
function httpGet() {


}

httpGet();

<?php if ($product["subscription_mode"] == "sub") { ?>
    $(document).ready(function() {
     httpGet();


        var i = 3;
        $("#secs").text("" + (i--));
        var interval = setInterval(() => {
            if (i == 0) {
                $('#redirect_url').attr("href","javascript: void(0)");
                clearInterval(interval);
                window.location.href = "<?= $redirect_url ?>";
            }
            $("#secs").text("" + (i--));
        }, 1000);
        $('#redirect_url').one('click', function() {
            clearInterval(interval);
            $(this).removeAttr('href');
            window.location.href = "<?= $redirect_url ?>";
        });
         $("#redirect_url").click(function(e){
             e.preventDefault();
             $("#redirect_url").off('click');
         });
    });
<?php } ?>
</script>
