<?php
session_start();
$key = md5(date("Y-m-d").":".$_SERVER['REMOTE_ADDR']);
if (isset($_SESSION['aW1iZXN0cmEuY29tLm15']) <> $key) {
    header("location:".$_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME']); // cara baru !
} else {
    include "_header5.php";
    include "_navtop.php";
    include "_navside.php";
    include "_functions.php";
    require "conn.php";

?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>Redeem</h2>
                    </div>
                </div>

                 <!-- /. ROW  -->
                <hr />                
            <div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                             Scan to Redeem
                        </div>
                        

    <div id="container">
      <div id="reader"></div>
      <h2 id="scanResultText"></h2>
      <a id="scanResultUrl" target="_blank"></a>
      <button id="refreshButton" onclick="refreshScanner()">New Scan</button>
    </div>

    <script src="https://unpkg.com/html5-qrcode"></script>

    <script>
      // Function to handle successful QR code scans
      function onScanSuccess(decodedText, decodedResult) {
        console.log(`Scan result: ${decodedText}`, decodedResult);

        if (isValidUrl(decodedText)) {
          var scanResultLink = document.getElementById("scanResultUrl");
          scanResultLink.href = decodedText;
          scanResultLink.innerText = decodedText;
        } else {
          document.getElementById("scanResultText").textContent = `Scan result: ${decodedText}`;
        }
        html5QrcodeScanner.clear();

        document.getElementById("refreshButton").style.display = "block";
      }

      // Function to refresh the QR code scanner
      function refreshScanner() {
        location.reload(true);
      }

      // Function to validate URLs
      function isValidUrl(url) {
        var pattern = new RegExp(
          "^(https?:\\/\\/)?" +
            "((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" +
            "((\\d{1,3}\\.){3}\\d{1,3}))" +
            "(\\:\\d+)?(\\/[-a-z\\d%@_.~+&:]*)*" +
            "(\\?[;&a-z\\d%@_.,~+&:=-]*)?" +
            "(\\#[-a-z\\d_]*)?$",
          "i"
        );
        return !!pattern.test(url);
      }

      // Initialize the QR code scanner
      var html5QrcodeScanner = new Html5QrcodeScanner("reader", { fps: 10, qrbox: 250 });

      // Render the QR code scanner with the defined success callback
      html5QrcodeScanner.render(onScanSuccess);
    </script>
                            
                       
                    </div>
                    <!--End Advanced Tables -->
                </div>
            </div>          
    </div>
             <!-- /. PAGE INNER  -->
            </div>
<?
    include "_footer2.php";
}
?>
