<?php
/**
 * ICYLAB OS - Editar Categoría
 */
require_once '../../core/config.php';
require_once '../../core/session.php';
require_once '../../core/database.php';
require_once '../../core/format.php';

require_login();
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$pdo = db();

$cat = $pdo->prepare("SELECT * FROM product_categories WHERE id = ? LIMIT 1");
$cat->execute([$id]);
$cat = $cat->fetch();

if (!$cat) die('Categoría no encontrada.');

$page_title = 'Editar Categoría';
$formErrors = $_SESSION['form_errors'] ?? array();
$formOld = $_SESSION['form_old'] ?? array();
unset($_SESSION['form_errors'], $_SESSION['form_old']);

$nameVal = $formOld['name'] ?? $cat['name'];

require_once '../../layouts/header.php';
require_once '../../layouts/sidebar.php';
?>

<main class="app-main">
    <?php require_once '../../layouts/topbar.php'; ?>
    <section class="content">
        <div class="page-header page-header-compact">
            <div class="page-header-content">
                <span class="label">Categorías</span>
                <h1>Editar Categoría</h1>
            </div>
        </div>

        <form action="/modules/categories/category_update.php" method="POST" class="opportunity-form">
            <input type="hidden" name="id" value="<?php echo $id; ?>">
            <section class="card form-card">
                <div class="card-body">
                    <div class="form-grid">
                        <div class="form-field is-required <?php echo isset($formErrors['name']) ? 'has-error' : ''; ?>">
                            <label class="label">Nombre de la Categoría</label>
                            <input class="input" type="text" name="name" value="<?php echo safe_text($nameVal); ?>" required>
                        </div>
                    </div>
                </div>
            </section>
            
            <section class="card form-card form-actions-card mt-6">
                <div class="card-body">
                    <div class="form-actions" style="margin-top: 0;">
                        <button class="btn btn-primary" type="submit"><i data-lucide="save"></i><span>Actualizar Categoría</span></button>
                        <a class="btn btn-outline" href="/modules/categories/index.php">Cancelar</a>
                    </div>
                </div>
            </section>
        </form>
    </section>
</main>
<?php require_once '../../layouts/footer.php'; ?>