<?php
require __DIR__ . '/../init.php';
$pdo = pdo();
$marquee = $pdo->prepare("SELECT s_value FROM settings WHERE s_key='marquee_text'"); $marquee->execute(); $marquee = $marquee->fetchColumn() ?: '';
$videos = $pdo->query("SELECT filename FROM videos WHERE active=1 ORDER BY uploaded_at DESC")->fetchAll();
?>
<!doctype html><html><head><meta charset="utf-8"><title>Display</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>body{background:#000;color:#fff}.marquee{position:fixed;bottom:0;width:100%;background:#111;padding:6px}.ticker{font-size:80px;text-align:center;padding:30px}</style>
</head><body>
<div class="container-fluid">
  <div class="ticker" id="ticker">-</div>
  <div class="d-flex justify-content-center" id="videoArea">
    <?php if(count($videos)): ?>
      <video id="vplayer" width="70%" controls autoplay muted loop>
        <source src="<?= htmlspecialchars($config->upload_url . '/' . $videos[0]['filename']) ?>">
      </video>
    <?php else: ?>
      <div class="text-muted">Suba videos en Backoffice</div>
    <?php endif; ?>
  </div>
</div>
<div class="marquee"><div id="marq"><?= htmlspecialchars($marquee) ?></div></div>

<script>
const api = '<?= site('api.php') ?>';
async function update(){
  try {
    const res = await fetch(api+'?action=get_display');
    const j = await res.json();
    if (j.ok){
      const sale = j.sale;
      document.getElementById('ticker').innerText = sale ? (sale.sale_code + '  TOTAL $' + parseFloat(sale.total).toFixed(2)) : '-';
      if (j.marquee) document.getElementById('marq').innerText = j.marquee;
      if (j.videos && j.videos.length > 0) {
        const player = document.getElementById('vplayer');
        if (player) player.src = '<?= $config->upload_url ?>/' + j.videos[0].filename;
      }
    }
  } catch(e){ console.error(e); }
}
update();
setInterval(update, 5000);
</script>
</body></html>