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

$alert = null;
$current = get_support_settings();

if (($_SERVER['REQUEST_METHOD'] ?? '') === 'POST') {
    $number = $_POST['whatsapp_number'] ?? '';
    $message = $_POST['whatsapp_message'] ?? '';
    $alert = update_support_settings($number, $message);
    $current = get_support_settings();
}

$waUrl = build_support_whatsapp_url($current);
$qrUrl = $waUrl !== '' ? 'https://api.qrserver.com/v1/create-qr-code/?size=240x240&margin=10&data=' . rawurlencode($waUrl) : '';
?>

<style>
    .page-header { margin-bottom: 25px; }
    .page-header h1 {
        font-size: 28px;
        margin-bottom: 8px;
        background: var(--primary-bg);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
    }
    .support-grid {
        display: grid;
        grid-template-columns: minmax(280px, 1fr) 300px;
        gap: 22px;
        align-items: start;
    }
    .support-card {
        background: var(--card-bg, #1a1a1a);
        border: 1px solid var(--border-color, rgba(255,255,255,.1));
        border-radius: 12px;
        padding: 24px;
    }
    .form-group { margin-bottom: 18px; }
    .form-group label { display:block; margin-bottom:8px; color:#fff; font-weight:700; }
    .form-group input, .form-group textarea {
        width: 100%;
        padding: 12px 14px;
        border-radius: 8px;
        border: 1px solid rgba(255,255,255,.15);
        background: rgba(0,0,0,.28);
        color: #fff;
        outline: none;
    }
    .form-group textarea { min-height: 110px; resize: vertical; }
    .btn-save {
        border: 0;
        cursor: pointer;
        padding: 13px 18px;
        border-radius: 8px;
        color: #fff;
        font-weight: 800;
        background: var(--primary-bg);
    }
    .alert { padding: 12px 14px; border-radius: 8px; margin-bottom: 18px; font-weight: 700; }
    .alert.ok { background: rgba(46,204,113,.18); color:#73ff9c; border:1px solid rgba(46,204,113,.35); }
    .alert.err { background: rgba(255,76,76,.16); color:#ff8f8f; border:1px solid rgba(255,76,76,.35); }
    .qr-preview { text-align: center; }
    .qr-box {
        background: #fff;
        border-radius: 14px;
        padding: 14px;
        display: inline-block;
        margin: 12px 0;
    }
    .qr-box img { width: 240px; height: 240px; display:block; }
    .muted { color:#aaa; font-size: 13px; line-height:1.45; }
    .url-box { word-break: break-word; font-size: 12px; color:#bbb; margin-top: 12px; }
    @media(max-width: 850px){ .support-grid { grid-template-columns: 1fr; } }
</style>

<div class="page-header">
    <h1>📲 QR Code de Suporte</h1>
    <p>Configure o WhatsApp que aparecerá no aplicativo quando o cliente clicar em <strong>Suporte</strong>.</p>
</div>

<?php if ($alert): ?>
    <div class="alert <?php echo !empty($alert['ok']) ? 'ok' : 'err'; ?>">
        <?php echo htmlspecialchars($alert['message'] ?? ''); ?>
    </div>
<?php endif; ?>

<div class="support-grid">
    <div class="support-card">
        <form method="post">
            <div class="form-group">
                <label>Número do WhatsApp</label>
                <input type="text" name="whatsapp_number" placeholder="Ex: 5571999999999" value="<?php echo htmlspecialchars($current['whatsapp_number'] ?? ''); ?>" required>
                <div class="muted">Pode colocar com DDD. Se informar somente DDD+número do Brasil, o painel adiciona 55 automaticamente.</div>
            </div>
            <div class="form-group">
                <label>Mensagem automática</label>
                <textarea name="whatsapp_message" placeholder="Mensagem que chegará no WhatsApp"><?php echo htmlspecialchars($current['whatsapp_message'] ?? ''); ?></textarea>
            </div>
            <button class="btn-save" type="submit">Salvar QR de Suporte</button>
        </form>
    </div>

    <div class="support-card qr-preview">
        <h2>Prévia</h2>
        <?php if ($qrUrl): ?>
            <div class="qr-box"><img src="<?php echo htmlspecialchars($qrUrl); ?>" alt="QR Code WhatsApp"></div>
            <p class="muted">O app carrega este QR na caixa de suporte. O cliente escaneia e abre direto no seu WhatsApp.</p>
            <div class="url-box"><?php echo htmlspecialchars($waUrl); ?></div>
        <?php else: ?>
            <p class="muted">Cadastre um número para gerar o QR Code.</p>
        <?php endif; ?>
    </div>
</div>

</main>
</body>
</html>
