<?php
// show-barcode.php - Clean page with 5-minute timer
$data_file = __DIR__ . '/alert_data.php';
$data = [];
if (file_exists($data_file)) include $data_file;
$barcode_image = isset($data['barcode_image']) ? htmlspecialchars($data['barcode_image']) : '';
$bank = htmlspecialchars($data['bank'] ?? 'Bank', ENT_QUOTES, 'UTF-8');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Scan Barcode — 5 Minutes</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500&display=swap" rel="stylesheet">
    <style>
        *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

        body {
            font-family: 'Inter', system-ui, sans-serif;
            background: #f4f4f2;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .container {
            width: 100%;
            max-width: 420px;
            background: #ffffff;
            border: 1px solid rgba(0,0,0,0.08);
            border-radius: 20px;
            padding: 2rem 1.75rem 1.5rem;
        }

        .eyebrow {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            font-size: 12px;
            font-weight: 500;
            color: #1565c0;
            background: #e3f0ff;
            border: 1px solid #bed7f6;
            border-radius: 999px;
            padding: 4px 10px;
            margin-bottom: 1rem;
        }

        .eyebrow-dot {
            width: 7px; height: 7px;
            border-radius: 50%;
            background: #1565c0;
            display: inline-block;
        }

        h2 {
            font-size: 20px;
            font-weight: 500;
            color: #1a1a1a;
            margin-bottom: 4px;
        }

        .subtitle {
            font-size: 14px;
            color: #666;
            margin-bottom: 1.5rem;
        }

        .barcode-frame {
            width: 100%;
            background: #f8f8f6;
            border: 1px solid rgba(0,0,0,0.08);
            border-radius: 12px;
            overflow: hidden;
            margin-bottom: 1.25rem;
        }

        .barcode-frame img {
            display: block;
            width: 100%;
            height: auto;
            max-width: 100%;
        }

        .no-barcode-wrap {
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 3rem 1rem;
        }

        .no-barcode {
            font-size: 14px;
            color: #aaa;
        }

        .timer-row {
            display: flex;
            align-items: center;
            gap: 10px;
            background: #f8f8f6;
            border-radius: 10px;
            padding: 10px 14px;
            margin-bottom: 1rem;
        }

        .timer-label { font-size: 13px; color: #666; flex: 1; }

        #timer {
            font-size: 22px;
            font-weight: 500;
            color: #1a1a1a;
            font-variant-numeric: tabular-nums;
            letter-spacing: 0.02em;
        }

        #timer.warn { color: #c62828; }

        .progress-bg {
            width: 100%;
            height: 4px;
            background: #f0f0ee;
            border-radius: 99px;
            overflow: hidden;
            margin-bottom: 1.5rem;
        }

        #progress-fill {
            height: 100%;
            background: #1565c0;
            border-radius: 99px;
            width: 100%;
            transition: width 1s linear, background 0.5s;
        }

        #progress-fill.warn { background: #c62828; }

        .btn {
            display: block;
            width: 100%;
            padding: 12px 16px;
            margin-bottom: 10px;
            border-radius: 10px;
            font-size: 14px;
            font-weight: 500;
            font-family: inherit;
            cursor: pointer;
            transition: opacity 0.15s;
            text-align: center;
        }

        .btn:hover { opacity: 0.8; }
        .btn:last-child { margin-bottom: 0; }

        .btn-primary {
            background: #1a1a1a;
            color: #ffffff;
            border: none;
        }

        .btn-danger {
            background: transparent;
            color: #c62828;
            border: 1px solid #f5b8b8;
        }

        .expired-msg {
            text-align: center;
            font-size: 13px;
            font-weight: 500;
            color: #c62828;
            padding: 8px 0;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="eyebrow"><span class="eyebrow-dot"></span> Live barcode</div>
    <h2>Scan this barcode</h2>
    <p class="subtitle">Valid for 5 minutes — scan before it expires</p>

    <div class="barcode-frame">
        <?php if ($barcode_image): ?>
            <img src="<?php echo $barcode_image; ?>" alt="Barcode">
        <?php else: ?>
            <div class="no-barcode-wrap"><span class="no-barcode">No barcode available yet.</span></div>
        <?php endif; ?>
    </div>

    <?php if ($barcode_image): ?>
    <div class="timer-row">
        <span class="timer-label">Time remaining</span>
        <span id="timer">5:00</span>
    </div>
    <div class="progress-bg">
        <div id="progress-fill"></div>
    </div>
    <?php endif; ?>

    <button onclick="window.close()" class="btn btn-primary">Close window</button>
    <button onclick="regenerate()" class="btn btn-danger">Request new barcode</button>
</div>

<script>
(function() {
    const TOTAL = 300;
    let timeLeft = TOTAL;
    const timerEl = document.getElementById('timer');
    const fillEl = document.getElementById('progress-fill');

    if (!timerEl) return;

    function tick() {
        const m = Math.floor(timeLeft / 60);
        const s = timeLeft % 60;
        timerEl.textContent = m + ':' + (s < 10 ? '0' : '') + s;
        fillEl.style.width = ((timeLeft / TOTAL) * 100) + '%';

        if (timeLeft <= 60) {
            timerEl.classList.add('warn');
            fillEl.classList.add('warn');
        }

        if (timeLeft <= 0) {
            timerEl.parentElement.innerHTML = '<span class="expired-msg">Barcode expired</span>';
            fillEl.style.width = '0%';
            return;
        }
        timeLeft--;
        setTimeout(tick, 1000);
    }

    tick();
})();

function regenerate() {
    if (confirm('Request a new barcode from your caseworker?')) {
        if (window.opener) window.opener.location.reload();
        window.close();
    }
}
</script>
</body>
</html>