<?php
require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/../includes/plugins.php';
require_login();
$u = current_user();

$st = db()->prepare("SELECT * FROM licenses WHERE user_id=? AND status='active' ORDER BY created_at DESC");
$st->execute([$u['id']]); $lics = $st->fetchAll();

$st = db()->prepare("SELECT * FROM orders WHERE user_id=? ORDER BY created_at DESC LIMIT 20");
$st->execute([$u['id']]); $orders = $st->fetchAll();

$page_title = 'Mis licencias';
require __DIR__ . '/../includes/header.php';
?>
<div class="player">
  <?php $active='license'; require __DIR__ . '/../includes/panel_side.php'; ?>
  <main class="pcontent">
    <div class="phead"><div class="eyebrow">Cuenta</div><h1>Mis licencias</h1><p>Cada plugin Premium tiene su propia licencia.</p></div>

    <?php if (isset($_GET['activated'])): ?>
      <div class="alert alert-ok" style="margin-bottom:16px">🎉 ¡Pago confirmado y licencia activada! Ya puedes descargar la build Premium.</div>
    <?php endif; ?>

    <?php if ($lics): ?>
      <div class="lgrid">
        <?php foreach ($lics as $l): $p = plugin_get($l['plugin_slug']); ?>
          <div class="lcard">
            <div style="display:flex;align-items:center;gap:9px;margin-bottom:12px">
              <span style="font-size:22px"><?= $p['icon'] ?? '🎮' ?></span>
              <b style="font-size:16px"><?= e($p['name'] ?? $l['plugin_slug']) ?></b>
              <?= plugin_badge($l['plugin_slug']) ?>
            </div>
            <div class="k">Llave de licencia</div>
            <div class="keyrow"><code><?= e($l['license_key']) ?></code></div>
            <div style="font-size:12px;color:var(--muted2);margin-top:10px">Activada el <?= e(date('d M Y', strtotime($l['created_at']))) ?></div>
          </div>
        <?php endforeach; ?>
      </div>
    <?php else: ?>
      <div class="alert alert-info">Aún no tienes licencias Premium. Ve a <a href="<?= e(BASE) ?>/panel/" style="color:var(--gold)">Mis descargas</a> para comprar.</div>
    <?php endif; ?>

    <?php if ($orders): ?>
      <h3 class="sub">Mis pedidos</h3>
      <div class="card" style="padding:6px 4px">
        <?php foreach ($orders as $o): $p = plugin_get($o['plugin_slug']);
          $bmap = ['pending'=>['b-free','Pendiente de verificar'],'verified'=>['b-green','Verificado ✓'],'rejected'=>['b-red','Rechazado']];
          [$cls,$lbl] = $bmap[$o['status']] ?? ['b-free',$o['status']]; ?>
          <div class="brow">
            <span style="font-size:20px"><?= $p['icon'] ?? '🎮' ?></span>
            <div><b style="font-size:14px"><?= e($p['name'] ?? $o['plugin_slug']) ?> Premium</b>
              <div style="font-size:12px;color:var(--muted2)"><?= e(date('d M Y H:i', strtotime($o['created_at']))) ?> · $<?= e(number_format((float)$o['amount'],2)) ?></div></div>
            <span class="sp"><span class="badge <?= $cls ?>"><?= e($lbl) ?></span></span>
          </div>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </main>
</div>
<?php require __DIR__ . '/../includes/footer.php'; ?>
