<?php
$serverUploadUrl = 'https://server.thefull.site/rom';
$dbPath = dirname(__DIR__) . '/api/db.db';
$emuAlert = '';
$emuAlertType = 'success';
try {
    $dbEmu = new PDO('sqlite:' . $dbPath);
    $dbEmu->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $cols = [];
    $stCols = $dbEmu->query('PRAGMA table_info(app_config)');
    while ($r = $stCols->fetch(PDO::FETCH_ASSOC)) $cols[] = (string)($r['name'] ?? '');
    if (!in_array('emuladores_enabled', $cols, true)) $dbEmu->exec('ALTER TABLE app_config ADD COLUMN emuladores_enabled INTEGER NOT NULL DEFAULT 1');
    if (!in_array('emuladores_block_message', $cols, true)) $dbEmu->exec("ALTER TABLE app_config ADD COLUMN emuladores_block_message TEXT NOT NULL DEFAULT 'Área de emuladores bloqueada pelo painel.'");
    $dbEmu->exec('INSERT OR IGNORE INTO app_config (id) VALUES (1)');
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_emuladores_status'])) {
        $enabled = isset($_POST['emuladores_enabled']) ? 1 : 0;
        $msg = trim((string)($_POST['emuladores_block_message'] ?? ''));
        if ($msg === '') $msg = 'Área de emuladores bloqueada pelo painel.';
        $dbEmu->prepare('UPDATE app_config SET emuladores_enabled=?, emuladores_block_message=? WHERE id=1')->execute([$enabled, $msg]);
        $emuAlert = 'Status dos emuladores salvo com sucesso.';
    }
    $emuCfg = $dbEmu->query('SELECT emuladores_enabled, emuladores_block_message FROM app_config WHERE id=1 LIMIT 1')->fetch(PDO::FETCH_ASSOC) ?: [];
} catch (Throwable $e) {
    $emuCfg = ['emuladores_enabled' => 1, 'emuladores_block_message' => 'Área de emuladores bloqueada pelo painel.'];
    $emuAlert = 'Não foi possível carregar a configuração dos emuladores.';
    $emuAlertType = 'danger';
}
function emu_h($v){ return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }
include('./includes/header.php');
?>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">

