<?php require __DIR__ . '/../../includes/navBar.php'; ?>

<div class="container my-5">

    <div class="row justify-content-center">

        <div class="col-xl-8 col-lg-9 col-md-10">

            <div class="card card-rounded card-shadow border-0">

                <!-- HEADER -->

                <div class="card-header header-dark px-4 py-3">

                    <div class="d-flex justify-content-between align-items-center flex-wrap gap-3">
                        <h4 class="mb-0 tittle-shadow">
                            <i class="fas fa-plus-square me-2"></i>
                            Crear Categoría
                        </h4>

                        <a href="index.php?opt=categorias" class="btn btn-soft-secondary text-white">
                            <i class="fas fa-arrow-left me-1"></i> Volver
                        </a>

                    </div>
                </div>

                <!-- BODY -->

                <div class="card-body p-4 bg-white">

                    <form id="form-crear-categoria" method="POST" action="?opt=crear_categoria" novalidate>

                        <!-- CAMPO OCULTO (para verificación de token) -->

                        <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">

                        <div class="row g-3">

                            <!-- NOMBRE -->
                    
                            <div class="col-md-6">
                                <label class="form-label">Nombre</label>
                                <input type="text" name="nombre" class="form-control">
                            </div>

                            <!-- PORCENTAJE DE GANANCIA -->

                            <div class="col-md-6">
                                <label class="form-label">Ganancia</label>
                                <input type="number" step="0.01" min="0" name="porcentaje_ganancia" placeholder="0.00" class="form-control">

                                <small class="text-muted">
                                    Admite valor sin asignar
                                </small>

                            </div>

                            <!-- DESCUENTO -->

                            <div class="col-md-6">
                                <label class="form-label">Descuento</label>
                                <input type="number" step="0.01" min="0" name="descuento" placeholder="0.00" class="form-control">

                                <small class="text-muted">
                                    Admite valor sin asignar
                                </small>

                            </div>

                        </div>

                        <!-- BOTON SUBMIT -->

                        <div class="d-flex justify-content-between align-items-center mt-4 pt-4 border-top">

                            <button type="submit" class="btn btn-success">
                                <i class="fas fa-plus-square me-1"></i> Crear Categoría
                            </button>

                        </div>

                    </form>

                    <!-- Errores provinientes de productoController -->

                    <!-- ERROR -->

                    <script>
                        const serverErrors = <?php 
                            if (!empty($_SESSION['flash_error'])) {
                                $errorsArray = explode('<br>', $_SESSION['flash_error']);
                                unset($_SESSION['flash_error']);
                                echo json_encode($errorsArray);
                            } else {
                                echo '[]';
                            }
                        ?>;
                    </script>

                    <!-- SUCCES -->

                    <script>
                        const serverSuccess = <?php
                            if (!empty($_SESSION['flash_success'])) {
                                $successArray = explode('<br>', $_SESSION['flash_success']);
                                unset($_SESSION['flash_success']);
                                echo json_encode($successArray);
                            } else {
                                echo '[]';
                            }
                        ?>;
                    </script>

                </div>
            </div>
        </div>
    </div>
</div>

<!-- Validar mediante js -->

<script src="js/productos/categoriValidator.js"></script>