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

                    <a href="?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-editar-producto" method="POST" action="index.php?opt=editar_producto&id=<?= $producto['id'] ?>" 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 bg-light text-muted"
                            style="cursor: not-allowed;"
                            value="<?= htmlspecialchars($producto['marca']) ?>" readonly>
                        </div>
                        
                        <!-- MODELO -->

                        <div class="col-md-6">
                            <label class="form-label">Modelo</label>
                            <input type="text"
                            name="modelo"
                            class="form-control bg-light text-muted"
                            style="cursor: not-allowed;"
                            value="<?= htmlspecialchars($producto['modelo']) ?>" readonly>
                        </div>
                        
                        <!-- PRECIO -->

                        <div class="col-md-6">
                            <label class="form-label">Precio ($)</label>
                            <input type="number"
                            class="form-control bg-light text-muted"
                            style="cursor: not-allowed;"
                            value="<?= number_format($producto['precio'], 2, '.', '') ?>"
                            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="number"
                            class="form-control bg-light text-muted"
                            style="cursor: not-allowed;"
                            value="<?= htmlspecialchars($producto['stock']) ?>"
                            readonly>

                            <small class="text-muted">
                                Se debe ingresar en Pedidos
                            </small>

                        </div>

                        <!-- CATEGORÍA -->

                        <div class="col-md-6">
                            <label class="form-label">Categoría</label>

                            <select name="categoria" class="form-select bg-light text-muted" style="cursor: not-allowed; opacity: 1;">

                                <?php foreach ($categorias as $c): ?>
                                    <option 
                                        value="<?= $c['id'] ?>"
                                        <?= ($c['id'] == ($producto['id_categoria'] ?? null)) ? 'selected' : '' ?>
                                    >
                                        <?= 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 bg-light text-muted" style="cursor: not-allowed; opacity: 1;">

                                <?php foreach ($tipos as $t): ?>
                                    <option 
                                        value="<?= $t['id'] ?>"
                                        <?= ($t['id'] == ($producto['id_tipo'] ?? null)) ? 'selected' : '' ?>
                                    >
                                        <?= htmlspecialchars($t['nombre']) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <!-- ID DE PEDIDO -->

                        <div class="col-md-6">
                            <label class="form-label">ID Pedido</label>
                            <input type="text"
                            class="form-control bg-light text-muted"
                            style="cursor: not-allowed;"
                            value="<?= htmlspecialchars($producto['id_pedido']) ?>"
                            readonly>

                            <small class="text-muted">
                                Solo con fines informativos
                            </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 campo-readonly"
                            value="<?= number_format($producto['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/productoValidator.js"></script>