<?php
require_once __DIR__.'/../../config/app.php';
require_once __DIR__.'/../../app/Models/Vehicle.php';

$user     = requireAuth(['owner']);
$vehicles = Vehicle::byOwner($user['id']);
$selVid   = (int)($_GET['vehicle'] ?? ($vehicles[0]['id'] ?? 0));
$selV     = $selVid ? Vehicle::find($selVid) : null;

$pageTitle  = t('generate_qr');
$activePage = BASE_URL.'/owner/qr.php';
require __DIR__.'/../../app/Views/shared/layout.php';

// Build verify URL
$verifyUrl = '';
if($selV) {
    $proto = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
    $host  = $_SERVER['HTTP_HOST'];
    $verifyUrl = $proto.'://'.$host.BASE_URL.'/verify.php?plate='.urlencode($selV['plate_number']);
}
?>

<!-- Vehicle tabs -->
<div style="display:flex;gap:8px;flex-wrap:wrap;margin-bottom:20px">
  <?php foreach($vehicles as $v): ?>
  <a href="?vehicle=<?= $v['id'] ?>"
     class="btn btn-sm <?= $selVid===$v['id']?'btn-primary':'btn-secondary' ?>">
    🚗 <?= e($v['plate_number']) ?>
  </a>
  <?php endforeach; ?>
</div>

<?php if(!$selV): ?>
<div class="card"><div class="empty-state">
  <div class="empty-state-icon">📱</div>
  <h3><?= t('no_vehicles') ?></h3>
  <a href="<?= BASE_URL ?>/owner/vehicles.php" class="btn btn-primary" style="margin-top:14px">+ <?= t('add_vehicle') ?></a>
</div></div>
<?php else: ?>

<div style="display:grid;grid-template-columns:auto 1fr;gap:24px;align-items:start">
  <!-- QR Card -->
  <div class="card" style="padding:28px;text-align:center;width:280px">
    <div id="qrcode" style="display:flex;justify-content:center;margin-bottom:16px"></div>
    <div class="qr-vehicle-info">
      <div style="font-size:13px;color:var(--text-muted)"><?= t('scan_to_verify') ?></div>
      <div class="qr-plate"><?= e($selV['plate_number']) ?></div>
    </div>
    <div style="margin-top:16px;display:flex;gap:10px;justify-content:center">
      <button class="btn btn-primary btn-sm" onclick="downloadQR()">⬇ <?= t('download_qr') ?></button>
      <button class="btn btn-secondary btn-sm" onclick="printQR()">🖨 <?= t('print_qr') ?></button>
    </div>
  </div>

  <!-- Info -->
  <div class="card">
    <div class="card-header"><span class="card-title"><?= t('vehicle_info') ?></span></div>
    <div class="card-body">
      <div class="verify-row"><span class="verify-key"><?= t('plate_number') ?></span><span class="verify-val"><span class="vehicle-plate-badge"><?= e($selV['plate_number']) ?></span></span></div>
      <div class="verify-row"><span class="verify-key"><?= t('brand') ?></span><span class="verify-val"><?= e($selV['brand']) ?></span></div>
      <div class="verify-row"><span class="verify-key"><?= t('model') ?></span><span class="verify-val"><?= e($selV['model']) ?></span></div>
      <div class="verify-row"><span class="verify-key"><?= t('color') ?></span><span class="verify-val"><?= e($selV['color']??'—') ?></span></div>
      <div class="verify-row"><span class="verify-key"><?= t('year') ?></span><span class="verify-val"><?= e($selV['year']??'—') ?></span></div>
      <hr>
      <div style="margin-top:12px">
        <div style="font-size:12px;color:var(--text-muted);margin-bottom:6px"><?= t('qr_code') ?> URL:</div>
        <code style="font-size:12.5px;background:var(--bg-primary);padding:8px 12px;border-radius:var(--radius-sm);display:block;word-break:break-all">
          <?= e($verifyUrl) ?>
        </code>
        <a href="<?= e($verifyUrl) ?>" target="_blank" class="btn btn-sm btn-secondary" style="margin-top:8px">
          🔗 <?= t('view') ?> →
        </a>
      </div>
    </div>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<script>
var qrUrl = <?= json_encode($verifyUrl) ?>;
var plate  = <?= json_encode($selV['plate_number']) ?>;
new QRCode(document.getElementById('qrcode'), {
    text: qrUrl, width:200, height:200,
    colorDark:'#000000', colorLight:'#ffffff',
    correctLevel: QRCode.CorrectLevel.H
});

function downloadQR() {
    var canvas = document.querySelector('#qrcode canvas');
    var img    = document.querySelector('#qrcode img');
    var link   = document.createElement('a');
    link.download = 'qr-' + plate + '.png';
    link.href = canvas ? canvas.toDataURL() : img.src;
    link.click();
}

function printQR() {
    var qr = document.getElementById('qrcode').innerHTML;
    var w  = window.open('','_blank');
    w.document.write('<html><body style="text-align:center;padding:40px;font-family:sans-serif"><h2><?= addslashes(t('vehicle_verification')) ?></h2><div style="margin:20px auto;display:inline-block">'+qr+'</div><div style="font-size:22px;font-weight:bold;letter-spacing:.1em;margin:10px">'+plate+'</div><div style="color:#666"><?= addslashes(t('scan_to_verify')) ?></div></body></html>');
    w.document.close(); w.print();
}
</script>
<?php endif; ?>

<?php require __DIR__.'/../../app/Views/shared/layout_end.php'; ?>
