<?php 
session_start();
// Jika tidak bisa login maka balik ke login.php
// jika masuk ke halaman ini melalui url, maka langsung menuju halaman login
if (!isset($_SESSION['login'])) {
    header('location:login.php');
    exit;
}

$_SESSION['role'];
require 'function.php';
require 'config.php';

include_once 'reset.php';

// Menampilkan semua data dari table comment_id berdasarkan nama secara Descending
$comment_id = query("SELECT * FROM comment_id ORDER BY no_id DESC");

// $whatsapp = query("SELECT * FROM users_id");

$result = mysqli_query($koneksi, "SELECT * FROM users_id" );

$whatsapp = mysqli_fetch_assoc($result);

$pesan = $whatsapp['whatsapp'];

$user_role = $whatsapp['role'];

$notifwa = $whatsapp['starsender'];

$hasil = mysqli_query($koneksi, "SELECT * FROM users_id" );

$check = mysqli_fetch_assoc($hasil);

$apikey = $check['apikey'];

$cat = get_cat();

if (isset($_POST['simpan'])) {
    $result = tambah($_POST);
    if ($result) {
        redirect( "/dashboard.php" );
        echo "<script>
                alert('Daftar tamu berhasil ditambahkan!');
            </script>";
    } else {
        // Jika fungsi tambah dari 0/data tidak tersimpan, maka munculkan alert dibawah
        echo "<script>
                alert('Daftar tamu gagal ditambahkan!');
            </script>";
    }
}
require 'header.php';
?>

    <!-- Container -->
    <div class="container" style="">
        <div class="row my-2">
            <div class="col-md">
                <h3 class="text-center fw-bold text-uppercase" style="margin-top: 15px;">DAFTAR TAMU UNDANGAN</h3>
                <hr>
            </div>
        </div>
        

    <div id="reader" width="100%" height="100%"></div>
    <script src="./dist/html5-qrcode.min.js"></script>
    <script src="./dist/app.min.js"></script>
    <script>
        function onScanSuccess(decodedText, decodedResult) {
            console.log(`Scan result: ${decodedText}`, decodedResult);
            document.getElementById("nama").value = decodedText;
        }
    
        setTimeout( () => {
            let html5QrcodeScanner = new Html5QrcodeScanner(
                "reader", { fps: 10, qrbox: 250 });
            html5QrcodeScanner.render(onScanSuccess);
        }, 1000)
    </script>

    <div class="col" style="padding: 10px; text-align: center;">
        <h5>Import data tamu dalam format csv</h5>
        <form id="testform" onsubmit="return checkForm(this);" action="barcode.php" method="post" enctype="multipart/form-data">
            <input type="text" class="form-control form-control-md" id="nama" name="nama" onblur="submitForm( 'testform' );">
            <hr>
            <button type="submit" id="" class="btn btn-primary" style="width:100%;" ><i class="bi bi-file-earmark-plus"></i>&nbsp;Simpan</button>
        </form>
    </div>
    

<?php 
if (isset($_POST['update'])) {
    if (update($_POST) > 0) {?>
        <script>
            swal("Success!", "Daftar tamu berhasil diupdate!", "success");
            document.location.href = 'dashboard.php';
        </script>
        <?php
    } else {?>

        <script>
            swal("Sory!", "Daftar tamu gagal diupdate!", "error");
        </script><?php
    }
}
?>


<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>

<script type="text/javascript">
    var submitForm = function( form ) {
   var viaID = Boolean( document.getElementById );
   try { 
   form = document.forms[ form ];
   } catch( e ) {
   form = (( viaID ) ? document.getElementById( form ) : (( document.all && !viaID ) ? document.all[ form ] : document.layers[ form ] ));
   } if (( form ) && ( form.nama.value || form.nama.length )) {
   // form.action = "action.php" // if you need to change something on the action URL, you can do it here.
   alert("Your barcode ( #" + form.nama.value + " ) entry has been validated, press ok to continue.");
   form.submit();
   return true;
   } alert("Insufficient data, please enter your barcode #");
   return false;
};
</script>
</body>

</html>