<?php
require_once '../../core/config.php';
require_once '../../core/session.php';
require_once '../../core/database.php';

require_login();
if ($_SERVER['REQUEST_METHOD'] !== 'POST') die('Método no permitido.');

$pdo = db();
$id = (int)($_POST['id'] ?? 0);
$name = trim($_POST['name'] ?? '');

if ($id <= 0 || $name === '') {
    $_SESSION['form_errors'] = ['name' => 'El nombre es obligatorio.'];
    $_SESSION['form_old'] = $_POST;
    header("Location: /modules/categories/category_edit.php?id=$id");
    exit;
}

$stmt = $pdo->prepare("UPDATE product_categories SET name = ? WHERE id = ?");
$stmt->execute([$name, $id]);

header('Location: /modules/categories/index.php');
exit;