<?php require __DIR__ . '/../../includes/navBar.php'?>
<?php require __DIR__ . '/../../includes/modal.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 title-shadow">
                        <i class="fas fa-edit me-2"></i>
                        Datos de la Categoría
                    </h4>

                    <a href="?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-editar-categoria" method="POST" action="index.php?opt=editar_categoria&id=<?= $categorias['id'] ?>" 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"
                            value="<?= htmlspecialchars($categorias['nombre']) ?>"
                            >
                        </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"
                            value="<?= number_format($categorias['porcentaje_ganancia'], 2, '.', '') ?>"
                            >

                            <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"
                            class="form-control"
                            value="<?= number_format($categorias['descuento'], 2, '.', '') ?>"
                            >

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

                        </div>

                        <!-- BOTON SUMBIT -->

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

                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save me-1"></i> Guardar cambios
                            </button>
                        </div>

                    </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>

                <!-- MODAL -->

                <script src="js/globalModal.js"></script>

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

<!-- Validar mediante js -->

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