<?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 Producto
                        </h4>

                        <a href="index.php?opt=productos" 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-producto" method="POST" action="?opt=crear_producto" novalidate>

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

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

                        <div class="row g-3">

                            <!-- MARCA -->
                    
                            <div class="col-md-6">
                                <label class="form-label">Marca</label>
                                <input type="text" name="marca" class="form-control">
                            </div>

                            <!-- MODELO -->

                            <div class="col-md-6">
                                <label class="form-label">Modelo</label>
                                <input type="text" name="modelo" class="form-control">
                            </div>

                            <!-- CATEGORÍA -->

                            <div class="col-md-6">
                                <label class="form-label">Categoría</label>
                                <select name="categoria" class="form-select">
                                    <option value="">Seleccione una categoría</option>

                                    <?php foreach ($categorias as $c): ?>
                                        <option value="<?= $c['id'] ?>">
                                            <?= htmlspecialchars($c['nombre']) ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>

                            <!-- TIPO -->

                            <div class="col-md-6">
                                <label class="form-label">Tipo</label>
                                <select name="tipo" class="form-select">
                                    <option value="">Seleccione un tipo</option>

                                    <?php foreach ($tipos as $t): ?>
                                        <option value="<?= $t['id'] ?>">
                                            <?= htmlspecialchars($t['nombre']) ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </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>

                            <!-- PRECIO -->

                            <div class="col-md-6">
                                <label class="form-label">Precio</label>
                                <input type="text" placeholder="No ingresar" class="form-control" readonly>

                                <small class="text-muted">
                                    Obtenido en base a ganancia de categoria
                                </small>

                            </div>

                            <!-- STOCK -->

                            <div class="col-md-6">
                                <label class="form-label">Stock</label>
                                <input type="text" placeholder="No ingresar" class="form-control" readonly>

                                <small class="text-muted">
                                    Se debe ingresar en Pedidos
                                </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 Producto
                            </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/productoValidator.js"></script>