<style>
.dns-wrap{
    width:100%;
    max-width:none;
    margin:0;
    padding:0 18px;
}
.dns-card{
    border-radius:18px;
    overflow:hidden;
    width:100%;
    margin-bottom:18px;
}
.dns-header{
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:14px;
    padding:18px 22px;
}
.dns-title{
    display:flex;
    align-items:center;
    gap:14px;
}
.dns-title .ic{
    width:44px;
    height:44px;
    min-width:44px;
    border-radius:14px;
    background:rgba(25,135,84,.10);
    border:1px solid rgba(25,135,84,.20);
    display:flex;
    align-items:center;
    justify-content:center;
}
.dns-title .ic i{
    font-size:24px;
    color:#198754;
}
.dns-title h5{
    margin:0;
    font-weight:900;
}
.dns-title span{
    display:block;
    margin-top:3px;
    font-size:13px;
    color:rgba(255,255,255,.55);
}
.badge-soft{
    display:inline-flex;
    align-items:center;
    gap:7px;
    padding:8px 12px;
    border-radius:999px;
    background:rgba(25,135,84,.10);
    border:1px solid rgba(25,135,84,.20);
    color:#198754;
    font-weight:900;
    white-space:nowrap;
}
.input-radius{
    border-radius:14px !important;
    min-height:48px;
}
.save-btn{
    border-radius:14px;
    min-height:46px;
    font-weight:900;
    padding-left:22px;
    padding-right:22px;
}
.info-box{
    background:rgba(0,0,0,.03);
    border:1px solid rgba(0,0,0,.08);
    border-radius:14px;
    padding:12px 14px;
    font-weight:700;
}
@media(max-width:575px){
    .dns-wrap{
        padding:0 8px;
    }
    .dns-header{
        align-items:flex-start;
        padding:16px;
    }
    .dns-title .ic{
        width:40px;
        height:40px;
        min-width:40px;
    }
    .dns-title .ic i{
        font-size:22px;
    }
    .dns-title h5{
        font-size:18px;
    }
    .badge-soft{
        padding:7px 10px;
        font-size:12px;
    }
    .save-btn{
        width:100%;
    }
}
.dns-wrap{
    width:100%;
    max-width:none;
    margin:0;
    padding:0 18px;
}
.dns-card{
    border-radius:18px;
    overflow:hidden;
    width:100%;
    margin-bottom:18px;
}
.dns-header{
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:14px;
    padding:18px 22px;
}
.dns-title{
    display:flex;
    align-items:center;
    gap:14px;
}
.dns-title .ic{
    width:44px;
    height:44px;
    min-width:44px;
    border-radius:14px;
    background:rgba(25,135,84,.10);
    border:1px solid rgba(25,135,84,.20);
    display:flex;
    align-items:center;
    justify-content:center;
}
.dns-title .ic i{
    font-size:24px;
    color:#198754;
}
.dns-title h5{
    margin:0;
    font-weight:900;
}
.dns-title span{
    display:block;
    margin-top:3px;
    font-size:13px;
    color:rgba(255,255,255,.55);
}
.badge-soft{
    display:inline-flex;
    align-items:center;
    gap:7px;
    padding:8px 12px;
    border-radius:999px;
    background:rgba(25,135,84,.10);
    border:1px solid rgba(25,135,84,.20);
    color:#198754;
    font-weight:900;
    white-space:nowrap;
}
.input-radius{
    border-radius:14px !important;
    min-height:48px;
}
.textarea-radius{
    border-radius:14px !important;
}
.save-btn{
    border-radius:14px;
    height:46px;
    font-weight:900;
    padding-left:22px;
    padding-right:22px;
}
.table-card{
    border-radius:18px;
    overflow:hidden;
    width:100%;
}
.table thead th{
    font-weight:900;
    white-space:nowrap;
}
.table td{
    vertical-align:middle;
}
.action-btn{
    border-radius:12px;
    font-weight:800;
    margin:2px 0;
}
.img-link{
    font-weight:900;
    text-decoration:none;
}
.status-on{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    border-radius:999px;
    padding:5px 10px;
    background:rgba(25,135,84,.12);
    color:#198754;
    font-weight:900;
}
.status-off{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    border-radius:999px;
    padding:5px 10px;
    background:rgba(220,53,69,.12);
    color:#dc3545;
    font-weight:900;
}
@media(max-width:575px){
    .dns-wrap{
        padding:0 8px;
    }
    .dns-header{
        align-items:flex-start;
        padding:16px;
    }
    .dns-title .ic{
        width:40px;
        height:40px;
        min-width:40px;
    }
    .dns-title .ic i{
        font-size:22px;
    }
    .dns-title h5{
        font-size:18px;
    }
    .badge-soft{
        padding:7px 10px;
        font-size:12px;
    }
    .save-btn{
        width:100%;
    }
}

/* SCROLLBAR CINZA - PC E MOBILE */
html{
  scrollbar-width: thin;
  scrollbar-color: #8f8f8f #1f1f1f;
}

body{
  scrollbar-width: thin;
  scrollbar-color: #8f8f8f #1f1f1f;
}

*::-webkit-scrollbar{
  width: 10px;
  height: 10px;
}

*::-webkit-scrollbar-track{
  background: #1f1f1f;
  border-radius: 999px;
}

