<?php
require_once __DIR__ . '/auth.php';
require_login();
check_csrf();

$eid = (int)($_GET['id'] ?? 0);
if (!$eid) { exit('ID inválido'); }

$tag = $pdo->prepare('SELECT tag_qr FROM equipment WHERE id=?');
$tag->execute([$eid]);
$tag = $tag->fetchColumn();

if (!$tag) {
  $tag = 'EQP-' . $eid . '-' . substr(bin2hex(random_bytes(4)), 0, 8);
  $pdo->prepare('UPDATE equipment SET tag_qr=? WHERE id=?')->execute([$tag, $eid]);
}

header('Location: '.base_url('qr_label.php?id='.$eid));
