<?php
require_once __DIR__ . '/../includes/functions.php';

$settings = get_support_settings();
$branding = get_branding();
$hasReseller = reseller_exists();
$renewalUrl = get_panel_public_url('renovar.php');
$waUrl = build_support_whatsapp_url($settings);
$message = $hasReseller ? 'Renove pelo painel' : ($settings['whatsapp_message'] ?? 'Ola, vim pelo aplicativo. Preciso de suporte ou renovacao.');
$qrTarget = $hasReseller ? $renewalUrl : $waUrl;
$qrUrl = $qrTarget !== '' ? 'https://api.qrserver.com/v1/create-qr-code/?size=220x220&margin=0&data=' . rawurlencode($qrTarget) : '';

header('Content-Type: text/html; charset=utf-8');
header('Access-Control-Allow-Origin: *');
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style>
    *{
        box-sizing:border-box;
        -webkit-tap-highlight-color:transparent;
    }

    html,body{
        margin:0;
        padding:0;
        width:100%;
        height:100%;
        background:transparent !important;
        overflow:hidden;
        font-family:Arial,Helvetica,sans-serif;
    }

    body{
        display:flex;
        align-items:center;
        justify-content:center;
    }

    .wrap{
        width:100%;
        height:100%;
        display:flex;
        align-items:center;
        justify-content:center;
        background:transparent !important;
        padding:0;
        margin:0;
    }

    .box{
        display:flex;
        align-items:center;
        justify-content:center;
        width:100%;
        height:100%;
        text-align:center;
        background:transparent !important;
    }

    .qr-link{
        display:flex;
        align-items:center;
        justify-content:center;
        width:100%;
        height:100%;
        text-decoration:none;
        background:transparent !important;
    }

    .qr{
        width:150px;
        height:150px;
        max-width:92vw;
        max-height:92vh;
        object-fit:contain;
        display:block;
        margin:0 auto;
        background:transparent !important;
        border:0;
        outline:0;
        box-shadow:none;
    }

    .warn{
        font-size:11px;
        color:#fff;
        padding:8px;
        line-height:1.25;
        text-align:center;
    }
</style>
</head>
<body>
    <div class="wrap">
        <div class="box">
            <?php if ($qrUrl && ($waUrl || $hasReseller)): ?>
                <?php if ($hasReseller): ?>
                    <a class="qr-link" href="<?php echo htmlspecialchars($renewalUrl, ENT_QUOTES, 'UTF-8'); ?>">
                        <img class="qr" src="<?php echo htmlspecialchars($qrUrl, ENT_QUOTES, 'UTF-8'); ?>" alt="QR Code suporte">
                    </a>
                <?php else: ?>
                    <a class="qr-link" href="<?php echo htmlspecialchars($waUrl, ENT_QUOTES, 'UTF-8'); ?>">
                        <img class="qr" src="<?php echo htmlspecialchars($qrUrl, ENT_QUOTES, 'UTF-8'); ?>" alt="QR Code WhatsApp">
                    </a>
                <?php endif; ?>
            <?php else: ?>
                <div class="warn">Configure o WhatsApp no painel em QR Suporte.</div>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>
