<?php
session_start();
if (!isset($_SESSION['role'])) {
    header("Location: ../login.php");
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Scan Balita</title>

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

<style>
body{
    font-family:Arial;
    text-align:center;
    background:#f5f5f5;
}
#reader{
    width:320px;
    margin:auto;
}
h2{
    margin-top:15px;
}
.info{
    color:#666;
    font-size:14px;
}
</style>
</head>
<body>

<h2>SCAN BARCODE BALITA</h2>
<div class="info">Arahkan kamera ke barcode buku KIA</div>
<br>

<div id="reader"></div>

<script>

function onScanSuccess(decodedText) {
    // get angka saja
    decodedText = decodedText.trim();

    // vibrate (kalau hp support)
    if (navigator.vibrate) navigator.vibrate(200);

    // langsung buka data balita
    window.location.href = "balita_detail.php?id=" + decodedText;
}

let html5QrcodeScanner = new Html5QrcodeScanner(
    "reader",
    {
        fps: 10,
        qrbox: 250,
        rememberLastUsedCamera: true
    },
    false
);

html5QrcodeScanner.render(onScanSuccess);

</script>

</body>
</html>
