<?php
require_once 'protege.php';
require_once '../config.php';

$cliente_id = (int)$_SESSION['cliente_id'];
$id = (int)($_GET['id'] ?? 0);

if ($id <= 0) {
    header('Location: painel.php');
    exit;
}

$stmt = $pdo->prepare("
    SELECT *
    FROM licencas
    WHERE id = ? AND cliente_id = ?
    LIMIT 1
");
$stmt->execute([$id, $cliente_id]);
$licenca = $stmt->fetch();

if (!$licenca) {
    header('Location: painel.php');
    exit;
}

$stmt = $pdo->prepare("
    SELECT *
    FROM ativacoes
    WHERE licenca_id = ?
    ORDER BY ultimo_acesso DESC
");
$stmt->execute([$id]);
$computadores = $stmt->fetchAll();

function diasRestantes($data)
{
    if (empty($data)) return 'Vitalícia';

    $hoje = new DateTime(date('Y-m-d'));
    $fim = new DateTime($data);

    if ($fim < $hoje) return 'Vencida';

    return $hoje->diff($fim)->days . ' dias';
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>Minha Licença - PromoCreative LED Mapper</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
*{box-sizing:border-box}
body{margin:0;background:#060b16;color:#fff;font-family:Arial,Helvetica,sans-serif}
.topbar{padding:22px 36px;background:#08101f;border-bottom:1px solid rgba(255,255,255,.08);display:flex;justify-content:space-between;align-items:center}
.topbar strong{font-size:21px}
.topbar a{color:#fff;text-decoration:none;background:#13223a;padding:11px 15px;border-radius:12px}
.main{max-width:1100px;margin:auto;padding:36px}
.card{background:#0d182b;border:1px solid rgba(255,255,255,.08);border-radius:24px;padding:28px;margin-bottom:24px}
h1{margin-top:0}
.key{font-family:Consolas,monospace;color:#00d4ff;font-size:24px;font-weight:bold;background:#07101f;padding:18px;border-radius:16px;word-break:break-all}
.grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:16px;margin-top:22px}
.box{background:#08111f;border-radius:18px;padding:18px}
.box span{display:block;color:#8fa3c7;font-size:13px;margin-bottom:6px}
.badge{display:inline-block;padding:7px 12px;border-radius:999px;font-size:12px}
.ativa{background:rgba(0,255,160,.12);color:#9fffc8}
.expirada{background:rgba(255,200,0,.12);color:#ffe58a}
.suspensa{background:rgba(255,122,0,.13);color:#ffc38a}
.bloqueada{background:rgba(255,60,60,.12);color:#ffc1c1}
.btn{display:inline-block;margin-top:18px;text-decoration:none;color:#fff;padding:13px 18px;border-radius:14px;background:linear-gradient(135deg,#ff7a00,#00aaff);font-weight:bold;border:0;cursor:pointer}
.btn2{background:#13223a}
table{width:100%;border-collapse:collapse;margin-top:15px}
th,td{padding:14px;border-bottom:1px solid rgba(255,255,255,.08);text-align:left;font-size:14px}
th{color:#8fa3c7;font-weight:normal}
.small{color:#8fa3c7}
@media(max-width:700px){.topbar{padding:18px;flex-direction:column;gap:12px;align-items:flex-start}.main{padding:20px}}
</style>
</head>

<body>

<div class="topbar">
    <strong>PromoCreative LED Mapper</strong>
    <div>
        <a href="painel.php">Voltar</a>
        <a href="logout.php">Sair</a>
    </div>
</div>

<div class="main">
    <div class="card">
        <h1>Minha licença</h1>

        <div class="key" id="licenca"><?= htmlspecialchars($licenca['chave']); ?></div>

        <button class="btn" onclick="copiarLicenca()">Copiar licença</button>
        <a class="btn btn2" href="pagar.php?id=<?= (int)$licenca['id']; ?>">Pagar / Renovar</a>

        <div class="grid">
            <div class="box">
                <span>Plano</span>
                <?= htmlspecialchars(ucfirst($licenca['tipo'])); ?>
            </div>

            <div class="box">
                <span>Status</span>
                <span class="badge <?= htmlspecialchars($licenca['status']); ?>">
                    <?= htmlspecialchars($licenca['status']); ?>
                </span>
            </div>

            <div class="box">
                <span>Vencimento</span>
                <?= $licenca['data_expiracao'] ? date('d/m/Y', strtotime($licenca['data_expiracao'])) : 'Vitalícia'; ?>
            </div>

            <div class="box">
                <span>Dias restantes</span>
                <?= diasRestantes($licenca['data_expiracao']); ?>
            </div>

            <div class="box">
                <span>Computadores usados</span>
                <?= count($computadores); ?> /
                <?= ((int)$licenca['limite_maquinas'] === 0) ? 'Ilimitado' : (int)$licenca['limite_maquinas']; ?>
            </div>

            <div class="box">
                <span>Valor</span>
                R$ <?= number_format((float)$licenca['valor'], 2, ',', '.'); ?>
            </div>
        </div>
    </div>

    <div class="card">
        <h2>Computadores ativados</h2>

        <?php if (empty($computadores)): ?>
            <p class="small">Nenhum computador ativado ainda.</p>
        <?php else: ?>
            <table>
                <thead>
                    <tr>
                        <th>Computador</th>
                        <th>Usuário Windows</th>
                        <th>Ativado em</th>
                        <th>Último acesso</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($computadores as $pc): ?>
                    <tr>
                        <td><?= htmlspecialchars($pc['nome_pc'] ?: 'Não informado'); ?></td>
                        <td><?= htmlspecialchars($pc['usuario_windows'] ?: '-'); ?></td>
                        <td><?= date('d/m/Y H:i', strtotime($pc['ativado_em'])); ?></td>
                        <td><?= date('d/m/Y H:i', strtotime($pc['ultimo_acesso'])); ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>
</div>

<script>
function copiarLicenca(){
    const texto = document.getElementById('licenca').innerText;
    navigator.clipboard.writeText(texto);
    alert('Licença copiada com sucesso!');
}
</script>

</body>
</html>