<?php
// admin/section_action.php — règle l'espacement (padding) d'une section (protégé).
require_once dirname(__DIR__) . '/inc/schema.php';
require_once dirname(__DIR__) . '/inc/auth.php';
auth_require();
header('Content-Type: application/json; charset=utf-8');
init_schema();
db()->exec("CREATE TABLE IF NOT EXISTS sections_reglages (cle TEXT PRIMARY KEY, pad_haut INTEGER DEFAULT -1, pad_bas INTEGER DEFAULT -1, maj_le TEXT DEFAULT (datetime('now')))");

$section = trim($_POST['section'] ?? '');
if ($section === '') { echo json_encode(['ok'=>false,'error'=>'Section manquante']); exit; }

// -1 = revenir au défaut (non défini)
$ph = (int)($_POST['pad_haut'] ?? -1);
$pb = (int)($_POST['pad_bas'] ?? -1);
$ph = $ph < 0 ? -1 : max(0, min(300, $ph));
$pb = $pb < 0 ? -1 : max(0, min(300, $pb));

db()->prepare("INSERT INTO sections_reglages (cle,pad_haut,pad_bas,maj_le)
    VALUES (?,?,?,datetime('now'))
    ON CONFLICT(cle) DO UPDATE SET pad_haut=excluded.pad_haut, pad_bas=excluded.pad_bas, maj_le=datetime('now')")
   ->execute([$section,$ph,$pb]);

echo json_encode(['ok'=>true,'section'=>$section,'pad_haut'=>$ph,'pad_bas'=>$pb]);
