let ultimoId = Math.max(...personas.map(p => p.id));
function verTarjeta(persona) {
document.getElementById('foto-tarjeta').src = persona.imagen;
document.getElementById('nombre-tarjeta').textContent = persona.nombre;
const fechaArray = persona.fecha.split('-');
const fechaNacimiento = new Date(`${fechaArray[2]}-${fechaArray[1]}-${fechaArray[0]}`);
const edad = calcularEdad(fechaNacimiento);
document.getElementById('edad-tarjeta').textContent = `${edad} Años`;
document.getElementById('inputId').value = persona.id;
document.getElementById('inputNombre').value = persona.nombre;
document.getElementById('inputFecha').value = `${fechaArray[2]}-${fechaArray[1]}-${fechaArray[0]}`;
document.getElementById('inputEstado').value = persona.estado;
}
function calcularEdad(fechaNacimiento) {
const hoy = new Date();
let edad = hoy.getFullYear() - fechaNacimiento.getFullYear();
const mes = hoy.getMonth() - fechaNacimiento.getMonth();
if (mes < 0 || (mes === 0 && hoy.getDate() < fechaNacimiento.getDate())) {
edad--;
}
return edad;
}
function guardarPersona() {
const id = document.getElementById('inputId').value;
const nombre = document.getElementById('inputNombre').value;
const fecha = document.getElementById('inputFecha').value;
const estado = document.getElementById('inputEstado').value;
const fechaNacimiento = new Date(fecha);
const edad = calcularEdad(fechaNacimiento);
if (edad < 18) {
alert(`No se puede guardar. La persona tiene ${edad} años y no es mayor de edad.`);
return false;
}
const fechaFormateada = `${fecha.slice(8, 10)}-${fecha.slice(5, 7)}-${fecha.slice(0, 4)}`;
if (id) {
// Actualizar persona existente
const index = personas.findIndex(p => p.id == id);
if (index !== -1) {
personas[index] = { ...personas[index], nombre, fecha: fechaFormateada, estado: parseInt(estado) };
actualizarTabla();
}
} else {
// Agregar nueva persona
ultimoId++;
const nuevaPersona = {
id: ultimoId,
nombre,
fecha: fechaFormateada,
estado: parseInt(estado),
imagen: 'img/default.png'
};
personas.push(nuevaPersona);
actualizarTabla();
}
document.getElementById('formulario-persona').reset();
document.getElementById('inputId').value = '';
return false;
}
function actualizarTabla() {
const tbody = document.querySelector('#tabla-personas tbody');
tbody.innerHTML = '';
personas.forEach(persona => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${persona.id}</td>
<td>${persona.nombre}</td>
<td>${persona.fecha}</td>
<td>
<span id="estado-${persona.id}"
class="btn-estado ${persona.estado ? 'activo' : 'inactivo'}">
${persona.estado ? 'Activo' : 'Inactivo'}
</span>
</td>
<td>
<button class="btn-ver" onclick="verTarjeta(${JSON.stringify(persona)})">
Ver
</button>
</td>
`;
tbody.appendChild(tr);
});
}
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('.btn-estado').forEach(btn => {
btn.addEventListener('click', function() {
const id = this.id.split('-')[1];
const persona = personas.find(p => p.id == id);
if (persona) {
persona.estado = !persona.estado;
this.textContent = persona.estado ? 'Activo' : 'Inactivo';
this.className = `btn-estado ${persona.estado ? 'activo' : 'inactivo'}`;
}
});
});
});