100 lines
4.1 KiB
JavaScript
100 lines
4.1 KiB
JavaScript
const listaConciertos = document.querySelector('.listaConciertos');
|
|
const formulario = document.getElementById('formulario');
|
|
const mensajeDiv = document.getElementById('mensaje');
|
|
const cerrarsesion = document.getElementById('cerrarSesion');
|
|
// Función para cargar conciertos
|
|
async function cargarConciertos() {
|
|
try {
|
|
const respuesta = await fetch('controladores/conciertos.php');
|
|
if (!respuesta.ok) {
|
|
throw new Error('Error al cargar conciertos');
|
|
}
|
|
const datos = await respuesta.text();
|
|
listaConciertos.innerHTML = datos;
|
|
//obtenerInfo();
|
|
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
mensajeDiv.textContent = 'No se pudieron cargar los conciertos';
|
|
mensajeDiv.style.color = 'red';
|
|
}
|
|
}
|
|
|
|
// Cargar conciertos al iniciar
|
|
cargarConciertos();
|
|
// Evento de búsqueda
|
|
buscadorBoton.addEventListener('click', (event) => {
|
|
event.preventDefault();
|
|
const termino = buscadorInput.value.trim();
|
|
if (termino) {
|
|
buscarConciertos(termino);
|
|
} else {
|
|
cargarConciertos();
|
|
}
|
|
});
|
|
|
|
|
|
// Insertar
|
|
formulario.addEventListener('submit', async (event) => {
|
|
event.preventDefault();
|
|
|
|
const nombre = document.getElementById("nombre_concierto").value;
|
|
const artista = document.getElementById("artista").value;
|
|
const fecha = document.getElementById("fecha").value;
|
|
const calle = document.getElementById("calle").value;
|
|
const colonia = document.getElementById("colonia").value;
|
|
const numero_direccion = document.getElementById("numero_direccion").value;
|
|
const codigo_postal = document.getElementById("codigo_postal").value;
|
|
const estado = document.getElementById("estado").value;
|
|
const capacidad_total = parseInt(document.getElementById("capacidad_total").value, 10);
|
|
|
|
// Obtener y validar capacidades de zonas
|
|
const zonas = [
|
|
{ nombre_zona: "General", capacidad: parseInt(document.getElementById("capacidad_general").value, 10) || 0, precio: parseFloat(document.getElementById("precio_general").value) || 0 },
|
|
{ nombre_zona: "Plata", capacidad: parseInt(document.getElementById("capacidad_plata").value, 10) || 0, precio: parseFloat(document.getElementById("precio_plata").value) || 0 },
|
|
{ nombre_zona: "Oro", capacidad: parseInt(document.getElementById("capacidad_oro").value, 10) || 0, precio: parseFloat(document.getElementById("precio_oro").value) || 0 },
|
|
{ nombre_zona: "VIP", capacidad: parseInt(document.getElementById("capacidad_vip").value, 10) || 0, precio: parseFloat(document.getElementById("precio_vip").value) || 0 }
|
|
];
|
|
|
|
const sumaCapacidades = zonas.reduce((total, zona) => total + zona.capacidad, 0);
|
|
|
|
// Validar que la suma de las capacidades de zonas no supere la capacidad total
|
|
if (sumaCapacidades > capacidad_total) {
|
|
mensajeDiv.innerHTML = `<div class="alert alert-danger">Error: La suma de las capacidades de las zonas (${sumaCapacidades}) no puede superar la capacidad total (${capacidad_total}).</div>`;
|
|
return;
|
|
}
|
|
|
|
const datosConcierto = {
|
|
nombre_concierto: nombre,
|
|
artista: artista,
|
|
fecha: fecha,
|
|
calle: calle,
|
|
colonia: colonia,
|
|
numero_direccion: numero_direccion,
|
|
codigo_postal: codigo_postal,
|
|
estado: estado,
|
|
capacidad_total: capacidad_total,
|
|
zonas: zonas
|
|
};
|
|
|
|
fetch("controladores/insertar_concierto.php", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(datosConcierto)
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.insercionCorrecta) {
|
|
mensajeDiv.innerHTML = `<div class="alert alert-success">Concierto registrado correctamente.</div>`;
|
|
formulario.reset();
|
|
cargarConciertos();
|
|
} else {
|
|
mensajeDiv.innerHTML = `<div class="alert alert-danger">Error: ${data.error}</div>`;
|
|
}
|
|
})
|
|
.catch(error => {
|
|
mensajeDiv.innerHTML = `<div class="alert alert-danger">Error en la solicitud.</div>`;
|
|
console.error("Error:", error);
|
|
});
|
|
});
|