<?php
require_once __DIR__.'/../config/config.php';require_once __DIR__.'/../config/database.php';require_once __DIR__.'/../includes/functions.php';require_once __DIR__.'/../includes/admin_functions.php';afRequireSuperAdmin();
if($_SERVER['REQUEST_METHOD']==='POST'){
 try{if(!validarCsrfToken($_POST[CSRF_TOKEN_NAME]??''))throw new RuntimeException('Sessão expirada.');$id=(int)($_POST['config_id']??0);$st=$pdo->prepare('SELECT * FROM af_configuracoes WHERE id=? LIMIT 1');$st->execute([$id]);$cfg=$st->fetch();if(!$cfg)throw new RuntimeException('Configuração não encontrada.');$value=afAdminConfigValue((string)($_POST['valor']??''),(string)$cfg['tipo']);$pdo->prepare('UPDATE af_configuracoes SET valor=?,atualizado_em=NOW() WHERE id=?')->execute([$value,$id]);afAdminLog($pdo,'configuracao_atualizada','configuracao',$id,['chave'=>$cfg['chave'],'antes'=>$cfg['valor'],'depois'=>$value]);setFlash('success','Configuração atualizada.');}
 catch(Throwable $e){setFlash('error',$e->getMessage());}header('Location: '.SITE_URL.'/pages/admin-configuracoes.php');exit;
}
$q=sanitize($_GET['q']??'');$params=[];$where='1=1';if($q!==''){$where='(chave LIKE ? OR descricao LIKE ?)';$params=['%'.$q.'%','%'.$q.'%'];}$st=$pdo->prepare("SELECT * FROM af_configuracoes WHERE {$where} ORDER BY chave");$st->execute($params);$items=$st->fetchAll()?:[];
$pageTitle='Admin • Configurações';$showNav=false;$bodyClass='admin-body';$adminPage='configuracoes';require __DIR__.'/../includes/header.php';?>
<div class="admin-shell"><?php require __DIR__.'/../includes/admin_nav.php';?><main class="admin-main"><header class="admin-topbar"><div><small>PARÂMETROS DO SISTEMA</small><h1>Configurações</h1></div><span class="admin-count"><?=count($items)?> chaves</span></header><?=renderFlash()?>
<div class="admin-warning">Alterações entram em vigor para leituras feitas pelo banco. Constantes definidas em <code>config.local.php</code> continuam tendo prioridade quando o código utiliza constantes.</div>
<form class="admin-filters" method="get"><input name="q" value="<?=esc($q)?>" placeholder="Pesquisar chave ou descrição"><button>Pesquisar</button></form>
<div class="admin-config-list"><?php foreach($items as $c):?><form method="post" class="admin-config-card"><?=camposCsrf()?><input type="hidden" name="config_id" value="<?=(int)$c['id']?>"><header><div><b><?=esc($c['chave'])?></b><small><?=esc($c['descricao']??'Sem descrição')?></small></div><span class="admin-pill"><?=esc($c['tipo'])?> • <?=$c['publica']?'pública':'privada'?></span></header><?php if($c['tipo']==='bool'):?><select name="valor"><option value="1" <?=$c['valor']=='1'?'selected':''?>>Ativado</option><option value="0" <?=$c['valor']=='0'?'selected':''?>>Desativado</option></select><?php elseif($c['tipo']==='json'):?><textarea name="valor" rows="4"><?=esc($c['valor'])?></textarea><?php else:?><input name="valor" value="<?=esc($c['valor'])?>"><?php endif;?><button class="admin-primary">Salvar</button></form><?php endforeach;?></div></main></div><script src="<?=SITE_URL?>/assets/js/admin.js?v=14.1"></script><?php require __DIR__.'/../includes/footer.php';?>