*::-webkit-scrollbar-thumb{
  background: linear-gradient(180deg, #b8b8b8, #7c7c7c);
  border-radius: 999px;
  border: 2px solid #1f1f1f;
}

*::-webkit-scrollbar-thumb:hover{
  background: linear-gradient(180deg, #d0d0d0, #909090);
}

.table-responsive{
  scrollbar-width: thin;
  scrollbar-color: #8f8f8f #1f1f1f;
}

@media (max-width: 768px){
  *::-webkit-scrollbar{
    width: 7px;
    height: 7px;
  }
}
</style>

<div class="dns-wrap">
    <?php if ($emuAlert !== ''): ?><div class="alert alert-<?= emu_h($emuAlertType) ?>"><?= emu_h($emuAlert) ?></div><?php endif; ?>
    <div class="card radius-10 dns-card">
        <div class="card-header bg-transparent dns-header">
            <div class="dns-title">
                <div class="ic"><i class="bi bi-shield-lock-fill"></i></div>
                <div>
                    <h5>Controle do card Emuladores</h5>
                    <span>Ative ou bloqueie o acesso da área gamer no aplicativo</span>
                </div>
            </div>
            <div class="dns-actions">
                <span class="badge-soft"><i class="bi bi-controller"></i> App</span>
            </div>
        </div>
        <div class="card-body">
            <form method="post" class="row g-3">
                <input type="hidden" name="save_emuladores_status" value="1">
                <div class="col-lg-4 col-md-6 col-12">
                    <label class="form-label" style="font-weight:900;">Status no aplicativo</label>
                    <div class="form-check form-switch" style="padding-top:10px;">
                        <input class="form-check-input" type="checkbox" name="emuladores_enabled" id="emuladoresEnabled" <?= ((int)($emuCfg['emuladores_enabled'] ?? 1) === 1) ? 'checked' : '' ?>>
                        <label class="form-check-label" for="emuladoresEnabled" style="font-weight:900;">Ativar card Emuladores</label>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <label class="form-label" style="font-weight:900;">Mensagem quando bloqueado</label>
                    <input class="form-control input-radius" name="emuladores_block_message" value="<?= emu_h($emuCfg['emuladores_block_message'] ?? 'Área de emuladores bloqueada pelo painel.') ?>" placeholder="Área de emuladores bloqueada pelo painel.">
                </div>
                <div class="col-lg-2 col-md-12 col-12 d-flex align-items-end">
                    <button class="btn btn-success save-btn w-100" type="submit"><i class="bi bi-save-fill"></i> Salvar</button>
                </div>
            </form>
        </div>
    </div>
    <div class="card radius-10 dns-card">
        <div class="card-header bg-transparent dns-header">
            <div class="dns-title">
                <div class="ic"><i class="bi bi-controller"></i></div>
                <div>
                    <h5>Subir ROM da comunidade</h5>
                    <span>Envie o jogo e a capa para aparecer no app em Jogos da comunidade</span>
                </div>
            </div>

            <div class="dns-actions">
                <span class="badge-soft"><i class="bi bi-cloud-upload-fill"></i> Envio</span>
            </div>
        </div>

        <div class="card-body">
            <form method="post" action="<?= htmlspecialchars($serverUploadUrl, ENT_QUOTES, 'UTF-8') ?>" enctype="multipart/form-data" class="row g-3">
                <div class="col-lg-4 col-md-6 col-12">
                    <label class="form-label" style="font-weight:900;">Tipo do emulador</label>
                    <select class="form-select input-radius" name="platform" required>
                        <option value="ps1">PS1</option>
                        <option value="super-nintendo">Super Nintendo</option>
                        <option value="nintendo">Nintendo</option>
                        <option value="gba">GBA</option>
                        <option value="master-system">Master System</option>
                    </select>
                </div>

                <div class="col-lg-4 col-md-6 col-12">
                    <label class="form-label" style="font-weight:900;">Nome do jogo</label>
                    <input class="form-control input-radius" name="title" maxlength="80" required placeholder="Ex: Top Gear">
                </div>

                <div class="col-lg-4 col-md-6 col-12">
                    <label class="form-label" style="font-weight:900;">Seu nome</label>
                    <input class="form-control input-radius" name="nickname" maxlength="24" placeholder="Opcional">
                </div>

                <div class="col-lg-6 col-md-6 col-12">
                    <label class="form-label" style="font-weight:900;">ROM do jogo</label>
                    <input class="form-control input-radius" type="file" name="rom_file" required accept=".zip,.7z,.sfc,.smc,.nes,.gba,.gb,.gbc,.sms,.iso,.bin,.cue,.pbp,.chd,.rom">
                </div>

                <div class="col-lg-6 col-md-6 col-12">
                    <label class="form-label" style="font-weight:900;">Foto/Capa</label>
                    <input class="form-control input-radius" type="file" name="cover_file" accept="image/*">
                </div>

                <div class="col-12">
                    <div class="info-box">
                        <strong>Formatos aceitos:</strong>
                        zip, 7z, sfc, smc, nes, gba, gb, gbc, sms, iso, bin, cue, pbp, chd e rom.
                    </div>
                </div>

                <div class="col-12">
                    <button class="btn btn-success save-btn" type="submit">
                        <i class="bi bi-cloud-arrow-up-fill"></i> Enviar ROM
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php include('./includes/footer.php'); ?>