<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_admin();

$pageTitle = 'Logotipo do App';
$pageSubtitle = 'Troque a logotipo principal do aplicativo.';
$pageIcon = 'fa-image';

if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST') {
    verify_csrf();

    try {
        $manualUrl = trim((string)($_POST['app_logo_url'] ?? ''));

        if ($manualUrl !== '') {
            if (!filter_var($manualUrl, FILTER_VALIDATE_URL)) {
                throw new RuntimeException('Informe uma URL valida para a logotipo.');
            }

            save_config_value('branding', 'app_logo_url', $manualUrl);
        }

        handle_app_logo_upload('app_logo');

        flash('success', 'Logotipo atualizada com sucesso.');
    } catch (Throwable $e) {
        flash('error', $e->getMessage());
    }

    redirect('branding.php');
}

$branding = get_config('branding');

$logoUrl = trim((string)($branding['app_logo_url'] ?? ''));

if ($logoUrl === '') {
    $logoUrl = panel_absolute_url('img/app_brand/home_logo.png');
}

require __DIR__ . '/includes/header.php';
?>

<div class="row">
    <div class="col-lg-7 mb-4">
        <div class="card bg-primary text-white h-100">
            <div class="card-header card-header-warning">
                <div class="max-card-title">
                    <div class="max-card-title-icon">
                        <i class="fa-solid fa-image"></i>
                    </div>

                    <div>
                        <h2>Logotipo do aplicativo</h2>
                        <p>Envie uma nova imagem ou informe uma URL pronta.</p>
                    </div>
                </div>
            </div>

            <div class="card-body">
                <form method="post" enctype="multipart/form-data">
                    <input type="hidden" name="csrf" value="<?=e(csrf_token())?>">

                    <div class="form-group">
                        <label for="app_logo">Enviar nova imagem</label>
                        <input class="form-control" id="app_logo" name="app_logo" type="file" accept="image/png,image/jpeg,image/webp">
                        <small class="form-text text-muted">Use PNG, JPG ou WebP com ate 2 MB.</small>
                    </div>

                    <div class="form-group">
                        <label for="app_logo_url">Ou usar URL pronta</label>
                        <input class="form-control" id="app_logo_url" name="app_logo_url" type="url" value="<?=e($branding['app_logo_url'] ?? '')?>" placeholder="https://.../logo.png">
                    </div>

                    <button class="btn btn-info" type="submit">
                        <i class="fa-solid fa-floppy-disk"></i>
                        Salvar logotipo
                    </button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-5 mb-4">
        <div class="card bg-primary text-white h-100">
            <div class="card-header card-header-warning">
                <div class="max-card-title">
                    <div class="max-card-title-icon">
                        <i class="fa-solid fa-eye"></i>
                    </div>

                    <div>
                        <h2>Previa</h2>
                        <p>Imagem atual do aplicativo.</p>
                    </div>
                </div>
            </div>

            <div class="card-body text-center">
                <img 
                    src="<?=e($logoUrl)?>" 
                    alt="Logo do aplicativo" 
                    style="width:160px;height:160px;object-fit:contain;border-radius:24px;background:rgba(0,0,0,.22);padding:16px;border:1px solid rgba(255,255,255,.14)"
                >
            </div>
        </div>
    </div>
</div>

<?php require __DIR__ . '/includes/footer.php'; ?>