<?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';
require_once __DIR__.'/../includes/admin_auth_view.php';
$token=(string)($_GET['token']??$_POST['token']??'');$error='';$success=false;
if($_SERVER['REQUEST_METHOD']==='POST'){
 try{if(!validarCsrfToken($_POST[CSRF_TOKEN_NAME]??''))throw new RuntimeException('Sessão expirada.');$pass=(string)($_POST['senha']??'');if(!hash_equals($pass,(string)($_POST['confirmar_senha']??'')))throw new RuntimeException('As senhas não coincidem.');afAdminResetPassword($pdo,$token,$pass);$success=true;}
 catch(Throwable $e){$error=$e->getMessage();}
}
afAdminAuthStart('Definir nova senha','Crie uma nova senha para a conta administrativa.');
if($error!=='')echo '<div class="admin-auth-alert">'.esc($error).'</div>';
if($success):?><div class="admin-auth-alert admin-auth-success">Senha atualizada. Todas as sessões antigas foram encerradas.</div><div class="admin-auth-links"><a href="<?=SITE_URL?>/admin/login.php">Entrar no painel →</a></div><?php else: ?>
<form method="post" class="admin-auth-form"><?=camposCsrf()?><input type="hidden" name="token" value="<?=esc($token)?>"><label>Nova senha<input type="password" name="senha" required autocomplete="new-password"><span class="admin-auth-password-help">Mínimo de 10 caracteres, com maiúscula, minúscula e número.</span></label><label>Confirmar senha<input type="password" name="confirmar_senha" required autocomplete="new-password"></label><button class="admin-auth-button">SALVAR NOVA SENHA</button></form>
<?php endif; afAdminAuthEnd();
