Current File : /var/www/pharmacius/insertar.php
<?php
require_once("menu.php");
require_once("model/productos_model.php");

$productoModel = new Productos_model();
$categoriasDisponibles = $productoModel->get_categorias();

$mensaje = '';
$tipoMensaje = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $datosNuevos = [
        'IDProducto' => $_POST['IDProducto'],
        'Referencia' => $_POST['Referencia'],
        'Nombre' => $_POST['Nombre'],
        'Enlace' => $_POST['Enlace'],
        'EAN13' => $_POST['EAN13'],
        'PrecioSinImpuestos' => $_POST['PrecioSinImpuestos'],
        'PrecioConImpuestos' => $_POST['PrecioConImpuestos'],
        'PrecioMayorista' => $_POST['PrecioMayorista'],
        'IDFiscal' => $_POST['IDFiscal'],
        'NombreFiscal' => $_POST['NombreFiscal'],
        'IDMarca' => $_POST['IDMarca'],
        'Peso' => $_POST['Peso'],
        'URLImagen' => $_POST['URLImagen'],
        'IDCategoria' => isset($_POST['Categorias']) ? implode(',', $_POST['Categorias']) : ''
    ];

    if ($productoModel->insertar_producto($datosNuevos)) {
        $_SESSION['message'] = "Producto insertado correctamente";
        header("Location: index.php?controlador=productos&action=home");
        exit();
    } else {
        $mensaje = "Error al insertar el producto.";
        $tipoMensaje = "danger";
    }
}
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Insertar Producto</title>
    <link rel="stylesheet" href="css/editarCss.css">
    <link rel="stylesheet" href="css/styles.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
</head>
<body class="m-0 p-0">
    <div class="container py-4">
        <h2 class="mb-4">Insertar Producto</h2>

        <?php if ($mensaje): ?>
            <div class="alert alert-<?= $tipoMensaje ?>"><?= $mensaje ?></div>
        <?php endif; ?>

        <div class="d-flex justify-content-center">
            <form method="POST" class="row g-3" style="max-width: 900px; width: 100%;">

                <div class="col-md-4">
                    <label>ID Producto:</label>
                    <input type="number" name="IDProducto" class="form-control" required>
                </div>

                <div class="col-md-4">
                    <label>Referencia:</label>
                    <input type="text" name="Referencia" class="form-control" required>
                </div>

                <div class="col-md-4">
                    <label>Nombre:</label>
                    <input type="text" name="Nombre" class="form-control" required>
                </div>

                <div class="col-md-4">
                    <label>Enlace:</label>
                    <input type="url" name="Enlace" class="form-control">
                </div>

                <div class="col-md-4">
                    <label>EAN13:</label>
                    <input type="text" name="EAN13" class="form-control">
                </div>

                <div class="col-md-4">
                    <label>Precio Sin Impuestos:</label>
                    <input type="number" step="0.01" name="PrecioSinImpuestos" class="form-control" required>
                </div>

                <div class="col-md-4">
                    <label>Precio Con Impuestos:</label>
                    <input type="number" step="0.01" name="PrecioConImpuestos" class="form-control" required>
                </div>

                <div class="col-md-4">
                    <label>Precio Mayorista:</label>
                    <input type="number" step="0.01" name="PrecioMayorista" class="form-control" required>
                </div>

                <div class="col-md-6">
                    <label>ID Fiscal:</label>
                    <select id="idfiscal" name="IDFiscal" class="form-select">
                        <option value="0">0</option>
                        <option value="1">1 - ES Standard rate (21%)</option>
                        <option value="2">2 - ES Reduced Rate (10%)</option>
                        <option value="3">3 - ES Super Reduced Rate (4%)</option>
                    </select>
                </div>

                <div class="col-md-6">
                    <label>Nombre Fiscal:</label>
                    <select id="nombrefiscal" name="NombreFiscal" class="form-select">
                        <option value="0">0</option>
                        <option value="ES Standard rate (21%)">ES Standard rate (21%)</option>
                        <option value="ES Reduced Rate (10%)">ES Reduced Rate (10%)</option>
                        <option value="ES Super Reduced Rate (4%)">ES Super Reduced Rate (4%)</option>
                    </select>
                </div>

                <div class="col-md-4">
                    <label>ID Marca:</label>
                    <input type="text" name="IDMarca" class="form-control">
                </div>

                <div class="col-md-4">
                    <label>Peso:</label>
                    <input type="number" step="0.01" name="Peso" class="form-control">
                </div>

                <div class="col-md-4">
                    <label>Imagen URL:</label>
                    <input type="text" name="URLImagen" class="form-control">
                </div>

                <div class="col-12">
                    <label>Categorías:</label>
                    <select name="Categorias[]" class="form-select" multiple size="6">
                        <?php foreach ($categoriasDisponibles as $cat): ?>
                            <option value="<?= $cat['IDCategoria'] ?>"><?= htmlspecialchars($cat['NombreCategoria']) ?></option>
                        <?php endforeach; ?>
                    </select>
                    <small class="text-muted">Mantén pulsada la tecla Ctrl (o Cmd en Mac) para seleccionar varias categorías</small>
                </div>

                <div class="col-12 text-center mt-4">
                    <button type="submit" class="btn btn-success me-3">
                        <i class="bi bi-check-circle"></i> Insertar Producto
                    </button>
                    <a href="index.php?controlador=productos&action=home" class="btn btn-secondary">
                        <i class="bi bi-arrow-left-circle"></i> Cancelar
                    </a>
                </div>
            </form>
        </div>
    </div>

    <script>
        const idToName = {
            "0": "0",
            "1": "ES Standard rate (21%)",
            "2": "ES Reduced Rate (10%)",
            "3": "ES Super Reduced Rate (4%)"
        };

        const nameToId = {
            "0": "0",
            "ES Standard rate (21%)": "1",
            "ES Reduced Rate (10%)": "2",
            "ES Super Reduced Rate (4%)": "3"
        };

        const idFiscalSelect = document.getElementById("idfiscal");
        const nombreFiscalSelect = document.getElementById("nombrefiscal");

        idFiscalSelect.addEventListener("change", function () {
            nombreFiscalSelect.value = idToName[this.value] || "0";
        });

        nombreFiscalSelect.addEventListener("change", function () {
            idFiscalSelect.value = nameToId[this.value] || "0";
        });
    </script>
</body>
</html>