Files
compabetoOG/admin/editar.cs
T
2024-06-19 20:51:46 -06:00

125 lines
4.4 KiB
C#

using compabetov2.database;
using compabetov2.database.modelos;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace compabetov2.admin
{
public partial class editar : Form
{
private Producto producto;
private crud ventanaPadre;
private string imagePath;
public editar (Producto producto, crud ventanaPadre)
{
this.producto = producto;
this.ventanaPadre = ventanaPadre;
imagePath = Path.Combine(Application.StartupPath, "Resources", producto.img);
InitializeComponent();
cargarInto();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Archivos de imagen (*.jpg, *.jpeg, *.png, *.bmp)|*.jpg;*.jpeg;*.png;*.bmp";
openFileDialog1.Title = "Seleccionar imagen";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
imagePath = openFileDialog1.FileName;
lbImg.Text = $"Imagen: {Path.GetFileName(imagePath)}";
}
}
private void cargarInto()
{
txtNombre.Text = producto.nombre;
txtPrecio.Text = producto.precion.ToString();
lbImg.Text = $"Imagen: {Path.GetFileName(imagePath)}"; ;
}
private void editar_Load(object sender, EventArgs e)
{
}
private void pictureBox2_Click(object sender, EventArgs e)
{
try {
decimal precio = decimal.Parse(txtPrecio.Text);
string nombre = txtNombre.Text;
//en caso de que descida cambiar la imagen
if (!imagePath.Equals(""))
{
Producto productoNuevo = new Producto(producto.idProducto, nombre, "", precio, imagePath, 0);
if (service.editarProducto(productoNuevo))
{
//guardar imagen nueva
string destinationFolder = Path.Combine(Application.StartupPath, "Resources");
string destinationFile = Path.Combine(destinationFolder, Path.GetFileName(imagePath));
File.Copy(imagePath, destinationFile, true);
ventanaPadre.llenarLayout();
//eliminar imagen antigua
string imagenEliminada = Path.Combine(destinationFolder, producto.img);
//se pone a esperar 1 segundo antes de eliminar la imagen
//esto para que al programa le de tiempo de dejar de utilizar la imagen y no lance una excepcion
Timer timer = new Timer();
timer.Interval = 1000; // 1 segundos
timer.Tick += delegate (object sender2, EventArgs e2)
{
try
{
File.Delete(imagenEliminada);
timer.Stop();
}
catch (Exception ex)
{
// Manejar la excepción de eliminación de archivo temporal
Console.WriteLine("Error al eliminar archivo temporal: " + ex.Message);
}
};
Close();
}
}
else
{
// si la imagen no es modificada
Producto productoNuevo = new Producto(producto.idProducto, nombre, "", precio, producto.img, 0);
if (service.editarProducto(productoNuevo))
{
ventanaPadre.llenarLayout();
Close();
}
}
}
catch(Exception ex)
{
MessageBox.Show("Por favor introduzca un precio valido", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
Close();
}
}
}