<?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 l.*, c.nome, c.email, c.whatsapp
    FROM licencas l
    INNER JOIN clientes c ON c.id = l.cliente_id
    WHERE l.id = ? AND l.cliente_id = ?
    LIMIT 1
");
$stmt->execute([$id, $cliente_id]);
$licenca = $stmt->fetch();

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

$referencia = 'PCLM-' . $licenca['id'] . '-' . time();

$stmt = $pdo->prepare("
    INSERT INTO pagamentos 
    (cliente_id, licenca_id, valor, status, metodo, referencia)
    VALUES (?, ?, ?, 'pendente', 'pix', ?)
");
$stmt->execute([
    $cliente_id,
    $licenca['id'],
    $licenca['valor'],
    $referencia
]);

$mensagemWhats = urlencode(
    "Olá, quero renovar minha licença PromoCreative LED Mapper.\n\n" .
    "Cliente: {$licenca['nome']}\n" .
    "E-mail: {$licenca['email']}\n" .
    "Licença: {$licenca['chave']}\n" .
    "Plano: {$licenca['tipo']}\n" .
    "Valor: R$ " . number_format((float)$licenca['valor'], 2, ',', '.') . "\n" .
    "Referência: {$referencia}"
);
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>Pagamento - PromoCreative LED Mapper</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
*{box-sizing:border-box}
body{
    margin:0;
    background:
        radial-gradient(circle at top left,rgba(0,170,255,.18),transparent 30%),
        radial-gradient(circle at bottom right,rgba(255,122,0,.14),transparent 30%),
        #060b16;
    color:#fff;
    font-family:Arial,Helvetica,sans-serif;
}
.topbar{
    padding:22px 36px;
    background:rgba(8,15,29,.96);
    border-bottom:1px solid rgba(255,255,255,.08);
    display:flex;
    justify-content:space-between;
    align-items:center;
}
.topbar a{
    color:#fff;
    text-decoration:none;
    background:#13223a;
    padding:11px 15px;
    border-radius:12px;
}
.main{
    max-width:1000px;
    margin:auto;
    padding:36px;
}
.card{
    background:rgba(13,24,43,.94);
    border:1px solid rgba(255,255,255,.08);
    border-radius:28px;
    padding:30px;
    box-shadow:0 24px 70px rgba(0,0,0,.35);
}
.grid{
    display:grid;
    grid-template-columns:1fr 1fr;
    gap:24px;
}
.key{
    font-family:Consolas,monospace;
    color:#00d4ff;
    background:#07101f;
    padding:15px;
    border-radius:15px;
    word-break:break-all;
    border:1px dashed rgba(0,170,255,.35);
}
.price{
    font-size:42px;
    font-weight:900;
    margin:15px 0;
}
.pixbox{
    background:#07101f;
    border:1px dashed rgba(0,170,255,.35);
    border-radius:22px;
    padding:24px;
    text-align:center;
}
.qrfake{
    width:210px;
    height:210px;
    margin:18px auto;
    border-radius:18px;
    background:
        linear-gradient(90deg,#fff 10px,transparent 10px),
        linear-gradient(#fff 10px,transparent 10px),
        #0d182b;
    background-size:28px 28px;
    opacity:.9;
}
.notice{
    color:#aabbe0;
    line-height:1.6;
}
.btn{
    display:inline-block;
    margin-top:18px;
    text-decoration:none;
    color:#fff;
    padding:14px 18px;
    border-radius:14px;
    background:linear-gradient(135deg,#ff7a00,#00aaff);
    font-weight:bold;
}
.btn.secondary{
    background:#13223a;
}
.info-line{
    padding:13px 0;
    border-bottom:1px solid rgba(255,255,255,.08);
}
.info-line span{
    display:block;
    color:#7f95bd;
    font-size:13px;
    margin-bottom:5px;
}
@media(max-width:850px){
    .grid{grid-template-columns:1fr}
    .main{padding:20px}
    .topbar{padding:18px;flex-direction:column;gap:12px;align-items:flex-start}
}
</style>
</head>

<body>

<div class="topbar">
    <strong>PromoCreative LED Mapper</strong>
    <div>
        <a href="minha_licenca.php?id=<?= (int)$licenca['id']; ?>">Voltar</a>
        <a href="logout.php">Sair</a>
    </div>
</div>

<div class="main">
    <div class="card">
        <h1>Pagamento / Renovação</h1>
        <p class="notice">
            Faça o pagamento e envie o comprovante pelo WhatsApp para liberação manual.
            Depois vamos integrar o PIX automático via Mercado Pago.
        </p>

        <div class="grid">
            <div>
                <h2>Dados da licença</h2>

                <div class="info-line">
                    <span>Licença</span>
                    <div class="key"><?= htmlspecialchars($licenca['chave']); ?></div>
                </div>

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

                <div class="info-line">
                    <span>Referência do pagamento</span>
                    <?= htmlspecialchars($referencia); ?>
                </div>

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

                <a class="btn" href="https://wa.me/5581999591897?text=<?= $mensagemWhats; ?>" target="_blank">
                    Enviar comprovante no WhatsApp
                </a>
            </div>

            <div class="pixbox">
                <h2>PIX</h2>
                <p class="notice">Use a chave PIX abaixo:</p>

                <div class="qrfake"></div>

                <div class="key">SUA-CHAVE-PIX-AQUI</div>

                <p class="notice">
                    Após pagar, envie o comprovante com a referência:
                    <br><b><?= htmlspecialchars($referencia); ?></b>
                </p>

                <a class="btn secondary" href="painel.php">Voltar ao painel</a>
            </div>
        </div>
    </div>
</div>

</body>
</html>