/** * Page fiche animal : détail, modification, périodes, décès. */ let animalCourant = null; let periodesPonte = []; let periodesCouvage = []; function recupererIdDepuisUrl() { const parametres = new URLSearchParams(window.location.search); return parametres.get("id"); } function afficherFiche() { const animal = animalCourant; let photoHtml = "
Pas de photo
"; if (animal.photo) { photoHtml = "\"Photo\""; } let actions = ""; if (animal.actif) { actions = "
" + "" + "" + "
"; } const dateFin = animal.dateFinDeVie ? "
Date de fin de vie
" + animal.dateFinDeVie + "
" : ""; document.getElementById("ficheAnimal").innerHTML = photoHtml + "
" + "

" + libelleType(animal.type) + "

" + "
" + "
Couleur de bague
" + (animal.couleurBague || "—") + "
" + "
Numéro de bague
" + (animal.numeroBague || "—") + "
" + "
Date de naissance
" + (animal.dateNaissance || "—") + "
" + "
État de santé
" + libelleSante(animal.etatSante) + "
" + "
Statut
" + (animal.actif ? "Actif" : "Archivé") + "
" + dateFin + "
" + actions + "
"; if (animal.actif) { document.getElementById("boutonModifier").addEventListener("click", ouvrirModification); document.getElementById("boutonDeces").addEventListener("click", ouvrirDeces); } /* Ponte et couvage : poules uniquement */ const sectionPeriodes = document.getElementById("sectionPeriodes"); if (animal.type === "poule") { sectionPeriodes.hidden = false; afficherPeriodes(); } else { sectionPeriodes.hidden = true; } } function htmlTableauPeriodes(periodes, typePeriode) { if (periodes.length === 0) { return "

Aucune période enregistrée.

"; } let lignes = periodes.map(function (periode) { const enCours = !periode.dateFin; const statut = enCours ? "En cours" : ""; let boutonCloture = ""; if (enCours && animalCourant.actif) { boutonCloture = ""; } return ( "" + "" + periode.dateDebut + "" + "" + (periode.dateFin || "—") + " " + statut + "" + "" + (periode.remarque || "—") + "" + "" + boutonCloture + "" + "" ); }).join(""); return ( "" + "" + "" + lignes + "" + "
DébutFinRemarque
" ); } function afficherPeriodes() { document.getElementById("listePonte").innerHTML = htmlTableauPeriodes(periodesPonte, "ponte"); document.getElementById("listeCouvage").innerHTML = htmlTableauPeriodes(periodesCouvage, "couvage"); document.querySelectorAll("[data-cloturer]").forEach(function (bouton) { bouton.addEventListener("click", async function () { const type = bouton.getAttribute("data-cloturer"); const id = bouton.getAttribute("data-id"); const aujourdHui = new Date().toISOString().slice(0, 10); const url = type === "ponte" ? "/api/periodes-ponte/" + id : "/api/periodes-couvage/" + id; await appelerApi(url, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ dateFin: aujourdHui }) }); await chargerFiche(); }); }); } function ouvrirModification() { document.getElementById("animalId").value = animalCourant.id; document.getElementById("type").value = animalCourant.type; document.getElementById("couleurBague").value = animalCourant.couleurBague || ""; document.getElementById("numeroBague").value = animalCourant.numeroBague || ""; document.getElementById("dateNaissance").value = animalCourant.dateNaissance || ""; document.getElementById("etatSante").value = animalCourant.etatSante || "bon"; document.getElementById("photo").value = ""; document.getElementById("erreurAnimal").hidden = true; document.getElementById("dialogueAnimal").showModal(); } function ouvrirDeces() { document.getElementById("dateFinDeVie").value = new Date().toISOString().slice(0, 10); document.getElementById("dialogueDeces").showModal(); } function ouvrirPeriode(type) { document.getElementById("typePeriode").value = type; document.getElementById("titrePeriode").textContent = type === "ponte" ? "Nouvelle période de ponte" : "Nouvelle période de couvage"; document.getElementById("formPeriode").reset(); document.getElementById("typePeriode").value = type; document.getElementById("dateDebut").value = new Date().toISOString().slice(0, 10); document.getElementById("erreurPeriode").hidden = true; document.getElementById("dialoguePeriode").showModal(); } async function chargerFiche() { const id = recupererIdDepuisUrl(); if (!id) { window.location.href = "/"; return; } const donnees = await appelerApi("/api/animaux/" + id); if (!donnees) { return; } animalCourant = donnees.animal; periodesPonte = donnees.periodesPonte; periodesCouvage = donnees.periodesCouvage; afficherFiche(); } document.getElementById("annulerAnimal").addEventListener("click", function () { document.getElementById("dialogueAnimal").close(); }); document.getElementById("annulerDeces").addEventListener("click", function () { document.getElementById("dialogueDeces").close(); }); document.getElementById("annulerPeriode").addEventListener("click", function () { document.getElementById("dialoguePeriode").close(); }); document.getElementById("boutonAjouterPonte").addEventListener("click", function () { ouvrirPeriode("ponte"); }); document.getElementById("boutonAjouterCouvage").addEventListener("click", function () { ouvrirPeriode("couvage"); }); document.getElementById("formAnimal").addEventListener("submit", async function (event) { event.preventDefault(); const erreurAnimal = document.getElementById("erreurAnimal"); erreurAnimal.hidden = true; const formData = new FormData(); formData.append("type", document.getElementById("type").value); formData.append("couleurBague", document.getElementById("couleurBague").value); formData.append("numeroBague", document.getElementById("numeroBague").value); formData.append("dateNaissance", document.getElementById("dateNaissance").value); formData.append("etatSante", document.getElementById("etatSante").value); const photo = document.getElementById("photo").files[0]; if (photo) { formData.append("photo", photo); } try { await appelerApi("/api/animaux/" + animalCourant.id, { method: "PUT", body: formData }); document.getElementById("dialogueAnimal").close(); await chargerFiche(); } catch (erreur) { erreurAnimal.textContent = erreur.message; erreurAnimal.hidden = false; } }); document.getElementById("formDeces").addEventListener("submit", async function (event) { event.preventDefault(); await appelerApi("/api/animaux/" + animalCourant.id + "/deces", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ dateFinDeVie: document.getElementById("dateFinDeVie").value }) }); document.getElementById("dialogueDeces").close(); window.location.href = "/archives.html"; }); document.getElementById("formPeriode").addEventListener("submit", async function (event) { event.preventDefault(); const erreurPeriode = document.getElementById("erreurPeriode"); erreurPeriode.hidden = true; const type = document.getElementById("typePeriode").value; const url = type === "ponte" ? "/api/periodes-ponte" : "/api/periodes-couvage"; const dateFin = document.getElementById("dateFin").value || null; try { await appelerApi(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ idAnimal: animalCourant.id, dateDebut: document.getElementById("dateDebut").value, dateFin: dateFin, remarque: document.getElementById("remarque").value }) }); document.getElementById("dialoguePeriode").close(); await chargerFiche(); } catch (erreur) { erreurPeriode.textContent = erreur.message; erreurPeriode.hidden = false; } }); async function demarrer() { const utilisateur = await verifierSession(); if (!utilisateur) { return; } brancherDeconnexion(); await chargerFiche(); } demarrer();