@model WebVentaCoche.Models.Product

@{
    ViewData["Title"] = "Editar Producto";
}

<h2>Editar Producto</h2>


<form id="editProductForm">
    <div class="form-group">
        <label for="Name">Nombre</label>
        <input type="text" class="form-control" id="Name" name="Name" value="@Model.Name" required />
    </div>
    <div class="form-group">
        <label for="ShortDescription">Descripción Corta</label>
        <textarea class="form-control" id="ShortDescription" name="ShortDescription">@Model.ShortDescription</textarea>
    </div>
    <div class="form-group">
        <label for="LongDescription">Descripción Larga</label>
        <textarea class="form-control" id="LongDescription" name="LongDescription">@Model.LongDescription</textarea>
    </div>
    <div class="form-group">
        <label for="Price">Precio</label>
        <input type="number" class="form-control" id="Price" name="Price"
               value="@Model.Price.ToString(System.Globalization.CultureInfo.InvariantCulture)"
               step="0.01" required />
    </div>
    <!-- Botón Guardar -->
    <button type="button" id="updateProduct" class="btn btn-primary mt-3">Guardar</button>

    <!-- Botón Volver a la lista -->
    <div class="text-right mt-3">
        <a href="/Admin/Products" class="btn btn-secondary">Volver a la lista</a>
    </div>
</form>

<hr />

<button id="deleteProduct" class="btn btn-danger">Eliminar Producto</button>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function () {
        const productId = @Model.Id;

        // PUT: Actualizar producto
        $('#updateProduct').click(function () {
            const updatedProduct = {
                id: productId,
                name: $('#Name').val(),
                shortDescription: $('#ShortDescription').val(),
                longDescription: $('#LongDescription').val(),
                price: parseFloat($('#Price').val()) // Asegúrate de que sea un número
                };

                $.ajax({
                url: `/Products/Edit/${productId}`,
                type: 'PUT',
                contentType: 'application/json',
                data: JSON.stringify(updatedProduct),
                success: function (response) {
                    alert(response.message);
                    window.location.href = '/Products';
                    },
                error: function () {
                    alert('Error al actualizar el producto.');
                    }
                });
            });

            // DELETE: Eliminar producto
        $('#deleteProduct').click(function () {
            if (confirm('¿Estás seguro de que deseas eliminar este producto?')) {
                $.ajax({
                    url: `/Products/Delete/${productId}`,
                    type: 'DELETE',
                    success: function (response) {
                        alert(response.message);
                        window.location.href = '/Products';
                        },
                    error: function () {
                        alert('Error al eliminar el producto.');
                        }
                    });
                }
            });
        });
</script>
