130 lines
6.0 KiB
PHP
130 lines
6.0 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// Check if user is logged in and is an Administrador
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'Administrador') {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$product = [
|
|
'id' => '',
|
|
'nombre' => '',
|
|
'provincia_id' => '',
|
|
'price' => '',
|
|
'description' => ''
|
|
];
|
|
$page_title = 'Crear Nuevo Producto';
|
|
$card_title = 'Nuevo Producto';
|
|
|
|
if (isset($_GET['id']) && !empty($_GET['id'])) {
|
|
$product_id = $_GET['id'];
|
|
|
|
// Fetch product data
|
|
$product_stmt = db()->prepare("SELECT * FROM products WHERE id = :id");
|
|
$product_stmt->bindParam(':id', $product_id);
|
|
$product_stmt->execute();
|
|
$product = $product_stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$product) {
|
|
$_SESSION['error_message'] = 'Producto no encontrado.';
|
|
header('Location: productos.php');
|
|
exit;
|
|
}
|
|
$page_title = 'Editar Producto';
|
|
$card_title = 'Editando: ' . htmlspecialchars($product['nombre']);
|
|
}
|
|
|
|
// Fetch all provinces for the dropdown
|
|
$provincias_stmt = db()->query("SELECT * FROM provincias ORDER BY nombre ASC");
|
|
$provincias = $provincias_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
include 'layout_header.php';
|
|
?>
|
|
|
|
<div class="content-wrapper">
|
|
<div class="content-header">
|
|
<div class="container-fluid">
|
|
<div class="row mb-2">
|
|
<div class="col-sm-6">
|
|
<h1 class="m-0"><?php echo $page_title; ?></h1>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<section class="content">
|
|
<div class="container-fluid">
|
|
<div class="row">
|
|
<div class="col-md-8">
|
|
<div class="card card-primary">
|
|
<div class="card-header">
|
|
<h3 class="card-title"><?php echo $card_title; ?></h3>
|
|
</div>
|
|
<form action="save_product.php" method="POST">
|
|
<div class="card-body">
|
|
<input type="hidden" name="id" value="<?php echo $product['id']; ?>">
|
|
|
|
<div class="form-group">
|
|
<label for="nombre">Nombre del Producto</label>
|
|
<input type="text" class="form-control" id="nombre" name="nombre" value="<?php echo htmlspecialchars($product['nombre']); ?>" required>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="provincia_id">Provincia (Opcional)</label>
|
|
<select class="form-control" id="provincia_id" name="provincia_id">
|
|
<option value="">- Sin provincia -</option>
|
|
<?php foreach ($provincias as $provincia) : ?>
|
|
<option value="<?php echo $provincia['id']; ?>" <?php echo ($product['provincia_id'] == $provincia['id']) ? 'selected' : ''; ?>>
|
|
<?php echo htmlspecialchars($provincia['nombre']); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="sku">SKU</label>
|
|
<input type="text" class="form-control" id="sku" name="sku" value="<?php echo htmlspecialchars($product['sku'] ?? ''); ?>">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="costo">Costo</label>
|
|
<input type="number" step="0.01" class="form-control" id="costo" name="costo" value="<?php echo htmlspecialchars($product['costo'] ?? '0.00'); ?>">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="precio_venta">Precio de Venta</label>
|
|
<input type="number" step="0.01" class="form-control" id="precio_venta" name="precio_venta" value="<?php echo htmlspecialchars($product['precio_venta'] ?? '0.00'); ?>">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="description">Descripción</label>
|
|
<textarea class="form-control" id="description" name="description" rows="3"><?php echo htmlspecialchars($product['description'] ?? ''); ?></textarea>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="cobertura">Cobertura</label>
|
|
<input type="text" class="form-control" id="cobertura" name="cobertura" value="<?php echo htmlspecialchars($product['cobertura'] ?? ''); ?>">
|
|
</div>
|
|
|
|
<div class="form-check">
|
|
<input type="checkbox" class="form-check-input" id="show_on_panel" name="show_on_panel" value="1" <?php echo (isset($product['show_on_panel']) && $product['show_on_panel']) ? 'checked' : ''; ?>>
|
|
<label class="form-check-label" for="show_on_panel">Mostrar en panel</label>
|
|
</div>
|
|
|
|
</div>
|
|
<div class="card-footer">
|
|
<button type="submit" class="btn btn-primary">Guardar Cambios</button>
|
|
<a href="productos.php" class="btn btn-secondary">Cancelar</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
</div>
|
|
|
|
<?php include 'layout_footer.php'; ?>
|