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

require_login();
require_permission('module.products');

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

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>Crear Nueva Categoría</h1>
            </div>
        </div>

        <?php if (!empty($formErrors)): ?>
            <div class="alert alert-error mb-4">
                <strong>Error.</strong> <?php echo implode('<br>', $formErrors); ?>
            </div>
        <?php endif; ?>

        <form action="/modules/categories/category_store.php" method="POST" class="opportunity-form">
            <input type="hidden" name="_csrf_token" value="<?php echo safe_text(csrf_token()); ?>">
            <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($formOld['name'] ?? ''); ?>" required autofocus>
                        </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>Guardar Categoría</span></button>
                        <button type="button" class="btn btn-outline" onclick="window.close()">Cerrar Pestaña</button>
                    </div>
                </div>
            </section>
        </form>
    </section>
</main>
<?php require_once '../../layouts/footer.php'; ?>
